@zhongguo168a/yxeditor-common 0.0.57 → 0.0.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1029,6 +1029,280 @@ let eventItemPool = new Pool({
1029
1029
  }
1030
1030
  });
1031
1031
 
1032
+ class DictIterator {
1033
+ break() {
1034
+ this._isBreak = true;
1035
+ }
1036
+ isBreak() {
1037
+ return this._isBreak;
1038
+ }
1039
+ }
1040
+ class Dictionary {
1041
+ constructor(data) {
1042
+ this.size = 0;
1043
+ if (data) {
1044
+ this._data = data;
1045
+ }
1046
+ else {
1047
+ this._data = {};
1048
+ }
1049
+ this._onSet = null;
1050
+ this._onDelete = null;
1051
+ }
1052
+ reset(data) {
1053
+ this._reset(data);
1054
+ }
1055
+ _reset(data, size) {
1056
+ this._data = data;
1057
+ if (size == undefined) {
1058
+ for (let _ in data) {
1059
+ size++;
1060
+ }
1061
+ }
1062
+ this.size = size;
1063
+ }
1064
+ getData() {
1065
+ return this._data;
1066
+ }
1067
+ /**
1068
+ * 如果使用了 onSet or onDelete,需要注意清理
1069
+ */
1070
+ clear() {
1071
+ this._data = {};
1072
+ this.size = 0;
1073
+ }
1074
+ /**
1075
+ * 克隆自身所有项到【target】对象,并返回【target】对象
1076
+ * @param target
1077
+ */
1078
+ clone(target) {
1079
+ for (const key in this._data) {
1080
+ let item = this._data[key];
1081
+ // @ts-ignore
1082
+ target.setData(key, item);
1083
+ }
1084
+ return target;
1085
+ }
1086
+ resetByDict(dict) {
1087
+ this._data = dict._data;
1088
+ this.size = dict.size;
1089
+ }
1090
+ onSet(caller, handler) {
1091
+ this._onSetCaller = caller;
1092
+ this._onSet = handler;
1093
+ }
1094
+ onDelete(caller, handler) {
1095
+ this._onDelete = handler;
1096
+ this._onDeleteCaller = caller;
1097
+ }
1098
+ /**
1099
+ * 设置键值对,如果没有发生变化,不会触发onSet函数
1100
+ * @param key
1101
+ * @param value
1102
+ */
1103
+ set(key, value) {
1104
+ this.setData(key, value);
1105
+ }
1106
+ setData(key, value) {
1107
+ if (this._data.hasOwnProperty(key)) ;
1108
+ else {
1109
+ this.size++;
1110
+ }
1111
+ let existed = this._data[key];
1112
+ if (existed == value) {
1113
+ return;
1114
+ }
1115
+ this._data[key] = value;
1116
+ if (this._onSet !== null) {
1117
+ this._onSet.call(this._onSetCaller, key, value);
1118
+ }
1119
+ }
1120
+ setByMap(m) {
1121
+ for (const key in m) {
1122
+ let item = m[key];
1123
+ // @ts-ignore
1124
+ this.setData(key, item);
1125
+ }
1126
+ }
1127
+ addDictionary(other) {
1128
+ if (other.length() == 0) {
1129
+ return;
1130
+ }
1131
+ for (const key in other._data) {
1132
+ let item = other._data[key];
1133
+ // @ts-ignore
1134
+ this.setData(key, item);
1135
+ }
1136
+ }
1137
+ deleteDictionary(other) {
1138
+ if (other.length() == 0) {
1139
+ return;
1140
+ }
1141
+ for (const key in other._data) {
1142
+ // @ts-ignore
1143
+ this.delete(key);
1144
+ }
1145
+ }
1146
+ /**
1147
+ * 如果不存在,返回 undefined
1148
+ * @param key
1149
+ */
1150
+ get(key) {
1151
+ return this._data[key];
1152
+ }
1153
+ exist(key) {
1154
+ return !!this._data[key];
1155
+ }
1156
+ delete(key) {
1157
+ this.deleteData(key);
1158
+ }
1159
+ deleteData(key) {
1160
+ const value = this._data[key];
1161
+ if (value === undefined) {
1162
+ return;
1163
+ }
1164
+ if (this._onDelete !== null) {
1165
+ this._onDelete.call(this._onDeleteCaller, key, value);
1166
+ }
1167
+ this._data[key] = null;
1168
+ delete this._data[key];
1169
+ this.size--;
1170
+ }
1171
+ exists(key) {
1172
+ return !!this._data[key];
1173
+ }
1174
+ length() {
1175
+ return this.size;
1176
+ }
1177
+ pop(key) {
1178
+ const value = this._data[key];
1179
+ this.deleteData(key);
1180
+ return [value, value !== undefined];
1181
+ }
1182
+ isEmpty() {
1183
+ return this.length() === 0;
1184
+ }
1185
+ items() {
1186
+ const items = [];
1187
+ for (const key in this._data) {
1188
+ let item = this._data[key];
1189
+ items.push(item);
1190
+ }
1191
+ return items;
1192
+ }
1193
+ keys() {
1194
+ const keys = [];
1195
+ for (const key in this._data) {
1196
+ keys.push(key);
1197
+ }
1198
+ return keys;
1199
+ }
1200
+ /**
1201
+ *
1202
+ * @param handler 可以使用【iterator】中断遍历
1203
+ * @return iterator 获取是否中断了
1204
+ */
1205
+ forEach(handler) {
1206
+ let iterator = new DictIterator();
1207
+ for (const key in this._data) {
1208
+ let val = this._data[key];
1209
+ // @ts-ignore
1210
+ handler(key, val, iterator);
1211
+ if (iterator.isBreak()) {
1212
+ return iterator;
1213
+ }
1214
+ }
1215
+ return iterator;
1216
+ }
1217
+ /**
1218
+ */
1219
+ first() {
1220
+ for (const key in this._data) {
1221
+ return this._data[key];
1222
+ }
1223
+ return undefined;
1224
+ }
1225
+ /**
1226
+ * @param handler 返回true结束迭代
1227
+ */
1228
+ firstByCondition(handler) {
1229
+ for (const key in this._data) {
1230
+ let val = this._data[key];
1231
+ // @ts-ignore
1232
+ if (handler(key, val)) {
1233
+ return val;
1234
+ }
1235
+ }
1236
+ }
1237
+ /**
1238
+ * 移除符合条件的项
1239
+ * @param cond 可以使用【iterator】中断遍历
1240
+ */
1241
+ removeByCondition(cond) {
1242
+ let newdata = this._data;
1243
+ let newsize = this.size;
1244
+ let iterator = new DictIterator();
1245
+ for (const key in this._data) {
1246
+ let val = this._data[key];
1247
+ // @ts-ignore
1248
+ if (cond(key, val, iterator)) {
1249
+ newsize--;
1250
+ delete newdata[key];
1251
+ }
1252
+ if (iterator.isBreak()) {
1253
+ break;
1254
+ }
1255
+ }
1256
+ this.size = newsize;
1257
+ }
1258
+ /**
1259
+ * 查找符合条件的项保存到【target】对象中,并返回【target】对象
1260
+ * @param cond 可以使用【iterator】中断遍历
1261
+ * @param target 存到目标对象中
1262
+ */
1263
+ findByCondition(cond, target) {
1264
+ if (!target) {
1265
+ throw new Error("需要设置目标对象");
1266
+ }
1267
+ let iterator = new DictIterator();
1268
+ let newdata = {};
1269
+ let newsize = 0;
1270
+ for (const key in this._data) {
1271
+ let val = this._data[key];
1272
+ // @ts-ignore
1273
+ if (cond(key, val, iterator)) {
1274
+ newsize++;
1275
+ newdata[key] = val;
1276
+ }
1277
+ if (iterator.isBreak()) {
1278
+ break;
1279
+ }
1280
+ }
1281
+ target._reset(newdata, newsize);
1282
+ return target;
1283
+ }
1284
+ groupByCondition(createKey, ITEM_CLASS) {
1285
+ let newdata = {};
1286
+ for (const key in this._data) {
1287
+ let val = this._data[key];
1288
+ // @ts-ignore
1289
+ let newkey = createKey(key, val);
1290
+ let dict = newdata[newkey];
1291
+ if (!dict) {
1292
+ if (ITEM_CLASS) {
1293
+ dict = new ITEM_CLASS();
1294
+ }
1295
+ else {
1296
+ dict = new Dictionary();
1297
+ }
1298
+ newdata[newkey] = dict;
1299
+ }
1300
+ dict.set(key, val);
1301
+ }
1302
+ return newdata;
1303
+ }
1304
+ }
1305
+
1032
1306
  class ConvertUtil {
1033
1307
  anyToBoolean(val, defaultValue = false) {
1034
1308
  if (val == null) {
@@ -2472,597 +2746,323 @@ class TreeRoot extends EventDispatcher {
2472
2746
  let child = children[i];
2473
2747
  child.id = i.toString();
2474
2748
  child.setTag("title", i.toString());
2475
- this.refresh(child);
2476
- }
2477
- }
2478
- this.dispatch(TreeRoot.EVENT_INSERT, { node: node, before: before });
2479
- }
2480
- walk(f, inChild) {
2481
- this.origin.walk(f, inChild);
2482
- }
2483
- walkPrev(f, inChild) {
2484
- this.origin.walkPrev(f, inChild);
2485
- }
2486
- /**
2487
- * 派发 refresh 消息
2488
- * @param node
2489
- */
2490
- refresh(node) {
2491
- this.dispatch(TreeRoot.EVENT_REFRESH, { node: node });
2492
- }
2493
- /**
2494
- * 克隆当前的树结
2495
- * * 新的tags克隆旧的data
2496
- * * 新的data指向旧的data
2497
- */
2498
- clone() {
2499
- let root = TreeRoot.create();
2500
- root.origin = this.origin.clone();
2501
- root.origin.fixDepthAndRoot(0, root);
2502
- return root;
2503
- }
2504
- toString() {
2505
- return `TreeRoot`;
2506
- }
2507
- createNode(id, isLeaf) {
2508
- let node = new TreeNode(id, isLeaf);
2509
- node.setRoot(this);
2510
- return node;
2511
- }
2512
- /**
2513
- * origin的第一个child
2514
- */
2515
- get firstNode() {
2516
- if (!this.origin) {
2517
- return null;
2518
- }
2519
- if (!this.origin.children) {
2520
- return null;
2521
- }
2522
- return this.origin.children[0];
2523
- }
2524
- }
2525
- /**
2526
- * 添加数据节点
2527
- * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2528
- */
2529
- TreeRoot.EVENT_ADD = "add";
2530
- /**
2531
- * 移除数据节点
2532
- * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2533
- */
2534
- TreeRoot.EVENT_REMOVE = "remove";
2535
- /**
2536
- * 插入数据节点
2537
- * dispatchParams = {node:TreeNode, before:TreeNode}
2538
- */
2539
- TreeRoot.EVENT_INSERT = "insert";
2540
- /**
2541
- * 刷新数据节点
2542
- * dispatchParams = {node:TreeNode}
2543
- */
2544
- TreeRoot.EVENT_REFRESH = "refresh";
2545
- /**
2546
- * 设置数据时触发
2547
- * dispatchParams = {node:TreeNode}
2548
- */
2549
- TreeRoot.EVENT_DATA_SET = "data_set";
2550
- /**
2551
- * 设置数据时触发
2552
- * dispatchParams = {node:TreeNode, tag:string, value:any}
2553
- */
2554
- TreeRoot.EVENT_TAGS_SET = "tags_set";
2555
- ///////////////////////////
2556
- class TreeRootPlugin {
2557
- }
2558
- class TreeNodePlugin {
2559
- }
2560
-
2561
- class ListNode {
2562
- static create(id, data) {
2563
- let node = new ListNode();
2564
- node.id = id;
2565
- node.data = data;
2566
- return node;
2567
- }
2568
- constructor() {
2569
- this.tags = {};
2570
- this.uuid = identutil.genOne();
2571
- }
2572
- getTags() {
2573
- if (!this.tags) {
2574
- this.tags = {};
2575
- }
2576
- return this.tags;
2577
- }
2578
- setTags(tags) {
2579
- let obj = this.getTags();
2580
- for (const key in tags) {
2581
- obj[key] = tags[key];
2582
- }
2583
- }
2584
- getTag(name) {
2585
- return this.getTags()[name];
2586
- }
2587
- setTag(name, value) {
2588
- this.getTags()[name] = value;
2589
- }
2590
- }
2591
- class ListSource extends EventDispatcher {
2592
- constructor() {
2593
- super(...arguments);
2594
- this._data = [];
2595
- }
2596
- /**
2597
- * 通过数组创建 ListSource。ListSource的id自动生成。如果需要指定编号,可设置 idCreator
2598
- * @param arr
2599
- * @param idCreator
2600
- */
2601
- static createByArray(arr, idCreator) {
2602
- let root = new ListSource();
2603
- for (let i = 0; i < arr.length; i++) {
2604
- let item = arr[i];
2605
- let id = "";
2606
- if (idCreator) {
2607
- id = idCreator(item);
2608
- }
2609
- else {
2610
- id = identutil.genOne();
2611
- }
2612
- let node = ListNode.create(id, item);
2613
- root.add(node, false);
2614
- }
2615
- return root;
2616
- }
2617
- static createByListItem(arr, target) {
2618
- if (!target) {
2619
- target = new ListSource();
2620
- }
2621
- for (let i = 0; i < arr.length; i++) {
2622
- let item = arr[i];
2623
- let id = "";
2624
- let node = ListNode.create(id);
2625
- node.id = item.id;
2626
- node.tags = item.tags;
2627
- node.data = item.data;
2628
- target.add(node, false);
2629
- }
2630
- return target;
2631
- }
2632
- at(index) {
2633
- return this._data[index];
2634
- }
2635
- first() {
2636
- return this._data[0];
2637
- }
2638
- last() {
2639
- return this._data[this._data.length - 1];
2640
- }
2641
- length() {
2642
- return this._data.length;
2643
- }
2644
- /**
2645
- * 添加项,无法添加相同的项
2646
- * @param node
2647
- * @param enableDispatch 默认为true
2648
- */
2649
- add(node, enableDispatch = true) {
2650
- if (this.contains(node)) {
2651
- return;
2652
- }
2653
- this._data.push(node);
2654
- node.root = this;
2655
- if (enableDispatch) {
2656
- this.dispatch(ListSource.EVENT_ADD, { node: node });
2657
- }
2658
- }
2659
- getById(id) {
2660
- for (let i = 0; i < this._data.length; i++) {
2661
- let node = this._data[i];
2662
- if (node.id == id) {
2663
- return node;
2664
- }
2665
- }
2666
- return null;
2667
- }
2668
- remove(node) {
2669
- let index = this._data.indexOf(node);
2670
- if (index == -1) {
2671
- return;
2672
- }
2673
- node.root = null;
2674
- this._data.splice(index, 1);
2675
- this.dispatch(ListSource.EVENT_REMOVE, { node: node, index: index });
2676
- }
2677
- removeAll() {
2678
- this._data.length = 0;
2679
- this.dispatch(ListSource.EVENT_REMOVE_ALL);
2680
- }
2681
- swap(a, b) {
2682
- let aidx = this.indexOf(a);
2683
- let bidx = this.indexOf(b);
2684
- let d = this._data;
2685
- [d[aidx], d[bidx]] = [d[bidx], a[aidx]];
2686
- this.dispatch(ListSource.EVENT_SWAP, { nodes: [a, b], indexs: [aidx, bidx] });
2687
- }
2688
- contains(node) {
2689
- return this.indexOf(node) != -1;
2690
- }
2691
- containsById(id) {
2692
- return this.indexOfById(id) != -1;
2693
- }
2694
- indexOf(node) {
2695
- for (let i = 0; i < this._data.length; i++) {
2696
- let item = this._data[i];
2697
- if (item.id == node.id) {
2698
- return i;
2699
- }
2700
- }
2701
- return -1;
2702
- }
2703
- indexOfById(id) {
2704
- for (let i = 0; i < this._data.length; i++) {
2705
- let item = this._data[i];
2706
- if (item.id == id) {
2707
- return i;
2708
- }
2709
- }
2710
- return -1;
2711
- }
2712
- setData(node, data) {
2713
- if (!node) {
2714
- return;
2749
+ this.refresh(child);
2750
+ }
2715
2751
  }
2716
- node.data = data;
2717
- this.dispatch(ListSource.EVENT_DATA_SET, { node: node, data: data });
2752
+ this.dispatch(TreeRoot.EVENT_INSERT, { node: node, before: before });
2718
2753
  }
