-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
1514 lines (1204 loc) · 39.9 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* Component class.
*
* @exports component
*/
var registeredComponents = {}; //require(""); - something here
var registeredAttributes = require("./attributes/attributeHandlers");
/* BindingContext class */
var ElementBindingContext = function(domElement, model, parent) {
this.domElement = domElement;
this.model = model;
this.parent = parent;
this.traverseChildren = true;
this.bindings = [];
}
ElementBindingContext.prototype = {
constructor: ElementBindingContext,
stopTraversing: function() {
this.traverseChildren = false;
},
keepTraversing: function() {
return this.traverseChildren;
},
addBinding: function(binding) {
this.bindings.push(binding);
},
clearBindings: function() {
var binding;
while(binding = this.bindings.pop()) binding.clean();
},
rebind: function(model, parent) {
this.clearBindings();
this.model = model;
this.parent = parent;
this.traverseChildren = true;
}
}
/* traverse DOM tree */
var applyModelOnDOMElement = function(domElement, model, parent) {
if(domElement.nodeType !== Node.ELEMENT_NODE) return;
var attributeName;
if(domElement.tesujiBindingContext == null) domElement.tesujiBindingContext = new ElementBindingContext(domElement, model, parent);
else domElement.tesujiBindingContext.rebind(model, parent);
if(registeredComponents[domElement.nodeName]) {
// handle specially
}
else {
for(var i = 0; i < domElement.attributes.length; i++) {
if(domElement.attributes[i].name.search('data-') === 0) {
// this a data attribute
attributeName = domElement.attributes[i].name.substring(5);
processDataAttribute(attributeName, domElement.attributes[i].value, domElement.tesujiBindingContext);
}
}
}
if(domElement.tesujiBindingContext.keepTraversing()) {
for(var i = 0; i < domElement.children.length; i++) {
applyModelOnDOMElement(domElement.children[i], model, parent);
}
}
}
/* process data attribute */
var processDataAttribute = function(name, value, elementBindingContext) {
var binding;
for(var i = 0; i < registeredAttributes.length; i++) {
if((registeredAttributes[i].pattern.constructor == String && registeredAttributes[i].pattern == name) || (registeredAttributes[i].pattern.constructor == RegExp && name.search(registeredAttributes[i].pattern) >= 0)) {
binding = new registeredAttributes[i](name, value, elementBindingContext);
elementBindingContext.addBinding(binding);
binding.apply();
}
}
}
/*
var readFunctionFactory = function(value, context) {
var argsNames = [];
var argsValues = [];
// attributes function arguments - currently just model
var args = {
m: context.model,
p: context.parent
}
// prepare arguments names and values
for(var key in args) {
argsNames.push(key);
argsValues.push(args[key]);
}
// function body
argsNames.push("return ("+value+");");
var fn = Function.apply(null, argsNames);
// return function
return function() {
return fn.apply(context.domElement, argsValues);
}
}
var writeFunctionFactory = function(value, context) {
var argsNames = [];
var argsValues = [];
// attributes function arguments - currently just model
var args = {
m: context.model,
p: context.parent
}
// prepare arguments names and values
for(var key in args) {
argsNames.push(key);
argsValues.push(args[key]);
}
// function body
argsNames.push("val");
argsNames.push(value+" = val;");
var fn = Function.apply(null, argsNames);
var argsCount = argsValues.length;
// return function
return function(val) {
argsValues[argsCount] = val;
fn.apply(context.domElement, argsValues);
}
}
var eventFunctionFactory = function(value, context) {
var argsNames = [];
var argsValues = [];
// attributes function arguments - currently just model
var args = {
m: context.model,
p: context.parent
}
// prepare arguments names and values
for(var key in args) {
argsNames.push(key);
argsValues.push(args[key]);
}
// function body
argsNames.push("event");
argsNames.push(value);
var fn = Function.apply(null, argsNames);
var argsCount = argsValues.length;
// return function
return function(event) {
argsValues[argsCount] = event;
fn.apply(context.domElement, argsValues);
}
}*/
/** Destroy all bindings of the DOM element on the model (opposite of applyModelOnDOMElement) */
var removeModelFromDOMElement = function(domElement) {
if(domElement.nodeType !== Node.ELEMENT_NODE) return;
if(domElement.tesujiBindingContext == null) return;
if(domElement.tesujiBindingContext.keepTraversing()) {
for(var i = 0; i < domElement.children.length; i++) {
removeModelFromDOMElement(domElement.children[i]);
}
}
domElement.tesujiBindingContext.clearBindings();
}
/**
* Abstract component class
* Represents some component of the web page. It is composed from HTML template and data model.
*/
var Component = function() {
this.parentElement = null;
//this.parentModel = null;
this.modelMap = [];
this.model = [];
this.templateNodes = [];
var parentModel = null;
Object.defineProperty(this, "parentModel", {
get: function() {
return parentModel;
},
set: function(v) {
parentModel = v;
}
}); // temp fix
}
Component.prototype = {
constructor: Component,
template: "",
name: null,
_keepHot: true,
createModel: function(model) {
return (model instanceof Array) ? model.slice() : [model];
},
/**
* Apply *initial* model on the template
*/
applyModel: function() {
if(this.model.length == 0) {
// no model item - don't render any HTML
this.parentElement.innerHTML = "";
this.templateNodes = [];
}
else {
// first template copy already in the DOM
this.bindModel(0);
// multiple injections
for(var i = 1; i < this.model.length; i++) {
// for other instances we need to clone
this.cloneTemplateNodes();
this.bindModel(i);
}
}
},
updateModel: function(newModel) {
var oldPos;
newModel = this.createModel(newModel);
for(var i = 0; i < newModel.length; i++) {
oldPos = this.model.indexOf(newModel[i]);
if(oldPos >= 0) {
// model already in the DOM
if(i != oldPos) {
// adjust template
this.moveTemplateNodes(oldPos, i);
// and model
this.model.splice(i, 0, this.model.splice(oldPos, 1)[0]);
}
}
else {
// check whether current old model is in a new model array
if(this.templateNodes[i] && newModel.indexOf(this.model[i]) == -1) {
// just replace model at i
this.model[i] = newModel[i];
// bind model and template nodes
this.bindModel(i);
}
else {
// insert templateNodes at i
this.cloneTemplateNodes(i);
// insert model at i
this.model.splice(i, 0, newModel[i]);
// bind model and template nodes
this.bindModel(i);
}
}
}
if(this.model.length > newModel.length) {
// remove redundant template nodes
for(var i = this.templateNodes.length-1; newModel.length <= i; i--) {
this.unbindModel(i);
this.removeTemplateNodes(i);
}
// and models
this.model.splice(newModel.length, this.model.length-newModel.length);
}
},
bindModel: function(n) {
for(var i = 0; i < this.templateNodes[n].length; i++) applyModelOnDOMElement(this.templateNodes[n][i], this.model[n], this.parentModel);
},
unbindModel: function(n) {
for(var i = 0; i < this.templateNodes[n].length; i++) removeModelFromDOMElement(this.templateNodes[n][i]);
},
/**
* Clone template nodes at the specified position. If ommited, template is cloned at the end.
*/
cloneTemplateNodes: function(position) {
if(!this.templateNodes[0]) {
// fill element.innerHTML with the template
this.parentElement.innerHTML = this.template;
// save template nodes
this.templateNodes.push(Array.prototype.slice.call(this.parentElement.childNodes));
return;
}
var clonedNode, clonedNodes = [];
var templateNodes = this.templateNodes[0]; // need to be changed, because this.templateNodes[0] doesn't have to exist.
if(position == null || position == this.templateNodes.length) {
// insert at the end
for(var i = 0; i < templateNodes.length; i++) {
clonedNode = templateNodes[i].cloneNode(true);
this.parentElement.appendChild(clonedNode);
clonedNodes.push(clonedNode);
}
this.templateNodes.push(clonedNodes);
}
else {
// insert at the position (before nodes of this.templateNodes[position])
for(var i = 0; i < templateNodes.length; i++) {
clonedNode = templateNodes[i].cloneNode(true);
this.parentElement.insertBefore(clonedNode, this.templateNodes[position][0]);
clonedNodes.push(clonedNode);
}
this.templateNodes.splice(position, 0, clonedNodes);
}
},
removeTemplateNodes: function(position) {
var templateNodes;
if(position == null) {
// remove last template nodes
templateNodes = this.templateNodes.pop();
}
else {
// remove template nodes on given position
templateNodes = this.templateNodes.splice(position, 1)[0];
}
for(var i = 0; i < templateNodes.length; i++) {
this.parentElement.removeChild(templateNodes[i]);
}
},
// currently only works if from > to
moveTemplateNodes: function(from, to) {
var templateNodes = this.templateNodes[from];
// insert at the position (before nodes of this.templateNodes[position])
for(var i = 0; i < templateNodes.length; i++) {
this.parentElement.insertBefore(templateNodes[i], this.templateNodes[to][0]);
}
this.templateNodes.splice(from, 1);
this.templateNodes.splice(to, 0, templateNodes);
}
}
/**
* Immutable component class
*
* HTML template and model are immutable - they cannot be changed after the component is initialized.
*
* ImmutableComponent class itself is an abstract class - it doesn't contain any template. However, you can extended it with a template and create your own component class.
* Be aware that HTML template != HTML output, HTML output is HTML template enriched with the model data, and even it can be composed from multiple copies of the template.
* So HTML output is mutable and depends on the model.
*
* Component objects contain method inject(?), which can be used to insert component into the DOM. And the component can be injected into any element any time.
*/
var ImmutableComponent = function(model) {
Component.call(this);
this.model = this.createModel(model);
}
ImmutableComponent.prototype = Object.create(Component.prototype);
ImmutableComponent.prototype.constructor = ImmutableComponent;
/**
* Inject component into the DOM element and produce output.
*/
ImmutableComponent.prototype.inject = function(element) {
// set parent element
this.parentElement = element;
this.parentElement.tesujiComponent = this;
// fill element.innerHTML with the template
this.parentElement.innerHTML = this.template;
// save template nodes
this.templateNodes.push(Array.prototype.slice.call(this.parentElement.childNodes));
// apply model
this.applyModel();
}
/**
* Binded component class
*
* It differs from the basic component in the fact, it doesn't contain its template. It uses HTML content of its parent DOM element as the template.
* Also it has mutable model - it can be changed. On the other side instance is strictly binded to its parent DOM element.
*/
var FixedComponent = function(parentElement) {
if(parentElement == null) throw new TypeError("Property 'parentElement' must not be null.");
Component.call(this);
// set parent element - it cannot be changed
this.parentElement = parentElement;
this.parentElement.tesujiComponent = this;
// save template html
this.template = parentElement.innerHTML;
// save template nodes
this.templateNodes.push(Array.prototype.slice.call(this.parentElement.childNodes));
}
FixedComponent.prototype = Object.create(Component.prototype);
FixedComponent.prototype.constructor = FixedComponent;
FixedComponent.prototype.setModel = function(model) {
this.model = this.createModel(model);
// and apply it
this.applyModel();
}
/**
* Possible calls:
*
* 1) component(String template[, String name, Function mappingFunction])
* Predefine component - its instances can be injected into the DOM
*
* 2) component(DOMElement element, <Model> model)
* create component 'on the fly' and fill it with the model
*
* 3) component()
* return Component class
*/
Component.fromHTML = function(template) {
if(typeof template != "string") {
throw new TypeError("Function 'Component.fromHTML' argument must be type of 'string'.");
}
// create a subclass of the Component
var NewComponent = function(model) {
ImmutableComponent.call(this, model);
}
NewComponent.prototype = Object.create(ImmutableComponent.prototype);
NewComponent.constructor = NewComponent;
// save its template
NewComponent.prototype.template = template;
/*// register component if it has name
if(typeof name == "string") {
NewComponent.prototype.name = name;
registeredComponents[name] = NewComponent;
}
// save mapping function if any
if(typeof mappingFunction == "function") NewComponent.prototype.getModelFromAttributes = mappingFunction;*/
return NewComponent;
}
Component.FixedComponent = FixedComponent;
/*
var component = function(template, name, parent) {
//if(template == null) return Component;
else if(typeof template == "string") {
// create a subclass of the Component
var NewComponent = function(model) {
ImmutableComponent.call(this, model);
}
NewComponent.prototype = Object.create(ImmutableComponent.prototype);
NewComponent.constructor = NewComponent;
// save its template
NewComponent.prototype.template = template;
// register component if it has name
if(typeof name == "string") {
NewComponent.prototype.name = name;
registeredComponents[name] = NewComponent;
}
// save mapping function if any
if(typeof mappingFunction == "function") NewComponent.prototype.getModelFromAttributes = mappingFunction;
return NewComponent;
}
else if(template instanceof HTMLElement && name != null) {
var compInstance;
if(template.tesujiComponent) {
compInstance = template.tesujiComponent;
compInstance.updateModel(name);
}
else {
compInstance = new BindedComponent(template);
compInstance.parentModel = parent;
compInstance.setModel(name);
}
return compInstance;
}
else {
throw new TypeError("Function 'component' called with invalid arguments.");
}
}*/
module.exports = Component;
},{"./attributes/attributeHandlers":14}],2:[function(require,module,exports){
/**
* Cool observables
*
* @exports observableObject
*/
"use strict";
var events = require("./utils/events");
var ConditionVariable = require("./utils/ConditionVariable");
var Subscribable = require("./utils/Subscribable");
/* TEMPORARY Computed object */
var Computed = function Computed(readFn, writeFn, context) {
this.readFn = readFn;
this.writeFn = writeFn;
this.context = context;
}
/** deeply freeze an object to make it fully immutable */
var deepFreeze = function(obj) {
if(obj == null || typeof obj != 'object' || Object.isFrozen(obj) || obj._keepHot) return;
Object.freeze(obj); // First freeze the object.
for (var key in obj) {
if(obj.hasOwnProperty(key)) deepFreeze(obj[key]); // Recursively call deepFreeze.
}
}
var setValue = function(context, value, key) {
// not null check
if(value == null && context.notNull) throw new TypeError("Trying to assign null to the property '"+key+"', but value of an observable property cannot be null (or undefined).");
if(context.condition != null) context.condition.test(value, key);
context.value = value
}
var createPropertyDescriptor = function(obj, key, parent) {
var context = {
value: undefined,
condition: null,
notNull: false,
subscription: null
}
if(obj[key] instanceof ConditionVariable) {
context.condition = obj[key];
context.value = obj[key].initialValue;
}
else if(obj[key] instanceof Computed) {
// computed variable
var descriptor = {
enumerable: false
}
// save object
context.value = obj[key];
// read function
if(context.value.readFn) {
descriptor.get = context.value.readFn.bind(context.value.context == null ? parent : context.value.context);
}
// write function
if(context.value.writeFn) {
descriptor.set = context.value.writeFn.bind(context.value.context == null ? parent : context.value.context);
}
return descriptor;
}
else if(obj[key] instanceof ModelArray) {
context.subscription = function() {
parent.notify(key, obj[key]);
}
obj[key].subscribe(context.subscription);
context.value = obj[key];
}
else {
context.value = obj[key];
}
deepFreeze(context.value);
return {
enumerable: true,
get: function() {
// trigger getter event
events.trigger("observable.get", context.value, parent, key);
return context.value;
},
set: function(new_val) {
//setValue(context, new_val, key);
// not null check
if(new_val == null && context.notNull) throw new TypeError("Trying to assign null to the property '"+key+"', but value of an observable property cannot be null (or undefined).");
if(context.condition != null) context.condition.test(new_val, key);
if(context.subscription) context.value.unsubscribe(context.subscription);
if(new_val instanceof ModelArray) {
context.subscription = function() {
parent.notify(key, new_val);
}
new_val.subscribe(context.subscription);
context.value = new_val;
}
else {
context.value = new_val
}
deepFreeze(context.value);
parent.notify(key, new_val);
}
}
}
var initObservableInstance = function(source) {
var properties = {};
for(var key in source) {
if(source.hasOwnProperty(key)) {
properties[key] = createPropertyDescriptor(source, key, this);
}
}
// define properties
Object.defineProperties(this, properties);
// seal object
Object.seal(this);
}
/**
* Create model class with a subscribable interface.
*/
var makeModelClass = function(parentClass, constructor) {
// inherits methods from parent class
constructor.prototype = Object.create(parentClass.prototype);
// inherits methods from Subscribable class
for(var key in Subscribable.prototype) {
constructor.prototype[key] = Subscribable.prototype[key];
}
constructor.prototype.constructor = constructor;
constructor.prototype._keepHot = true;
return constructor;
}
/* generic Model */
var Model = makeModelClass(Object, function Model(source) {
// call Subscribable constructor
Subscribable.call(this);
// init model
initObservableInstance.call(this, source);
});
/* Model Array */
var ModelArray = makeModelClass(Array, function ModelArray(source) {
// call Subscribable constructor
Subscribable.call(this);
this.length = 0;
for(var i = 0; i < source.length; i++) {
// use old method for initialization
Array.prototype.push.call(this, source[i]);
}
});
var arrayAlteringMethods = ["pop", "push", "reverse", "shift", "unshift", "splice", "sort"];
var alteredArrayMethod = function(name) {
return function() {
var response = Array.prototype[name].apply(this, arguments);
this.notify();
return response;
}
}
for(var i = 0; i < arrayAlteringMethods.length; i++) {
ModelArray.prototype[arrayAlteringMethods[i]] = alteredArrayMethod(arrayAlteringMethods[i]);
}
ModelArray.prototype.constructor = ModelArray;
Model.Array = ModelArray;
Model.extend = function(constructor) {
var submodel = function() {
// call parent constructor
constructor.apply(this, arguments);
// init the magic
Model.call(this, this);
}
submodel.prototype = Object.create(Model.prototype);
submodel.prototype.constructor = submodel;
return submodel;
}
Model.computed = function(readFn, writeFn, context) {
return new Computed(readFn, writeFn, context);
}
// export it
module.exports = Model;
},{"./utils/ConditionVariable":16,"./utils/Subscribable":18,"./utils/events":20}],3:[function(require,module,exports){
/** data-text handler */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
read: function(value) {
if(value) {
this.context.domElement.checked = true;
}
else {
this.context.domElement.checked = false;
}
}
}, 'checked');
},{"./ElementAttributeBinding":6}],4:[function(require,module,exports){
/** data-class handler */
/* value = Object with JS style definitions */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
read: function(value) {
if(typeof value != "object") throw TypeError("Attribute data-class accepts objects, '"+(typeof value)+"' was given.");
for(var key in value) {
if(value.hasOwnProperty(key)) {
if(value[key]) {
this.context.domElement.classList.add(key);
}
else {
this.context.domElement.classList.remove(key);
}
}
}
}
}, 'class');
},{"./ElementAttributeBinding":6}],5:[function(require,module,exports){
/** data-component handler */
/* value = <component> */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
read: function(value) {
var Component = require("../Component");
if(!(value instanceof Component)) throw TypeError("Attribute data-component accepts instances of 'Component', instance of '"+(value.constructor && value.constructor.name ? value.constructor.name : "[unknown]")+"' was given.");
value.parentModel = this.context.model;
value.inject(this.context.domElement);
this.context.stopTraversing();
},
}, 'component');
},{"../Component":1,"./ElementAttributeBinding":6}],6:[function(require,module,exports){
/** Abstract binding class - represents binding between model and element for certain data attribute */
var Observer = require("../utils/Observer");
var ElementAttributeBinding = function ElementAttributeBinding(attributeName, attributeValue, elementBindingContext) {
this.name = attributeName;
this.value = attributeValue;
this.context = elementBindingContext;
}
ElementAttributeBinding.prototype = {
constructor: ElementAttributeBinding,
/**
* Signature: read(newValue)
* This function is called with a computed value, any time it is changed.
*/
read: null,
/**
* Signature: event(callback)
* Call callback, when something happen
*/
event: null,
/**
* apply the binding
*/
apply: function() {
if(this.read) {
// 1. parse value
this.computed_observable = new Observer(this.createFunction("return ("+this.value+");", ["m", "p"], [this.context.model, this.context.parent], this.context.domElement), {
/* for debugging only */
domElement: this.context.domElement,
attribute: this.name,
value: this.value
});
// 2. add subscribers
this.read_binded = this.read.bind(this);
this.computed_observable.subscribe(this.read_binded);
// 3. initial element update
this.read(this.computed_observable.currentValue);
}
if(this.event) {
this.event(this.createEventFunction());
}
/*if(registeredAttributes[i].event) {
// It is a data handler's job
this.event(name, eventFunctionFactory(value, context), context);
}*/
},
/**
* Static helper fuction factory
*/
createFunction: function(functionBody, argNames, argValues, context) {
// store context
var functionArgs = argNames.concat(functionBody);
var fn = Function.apply(null, functionArgs);
// return function
return function() {
// mix predefined arguments with passed
var args = argValues.concat(Array.prototype.slice.call(arguments));
return fn.apply(context, args);
}
},
createEventFunction: function(functionBody) {
return this.createFunction(this.value, ["m", "p", "event"], [this.context.model, this.context.parent], this.context.domElement);
},
/**
* Cleaning work - after unbind.
*/
clean: function() {
if(this.read && this.computed_observable) {
this.computed_observable.unsubscribe(this.read_binded);
delete this.computed_observable;
delete this.read_binded;
}
},
}
/**
* Attribute pattern,
*/
ElementAttributeBinding.pattern = null;
/**
* Helper for creating attribute bindings.
*/
ElementAttributeBinding.extend = function(proto, pattern) {
var NewBindingClass = function(attributeName, attributeValue, elementBindingContext){
ElementAttributeBinding.call(this, attributeName, attributeValue, elementBindingContext);
};
NewBindingClass.prototype = Object.create(ElementAttributeBinding.prototype);
NewBindingClass.prototype.constructor = NewBindingClass;
for(var method in proto) {
if(proto.hasOwnProperty(method)) NewBindingClass.prototype[method] = proto[method];
}
NewBindingClass.pattern = pattern;
return NewBindingClass;
}
module.exports = ElementAttributeBinding;
},{"../utils/Observer":17}],7:[function(require,module,exports){
/** data-on* handler */
/* callback = event callback */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
event: function(callback) {
var evName = this.name.substring(2);
this.event_callback = callback;
this.context.domElement.addEventListener(evName, callback);
},
clean: function() {
var evName = this.name.substring(2);
this.context.domElement.removeEventListener(evName, this.event_callback);
delete this.event_callback;
}
}, /on.*/i);
},{"./ElementAttributeBinding":6}],8:[function(require,module,exports){
/** data-text handler */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
read: function(value) {
this.context.domElement.innerHTML = value;
this.context.stopTraversing();
},
}, 'html');
},{"./ElementAttributeBinding":6}],9:[function(require,module,exports){
/** data-if handler */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
read: function(value) {
if(value) {
this.context.domElement.style.display = "";
}
else {
this.context.domElement.style.display = "none";
}
},
}, 'if');
},{"./ElementAttributeBinding":6}],10:[function(require,module,exports){
/** data-style handler */
/* value = Object with JS style definitions */
var ElementAttributeBinding = require("./ElementAttributeBinding");
module.exports = ElementAttributeBinding.extend({
read: function(value) {
if(typeof value != "object") throw TypeError("Attribute data-style accepts objects, '"+(typeof value)+"' was given.");
for(var key in value) {
if(value.hasOwnProperty(key)) {
if(value[key] != null && value[key].valueOf() !== false) {
this.context.domElement.style[key] = value[key];
}
}
}
}
}, 'style');