@zhongguo168a/yxeditor-common 0.0.58 → 0.0.59

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,280 +1029,6 @@ 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
-
1306
1032
  class ConvertUtil {
1307
1033
  anyToBoolean(val, defaultValue = false) {
1308
1034
  if (val == null) {
@@ -1395,44 +1121,41 @@ class ConvertUtil {
1395
1121
  }
1396
1122
  const convertutil = new ConvertUtil();
1397
1123
 
1398
- class MapObject extends Dictionary {
1399
- constructor(object) {
1400
- super();
1401
- this.object = object;
1402
- }
1403
- getValue(key) {
1404
- let value = this.get(key);
1405
- if (!value) {
1406
- value = new MapValue(this, key);
1407
- this.set(key, value);
1408
- }
1409
- return value;
1410
- }
1411
- }
1412
- class MapValue {
1413
- constructor(obj, key) {
1414
- this.obj = obj;
1415
- this.key = key;
1416
- }
1417
- set(value) {
1418
- this.obj[this.key] = value;
1419
- }
1420
- isUndefined() {
1421
- return this.obj[this.key] == undefined;
1422
- }
1423
- toNumber() {
1424
- return maputil.number(this.obj, this.key);
1425
- }
1426
- toString() {
1427
- return maputil.string(this.obj, this.key);
1428
- }
1429
- toBoolean() {
1430
- return maputil.boolean(this.obj, this.key);
1431
- }
1432
- toAny() {
1433
- return this.obj[this.key];
1434
- }
1435
- }
1124
+ // export class MapObject extends Dictionary<string, MapValue> {
1125
+ // constructor(public object: {}) {
1126
+ // super();
1127
+ // }
1128
+ // getValue(key: string): MapValue {
1129
+ // let value = this.get(key);
1130
+ // if (!value) {
1131
+ // value = new MapValue(this, key);
1132
+ // this.set(key, value);
1133
+ // }
1134
+ // return value;
1135
+ // }
1136
+ // }
1137
+ // export class MapValue {
1138
+ // constructor(public obj: {}, public key: any) {
1139
+ // }
1140
+ // set(value: any) {
1141
+ // this.obj[this.key] = value;
1142
+ // }
1143
+ // isUndefined(): boolean {
1144
+ // return this.obj[this.key] == undefined;
1145
+ // }
1146
+ // toNumber(): number {
1147
+ // return maputil.number(this.obj, this.key);
1148
+ // }
1149
+ // toString(): string {
1150
+ // return maputil.string(this.obj, this.key);
1151
+ // }
1152
+ // toBoolean(): boolean {
1153
+ // return maputil.boolean(this.obj, this.key);
1154
+ // }
1155
+ // toAny(): any {
1156
+ // return this.obj[this.key];
1157
+ // }
1158
+ // }
1436
1159
  class MapUtil {
1437
1160
  /**
1438
1161
  * 获取所欲的值
@@ -2751,318 +2474,592 @@ class TreeRoot extends EventDispatcher {
2751
2474
  }
2752
2475
  this.dispatch(TreeRoot.EVENT_INSERT, { node: node, before: before });
2753
2476
  }
2754
- walk(f, inChild) {
2755
- this.origin.walk(f, inChild);
2477
+ walk(f, inChild) {
2478
+ this.origin.walk(f, inChild);
2479
+ }
2480
+ walkPrev(f, inChild) {
2481
+ this.origin.walkPrev(f, inChild);
2482
+ }
2483
+ /**
2484
+ * 派发 refresh 消息
2485
+ * @param node
2486
+ */
2487
+ refresh(node) {
2488
+ this.dispatch(TreeRoot.EVENT_REFRESH, { node: node });
2489
+ }
2490
+ /**
2491
+ * 克隆当前的树结
2492
+ * * 新的tags克隆旧的data
2493
+ * * 新的data指向旧的data
2494
+ */
2495
+ clone() {
2496
+ let root = TreeRoot.create();
2497
+ root.origin = this.origin.clone();
2498
+ root.origin.fixDepthAndRoot(0, root);
2499
+ return root;
2500
+ }
2501
+ toString() {
2502
+ return `TreeRoot`;
2503
+ }
2504
+ createNode(id, isLeaf) {
2505
+ let node = new TreeNode(id, isLeaf);
2506
+ node.setRoot(this);
2507
+ return node;
2508
+ }
2509
+ /**
2510
+ * origin的第一个child
2511
+ */
2512
+ get firstNode() {
2513
+ if (!this.origin) {
2514
+ return null;
2515
+ }
2516
+ if (!this.origin.children) {
2517
+ return null;
2518
+ }
2519
+ return this.origin.children[0];
2520
+ }
2521
+ }
2522
+ /**
2523
+ * 添加数据节点
2524
+ * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2525
+ */
2526
+ TreeRoot.EVENT_ADD = "add";
2527
+ /**
2528
+ * 移除数据节点
2529
+ * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2530
+ */
2531
+ TreeRoot.EVENT_REMOVE = "remove";
2532
+ /**
2533
+ * 插入数据节点
2534
+ * dispatchParams = {node:TreeNode, before:TreeNode}
2535
+ */
2536
+ TreeRoot.EVENT_INSERT = "insert";
2537
+ /**
2538
+ * 刷新数据节点
2539
+ * dispatchParams = {node:TreeNode}
2540
+ */
2541
+ TreeRoot.EVENT_REFRESH = "refresh";
2542
+ /**
2543
+ * 设置数据时触发
2544
+ * dispatchParams = {node:TreeNode}
2545
+ */
2546
+ TreeRoot.EVENT_DATA_SET = "data_set";
2547
+ /**
2548
+ * 设置数据时触发
2549
+ * dispatchParams = {node:TreeNode, tag:string, value:any}
2550
+ */
2551
+ TreeRoot.EVENT_TAGS_SET = "tags_set";
2552
+ ///////////////////////////
2553
+ class TreeRootPlugin {
2554
+ }
2555
+ class TreeNodePlugin {
2556
+ }
2557
+
2558
+ class ListNode {
2559
+ static create(id, data) {
2560
+ let node = new ListNode();
2561
+ node.id = id;
2562
+ node.data = data;
2563
+ return node;
2564
+ }
2565
+ constructor() {
2566
+ this.tags = {};
2567
+ this.uuid = identutil.genOne();
2568
+ }
2569
+ getTags() {
2570
+ if (!this.tags) {
2571
+ this.tags = {};
2572
+ }
2573
+ return this.tags;
2574
+ }
2575
+ setTags(tags) {
2576
+ let obj = this.getTags();
2577
+ for (const key in tags) {
2578
+ obj[key] = tags[key];
2579
+ }
2580
+ }
2581
+ getTag(name) {
2582
+ return this.getTags()[name];
2583
+ }
2584
+ setTag(name, value) {
2585
+ this.getTags()[name] = value;
2586
+ }
2587
+ }
2588
+ class ListSource extends EventDispatcher {
2589
+ constructor() {
2590
+ super(...arguments);
2591
+ this._data = [];
2592
+ }
2593
+ /**
2594
+ * 通过数组创建 ListSource。ListSource的id自动生成。如果需要指定编号,可设置 idCreator
2595
+ * @param arr
2596
+ * @param idCreator
2597
+ */
2598
+ static createByArray(arr, idCreator) {
2599
+ let root = new ListSource();
2600
+ for (let i = 0; i < arr.length; i++) {
2601
+ let item = arr[i];
2602
+ let id = "";
2603
+ if (idCreator) {
2604
+ id = idCreator(item);
2605
+ }
2606
+ else {
2607
+ id = identutil.genOne();
2608
+ }
2609
+ let node = ListNode.create(id, item);
2610
+ root.add(node, false);
2611
+ }
2612
+ return root;
2613
+ }
2614
+ static createByListItem(arr, target) {
2615
+ if (!target) {
2616
+ target = new ListSource();
2617
+ }
2618
+ for (let i = 0; i < arr.length; i++) {
2619
+ let item = arr[i];
2620
+ let id = "";
2621
+ let node = ListNode.create(id);
2622
+ node.id = item.id;
2623
+ node.tags = item.tags;
2624
+ node.data = item.data;
2625
+ target.add(node, false);
2626
+ }
2627
+ return target;
2628
+ }
2629
+ at(index) {
2630
+ return this._data[index];
2631
+ }
2632
+ first() {
2633
+ return this._data[0];
2634
+ }
2635
+ last() {
2636
+ return this._data[this._data.length - 1];
2637
+ }
2638
+ length() {
2639
+ return this._data.length;
2640
+ }
2641
+ /**
2642
+ * 添加项,无法添加相同的项
2643
+ * @param node
2644
+ * @param enableDispatch 默认为true
2645
+ */
2646
+ add(node, enableDispatch = true) {
2647
+ if (this.contains(node)) {
2648
+ return;
2649
+ }
2650
+ this._data.push(node);
2651
+ node.root = this;
2652
+ if (enableDispatch) {
2653
+ this.dispatch(ListSource.EVENT_ADD, { node: node });
2654
+ }
2655
+ }
2656
+ getById(id) {
2657
+ for (let i = 0; i < this._data.length; i++) {
2658
+ let node = this._data[i];
2659
+ if (node.id == id) {
2660
+ return node;
2661
+ }
2662
+ }
2663
+ return null;
2664
+ }
2665
+ remove(node) {
2666
+ let index = this._data.indexOf(node);
2667
+ if (index == -1) {
2668
+ return;
2669
+ }
2670
+ node.root = null;
2671
+ this._data.splice(index, 1);
2672
+ this.dispatch(ListSource.EVENT_REMOVE, { node: node, index: index });
2673
+ }
2674
+ removeAll() {
2675
+ this._data.length = 0;
2676
+ this.dispatch(ListSource.EVENT_REMOVE_ALL);
2677
+ }
2678
+ swap(a, b) {
2679
+ let aidx = this.indexOf(a);
2680
+ let bidx = this.indexOf(b);
2681
+ let d = this._data;
2682
+ [d[aidx], d[bidx]] = [d[bidx], a[aidx]];
2683
+ this.dispatch(ListSource.EVENT_SWAP, { nodes: [a, b], indexs: [aidx, bidx] });
2684
+ }
2685
+ contains(node) {
2686
+ return this.indexOf(node) != -1;
2687
+ }
2688
+ containsById(id) {
2689
+ return this.indexOfById(id) != -1;
2690
+ }
2691
+ indexOf(node) {
2692
+ for (let i = 0; i < this._data.length; i++) {
2693
+ let item = this._data[i];
2694
+ if (item.id == node.id) {
2695
+ return i;
2696
+ }
2697
+ }
2698
+ return -1;
2699
+ }
2700
+ indexOfById(id) {
2701
+ for (let i = 0; i < this._data.length; i++) {
2702
+ let item = this._data[i];
2703
+ if (item.id == id) {
2704
+ return i;
2705
+ }
2706
+ }
2707
+ return -1;
2708
+ }
2709
+ setData(node, data) {
2710
+ if (!node) {
2711
+ return;
2712
+ }
2713
+ node.data = data;
2714
+ this.dispatch(ListSource.EVENT_DATA_SET, { node: node, data: data });
2756
2715
  }
2757
- walkPrev(f, inChild) {
2758
- this.origin.walkPrev(f, inChild);
2716
+ setNodeTags(node, tag, value) {
2717
+ let tags = node.getTags();
2718
+ if (tags[tag] === value) {
2719
+ return;
2720
+ }
2721
+ tags[tag] = value;
2722
+ this.dispatch(ListSource.EVENT_TAGS_SET, { node: node, tag: tag, value: value });
2759
2723
  }
2760
2724
  /**
2761
2725
  * 派发 refresh 消息
2762
2726
  * @param node
2763
2727
  */
2764
2728
  refresh(node) {
2765
- this.dispatch(TreeRoot.EVENT_REFRESH, { node: node });
2766
- }
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;
2777
- }
2778
- toString() {
2779
- return `TreeRoot`;
2729
+ this.dispatch(ListSource.EVENT_REFRESH, { node: node });
2780
2730
  }
2781
- createNode(id, isLeaf) {
2782
- let node = new TreeNode(id, isLeaf);
2783
- node.setRoot(this);
2784
- return node;
2731
+ walk(f) {
2732
+ for (let i = 0; i < this.length(); i++) {
2733
+ let item = this._data[i];
2734
+ if (!f(item)) {
2735
+ return false;
2736
+ }
2737
+ }
2785
2738
  }
2786
- /**
2787
- * origin的第一个child
2788
- */
2789
- get firstNode() {
2790
- if (!this.origin) {
2791
- return null;
2739
+ walkPrev(node, f) {
2740
+ let index = this.indexOf(node);
2741
+ if (index == -1) {
2742
+ return;
2792
2743
  }
2793
- if (!this.origin.children) {
2794
- return null;
2744
+ for (let i = index; i >= 0; i--) {
2745
+ let item = this._data[i];
2746
+ if (!f(item)) {
2747
+ return false;
2748
+ }
2795
2749
  }
2796
- return this.origin.children[0];
2750
+ }
2751
+ get data() {
2752
+ return this._data;
2797
2753
  }
2798
2754
  }
2799
2755
  /**
2800
- * 添加数据节点
2801
- * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2756
+ * 设置数据时触发
2757
+ * dispatchParams = {node:ListNode}
2802
2758
  */
2803
- TreeRoot.EVENT_ADD = "add";
2759
+ ListSource.EVENT_ADD = "add";
2804
2760
  /**
2805
- * 移除数据节点
2806
- * dispatchParams = {node:TreeNode, parent:TreeNode|TreeRoot}
2761
+ * 设置数据时触发
2762
+ * dispatchParams = {node:ListNode, index:number}
2807
2763
  */
2808
- TreeRoot.EVENT_REMOVE = "remove";
2764
+ ListSource.EVENT_REMOVE = "remove";
2809
2765
  /**
2810
- * 插入数据节点
2811
- * dispatchParams = {node:TreeNode, before:TreeNode}
2766
+ * 设置数据时触发
2812
2767
  */
2813
- TreeRoot.EVENT_INSERT = "insert";
2768
+ ListSource.EVENT_REMOVE_ALL = "remove_all";
2814
2769
  /**
2815
2770
  * 刷新数据节点
2816
- * dispatchParams = {node:TreeNode}
2771
+ * dispatchParams = {node:ListNode}
2817
2772
  */
2818
- TreeRoot.EVENT_REFRESH = "refresh";
2773
+ ListSource.EVENT_REFRESH = "refresh";
2819
2774
  /**
2820
2775
  * 设置数据时触发
2821
- * dispatchParams = {node:TreeNode}
2776
+ * dispatchParams = nodes: [ListNode], indexs: [number]
2822
2777
  */
2823
- TreeRoot.EVENT_DATA_SET = "data_set";
2778
+ ListSource.EVENT_SWAP = "swap";
2824
2779
  /**
2825
2780
  * 设置数据时触发
2826
- * dispatchParams = {node:TreeNode, tag:string, value:any}
2781
+ * dispatchParams = {node:ListNode, data:any}
2827
2782
  */
2828
- TreeRoot.EVENT_TAGS_SET = "tags_set";
2829
- ///////////////////////////
2830
- class TreeRootPlugin {
2831
- }
2832
- class TreeNodePlugin {
2833
- }
2783
+ ListSource.EVENT_DATA_SET = "data_set";
2784
+ /**
2785
+ * 设置数据时触发
2786
+ * dispatchParams = {node:ListNode, tag:string, value:any}
2787
+ */
2788
+ ListSource.EVENT_TAGS_SET = "tags_set";
2834
2789
 
2835
- class ListNode {
2836
- static create(id, data) {
2837
- let node = new ListNode();
2838
- node.id = id;
2839
- node.data = data;
2840
- return node;
2790
+ class DictIterator {
2791
+ break() {
2792
+ this._isBreak = true;
2841
2793
  }
2842
- constructor() {
2843
- this.tags = {};
2844
- this.uuid = identutil.genOne();
2794
+ isBreak() {
2795
+ return this._isBreak;
2845
2796
  }
2846
- getTags() {
2847
- if (!this.tags) {
2848
- this.tags = {};
2797
+ }
2798
+ class Dictionary {
2799
+ constructor(data) {
2800
+ this.size = 0;
2801
+ if (data) {
2802
+ this._data = data;
2849
2803
  }
2850
- return this.tags;
2804
+ else {
2805
+ this._data = {};
2806
+ }
2807
+ this._onSet = null;
2808
+ this._onDelete = null;
2851
2809
  }
2852
- setTags(tags) {
2853
- let obj = this.getTags();
2854
- for (const key in tags) {
2855
- obj[key] = tags[key];
2810
+ reset(data) {
2811
+ this._reset(data);
2812
+ }
2813
+ _reset(data, size) {
2814
+ this._data = data;
2815
+ if (size == undefined) {
2816
+ for (let _ in data) {
2817
+ size++;
2818
+ }
2856
2819
  }
2820
+ this.size = size;
2857
2821
  }
2858
- getTag(name) {
2859
- return this.getTags()[name];
2822
+ getData() {
2823
+ return this._data;
2860
2824
  }
2861
- setTag(name, value) {
2862
- this.getTags()[name] = value;
2825
+ /**
2826
+ * 如果使用了 onSet or onDelete,需要注意清理
2827
+ */
2828
+ clear() {
2829
+ this._data = {};
2830
+ this.size = 0;
2863
2831
  }
2864
- }
2865
- class ListSource extends EventDispatcher {
2866
- constructor() {
2867
- super(...arguments);
2868
- this._data = [];
2832
+ /**
2833
+ * 克隆自身所有项到【target】对象,并返回【target】对象
2834
+ * @param target
2835
+ */
2836
+ clone(target) {
2837
+ for (const key in this._data) {
2838
+ let item = this._data[key];
2839
+ // @ts-ignore
2840
+ target.setData(key, item);
2841
+ }
2842
+ return target;
2843
+ }
2844
+ resetByDict(dict) {
2845
+ this._data = dict._data;
2846
+ this.size = dict.size;
2847
+ }
2848
+ onSet(caller, handler) {
2849
+ this._onSetCaller = caller;
2850
+ this._onSet = handler;
2851
+ }
2852
+ onDelete(caller, handler) {
2853
+ this._onDelete = handler;
2854
+ this._onDeleteCaller = caller;
2869
2855
  }
2870
2856
  /**
2871
- * 通过数组创建 ListSource。ListSource的id自动生成。如果需要指定编号,可设置 idCreator
2872
- * @param arr
2873
- * @param idCreator
2857
+ * 设置键值对,如果没有发生变化,不会触发onSet函数
2858
+ * @param key
2859
+ * @param value
2874
2860
  */
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);
2861
+ set(key, value) {
2862
+ this.setData(key, value);
2863
+ }
2864
+ setData(key, value) {
2865
+ if (this._data.hasOwnProperty(key)) ;
2866
+ else {
2867
+ this.size++;
2868
+ }
2869
+ let existed = this._data[key];
2870
+ if (existed == value) {
2871
+ return;
2872
+ }
2873
+ this._data[key] = value;
2874
+ if (this._onSet !== null) {
2875
+ this._onSet.call(this._onSetCaller, key, value);
2888
2876
  }
2889
- return root;
2890
2877
  }
2891
- static createByListItem(arr, target) {
2892
- if (!target) {
2893
- target = new ListSource();
2878
+ setByMap(m) {
2879
+ for (const key in m) {
2880
+ let item = m[key];
2881
+ // @ts-ignore
2882
+ this.setData(key, item);
2894
2883
  }
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);
2884
+ }
2885
+ addDictionary(other) {
2886
+ if (other.length() == 0) {
2887
+ return;
2888
+ }
2889
+ for (const key in other._data) {
2890
+ let item = other._data[key];
2891
+ // @ts-ignore
2892
+ this.setData(key, item);
2893
+ }
2894
+ }
2895
+ deleteDictionary(other) {
2896
+ if (other.length() == 0) {
2897
+ return;
2898
+ }
2899
+ for (const key in other._data) {
2900
+ // @ts-ignore
2901
+ this.delete(key);
2903
2902
  }
2904
- return target;
2905
- }
2906
- at(index) {
2907
- return this._data[index];
2908
2903
  }
2909
- first() {
2910
- return this._data[0];
2904
+ /**
2905
+ * 如果不存在,返回 undefined
2906
+ * @param key
2907
+ */
2908
+ get(key) {
2909
+ return this._data[key];
2911
2910
  }
2912
- last() {
2913
- return this._data[this._data.length - 1];
2911
+ exist(key) {
2912
+ return !!this._data[key];
2914
2913
  }
2915
- length() {
2916
- return this._data.length;
2914
+ delete(key) {
2915
+ this.deleteData(key);
2917
2916
  }
2918
- /**
2919
- * 添加项,无法添加相同的项
2920
- * @param node
2921
- * @param enableDispatch 默认为true
2922
- */
2923
- add(node, enableDispatch = true) {
2924
- if (this.contains(node)) {
2917
+ deleteData(key) {
2918
+ const value = this._data[key];
2919
+ if (value === undefined) {
2925
2920
  return;
2926
2921
  }
2927
- this._data.push(node);
2928
- node.root = this;
2929
- if (enableDispatch) {
2930
- this.dispatch(ListSource.EVENT_ADD, { node: node });
2931
- }
2932
- }
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
- }
2922
+ if (this._onDelete !== null) {
2923
+ this._onDelete.call(this._onDeleteCaller, key, value);
2939
2924
  }
2940
- return null;
2925
+ this._data[key] = null;
2926
+ delete this._data[key];
2927
+ this.size--;
2941
2928
  }
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 });
2929
+ exists(key) {
2930
+ return !!this._data[key];
2950
2931
  }
2951
- removeAll() {
2952
- this._data.length = 0;
2953
- this.dispatch(ListSource.EVENT_REMOVE_ALL);
2932
+ length() {
2933
+ return this.size;
2954
2934
  }
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] });
2935
+ pop(key) {
2936
+ const value = this._data[key];
2937
+ this.deleteData(key);
2938
+ return [value, value !== undefined];
2961
2939
  }
2962
- contains(node) {
2963
- return this.indexOf(node) != -1;
2940
+ isEmpty() {
2941
+ return this.length() === 0;
2964
2942
  }
2965
- containsById(id) {
2966
- return this.indexOfById(id) != -1;
2943
+ items() {
2944
+ const items = [];
2945
+ for (const key in this._data) {
2946
+ let item = this._data[key];
2947
+ items.push(item);
2948
+ }
2949
+ return items;
2967
2950
  }
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
- }
2951
+ keys() {
2952
+ const keys = [];
2953
+ for (const key in this._data) {
2954
+ keys.push(key);
2974
2955
  }
2975
- return -1;
2956
+ return keys;
2976
2957
  }
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;
2958
+ /**
2959
+ *
2960
+ * @param handler 可以使用【iterator】中断遍历
2961
+ * @return iterator 获取是否中断了
2962
+ */
2963
+ forEach(handler) {
2964
+ let iterator = new DictIterator();
2965
+ for (const key in this._data) {
2966
+ let val = this._data[key];
2967
+ // @ts-ignore
2968
+ handler(key, val, iterator);
2969
+ if (iterator.isBreak()) {
2970
+ return iterator;
2982
2971
  }
2983
2972
  }
2984
- return -1;
2973
+ return iterator;
2985
2974
  }
2986
- setData(node, data) {
2987
- if (!node) {
2988
- return;
2975
+ /**
2976
+ */
2977
+ first() {
2978
+ for (const key in this._data) {
2979
+ return this._data[key];
2989
2980
  }
2990
- node.data = data;
2991
- this.dispatch(ListSource.EVENT_DATA_SET, { node: node, data: data });
2981
+ return undefined;
2992
2982
  }
2993
- setNodeTags(node, tag, value) {
2994
- let tags = node.getTags();
2995
- if (tags[tag] === value) {
2996
- return;
2983
+ /**
2984
+ * @param handler 返回true结束迭代
2985
+ */
2986
+ firstByCondition(handler) {
2987
+ for (const key in this._data) {
2988
+ let val = this._data[key];
2989
+ // @ts-ignore
2990
+ if (handler(key, val)) {
2991
+ return val;
2992
+ }
2997
2993
  }
2998
- tags[tag] = value;
2999
- this.dispatch(ListSource.EVENT_TAGS_SET, { node: node, tag: tag, value: value });
3000
2994
  }
3001
2995
  /**
3002
- * 派发 refresh 消息
3003
- * @param node
2996
+ * 移除符合条件的项
2997
+ * @param cond 可以使用【iterator】中断遍历
3004
2998
  */
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;
2999
+ removeByCondition(cond) {
3000
+ let newdata = this._data;
3001
+ let newsize = this.size;
3002
+ let iterator = new DictIterator();
3003
+ for (const key in this._data) {
3004
+ let val = this._data[key];
3005
+ // @ts-ignore
3006
+ if (cond(key, val, iterator)) {
3007
+ newsize--;
3008
+ delete newdata[key];
3009
+ }
3010
+ if (iterator.isBreak()) {
3011
+ break;
3013
3012
  }
3014
3013
  }
3014
+ this.size = newsize;
3015
3015
  }
3016
- walkPrev(node, f) {
3017
- let index = this.indexOf(node);
3018
- if (index == -1) {
3019
- return;
3016
+ /**
3017
+ * 查找符合条件的项保存到【target】对象中,并返回【target】对象
3018
+ * @param cond 可以使用【iterator】中断遍历
3019
+ * @param target 存到目标对象中
3020
+ */
3021
+ findByCondition(cond, target) {
3022
+ if (!target) {
3023
+ throw new Error("需要设置目标对象");
3020
3024
  }
3021
- for (let i = index; i >= 0; i--) {
3022
- let item = this._data[i];
3023
- if (!f(item)) {
3024
- return false;
3025
+ let iterator = new DictIterator();
3026
+ let newdata = {};
3027
+ let newsize = 0;
3028
+ for (const key in this._data) {
3029
+ let val = this._data[key];
3030
+ // @ts-ignore
3031
+ if (cond(key, val, iterator)) {
3032
+ newsize++;
3033
+ newdata[key] = val;
3034
+ }
3035
+ if (iterator.isBreak()) {
3036
+ break;
3025
3037
  }
3026
3038
  }
3039
+ target._reset(newdata, newsize);
3040
+ return target;
3027
3041
  }
3028
- get data() {
3029
- return this._data;
3042
+ groupByCondition(createKey, ITEM_CLASS) {
3043
+ let newdata = {};
3044
+ for (const key in this._data) {
3045
+ let val = this._data[key];
3046
+ // @ts-ignore
3047
+ let newkey = createKey(key, val);
3048
+ let dict = newdata[newkey];
3049
+ if (!dict) {
3050
+ if (ITEM_CLASS) {
3051
+ dict = new ITEM_CLASS();
3052
+ }
3053
+ else {
3054
+ dict = new Dictionary();
3055
+ }
3056
+ newdata[newkey] = dict;
3057
+ }
3058
+ dict.set(key, val);
3059
+ }
3060
+ return newdata;
3030
3061
  }
3031
3062
  }
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
3063
 
3067
3064
  class DictNode {
3068
3065
  static create(id, data) {
@@ -6391,5 +6388,5 @@ class WidgetUtil {
6391
6388
  }
6392
6389
  var widgetutil = new WidgetUtil();
6393
6390
 
6394
- export { ArrayUtil, AssetLoader, AssetURI, BitUtil, CallbackList, CameraUtil, CanvasUtil, Controller, ControllerDict, ControllerRepo, ConvertUtil, DictIterator, DictNode, DictSource, Dictionary, Dispatcher, DocUtil, ErrorUtil, EventDispatcher, EventItem, EventUtil, Factory, FloatUtil, GeometryUtil, IdentUtil, LOGGER_EVENT, LinkError, List, ListIterator, ListNode, ListSource, Listener, LogLevel, LogManager, Logger, MapObject, MapUtil, MapValue, MathUtil, Model, NetUtil, PathUtil, Pool, RandUtil, Route, RouteController, RouteList, RouteView, SampleCallbackList, SamplePool, SamplePoolSet, ScaleUtil, SimpleModel, StringUtil, TimeUtil, Timer, TreeIterator, TreeNode, TreeNodePlugin, TreeRoot, TreeRootPlugin, TreeUtil, UIManager, UIObjectRepo, UIPoolConfig, UISingleConfig, UIUtil, ViewDict, ViewEvent, ViewHistory, ViewList, ViewRepo, ViewState, WidgetAlign, WidgetUtil, XAssetManager, XEvent, arrayutil, assetx, bitutil, camerautil, canvasutil, convertutil, ctrlrepo, docutil, errorutil, eventmgr, eventutil, floatutil, formatutil, geoutil, identutil, loadAtlas, logmgr, maputil, mathutil, netutil, parseAtlas, parsePlist, pathutil, randutil, resutil, scaleutil, stringutil, timer, timeutil, treeutil, uimgr, uirepo, uiutil, viewhistorys, viewrepo, widgetutil };
6391
+ export { ArrayUtil, AssetLoader, AssetURI, BitUtil, CallbackList, CameraUtil, CanvasUtil, Controller, ControllerDict, ControllerRepo, ConvertUtil, DictIterator, DictNode, DictSource, Dictionary, Dispatcher, DocUtil, ErrorUtil, EventDispatcher, EventItem, EventUtil, Factory, FloatUtil, GeometryUtil, IdentUtil, LOGGER_EVENT, LinkError, List, ListIterator, ListNode, ListSource, Listener, LogLevel, LogManager, Logger, MapUtil, MathUtil, Model, NetUtil, PathUtil, Pool, RandUtil, Route, RouteController, RouteList, RouteView, SampleCallbackList, SamplePool, SamplePoolSet, ScaleUtil, SimpleModel, StringUtil, TimeUtil, Timer, TreeIterator, TreeNode, TreeNodePlugin, TreeRoot, TreeRootPlugin, TreeUtil, UIManager, UIObjectRepo, UIPoolConfig, UISingleConfig, UIUtil, ViewDict, ViewEvent, ViewHistory, ViewList, ViewRepo, ViewState, WidgetAlign, WidgetUtil, XAssetManager, XEvent, arrayutil, assetx, bitutil, camerautil, canvasutil, convertutil, ctrlrepo, docutil, errorutil, eventmgr, eventutil, floatutil, formatutil, geoutil, identutil, loadAtlas, logmgr, maputil, mathutil, netutil, parseAtlas, parsePlist, pathutil, randutil, resutil, scaleutil, stringutil, timer, timeutil, treeutil, uimgr, uirepo, uiutil, viewhistorys, viewrepo, widgetutil };
6395
6392
  //# sourceMappingURL=index.esm.js.map