2719
- setNodeTags(node, tag, value) {
2720
- let tags = node.getTags();
2721
- if (tags[tag] === value) {
2722
- return;
2723
- }
2724
- tags[tag] = value;
2725
- this.dispatch(ListSource.EVENT_TAGS_SET, { node: node, tag: tag, value: value });
2754
+ walk(f, inChild) {
2755
+ this.origin.walk(f, inChild);
2756
+ }
2757
+ walkPrev(f, inChild) {
2758
+ this.origin.walkPrev(f, inChild);
2726
2759
  }
2727
2760
  /**
2728
2761
  * 派发 refresh 消息
2729
2762
  * @param node
2730
2763
  */
2731
2764
  refresh(node) {
2732
- this.dispatch(ListSource.EVENT_REFRESH, { node: node });
2765
+ this.dispatch(TreeRoot.EVENT_REFRESH, { node: node });
2733
2766
  }
2734
- walk(f) {
2735
- for (let i = 0; i < this.length(); i++) {
2736
- let item = this._data[i];
2737
- if (!f(item)) {
2738
- return false;
2739
- }
2740
- }
2767
+ /**
2768
+ * 克隆当前的树结
2769
+ * * 新的tags克隆旧的data
2770
+ * * 新的data指向旧的data
2771
+ */
2772
+ clone() {
2773
+ let root = TreeRoot.create();
2774
+ root.origin = this.origin.clone();
2775
+ root.origin.fixDepthAndRoot(0, root);
2776
+ return root;
2741
2777
  }
2742
- walkPrev(node, f) {
2743
- let index = this.indexOf(node);
2744
- if (index == -1) {
2745
- return;
2778
+ toString() {
2779
+ return `TreeRoot`;
2780
+ }
2781
+ createNode(id, isLeaf) {
2782
+ let node = new TreeNode(id, isLeaf);
2783
+ node.setRoot(this);
2784
+ return node;
2785
+ }
2786
+ /**
2787
+ * origin的第一个child
2788
+ */
2789
+ get firstNode() {
2790
+ if (!this.origin) {
2791
+ return null;
2746
2792
  }
2747
- for (let i = index; i >= 0; i--) {
2748
- let item = this._data[i];
2749
- if (!f(item)) {
2750
- return false;
2751
- }
2793
+ if (!this.origin.children) {
2794
+ return null;
2752
2795
  }
2753
- }
2754
- get data() {
2755
- return this._data;
2796
+ return this.origin.children[0];
2756
2797
  }
2757
2798
  }
2758
2799
  /**
2759
- * 设置数据时触发
2760
- * dispatchParams = {node:ListNode}
2800
+ * 添加数据节点
2801
+ * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2761
2802
  */
2762
- ListSource.EVENT_ADD = "add";
2803
+ TreeRoot.EVENT_ADD = "add";
2763
2804
  /**
2764
- * 设置数据时触发
2765
- * dispatchParams = {node:ListNode, index:number}
2805
+ * 移除数据节点
2806
+ * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2766
2807
  */
2767
- ListSource.EVENT_REMOVE = "remove";
2808
+ TreeRoot.EVENT_REMOVE = "remove";
2768
2809
  /**
2769
- * 设置数据时触发
2810
+ * 插入数据节点
2811
+ * dispatchParams = {node:TreeNode, before:TreeNode}
2770
2812
  */
2771
- ListSource.EVENT_REMOVE_ALL = "remove_all";
2813
+ TreeRoot.EVENT_INSERT = "insert";
2772
2814
  /**
2773
2815
  * 刷新数据节点
2774
- * dispatchParams = {node:ListNode}
2775
- */
2776
- ListSource.EVENT_REFRESH = "refresh";
2777
- /**
2778
- * 设置数据时触发
2779
- * dispatchParams = nodes: [ListNode], indexs: [number]
2816
+ * dispatchParams = {node:TreeNode}
2780
2817
  */
2781
- ListSource.EVENT_SWAP = "swap";
2818
+ TreeRoot.EVENT_REFRESH = "refresh";
2782
2819
  /**
2783
2820
  * 设置数据时触发
2784
- * dispatchParams = {node:ListNode, data:any}
2821
+ * dispatchParams = {node:TreeNode}
2785
2822
  */
2786
- ListSource.EVENT_DATA_SET = "data_set";
2823
+ TreeRoot.EVENT_DATA_SET = "data_set";
2787
2824
  /**
2788
2825
  * 设置数据时触发
2789
- * dispatchParams = {node:ListNode, tag:string, value:any}
2826
+ * dispatchParams = {node:TreeNode, tag:string, value:any}
2790
2827
  */
2791
- ListSource.EVENT_TAGS_SET = "tags_set";
2792
-
2793
- class DictIterator {
2794
- break() {
2795
- this._isBreak = true;
2796
- }
2797
- isBreak() {
2798
- return this._isBreak;
2799
- }
2828
+ TreeRoot.EVENT_TAGS_SET = "tags_set";
2829
+ ///////////////////////////
2830
+ class TreeRootPlugin {
2800
2831
  }
2801
- class Dictionary {
2802
- constructor(data) {
2803
- this.size = 0;
2804
- if (data) {
2805
- this._data = data;
2806
- }
2807
- else {
2808
- this._data = {};
2809
- }
2810
- this._onSet = null;
2811
- this._onDelete = null;
2832
+ class TreeNodePlugin {
2833
+ }
2834
+
2835
+ class ListNode {
2836
+ static create(id, data) {
2837
+ let node = new ListNode();
2838
+ node.id = id;
2839
+ node.data = data;
2840
+ return node;
2812
2841
  }
2813
- reset(data) {
2814
- this._reset(data);
2842
+ constructor() {
2843
+ this.tags = {};
2844
+ this.uuid = identutil.genOne();
2815
2845
  }
2816
- _reset(data, size) {
2817
- this._data = data;
2818
- if (size == undefined) {
2819
- for (let _ in data) {
2820
- size++;
2821
- }
2846
+ getTags() {
2847
+ if (!this.tags) {
2848
+ this.tags = {};
2822
2849
  }
2823
- this.size = size;
2824
- }
2825
- getData() {
2826
- return this._data;
2827
- }
2828
- /**
2829
- * 如果使用了 onSet or onDelete,需要注意清理
2830
- */
2831
- clear() {
2832
- this._data = {};
2833
- this.size = 0;
2850
+ return this.tags;
2834
2851
  }
2835
- /**
2836
- * 克隆自身所有项到【target】对象,并返回【target】对象
2837
- * @param target
2838
- */
2839
- clone(target) {
2840
- for (const key in this._data) {
2841
- let item = this._data[key];
2842
- // @ts-ignore
2843
- target.setData(key, item);
2852
+ setTags(tags) {
2853
+ let obj = this.getTags();
2854
+ for (const key in tags) {
2855
+ obj[key] = tags[key];
2844
2856
  }
2845
- return target;
2846
2857
  }
2847
- resetByDict(dict) {
2848
- this._data = dict._data;
2849
- this.size = dict.size;
2858
+ getTag(name) {
2859
+ return this.getTags()[name];
2850
2860
  }
2851
- onSet(caller, handler) {
2852
- this._onSetCaller = caller;
2853
- this._onSet = handler;
2861
+ setTag(name, value) {
2862
+ this.getTags()[name] = value;
2854
2863
  }
2855
- onDelete(caller, handler) {
2856
- this._onDelete = handler;
2857
- this._onDeleteCaller = caller;
2864
+ }
2865
+ class ListSource extends EventDispatcher {
2866
+ constructor() {
2867
+ super(...arguments);
2868
+ this._data = [];
2858
2869
  }
2859
2870
  /**
2860
- * 设置键值对,如果没有发生变化,不会触发onSet函数
2861
- * @param key
2862
- * @param value
2871
+ * 通过数组创建 ListSource。ListSource的id自动生成。如果需要指定编号,可设置 idCreator
2872
+ * @param arr
2873
+ * @param idCreator
2863
2874
  */
2864
- set(key, value) {
2865
- this.setData(key, value);
2866
- }
2867
- setData(key, value) {
2868
- if (this._data.hasOwnProperty(key)) ;
2869
- else {
2870
- this.size++;
2871
- }
2872
- let existed = this._data[key];
2873
- if (existed == value) {
2874
- return;
2875
- }
2876
- this._data[key] = value;
2877
- if (this._onSet !== null) {
2878
- this._onSet.call(this._onSetCaller, key, value);
2879
- }
2880
- }
2881
- setByMap(m) {
2882
- for (const key in m) {
2883
- let item = m[key];
2884
- // @ts-ignore
2885
- this.setData(key, item);
2886
- }
2887
- }
2888
- addDictionary(other) {
2889
- if (other.length() == 0) {
2890
- return;
2891
- }
2892
- for (const key in other._data) {
2893
- let item = other._data[key];
2894
- // @ts-ignore
2895
- this.setData(key, item);
2875
+ static createByArray(arr, idCreator) {
2876
+ let root = new ListSource();
2877
+ for (let i = 0; i < arr.length; i++) {
2878
+ let item = arr[i];
2879
+ let id = "";
2880
+ if (idCreator) {
2881
+ id = idCreator(item);
2882
+ }
2883
+ else {
2884
+ id = identutil.genOne();
2885
+ }
2886
+ let node = ListNode.create(id, item);
2887
+ root.add(node, false);
2896
2888
  }
2889
+ return root;
2897
2890
  }
2898
- deleteDictionary(other) {
2899
- if (other.length() == 0) {
2900
- return;
2891
+ static createByListItem(arr, target) {
2892
+ if (!target) {
2893
+ target = new ListSource();
2901
2894
  }
2902
- for (const key in other._data) {
2903
- // @ts-ignore
2904
- this.delete(key);
2895
+ for (let i = 0; i < arr.length; i++) {
2896
+ let item = arr[i];
2897
+ let id = "";
2898
+ let node = ListNode.create(id);
2899
+ node.id = item.id;
2900
+ node.tags = item.tags;
2901
+ node.data = item.data;
2902
+ target.add(node, false);
2905
2903
  }
2904
+ return target;
2906
2905
  }
2907
- /**
2908
- * 如果不存在,返回 undefined
2909
- * @param key
2910
- */
2911
- get(key) {
2912
- return this._data[key];
2906
+ at(index) {
2907
+ return this._data[index];
2913
2908
  }
2914
- exist(key) {
2915
- return !!this._data[key];
2909
+ first() {
2910
+ return this._data[0];
2916
2911
  }
2917
- delete(key) {
2918
- this.deleteData(key);
2912
+ last() {
2913
+ return this._data[this._data.length - 1];
2919
2914
  }
2920
- deleteData(key) {
2921
- const value = this._data[key];
2922
- if (value === undefined) {
2915
+ length() {
2916
+ return this._data.length;
2917
+ }
2918
+ /**
2919
+ * 添加项,无法添加相同的项
2920
+ * @param node
2921
+ * @param enableDispatch 默认为true
2922
+ */
2923
+ add(node, enableDispatch = true) {
2924
+ if (this.contains(node)) {
2923
2925
  return;
2924
2926
  }
2925
- if (this._onDelete !== null) {
2926
- this._onDelete.call(this._onDeleteCaller, key, value);
2927
+ this._data.push(node);
2928
+ node.root = this;
2929
+ if (enableDispatch) {
2930
+ this.dispatch(ListSource.EVENT_ADD, { node: node });
2927
2931
  }
2928
- this._data[key] = null;
2929
- delete this._data[key];
2930
- this.size--;
2931
2932
  }
2932
- exists(key) {
2933
- return !!this._data[key];
2933
+ getById(id) {
2934
+ for (let i = 0; i < this._data.length; i++) {
2935
+ let node = this._data[i];
2936
+ if (node.id == id) {
2937
+ return node;
2938
+ }
2939
+ }
2940
+ return null;
2934
2941
  }
2935
- length() {
2936
- return this.size;
2942
+ remove(node) {
2943
+ let index = this._data.indexOf(node);
2944
+ if (index == -1) {
2945
+ return;
2946
+ }
2947
+ node.root = null;
2948
+ this._data.splice(index, 1);
2949
+ this.dispatch(ListSource.EVENT_REMOVE, { node: node, index: index });
2937
2950
  }
2938
- pop(key) {
2939
- const value = this._data[key];
2940
- this.deleteData(key);
2941
- return [value, value !== undefined];
2951
+ removeAll() {
2952
+ this._data.length = 0;
2953
+ this.dispatch(ListSource.EVENT_REMOVE_ALL);
2942
2954
  }
2943
- isEmpty() {
2944
- return this.length() === 0;
2955
+ swap(a, b) {
2956
+ let aidx = this.indexOf(a);
2957
+ let bidx = this.indexOf(b);
2958
+ let d = this._data;
2959
+ [d[aidx], d[bidx]] = [d[bidx], a[aidx]];
2960
+ this.dispatch(ListSource.EVENT_SWAP, { nodes: [a, b], indexs: [aidx, bidx] });
2945
2961
  }
2946
- items() {
2947
- const items = [];
2948
- for (const key in this._data) {
2949
- let item = this._data[key];
2950
- items.push(item);
2951
- }
2952
- return items;
2962
+ contains(node) {
2963
+ return this.indexOf(node) != -1;
2953
2964
  }
2954
- keys() {
2955
- const keys = [];
2956
- for (const key in this._data) {
2957
- keys.push(key);
2965
+ containsById(id) {
2966
+ return this.indexOfById(id) != -1;
2967
+ }
2968
+ indexOf(node) {
2969
+ for (let i = 0; i < this._data.length; i++) {
2970
+ let item = this._data[i];
2971
+ if (item.id == node.id) {
2972
+ return i;
2973
+ }
2958
2974
  }
2959
- return keys;
2975
+ return -1;
2960
2976
  }
2961
- /**
2962
- *
2963
- * @param handler 可以使用【iterator】中断遍历
2964
- * @return iterator 获取是否中断了
2965
- */
2966
- forEach(handler) {
2967
- let iterator = new DictIterator();
2968
- for (const key in this._data) {
2969
- let val = this._data[key];
2970
- // @ts-ignore
2971
- handler(key, val, iterator);
2972
- if (iterator.isBreak()) {
2973
- return iterator;
2977
+ indexOfById(id) {
2978
+ for (let i = 0; i < this._data.length; i++) {
2979
+ let item = this._data[i];
2980
+ if (item.id == id) {
2981
+ return i;
2974
2982
  }
2975
2983
  }
2976
- return iterator;
2984
+ return -1;
2977
2985
  }
2978
- /**
2979
- */
2980
- first() {
2981
- for (const key in this._data) {
2982
- return this._data[key];
2986
+ setData(node, data) {
2987
+ if (!node) {
2988
+ return;
2983
2989
  }
2984
- return undefined;
2990
+ node.data = data;
2991
+ this.dispatch(ListSource.EVENT_DATA_SET, { node: node, data: data });
2985
2992
  }
2986
- /**
2987
- * @param handler 返回true结束迭代
2988
- */
2989
- firstByCondition(handler) {
2990
- for (const key in this._data) {
2991
- let val = this._data[key];
2992
- // @ts-ignore
2993
- if (handler(key, val)) {
2994
- return val;
2995
- }
2993
+ setNodeTags(node, tag, value) {
2994
+ let tags = node.getTags();
2995
+ if (tags[tag] === value) {
2996
+ return;
2996
2997
  }
2998
+ tags[tag] = value;
2999
+ this.dispatch(ListSource.EVENT_TAGS_SET, { node: node, tag: tag, value: value });
2997
3000
  }
2998
3001
  /**
2999
- * 移除符合条件的项
3000
- * @param cond 可以使用【iterator】中断遍历
3002
+ * 派发 refresh 消息
3003
+ * @param node
3001
3004
  */
3002
- removeByCondition(cond) {
3003
- let newdata = this._data;
3004
- let newsize = this.size;
3005
- let iterator = new DictIterator();
3006
- for (const key in this._data) {
3007
- let val = this._data[key];
3008
- // @ts-ignore
3009
- if (cond(key, val, iterator)) {
3010
- newsize--;
3011
- delete newdata[key];
3012
- }
3013
- if (iterator.isBreak()) {
3014
- break;
3005
+ refresh(node) {
3006
+ this.dispatch(ListSource.EVENT_REFRESH, { node: node });
3007
+ }
3008
+ walk(f) {
3009
+ for (let i = 0; i < this.length(); i++) {
3010
+ let item = this._data[i];
3011
+ if (!f(item)) {
3012
+ return false;
3015
3013
  }
3016
3014
  }
3017
- this.size = newsize;
3018
3015
  }
3019
- /**
3020
- * 查找符合条件的项保存到【target】对象中,并返回【target】对象
3021
- * @param cond 可以使用【iterator】中断遍历
3022
- * @param target 存到目标对象中
3023
- */
3024
- findByCondition(cond, target) {
3025
- if (!target) {
3026
- throw new Error("需要设置目标对象");
3016
+ walkPrev(node, f) {
3017
+ let index = this.indexOf(node);
3018
+ if (index == -1) {
3019
+ return;
3027
3020
  }
3028
- let iterator = new DictIterator();
3029
- let newdata = {};
3030
- let newsize = 0;
3031
- for (const key in this._data) {
3032
- let val = this._data[key];
3033
- // @ts-ignore
3034
- if (cond(key, val, iterator)) {
3035
- newsize++;
3036
- newdata[key] = val;
3037
- }
3038
- if (iterator.isBreak()) {
3039
- break;
3021
+ for (let i = index; i >= 0; i--) {
3022
+ let item = this._data[i];
3023
+ if (!f(item)) {
3024
+ return false;
3040
3025
  }
3041
3026
  }
3042
- target._reset(newdata, newsize);
3043
- return target;
3044
3027
  }
3045
- groupByCondition(createKey, ITEM_CLASS) {
3046
- let newdata = {};
3047
- for (const key in this._data) {
3048
- let val = this._data[key];
3049
- // @ts-ignore
3050
- let newkey = createKey(key, val);
3051
- let dict = newdata[newkey];
3052
- if (!dict) {
3053
- if (ITEM_CLASS) {
3054
- dict = new ITEM_CLASS();
3055
- }
3056
- else {
3057
- dict = new Dictionary();
3058
- }
3059
- newdata[newkey] = dict;
3060
- }
3061
- dict.set(key, val);
3062
- }
3063
- return newdata;
3028
+ get data() {
3029
+ return this._data;
3064
3030
  }
3065
3031
  }
3032
+ /**
3033
+ * 设置数据时触发
3034
+ * dispatchParams = {node:ListNode}
3035
+ */
3036
+ ListSource.EVENT_ADD = "add";
3037
+ /**
3038
+ * 设置数据时触发
3039
+ * dispatchParams = {node:ListNode, index:number}
3040
+ */
3041
+ ListSource.EVENT_REMOVE = "remove";
3042
+ /**
3043
+ * 设置数据时触发
3044
+ */
3045
+ ListSource.EVENT_REMOVE_ALL = "remove_all";
3046
+ /**
3047
+ * 刷新数据节点
3048
+ * dispatchParams = {node:ListNode}
3049
+ */
3050
+ ListSource.EVENT_REFRESH = "refresh";
3051
+ /**
3052
+ * 设置数据时触发
3053
+ * dispatchParams = nodes: [ListNode], indexs: [number]
3054
+ */
3055
+ ListSource.EVENT_SWAP = "swap";
3056
+ /**
3057
+ * 设置数据时触发
3058
+ * dispatchParams = {node:ListNode, data:any}
3059
+ */
3060
+ ListSource.EVENT_DATA_SET = "data_set";
3061
+ /**
3062
+ * 设置数据时触发
3063
+ * dispatchParams = {node:ListNode, tag:string, value:any}
3064
+ */
3065
+ ListSource.EVENT_TAGS_SET = "tags_set";
3066
3066
 
3067
3067
  class DictNode {
3068
3068
  static create(id, data) {