-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1611 lines (1316 loc) · 69.5 KB
/
index.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
/** Data Context functions for Browser and node.js. @preserve Copyright (c) 2020 Manuel Lõhmus.*/
"use strict";
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? //if node
module.exports = factory(exports) : //node
(global = typeof globalThis !== 'undefined' ? globalThis : global || self,
global.DC = factory(global.DC = {}));
}(this, (function (DC) {
if (DC && DC.name === "DC") return DC;
else DC.name = "DC";
var isNode = typeof exports !== 'undefined';
DC.IsDebuger = false;
DC.IsAutoProperty = false;
//#region function for IE
if (!isNode) {
/** String */
String.prototype.startsWith || Object.defineProperty(String.prototype, 'startsWith', {
value: function (search, position) {
var pos = position > 0 ? position | 0 : 0;
return typeof search === "string" && this.substring(pos, pos + search.length) === search;
}
});
String.prototype.endsWith || Object.defineProperty(String.prototype, 'endsWith', {
value: function (search, length) {
length = (length === undefined || length > this.length) ? this.length : length;
return typeof search === "string" && this.substring(length - search.length, length) === search;
}
});
String.prototype.trimStart || Object.defineProperty(String.prototype, 'trimStart', {
value: function (str) {
if (this.startsWith(str)) {
return this.substring(str.length);
}
return this.replace(/^[\s\uFEFF\xA0]+/g, '');
}
});
String.prototype.trimEnd || Object.defineProperty(String.prototype, 'trimEnd', {
value: function (str) {
if (this.endsWith(str)) {
return this.substring(0, this.length - str.length);
}
return this.replace(/[\s\uFEFF\xA0]+$/g, '');
}
});
String.prototype.includes || Object.defineProperty(String.prototype, 'includes', {
value: function (search) {
return this.indexOf(search) > -1;
}
});
/** Math */
Math.cbrt || Object.defineProperty(Math, 'cbrt', {
value: function cbrt(x) {
// ensure negative numbers remain negative:
return x < 0 ? -Math.pow(-x, 1 / 3) : Math.pow(x, 1 / 3);
}
});
}
//#endregion
//#region Item
DC.Item = (function () {
/**
* Creates a new Item.
* @param {any} obj Optional. Init. data.
* @param {boolean} setIsModified
* @returns {Item} .
*/
function Item(obj, setIsModified) {
var item = {};
item["-type"] = "DC.Item";
var _bindId = 0;
var _binds = {};
Object.defineProperty(item, "-binds", { get: function () { return _binds; } });
var values = {};
Object.defineProperty(item, "-values", { get: function () { return values; } });
Object.defineProperty(item, "isItem", { get: function () { return true; } });
var parent = null;
Object.defineProperty(item, "parent", { get: function () { return parent; }, set: function (value) { parent = value; } });
var parentPropertyName = null;
Object.defineProperty(item, "parentPropertyName", { get: function () { return parentPropertyName; }, set: function (value) { parentPropertyName = value; } });
var propertyNames = [];
/** Defined properties in 'Item' */
Object.defineProperty(item, "propertyNames", { get: function () { return propertyNames; } });
Object.defineProperty(item, "isEmpty", { get: function () { return !item.propertyNames.length; } });
Object.defineProperty(item, "isModified", { value: true, writable: true, configurable: true });
Object.defineProperty(item, "isOverwriting", { value: false, writable: true, configurable: true });
/**
* Invoke bind property update.
* @param {string} propertyName Optional. If it is undefined, then invoke all properties.
* @param {any} newValue Optional.
* @param {any} oldValue Optional.
* @param {ArrayOfString} propertyPath Optional.
* @param {Object} binds Optional.
* @returns {Number} Returns invoke count.
*/
item.bindInvoke = function (propertyName, newValue, oldValue, propertyPath, binds) {
var count = 0;
var source = binds ? binds : _binds;
source = propertyName ? source[propertyName] : source;
if (source)
for (var key in source) {
if (typeof source[key] === "function") {
if (source[key] && (!source[key].element
|| source[key].element && source[key].element.isConnected !== undefined && source[key].element.isConnected
|| source[key].element && (source[key].element.parentElement || source[key].element.parentNode))) {
source[key]({ newValue: newValue, oldValue: oldValue, propertyPath: propertyPath });
count++;
}
else {
if (source[key].element && typeof source[key].element.removeBinding === "function") {
source[key].element.removeBinding();
}
delete source[key];
}
}
else {
var c = item.bindInvoke(propertyName, newValue, oldValue, propertyPath, source[key]);
if (c === 0)
delete source[key];
else
count += c;
}
}
if (propertyName && _binds && _binds[""])
for (var k in _binds[""])
if (typeof _binds[""][k] === "function")
_binds[""][k]({ newValue: newValue, oldValue: oldValue, propertyPath: propertyPath });
if (propertyName && _binds && _binds["."])
for (var l in _binds["."])
if (typeof _binds["."][l] === "function") {
if (propertyPath && (propertyPath[0] === "value"
|| newValue && (newValue.action === "add" || newValue.action === "remove" || newValue.action === "modified"))
&& (newValue !== oldValue || newValue && oldValue && newValue.toString() !== oldValue.toString())) {
_binds["."][l]({ newValue: newValue, oldValue: oldValue, propertyPath: propertyPath });
}
}
if (propertyName && item.parent && typeof item.parent.bindInvoke === "function") {
if (!propertyPath) propertyPath = Array();
propertyPath.push(item.parentPropertyName);
item.parent.bindInvoke("parent", newValue, oldValue, propertyPath);
}
return count;
};
/**
* Add bind function in property.
* @param {string} propertyName Property name.
* @param {function} callback Callback function 'function(val, element[, parentPropertyValue]){...'.
* @param {HTMLElement} element Optional. Parameter 'element' is html element. If element.parentElement is undefined or null, auto delete callback function.
* @returns {function} Returns remove bind function.
*/
item.addBind = function (propertyName, callback, element) {
if (typeof propertyName === "string" && typeof callback === "function") {
if (propertyName && propertyName !== "." && !item[propertyName])
item.defineProperty(propertyName, null);
if (!element)
element = { parentElement: {} };
var binding = function (parentPropertyValue) { if (item[propertyName] || parentPropertyValue) callback(item[propertyName], element, parentPropertyValue); };
var bindingId = _bindId++;
if (!_binds[propertyName])
_binds[propertyName] = {};
else
Object.keys(_binds[propertyName]).forEach(function (key) {
if (_binds[propertyName][key] && !_binds[propertyName][key].element
|| _binds[propertyName][key] && _binds[propertyName][key].element && _binds[propertyName][key].element.isConnected !== undefined && !_binds[propertyName][key].element.isConnected
|| _binds[propertyName][key] && _binds[propertyName][key].element && !(_binds[propertyName][key].element.parentElement || _binds[propertyName][key].element.parentNode)
) {
//debugger;
if (_binds[propertyName][key] && _binds[propertyName][key].element
&& typeof _binds[propertyName][key].element.removeBinding === "function") {
_binds[propertyName][key].element.removeBinding();
}
delete _binds[propertyName][key];
}
});
_binds[propertyName][bindingId] = binding;
_binds[propertyName][bindingId].element = element;
var removeBinding = function () { delete _binds[propertyName][bindingId]; };
element.removeBinding = removeBinding;
binding();
var count = Object.keys(_binds[propertyName]).length;
if (count > 500) {
if (typeof DC.GetPathByItem === "function") {
console.warn("binds count: > 500 => datacontext" + DC.GetPathByItem(item[propertyName]) + "=" + item[propertyName]);
} else {
console.warn("binds count: > 500 => " + propertyName + "=" + item[propertyName]);
}
}
return removeBinding;
}
};
/**
* Define property.
* @param {string} propertyName Property name.
* @param {Object} val Optional. Property value.
* @param {function(val)} setterFn Optional. function (val) { ... return val; }
* @param {function(val)} getterFn Optional. function (val) { ... return val; }
* @returns {Object} Returns value.
*/
item.defineProperty = function (propertyName, val, setterFn, getterFn) {
if (val && val.hasOwnProperty("value")) { val = val.value; }
if (propertyName && propertyName !== "parent") {
if (propertyNames.indexOf(propertyName) > -1)
item.deleteProperty(propertyName);
if (item[propertyName])
delete item[propertyName];
propertyNames.push(propertyName);
Object.defineProperty(item, propertyName, {
configurable: true,
get: function () {
if (typeof getterFn === "function")
return getterFn(values[propertyName]);
else
return values[propertyName];
},
set: function (value) {
var oldValue = IsItem(values[propertyName])
&& (propertyName === "value" || values[propertyName].propertyNames.length === 0)
? values[propertyName].value
: values[propertyName];
if (typeof setterFn === "function" && propertyName === "value") {
value = setterFn(value);
}
if (value !== oldValue
&& ((!value || !value.value) && (!oldValue || !oldValue.value)
|| (value && value.value || oldValue && oldValue.value)
&& value + "" !== oldValue + "")) {
if (IsItem(values[propertyName]) && !IsItem(value) && !IsCollection(value)) {
values[propertyName].init(value);
}
else if (propertyName !== "value" && !values.hasOwnProperty(propertyName)
&& !IsItem(value) && !IsCollection(value)) {
if (typeof value === "object") {
if (typeof setterFn === "function")
value = setterFn(value);
values[propertyName] = Array.isArray(value) ? DC.Collection() : DC.Item();
if (item.isOverwriting) values[propertyName].overwrite(value);
else values[propertyName].init(value);
}
else {
var val = DC.Item();
val.defineProperty("value", value, setterFn);
value = val;
values[propertyName] = value;
}
}
else if (propertyName !== "value" && !IsItem(value) && !IsCollection(value)) {
values[propertyName] = DC.Item();
values[propertyName].defineProperty("value", oldValue, setterFn);
setTimeout(function () {
values[propertyName].value = value;
});
}
else {
values[propertyName] = value;
}
if (values[propertyName] && values[propertyName].hasOwnProperty("parent")) { values[propertyName].parent = item; }
if (values[propertyName] && values[propertyName].hasOwnProperty("parentPropertyName")) { values[propertyName].parentPropertyName = propertyName; }
if (item[propertyName] && item[propertyName].hasOwnProperty("isModified"))
item[propertyName].isModified = true;
else if (propertyName === "value")
item.isModified = true;
if (propertyName === "value" && item.parent && item.parentPropertyName) {
setTimeout(function () { item.parent.bindInvoke(item.parentPropertyName, values[propertyName], oldValue, Array(propertyName, item.parentPropertyName)); }, 50);
}
}
}
});
if (val !== undefined) {
item[propertyName] = val;
}
if (item[propertyName] && item[propertyName].hasOwnProperty("parent")) { item[propertyName].parent = item; }
if (item[propertyName] && item[propertyName].hasOwnProperty("parentPropertyName")) { item[propertyName].parentPropertyName = propertyName; }
return item[propertyName];
}
};
/**
* Set value in property.
* @param {string} propertyName Property name.
* @param {object} val Property value.
* @param {boolean} isAutoProperty Property auto gen.
* @returns {object} Returns value.
*/
item.setValue = function (propertyName, val, isAutoProperty) {
if (propertyName) {
if ((isAutoProperty || DC.IsAutoProperty || propertyName === "value")
&& propertyNames.indexOf(propertyName) < 0 && item[propertyName] === undefined) {
val = item.defineProperty(propertyName, val);
if (!isNode && !isAutoProperty && propertyName !== "value")
console.info("AutoProperty!", "propertyName:'" + propertyName + "'", "value:'" + val + "'");
}
else if (item[propertyName] && typeof item[propertyName].init === "function")
item[propertyName].init(val);
else if (propertyName === "value" && val === null) {
item.defineProperty("value", val);
}
else {
item[propertyName] = val;
}
}
return item[propertyName];
};
/**
* Delete property.
* @param {string} propertyName Property name.
*/
item.deleteProperty = function (propertyName) {
if (values[propertyName] && values[propertyName].parent === item) { values[propertyName].parent = null; }
delete values[propertyName];
delete item[propertyName];
if (propertyNames.indexOf(propertyName) > -1)
propertyNames.splice(propertyNames.indexOf(propertyName), 1);
};
item.clear = function () {
var newObj = DC.CreateByType(item["-type"]);
while (item.propertyNames.length)
item.deleteProperty(item.propertyNames[0]);
item.isModified = true;
if (newObj && newObj.propertyNames && newObj.propertyNames.length) { item.init(newObj); }
item.bindInvoke(undefined, item);
};
/**
* Load Item data.item["-type"]
* @param {object} obj Data object.
* @param {boolean} setIsModified .
*/
item.overwrite = function (obj, setIsModified) {
var overwriting = item.isOverwriting;
item.isOverwriting = true;
item.init(obj, setIsModified);
if (setIsModified) item.isModified = true;
else if (typeof setIsModified === "boolean") item.isModified = false;
item.isOverwriting = overwriting;
};
item.init = function (obj, setIsModified) {
if (Array.isArray(obj) || IsCollection(obj)) {
item.setValue("value", DC.Collection(), true);
item.isOverwriting ? item.value.overwrite(obj, setIsModified) : item.value.init(obj, setIsModified);
}
else if (typeof obj === "object" && obj !== null) {
if (obj["-isEmpty"]) {
item.clear();
if (setIsModified) item.isModified = true;
else if (typeof setIsModified === "boolean") item.isModified = false;
return;
}
var keys = IsItem(obj) ? obj.propertyNames : Object.keys(obj);
//var names = keys.slice(0);
var names = keys.filter(function (key) { return key[0] !== "-" });
if (!DC.IsAutoProperty/* && !item.isOverwriting*/)
if (keys.length > 1 && keys.indexOf("value") > -1) // illogical 'value'
keys = keys.filter(function (k) { return k !== "value"; });
keys.forEach(function (key) {
names = names.filter(function (name) { return name !== key; });
if (key[0] === "-") {
if (key !== '-type' || item['-type'] === "DC.Item")
item[key] = obj[key];
}
else if (typeof obj[key] === "string" && obj[key].trim() === "-deleted") {
item.deleteProperty(key);
item.isModified = true;
if (typeof setIsModified === "boolean") item.isModified = false;
return;
}
else if (IsItem(item[key]) || IsCollection(item[key])) {
item.isOverwriting ? item[key].overwrite(obj[key], setIsModified) : item[key].init(obj[key], setIsModified);
if (setIsModified) item[key].isModified = true;
else if (typeof setIsModified === "boolean") item[key].isModified = false;
}
else {
var value = obj[key];
if (Array.isArray(obj[key]) || IsCollection(obj[key])) {
value = DC.CreateByType(obj[key]["-type"], obj[key])
if (!value) value = DC.Collection();
item.isOverwriting ? value.overwrite(obj[key], setIsModified) : value.init(obj[key], setIsModified);
}
else if (typeof obj[key] === "object" && obj[key] !== null) {
value = DC.CreateByType(obj[key]["-type"], obj[key])
if (!value) value = DC.Item();
item.isOverwriting ? value.overwrite(obj[key], setIsModified) : value.init(obj[key], setIsModified);
}
item.setValue(key, value, true);
if (item[key] && item[key].hasOwnProperty("isModified"))
if (setIsModified) item[key].isModified = true;
else if (typeof setIsModified === "boolean") item[key].isModified = false;
}
});
if (!DC.IsAutoProperty && !item.isOverwriting)
names.forEach(function (name) { item.deleteProperty(name); });
if (setIsModified) item.isModified = true;
else if (typeof setIsModified === "boolean") item.isModified = false;
}
else if (obj === undefined) {
if (item["value"]) {
item.clear();
if (setIsModified) item.isModified = true;
else if (typeof setIsModified === "boolean") item.isModified = false;
}
}
else {
if (!DC.IsAutoProperty && !item.isOverwriting) {
var arr = item.propertyNames.slice(0);
arr.forEach(function (key) {
if (key !== "value")
item.deleteProperty(key);
});
}
if (typeof obj === "string" && obj.trim() === "-deleted") {
if (item.parent) {
item.parent.isModified = true;
if (typeof setIsModified === "boolean") item.parent.isModified = false;
item.parent.deleteProperty(item.parentPropertyName);
}
return;
}
item.setValue("value", obj, true);
if (setIsModified) item.isModified = true;
else if (typeof setIsModified === "boolean") item.isModified = false;
}
};
/**
* Get json string.
* @param {function} predict 'predict' is selected function - Boolean function(key, value){...
* @returns {string} Returns JSON string.
*/
item.toJSON = function (predict) {
function pushPair(key, predict) {
if (item[key] === undefined) return;
var json = getJSON(item[key], predict);
if (json) {
sArr.push("\"");
sArr.push(key);
sArr.push("\"");
sArr.push(":");
sArr.push(json);
sArr.push(",");
}
}
function getJSON(val, predict) {
if (IsCollection(val) || IsItem(val))
return val.toJSON(predict);
else if (val === undefined)
return "null";
else
return JSON.stringify(val);
}
predict = typeof predict === "function" ? predict : function () { return true; };
var json = "";
if (propertyNames.length === 1 && propertyNames[0] === "value" && !IsCollection(item.parent))
json = getJSON(item["value"], predict);
else {
var sArr = ["{"];
if (typeof item["-type"] === "string") {
sArr.push("\"-type\":\"");
sArr.push(item["-type"]);
sArr.push("\"");
sArr.push(",");
}
if (IsCollection(item.parent)) {
sArr.push("\"-id\":\"");
sArr.push(item.parentPropertyName);
sArr.push("\"");
sArr.push(",");
}
if (item.isEmpty) {
sArr.push("\"-isEmpty\":\"");
sArr.push("true");
sArr.push("\"");
sArr.push(",");
}
propertyNames.forEach(function (key) { pushPair(key, predict); });
if (sArr[sArr.length - 1] === ",") { sArr.pop(); }
sArr.push("}");
json = sArr.join("");
}
var thisObj = {
key: item.parentPropertyName,
value: item,
json: json,
predict: predict
};
if (predict.call(thisObj, thisObj.key, thisObj.value)) {
return thisObj.json;
}
return "";
};
item.toPrettyJSON = function (predict) {
var json = item.toJSON(predict);
if (!json) return json;
return JSON.stringify(JSON.parse(json), null, 2);
};
/**
* Returns property names.
* @returns {string} .
*/
item.toString = function () {
if (item.propertyNames.length === 1 && item.propertyNames[0] === "value")
return item.value + "";
return DC.IsDebuger ? "typeOf " + item["-type"] + " properties[" + propertyNames + "]" : "";
};
item.toObject = function () { return JSON.parse(item.toJSON()); };
if (obj !== undefined) { item.init(obj, setIsModified); }
return item;
}
return Item;
})();
var IsItem = function (obj) { return obj && obj.isItem ? true : false; };
DC.IsItem = IsItem;
//#endregion
//#region Collection
DC.Collection = (function () {
/**
* Creates a new Collection.
* @param {any[]} obj Optional. Init. data.
* @param {string} itemType Optional. Item type.
* @param {boolean} setIsModified Optional.
* @returns {Collection} .
*/
function Collection(obj, itemType, setIsModified) {
var collection = {};
collection["-type"] = "DC.Collection";
var _bindId = 0;
var _binds = {};
Object.defineProperty(collection, "-binds", { get: function () { return _binds; } });
var idCount = 0;
var values = {};
Object.defineProperty(collection, "-values", { get: function () { return values; } });
collection.itemType = itemType;
Object.defineProperty(collection, "isCollection", { get: function () { return true; } });
var propertyNames = [];
/** Defined properties in 'collection' */
Object.defineProperty(collection, "propertyNames", { get: function () { return propertyNames; } });
Object.defineProperty(collection, "length", { get: function () { return propertyNames.length; } });
var parent = null;
/** Parent object */
Object.defineProperty(collection, "parent", { get: function () { return parent; }, set: function (value) { parent = value; } });
var parentPropertyName = null;
Object.defineProperty(collection, "parentPropertyName", { get: function () { return parentPropertyName; }, set: function (value) { parentPropertyName = value; } });
var removedIDs = [];
Object.defineProperty(collection, "removedIDs", { get: function () { return removedIDs; } });
Object.defineProperty(collection, "isEmpty", { get: function () { return !collection.length && !collection.removedIDs.length; } });
var isModified = true;
Object.defineProperty(collection, "isModified", {
get: function () {
if (isModified)
return isModified;
return collection.filter(function (item) { return item.isModified; }).length > 0;
},
set: function (val) { isModified = val; },
configurable: true
});
Object.defineProperty(collection, "isOverwriting", { value: false, writable: true, configurable: true });
Object.defineProperty(collection, "firstItem", { get: function () { return values[propertyNames[0]]; } });
Object.defineProperty(collection, "lastItem", { get: function () { return values[propertyNames[propertyNames.length - 1]]; } });
function push(obj, id) {
if (idCount < id) { idCount = id; }
var propertyName = "id_" + idCount++;
if (obj && obj.hasOwnProperty("parent")) { obj.parent = collection; }
if (obj && obj.hasOwnProperty("parentPropertyName")) { obj.parentPropertyName = propertyName; }
obj["-id"] = propertyName;
values[propertyName] = obj;
propertyNames.push(propertyName);
Object.defineProperty(collection, propertyName, {
configurable: true,
get: function () { return values[propertyName]; },
set: function (value) {
if (value !== values[propertyName]) {
var oldValue = values[propertyName];
if (values[propertyName] && typeof values[propertyName].setValue === "function"
&& !IsItem(value) && !IsCollection(value))
values[propertyName] = DC.Item(value);
else
values[propertyName] = value;
if (values[propertyName] && values[propertyName].hasOwnProperty("parent")) { values[propertyName].parent = collection; }
if (values[propertyName] && values[propertyName].hasOwnProperty("parentPropertyName")) { values[propertyName].parentPropertyName = propertyName; }
if (!collection.isOverwriting) {
collection.bindInvoke(propertyName, values[propertyName], oldValue, Array(propertyName));
}
}
}
});
var index = propertyNames.length - 1;
Object.defineProperty(collection, index, {
configurable: true,
get: function () {
return propertyNames[index] && values[propertyNames[index]]
? values[propertyNames[index]]
: undefined;
},
set: function (value) {
if (propertyNames[index] && value !== values[propertyNames[index]]) {
var oldValue = values[propertyNames[index]];
if (values[propertyNames[index]] && typeof values[propertyNames[index]].setValue === "function"
&& !IsItem(value) && !IsCollection(value)) {
values[propertyNames[index]] = DC.Item(value);
}
else {
values[propertyNames[index]] = value;
values[propertyNames[index]]["-id"] = propertyNames[index];
}
if (values[propertyNames[index]] && values[propertyNames[index]].hasOwnProperty("parent")) { values[propertyNames[index]].parent = collection; }
if (values[propertyNames[index]] && values[propertyNames[index]].hasOwnProperty("parentPropertyName")) { values[propertyNames[index]].parentPropertyName = propertyNames[index]; }
if (!collection.isOverwriting) { collection.bindInvoke(index, values[propertyNames[index]], oldValue, [index]); }
}
}
});
return propertyNames.length;
}
collection["-values"].push = push;
function splice(start, deleteCount) {
var result = [];
var removed = propertyNames.splice(start, deleteCount);
removed.forEach(function (propertyName) {
removedIDs.push(propertyName);
values[propertyName].parent = null;
result.push(values[propertyName]);
delete values[propertyName];
delete collection[propertyName];
if (collection.hasOwnProperty(propertyNames.length))
delete collection[propertyNames.length];
});
return result;
}
collection["-values"].splice = splice;
/**
* Invoke bind Collection update.
* @param {Object} val Optional. 'val' is object { action: 'addBind|add|remove|modified', items: [], source: [] }
* @returns {Number} Returns invoke count.
*/
collection.bindInvokeCollection = function (val) {
var count = 0;
for (var key in _binds) {
if (key === "parent" || key === "values") continue;
if (!_binds[key].element || _binds[key].element && _binds[key].element.parentElement) {
if (typeof _binds[key] === "function") {
_binds[key](val);
count++;
}
}
else
delete _binds[key];
}
if (val && _binds && _binds[""])
for (var k in _binds[""])
if (typeof _binds[""][k] === "function")
_binds[""][k]({ newValue: val, oldValue: null, propertyPath: Array(collection.parentPropertyName) });
if (val && _binds && _binds["."])
for (var l in _binds["."])
if (typeof _binds["."][l] === "function") {
if (val && (val.action === "add" || val.action === "remove" || val.action === "modified")) {
_binds["."][l]({ newValue: val, oldValue: null, propertyPath: Array(collection.parentPropertyName) });
}
}
if (val && collection.parent && typeof collection.parent.bindInvoke === "function") {
collection.parent.bindInvoke("parent", val, null, Array(collection.parentPropertyName));
}
return count;
};
/**
* Add bind function in collection.
* @param {function} callback function(val){... // 'val' is object { action: 'addBind|add|remove|modified', items: [], source: [] }.
* @param {HTMLElement} element Optional. 'element' is html element. If element.parentElement is undefined or null, auto delete callback function.
* @returns {function} Returns remove bind function.
*/
collection.addBindCollection = function (callback, element) {
if (typeof callback === "function") {
var bindingId = _bindId++;
if (!element)
element = { parentElement: {} };
_binds[bindingId] = function (val) { callback(val, element); };
_binds[bindingId].element = element;
var removeBinding = function () { delete _binds[bindingId]; };
element.removeBinding = removeBinding;
_binds[bindingId]({
action: "addBind",
items: collection.toArray(true),
parent: collection.parent,
source: collection,
toString: function () { return "action:'addBind', actionItems.length:" + collection.length + " source:" + collection.toString(); }
});
var count = Object.keys(_binds).length;
if (count > 500) {
if (typeof DC.GetPathByItem === "function") {
console.warn("binds count: > 500 => datacontext" + DC.GetPathByItem(collection) + "=" + collection.toString());
} else {
console.warn("binds count: > 500 => " + collection.toString());
}
}
return removeBinding;
}
};
/**
* Invoke bind Collection Item update.
* @param {String} propertyName property name .
* @param {any} newValue Optional.
* @param {any} oldValue Optional.
* @param {ArrayOfString} propertyPath Optional.
* @param {Object} binds Optional.
* @returns {Number} Returns invoke count.
*/
collection.bindInvoke = function (propertyName, newValue, oldValue, propertyPath, binds) {
var count = 0;
var source = values[propertyName] ? values[propertyName].binds : null;
if (!source) {
source = binds ? binds : (function () {
var resut = [];
collection.toArray(true).forEach(function (val) {
if (val) {
if (!propertyName && val.binds)
resut.push(val.binds);
else if (val["-binds"] && val["-binds"][propertyName])
resut.push(val["-binds"][propertyName]);
}
});
return resut;
})();
}
if (source)
for (var key in source) {
if (typeof source[key] === "function") {
if (!source[key].element || source[key].element && source[key].element.parentElement) {
source[key]({ newValue: newValue, oldValue: oldValue, propertyPath: propertyPath });
count++;
}
else
delete source[key];
}
else if (source[key]) {
var c = collection.bindInvoke(propertyName, newValue, oldValue, propertyPath, source[key]);
if (c === 0)
delete source[key];
else
count += c;
}
else
delete source[key];
}
if (_binds && _binds[""])
for (var k in _binds[""])
if (typeof _binds[""][k] === "function")
_binds[""][k]({ newValue: newValue, oldValue: oldValue, propertyPath: propertyPath });
if (_binds && _binds["."])
for (var l in _binds["."])
if (typeof _binds["."][l] === "function") {
if (propertyPath && (propertyPath[0] === "value"
|| newValue && (newValue.action === "add" || newValue.action === "remove" || newValue.action === "modified"))
&& (newValue !== oldValue || newValue.toString() !== oldValue.toString())) {
_binds["."][l]({ newValue: newValue, oldValue: oldValue, propertyPath: propertyPath });
}
}
if (collection.parent && typeof collection.parent.bindInvoke === "function") {
if (!propertyPath) propertyPath = Array();
propertyPath.push(collection.parentPropertyName);
collection.parent.bindInvoke("parent", newValue, oldValue, propertyPath);
}
return count;
};
/**
* Add bind item function in collection.
* @param {String} propertyName property name.
* @param {function} callback function(val, element, parentPropertyValue){...
* @param {HTMLElement} element Optional.'element' is html element.If element.parentElement is undefined or null, auto delete callback function.
* @returns {function} Returns remove bind function.
*/
collection.addBind = function (propertyName, callback, element) {
if (typeof propertyName === "string" && typeof callback === "function") {
if (!element)
element = { parentElement: {} };
var binding = function (parentPropertyValue) { callback(collection[propertyName], element, parentPropertyValue); };
var bindingId = _bindId++;
if (!_binds[propertyName])
_binds[propertyName] = {};
else
Object.keys(_binds[propertyName]).forEach(function (key) {
if (_binds[propertyName][key] && !_binds[propertyName][key].element
|| _binds[propertyName][key] && _binds[propertyName][key].element && _binds[propertyName][key].element.isConnected !== undefined && !_binds[propertyName][key].element.isConnected
|| _binds[propertyName][key] && _binds[propertyName][key].element && !(_binds[propertyName][key].element.parentElement || _binds[propertyName][key].element.parentNode)
) {
//debugger;
if (_binds[propertyName][key] && _binds[propertyName][key].element
&& typeof _binds[propertyName][key].element.removeBinding === "function") {
_binds[propertyName][key].element.removeBinding();
}
delete _binds[propertyName][key];
}
});
_binds[propertyName][bindingId] = binding;
_binds[propertyName][bindingId].element = element;
var removeBinding = function () { delete _binds[propertyName][bindingId]; };
element.removeBinding = removeBinding;
binding();
var count = Object.keys(_binds[propertyName]).length;
if (count > 500) {
if (typeof DC.GetPathByItem === "function") {
console.warn("binds count: > 500 => datacontext" + DC.GetPathByItem(collection[propertyName]) + "=" + collection[propertyName]);
} else {
console.warn("binds count: > 500 => " + propertyName + "=" + collection[propertyName]);
}
}
return removeBinding;
}
};
function parseItem(obj, setIsModified) {
var item;
if ((IsItem(obj) || IsCollection(obj)) && !collection.itemType) {
item = obj;
}
else if (typeof eval(collection.itemType) === "function") {