zhitong.yu
8 天以前 378d781e6f35f89652aa36e079a8b7fc44cea77e
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
/**
 * 原生js项目widget模块化框架
 */
 
;(function () {
  const Loader = window.Loader //loader.js中定义
  const jQuery = window.jQuery
  if (!jQuery) {
    throw new Error("请引入 jQuery 库")
  }
 
  const layer = window.layer // 请引入layer弹窗插件
  if (!window.layer) {
    throw new Error("请引入 layer.js弹窗 库")
  }
 
  // 内部参数
  let thismap
  let basePath = ""
  let widgetsdata = []
  let defoptions
  let cacheVersion
  let isdebuger
  const removeKeys = ["_class"]
 
  /**
   * widget事件类型枚举
   *  @enum {string}
   */
  const WidgetEventType = {
    /**
     * 在实例初始化之后、创建之前执行
     */
    beforeCreate: "beforeCreate",
    /**
     * 实例创建后执行
     */
    created: "created",
    /**
     * 在activat挂载开始之前调用
     */
    beforeActivate: "beforeActivate",
    /**
     * activate方法调用后
     */
    activated: "activated",
    /**
     *view弹窗构造完成后后调用
     */
    openView: "openView",
    /**
     * 实例销毁之前调用
     */
    beforeDisable: "beforeDisable",
    /**
     *实例销毁完成调用
     */
    disabled: "disabled",
 
    /**
     *加载完成 未做任何其他处理前
     */
    loadBefore: "loadBefore",
    /**
     *加载完成,执行所有内部处理后
     */
    load: "load"
  }
 
 /**
 * 初始化widget管理器,在构造完成map后调用一次即可。
 *
 * @param {Map} map 地图对象
 * @param {object} [widgetcfg={}] 全局配置(一般存放在widget.json),包括:
 * @param {BaseWidget.widgetOptions} [widgetcfg.defaultOptions] 所有widget的默认参数值,可以系统内所有widget相同配置统一在此处传入,额外的个性化的再配置到各widget中。
 * @param {BaseWidget.widgetOptions[]} [widgetcfg.openAtStart] 默认自启动并不可释放的插件,其中autoDisable和openAtStart固定,设置无效。
 * @param {BaseWidget.widgetOptions[]} [widgetcfg.widgets] 所有插件配置,传入后后续激活时,只用传入uri即可。
 * @param {string} [widgetcfg.version] 加载资源时,附加的参数,主要为了清理浏览器缓存,可选值:"time"(实时时间戳)或固定的字符串值,每次发布新版本换下固定值。
 * @param {boolean} [widgetcfg.debugger] 是否显示插件测试栏,true时会在地图下侧显示所有插件测试按钮,方便测试。
 *
 * @param {string} [_basePath=''] widgets目录所在的主路径(统一前缀), 如果widgets目录不在主页面一起或存在路由时,可以传入自定义主目录,值为 widgets目录相对于当前html页面的相对路径。
 * @return {void}  无
 * @example
let widgetCfg ={
  "version": "2017",
  "defaultOptions": {
    "style": "dark",
    "windowOptions": {
      "skin": "layer-mars-dialog animation-scale-up",
      "position": {
        "top": 50,
        "right": 10
      },
      "maxmin": false,
      "resize": true
    },
    "autoReset": false,
    "autoDisable": true,
    "disableOther": true
  },
  "openAtStart": [
    {
      "name": "放大缩小按钮",
      "uri": "widgets/toolButton/zoom.js"
    }
  ],
  "widgets": [
    {
      "name": "模板-div弹窗",
      "uri": "widgets/_example_divwin/widget.js"
    },
    {
      "name": "模板-append模板",
      "uri": "widgets/_example_append/widget.js"
    }
  ]
}
es5widget.init(map, widgetCfg, './')
 */
  function init(map, widgetcfg = {}, _basePath = "") {
    thismap = map
    basePath = _basePath
 
    widgetsdata = []
    defoptions = mars3d.Util.merge(
      {
        windowOptions: { position: "rt", maxmin: false, resize: true },
        autoDisable: true,
        disableOther: true
      },
      widgetcfg.defaultOptions
    )
 
    cacheVersion = widgetcfg.version
    if (cacheVersion === "time") {
      cacheVersion = new Date().getTime()
    }
 
    // 将自启动的加入
    let arrtemp = widgetcfg.openAtStart
    if (arrtemp && arrtemp.length > 0) {
      for (let i = 0; i < arrtemp.length; i++) {
        const item = arrtemp[i]
        if (!item.hasOwnProperty("uri") || item.uri === "") {
          // eslint-disable-next-line no-console
          console.error("widget未配置uri", item)
          continue
        }
        if (item.hasOwnProperty("visible") && !item.visible) {
          continue
        }
 
        item.autoDisable = false
        item.openAtStart = true
        item._nodebug = true
 
        bindDefOptions(item)
 
        item._firstConfigBak = { ...item }
        widgetsdata.push(item)
      }
    }
 
    // 显示测试栏
    // 为了方便测试,所有widget会在页面下侧生成一排按钮,每个按钮对应一个widget,单击后激活对应widget
    isdebuger = widgetcfg.debugger
    if (isdebuger) {
      const inhtml =
        '<div id="widget-testbar" class="mars3d-widgetbar animation-slide-bottom no-print-view" > ' +
        '     <div style="height: 30px; line-height:30px;"><b style="color: #4db3ff;">widget测试栏</b>&nbsp;&nbsp;<button  id="widget-testbar-remove"  type="button" class="btn btn-link btn-xs">关闭</button> </div>' +
        '     <button id="widget-testbar-disableAll" type="button" class="btn btn-info" ><i class="fa fa-globe"></i>漫游</button>' +
        "</div>"
      jQuery("body").append(inhtml)
 
      jQuery("#widget-testbar-remove").click(function (e) {
        removeDebugeBar()
      })
      jQuery("#widget-testbar-disableAll").click(function (e) {
        disableAll()
      })
    }
 
    // 将配置的加入
    arrtemp = widgetcfg.widgets
    if (arrtemp && arrtemp.length > 0) {
      for (let i = 0; i < arrtemp.length; i++) {
        const item = arrtemp[i]
        if (item.type === "group") {
          let inhtml =
            ' <div class="btn-group dropup">  <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-align-justify"></i>' +
            item.name +
            ' <span class="caret"></span></button> <ul class="dropdown-menu">'
          for (let j = 0; j < item.children.length; j++) {
            const childItem = item.children[j]
            if (!childItem.hasOwnProperty("uri") || childItem.uri === "") {
              // eslint-disable-next-line no-console
              console.error("widget未配置uri", childItem)
              continue
            }
 
            inhtml +=
              ' <li data-widget="' + childItem.uri + '" class="widget-btn" ><a href="#"><i class="fa fa-star"></i>' + childItem.name + "</a></li>"
 
            bindDefOptions(childItem)
            childItem._firstConfigBak = { ...childItem }
            widgetsdata.push(childItem) // 将配置的加入
          }
          inhtml += "</ul></div>"
 
          if (isdebuger && !item._nodebug) {
            jQuery("#widget-testbar").append(inhtml)
          }
        } else {
          if (!item.hasOwnProperty("uri") || item.uri === "") {
            // eslint-disable-next-line no-console
            console.error("widget未配置uri", item)
            continue
          }
 
          // 显示测试栏
          if (isdebuger && !item._nodebug) {
            const inhtml =
              '<button type="button" class="btn btn-primary widget-btn" data-widget="' +
              item.uri +
              '"  > <i class="fa fa-globe"></i>' +
              item.name +
              " </button>"
            jQuery("#widget-testbar").append(inhtml)
          }
 
          bindDefOptions(item)
          item._firstConfigBak = { ...item }
          widgetsdata.push(item) // 将配置的加入
        }
      }
 
      if (isdebuger) {
        jQuery("#widget-testbar .widget-btn").each(function () {
          jQuery(this).click(function (e) {
            const uri = jQuery(this).attr("data-widget")
            if (!uri || uri === "") {
              return
            }
 
            if (isActivate(uri)) {
              disable(uri)
            } else {
              activate(uri)
            }
          })
        })
      }
    }
 
    for (let i = 0; i < widgetsdata.length; i++) {
      const item = widgetsdata[i]
 
      if (item.openAtStart || item.createAtStart) {
        _arrLoadWidget.push(item)
      }
    }
 
    jQuery(window).resize(function () {
      for (let i = 0; i < widgetsdata.length; i++) {
        const item = widgetsdata[i]
        if (item._class) {
          item._class.indexResize() // BaseWidget: indexResize
        }
      }
    })
 
    if (isdebuger) {
      const hash = getLocationParam()
      if (hash) {
        activate(hash)
      }
    }
 
    loadWidgetJs()
  }
 
  /**
   * 获取默认init时中传入配置的 windowOptions 参数
   * @return {object} windowOptions参数默认值
   * @see BaseWidget.widgetOptions
   */
  function getDefWindowOptions() {
    return mars3d.Util.clone(defoptions.windowOptions, removeKeys)
  }
 
  function getLocationParam() {
    let param = window.location.toString()
    if (param.indexOf("#") === -1) {
      return ""
    }
    param = param.split("#")
    if (param && param.length > 0) {
      return param[1]
    }
  }
 
  function bindDefOptions(item) {
    // 赋默认值至options(跳过已存在设置值)
    if (defoptions) {
      for (const aa in defoptions) {
        if (aa === "windowOptions") {
          // for (var jj in defoptions['windowOptions']) {
          //    if (!item['windowOptions'].hasOwnProperty(jj)) {
          //        item['windowOptions'][jj] = defoptions['windowOptions'][jj];
          //    }
          // }
        } else if (!item.hasOwnProperty(aa)) {
          item[aa] = defoptions[aa]
        }
      }
    }
 
    // 赋值内部使用属性
    item.path = getFilePath(basePath + item.uri)
    item.name = item.name || item.label // 兼容name和label命名
  }
 
  /**
 * 激活指定 widget模块
 *
 * @param {string|BaseWidget.widgetOptions} item 指widget模块的uri 或 指模块的配置参数,当有配置参数时,参数优先级是:
 * 【activate方法传入的配置 > init方法传入的配置(widget.json) > widget.js内部配置的】
 * @param {Map} [item.map] 当单页面简单场景没有init时,也可以传入map来使用单个widget
 * @param {boolean} [noDisableOther=false]  不释放其他已激活的widget
 * @return {BaseWidget.widgetOptions}  指widget模块对象
 * @example
//常用方式,直接使用uri
es5widget.activate("widgets/bookmark/widget.js");
 
//使用对象,可以传入更多参数,具体参数参看配置项手册,。
es5widget.activate({
  name:"视角书签"
  uri: "widgets/bookmark/widget.js",
  autoDisable: true,
  testdata:'测试数据1987', //传数据进widget内部,widget内部使用this.config.testdata获取到传的数据
  success:function(thisWidget){
    //创建完成的回调方法
  }
});
 */
  function activate(item, noDisableOther) {
    if (!thismap && item.map) {
      init(item.map, {}, item.basePath)
    }
 
    // 参数是字符串id或uri时
    if (typeof item === "string") {
      item = { uri: item }
 
      if (noDisableOther != null) {
        // 是否释放其他已激活的widget
        item.disableOther = !noDisableOther
      }
    } else {
      if (!item.uri) {
        // eslint-disable-next-line no-console
        console.error("activate激活widget时需要uri参数!", item)
      }
    }
 
    let thisItem
    for (let i = 0; i < widgetsdata.length; i++) {
      const othitem = widgetsdata[i]
      if (item.uri === othitem.uri || (othitem.id && item.uri === othitem.id)) {
        thisItem = othitem
        if (thisItem.isloading) {
          return thisItem
        } // 激活了正在loading的widget 防止快速双击了菜单
 
        // 赋值
        for (const aa in item) {
          if (aa === "uri") {
            continue
          }
          thisItem[aa] = item[aa]
        }
        break
      }
    }
 
    if (!thisItem) {
      bindDefOptions(item)
      thisItem = item
      // 非config中配置的,外部传入,首次激活
      if (!item._firstConfigBak) {
        item._firstConfigBak = { ...item }
      }
      widgetsdata.push(item)
    }
 
    if (isdebuger) {
      // eslint-disable-next-line no-console
      console.log("开始激活widget:" + thisItem.uri)
      window.location.hash = "#" + thisItem.uri
    }
 
    // 释放其他已激活的widget
    if (thisItem.disableOther) {
      if (Array.isArray(thisItem.disableOther)) {
        disable(thisItem.disableOther)
      } else {
        disableAll(thisItem.uri, thisItem.group)
      }
    }
 
    if (thisItem.group) {
      disableGroup(thisItem.group, thisItem.uri)
    }
 
    // 激活本widget
    if (thisItem._class) {
      if (thisItem._class.isActivate) {
        // 已激活时
        if (thisItem._class.update) {
          // 刷新
          thisItem._class.update()
        } else {
          // 重启
          _reStart(thisItem)
        }
      } else {
        thisItem._class.activateBase() // BaseWidget: activateBase
      }
    } else {
      for (let i = 0; i < _arrLoadWidget.length; i++) {
        if (_arrLoadWidget[i].uri === thisItem.uri) {
          // 如果已在加载列表中的直接跳出
          return _arrLoadWidget[i]
        }
      }
      _arrLoadWidget.push(thisItem)
 
      if (_arrLoadWidget.length === 1) {
        loadWidgetJs()
      }
    }
    return thisItem
  }
 
  let timetemp
  // 重启
  function _reStart(thisItem) {
    clearInterval(timetemp)
 
    thisItem._class.disableBase()
    timetemp = setInterval(function () {
      if (thisItem._class.isActivate) {
        return
      }
      thisItem._class.activateBase()
      clearInterval(timetemp)
    }, 200)
  }
 
  /**
   * 获取指定的widget配置信息
   *
   * @param {string} uri widget的uri 或 id
   * @return {BaseWidget.widgetOptions} widget配置信息
   */
  function getWidget(uri) {
    for (let i = 0; i < widgetsdata.length; i++) {
      const item = widgetsdata[i]
 
      if (uri === item.uri || uri === item.id) {
        return item
      }
    }
  }
 
  /**
   * 获取指定的widget 对应的实例化对象
   *
   * @param {string} uri widget的uri 或 id
   * @return {BaseWidget} widget对应的实例化对象
   */
  function getClass(uri) {
    const item = getWidget(uri)
    if (item) {
      return item._class
    } else {
      return null
    }
  }
 
  /**
   * 获取widget的当前激活状态
   *
   * @param {string} uri widget的uri 或 id
   * @return {boolean} 是否激活
   */
  function isActivate(uri) {
    const _class = getClass(uri)
    if (!_class) {
      return false
    }
    return _class.isActivate
  }
 
  /**
   * 设置view弹窗的显示和隐藏,基于修改css实现
   *
   * @param {string} uri widget的uri 或 id
   * @param {boolean} show 是否显示
   * @param {number} [index] 当有多个view时,可以指定单个操作的view的index
   * @return {boolean} 是否成功设置
   */
  function setViewShow(uri, show, index) {
    const _class = getClass(uri)
    if (_class) {
      _class.setViewShow(show, index)
    }
    return _class?.isActivate
  }
 
  /**
   * 释放指定的widget
   *
   * @param {string|string[]} uri widget的uri 或 id
   * @return {boolean} 是否成功调用了释放
   */
  function disable(uri) {
    if (!uri) {
      return false
    }
 
    if (Array.isArray(uri)) {
      const arrUri = uri
      for (let i = 0; i < widgetsdata.length; i++) {
        const item = widgetsdata[i]
 
        for (let j = 0; j < arrUri.length; j++) {
          const uri = arrUri[j]
          if (item._class && (uri === item.uri || uri === item.id)) {
            item._class.disableBase()
            arrUri.splice(j, 1)
            break
          }
        }
      }
    } else {
      if (typeof uri === "object") {
        uri = uri.uri
      }
      for (let i = 0; i < widgetsdata.length; i++) {
        const item = widgetsdata[i]
 
        if (item._class && (uri === item.uri || uri === item.id)) {
          item._class.disableBase()
          return true
        }
      }
    }
    return false
  }
 
  /**
   * 关闭释放所有widget
   *
   * @export
   * @param {string|boolean} [nodisable] 传string时 指定不释放的widget的uri或id ,传true值强制释放所有widget(默认autoDisable为false的widet不会释放)
   * @param {string} [group] 指定强制释放的group名(默认autoDisable为false的widet不会释放),传入group值后会强制释放所有同group组的widget
   * @return {void}  无
   */
  function disableAll(nodisable, group) {
    for (let i = 0; i < widgetsdata.length; i++) {
      const item = widgetsdata[i]
 
      if (group && item.group === group) {
        // 同组别的全部释放
      } else {
        if (nodisable !== true && !item.autoDisable) {
          continue
        }
      }
 
      // 指定不释放的跳过
      if (nodisable && (nodisable === item.uri || nodisable === item.id)) {
        continue
      }
 
      if (item._class) {
        item._class.disableBase() // BaseWidget: disableBase
      }
    }
  }
 
  /**
   * 关闭释放同组widget
   *
   * @param {string} group 指定强制释放的group名
   * @param {string} [nodisable]  指定不释放的widget的uri或id
   * @return {void}  无
   */
  function disableGroup(group, nodisable) {
    if (!group) {
      return
    }
 
    for (let i = 0; i < widgetsdata.length; i++) {
      const item = widgetsdata[i]
      if (item.group === group) {
        // 指定不释放的跳过
        if (nodisable && (nodisable === item.uri || nodisable === item.id)) {
          continue
        }
        if (item._class) {
          item._class.disableBase() /// /BaseWidget: disableBase
        }
      }
    }
  }
 
  /**
   * 遍历所有widget
   * @param {Function} method 回调方法
   * @return {void}  无
   */
  function eachWidget(method) {
    for (let i = 0; i < widgetsdata.length; i++) {
      const item = widgetsdata[i]
      method(item)
    }
  }
 
  const _arrLoadWidget = []
  let loadItem
  let isloading
  function loadWidgetJs() {
    if (_arrLoadWidget.length === 0) {
      return
    }
 
    if (isloading) {
      setTimeout(loadWidgetJs, 500)
      return
    }
    isloading = true
 
    loadItem = _arrLoadWidget[0]
    loadItem.isloading = true
    let _uri = loadItem.uri
    if (cacheVersion) {
      if (_uri.indexOf("?") === -1) {
        _uri += "?cache=" + cacheVersion
      } else {
        _uri += "&cache=" + cacheVersion
      }
    }
 
    if (window.NProgress) {
      window.NProgress.start()
    }
 
    fire(WidgetEventType.loadBefore, {
      sourceTarget: loadItem
    })
 
    Loader.async([basePath + _uri], function () {
      isloading = false
      loadItem.isloading = false
 
      if (window.NProgress) {
        window.NProgress.done(true)
      }
 
      _arrLoadWidget.shift()
      loadWidgetJs()
    })
  }
 
  /**
   * 绑定类到当前对应js的widget中。
   *
   * @param {BaseWidget} _class 定义的BaseWidget子类
   * @return {object} 实例化后的对象
   */
  function bindClass(_class) {
    fire(WidgetEventType.load, {
      sourceTarget: _class
    })
 
    if (!loadItem) {
      const _jspath = getThisJSPath()
      for (let i = 0; i < widgetsdata.length; i++) {
        const item = widgetsdata[i]
        if (_jspath.endsWith(item.uri)) {
          item.isloading = false
          item._class = new _class(thismap, item)
          item._class.activateBase() // BaseWidget: activateBase
          return item._class
        }
      }
    } else {
      loadItem.isloading = false
      loadItem._class = new _class(thismap, loadItem)
      loadItem._class.activateBase() // BaseWidget: activateBase
      return loadItem._class
    }
  }
 
  function getThisJSPath() {
    let jsPath
    const js = document.scripts
    for (let i = js.length - 1; i >= 0; i--) {
      jsPath = js[i].src
      if (!jsPath) {
        continue
      }
      if (jsPath.indexOf("widgets") === -1) {
        continue
      }
      // jsPath = jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
      return jsPath
    }
    return ""
  }
 
  // 获取路径
  function getFilePath(file) {
    const pos = file.lastIndexOf("/")
    return file.substring(0, pos + 1)
  }
 
  /**
   * 移除Widget测试栏(当有开启debugger时)
   * @return {void}  无
   */
  function removeDebugeBar() {
    jQuery("#widget-testbar").remove()
  }
 
  /**
   * 获取配置的version配置参数,用于附加清除浏览器缓存
   * @return {string} 配置的version参数
   */
  function getCacheVersion() {
    return cacheVersion
  }
 
  /**
   * 获取init方法传入的主目录配置参数
   * @return {string} 主目录配置参数
   */
  function getBasePath() {
    return basePath
  }
 
  /**
   * 销毁对象
   * @return {void}  无
   */
  function destroy() {
    for (let i = 0; i < widgetsdata.length; i++) {
      const item = widgetsdata[i]
 
      if (item._class) {
        item._class.disableBase() // BaseWidget: disableBase
 
        if (item._class.destroy) {
          item._class.destroy()
        }
        delete item._class
      }
    }
 
    thismap = null
  }
 
  // 事件相关
  const eventTarget = new mars3d.BaseClass()
 
  /**
   * 绑定指定类型事件监听器
   *
   * @param {WidgetEventType|WidgetEventType[]} types 事件类型
   * @param {Function} [fn] 绑定的监听器回调方法
   * @param {object} [context]  侦听器的上下文(this关键字将指向的对象)。
   * @return {void}  无
   */
  function on(types, fn, context) {
    return eventTarget.on(types, fn, context)
  }
 
  /**
   * 解除绑定指定类型事件监听器
   *
   * @param {WidgetEventType|WidgetEventType[]} types 事件类型
   * @param {Function} [fn] 绑定的监听器回调方法
   * @param {object} [context]  侦听器的上下文(this关键字将指向的对象)。
   * @return {void}  无
   */
  function off(types, fn, context) {
    return eventTarget.off(types, fn, context)
  }
 
  /**
   * 触发指定类型的事件。
   *
   * @param {WidgetEventType} type 事件类型
   * @param {object} data 传输的数据或对象,可在事件回调方法中event对象中获取进行使用
   * @param {BaseClass|object} [propagate] 将事件传播给父类 (用addEventParent设置)
   * @return {void}  无
   */
  function fire(type, data, propagate) {
    return eventTarget.fire(type, data, propagate)
  }
 
  /**
   * 绑定一次性执行的指定类型事件监听器
   * 与on类似,监听器只会被触发一次,然后被删除。
   *
   * @param {WidgetEventType|WidgetEventType[]} types 事件类型
   * @param {Function} [fn] 绑定的监听器回调方法
   * @param {object} [context]  侦听器的上下文(this关键字将指向的对象)。
   * @return {void}  无
   */
  function once(types, fn, context) {
    return eventTarget.once(types, fn, context)
  }
 
  /**
   * 是否有绑定指定的事件
   *
   * @param {WidgetEventType} type 事件类型
   * @param {BaseClass} [propagate] 是否判断指定的父类 (用addEventParent设置的)
   * @return {boolean} 是否存在
   */
  function listens(type, propagate) {
    return eventTarget.listens(type, propagate)
  }
 
  let _resources_cache = []
 
  /**
   * widget 配置参数
   *
   * @typedef {object} BaseWidget.widgetOptions
   *
   * @property {string} name 必须,中文名称,用于标识和弹窗标题。
   * @property {string} uri 必须,JS文件路径,路径是相对于widgets目录的路径。如:"widgets/bookmark/widget.js"
   * @property {string} [id] 定义该插件的唯一标识,方便后续判断。
   * @property {boolean} [autoDisable=true] 激活其他新插件时,是否自动释放本插件
   * @property {boolean} [disableOther=true] 激活本插件时,是否释放其它已激活的插件
   * @property {string} [group] 配置group后,同group下的widget互斥,打开任意一个会自动释放其他的
   * @property {object} [windowOptions] 存在弹窗的插件的弹窗相关参数配置,更多参数请参考 [layer弹窗API]{@link https://layui.gitee.io/v2/docs/modules/layer.html} 包括:
   * @property {number|string} [windowOptions.width] 窗口宽度,可以是 像素数字(像素值) 或者 字符串(屏幕宽度百分比),示例:200 或 "20%"
   * @property {number|string} [windowOptions.height] 窗口高度,可以是 像素数字(像素值) 或者 字符串(屏幕高度百分比),示例:600 或 "50%"
   * @property {string|object} [windowOptions.position='auto'] 窗口所在位置坐标,配置字符串可选值:auto垂直水平居中,t顶部,b底部,r右边缘,l左边缘,lt左上角,lb左下角,rt右上角,rb右下角;也可以配置对象:
   * @property {number|string} [windowOptions.position.top] 位置css的top值,可以是 像素数字(像素值) 或者 字符串(屏幕高度百分比),示例:10 或 "5%"
   * @property {number|string} [windowOptions.position.bottom] 位置css的top值,可以是 像素数字(像素值) 或者 字符串(屏幕高度百分比),示例:10 或 "5%"
   * @property {number|string} [windowOptions.position.left] 位置css的top值,可以是 像素数字(像素值) 或者 字符串(屏幕宽度百分比),示例:10 或 "5%"
   * @property {number|string} [windowOptions.position.right] 位置css的top值,可以是 像素数字(像素值) 或者 字符串(屏幕宽度百分比),示例:10 或 "5%"
   * @property {number} [windowOptions.minHeight] 限定的窗口最小高度(像素值),默认不限制
   * @property {number} [windowOptions.maxHeight] 限定的窗口最大高度(像素值),默认不限制
   * @property {number} [windowOptions.minWidth] 限定的窗口最小宽度(像素值),默认不限制
   * @property {number} [windowOptions.maxWidth] 限定的窗口最大宽度(像素值),默认不限制
   *
   * @property {boolean} [windowOptions.maxmin=true] 是否可以在弹层右下角拖动来拉伸尺寸
   * @property {number|Array} [windowOptions.shade=0] 遮罩,默认为0不显示,可配置数字0.3透明度的黑色背景('#000'),其他颜色,可以shade: [0.8, '#393D49']
   * @property {boolean} [windowOptions.shadeClose=false] 当shade是存在的,点击弹层外区域后是否关闭弹窗。
   * @property {number} [windowOptions.closeBtn=1] 当为0时,不显示关闭按钮,配置1和2来展示两种风格的关闭按钮
   * @property {number} [windowOptions.noTitle=false] 是否不显示标题,为true是不显示标题
   * @property {boolean} [windowOptions.show=true] 激活后是否显示弹窗,false时激活后自动隐藏弹窗。
   *
   * @property {boolean} [openAtStart=false] 打开系统后是否自动启动本插件
   * @property {string} [style] 添加到widget的view中的class样式名
   * @property {object} [css] 添加到widget的css值
   * @property {*} [多个参数] 传入数据等,定义的任意参数在widget内部方法中都可以通过this.config获取到
   */
 
  /**
 * 原生JS技术栈下,widget基础类,
 * 需要继承后使用,不用手动实例化,框架内部自动实例化及相关处理。
 *
 * @param {Map} map 地图对象
 * @param {BaseWidget.widgetOptions} options 配置参数
 * @class BaseWidget
 * @extends {BaseClass}
 * @see [支持的事件类型]{@link WidgetEventType}
 
 * @example
//使用示例
class MyWidget extends es5widget.BaseWidget {
  //外部资源配置
  get resources() {
    return [
      'js/test.js', //当前同目录下
      './lib/dom2img/dom-to-image.js', //主页面相同目录下
    ]
  }
  //弹窗配置
  get view() {
    return {
      type: 'window',
      url: 'view.html',
      windowOptions: {  width: 250 },
    }
  }
  //初始化[仅执行1次]
  create() {}
  //每个窗口创建完成后调用
  winCreateOK(opt, result) {
    this.viewWindow = result
  }
  //打开激活
  activate() {}
  //关闭释放
  disable() {
    this.viewWindow = null
  }
}
 
//注册到widget管理器中。
es5widget.bindClass(MyWidget)
 */
  class BaseWidget extends mars3d.BaseClass {
    constructor(map, options) {
      super(options)
 
      /**
       *  获取当前地图
       * @type {Map}
       * @readonly
       */
      this.map = map
 
      /**
       *  获取当前配置参数
       * @type {BaseWidget.widgetOptions}
       * @readonly
       */
      this.options = options // 配置的config信息
 
      /**
       *  获取当前配置参数,别名,同options
       * @type {BaseWidget.widgetOptions}
       * @readonly
       */
      this.config = options
 
      /**
       *  获取当前widget的目录路径
       * @type {string}
       * @readonly
       */
      this.path = options.path || "" // 当前widget目录相对路径
 
      /**
       *  是否激活状态
       * @type {boolean}
       * @readonly
       */
      this.isActivate = false
 
      /**
       *  是否已创建
       * @type {boolean}
       * @readonly
       */
      this.isCreate = false
 
      this._viewcreate_allcount = 0
      this._viewcreate_okcount = 0
      this._viewConfig = this.view
 
      this.init()
    }
 
    /**
     * 该模块依赖的外部js、css资源文件,会在实例化之前加入的页面中。
     * 默认引用是当前widget所在同path目录的资源,
     * 相当于html主页面的资源 或 外部资源 请 以 “/” 或 “.” 或 “http” 开始的url
     * @type {string[]}
     * @readonly
     * @abstract
     */
    get resources() {
      return null
    }
 
    /**
     * 定义关联的view弹窗或页面配置信息,目前支持3种类型,
     * (1)type:'window',iframe模式弹窗 ,参考_example示例, 独立的html子页面,比较自由,简单粗暴、无任何限制;可以每个页面用不同的UI和第三方插件不用考虑冲突问题;任何水平的开发人员均容易快速开发。
     * (2)type:'divwindow',div元素模式弹窗 参考_example_divwin示例,可直接互相访问,这种模式弊端是易引起模块间id命名冲突,在css和html中命名时需注意。
     * (3)type:'append',任意html元素 参考_example_append示例,任意div节点,比较自由。
     * 为空时表示当前模块无关联的view页面,
     * 其中url地址规则,参考resources说明
     * @type {object|object[]}
     * @readonly
     * @abstract
     */
    get view() {
      return null
    }
 
    //= =============激活插件=================
    /**
     * 激活widget,同 es5widget.activate方法
     * @return {void}  无
     */
    activateBase() {
      const that = this
 
      if (this.isActivate) {
        // 已激活状态时跳出
        this.eachView(function (viewopt) {
          if (viewopt._dom) {
            // 将层置顶
            jQuery(".layui-layer").each(function () {
              jQuery(this).css("z-index", 19891000)
            })
            jQuery(viewopt._dom).css("z-index", 19891014)
          }
        })
        return
      }
 
      eventTarget.fire(WidgetEventType.beforeActivate, {
        sourceTarget: this
      })
      this.beforeActivate()
      this.isActivate = true
 
      if (!this.isCreate) {
        eventTarget.fire(WidgetEventType.beforeCreate, {
          sourceTarget: this
        })
 
        // 首次进行创建
        if (this.resources && this.resources.length > 0) {
          const resources = []
 
          for (let i = 0; i < this.resources.length; i++) {
            let _resource = this.resources[i]
            _resource = this._getUrl(_resource)
 
            if (_resources_cache.indexOf(_resource) !== -1) {
              continue
            } // 不加重复资源
 
            resources.push(_resource)
          }
          _resources_cache = _resources_cache.concat(resources) // 不加重复资源
 
          Loader.async(resources, function () {
            const result = that.create(function () {
              that._createWidgetView()
              that.isCreate = true
            })
            eventTarget.fire(WidgetEventType.created, {
              sourceTarget: that
            })
 
            if (result) {
              return
            }
            if (that.options.createAtStart) {
              that.options.createAtStart = false
              that.isActivate = false
              that.isCreate = true
              return
            }
            that._createWidgetView()
            that.isCreate = true
          })
          return
        } else {
          const result = this.create(function () {
            that._createWidgetView()
            this.isCreate = true
          })
          eventTarget.fire(WidgetEventType.created, {
            sourceTarget: this
          })
 
          if (result) {
            return
          }
          if (that.options.createAtStart) {
            that.options.createAtStart = false
            that.isActivate = false
            that.isCreate = true
            return
          }
        }
        this.isCreate = true
      }
      this._createWidgetView()
    }
 
    /**
     * 构造方法完成后的钩子方法,子类继承后按需使用
     * @return {void}  无
     * @abstract
     */
    init() {}
 
    /**
     * 模块初始化,仅首次初始化执行1次
     * @param {Function} [endfun] 当create内存在异步时,可以异步后调用下endfun
     * @return {void}  无
     * @abstract
     */
    create() {}
 
    // 创建插件的view
    _createWidgetView() {
      const viewopt = this._viewConfig
      if (viewopt === undefined || viewopt == null) {
        this._startActivate()
      } else if (Array.isArray(viewopt)) {
        this._viewcreate_allcount = viewopt.length
        this._viewcreate_okcount = 0
 
        for (let i = 0; i < viewopt.length; i++) {
          this.createItemView(viewopt[i])
        }
      } else {
        this._viewcreate_allcount = 1
        this._viewcreate_okcount = 0
        this.createItemView(viewopt)
      }
    }
 
    /**
     * 遍历所有view配置
     *
     * @param {Function} callback 回调方法
     * @param {number} [index] 当有多个view时,可以指定单个操作的view的index
     * @return {*} callback执行的返回结果
     */
    eachView(callback, index) {
      const viewopt = this._viewConfig
      if (viewopt === undefined || viewopt == null) {
        return false
      } else if (Array.isArray(viewopt)) {
        let hascal = false
        if (index != null) {
          return callback(viewopt[index])
        }
        for (let i = 0; i < viewopt.length; i++) {
          hascal = callback(viewopt[i])
        }
        return hascal
      } else {
        return callback(viewopt)
      }
    }
 
    createItemView(viewopt) {
      const that = this
      switch (viewopt.type) {
        case "divwindow":
          this._openDivWindow(viewopt)
          break
        case "append":
          that.getHtml(this._getUrl(viewopt.url), function (html) {
            that._appendView(viewopt, html)
          })
          break
        case "custom": // 自定义
          viewopt.open(
            this._getUrl(viewopt.url),
            function (html) {
              that.winCreateOK(viewopt, html)
              eventTarget.fire(WidgetEventType.openView, {
                sourceTarget: that,
                view: viewopt,
                dom: html
              })
              that._viewcreate_okcount++
              if (that._viewcreate_okcount >= that._viewcreate_allcount) {
                that._startActivate(html)
              }
            },
            this
          )
          break
        case "window":
        default:
          this._openWindow(viewopt)
          break
      }
    }
 
    //= =============layer弹窗=================
    _openWindow(viewopt) {
      const view_url = this._getUrl(viewopt.url)
 
      const opts = {
        type: 2,
        content: [view_url, "no"],
        success: (layero, index) => {
          if (!this.isActivate) {
            layer.close(index)
            return
          }
          if (viewopt._layerIdx !== index) {
            layer.close(viewopt._layerIdx)
            viewopt._layerIdx = index
          }
 
          viewopt._layerOpening = false
          viewopt._dom = layero
 
          // 得到iframe页的窗口对象,执行iframe页的方法:viewWindow.method();
          const viewWindow = window[layero.find("iframe")[0].name]
 
          // 绑定常用对象到子页面,方便直接使用
          viewWindow.map = this.map
          viewWindow.mars3d = mars3d
          viewWindow.Cesium = mars3d.Cesium
          viewWindow.es5widget = window.es5widget
 
          // 设置css
          if (this.options.css) {
            jQuery("#layui-layer" + viewopt._layerIdx).css(this.options.css)
          }
 
          // 隐藏弹窗
          if (viewopt.windowOptions.hasOwnProperty("show") && !viewopt.windowOptions.show) {
            jQuery(layero).hide()
          }
 
          layer.setTop(layero)
 
          this.winCreateOK(viewopt, viewWindow)
          eventTarget.fire(WidgetEventType.openView, {
            sourceTarget: this,
            view: viewopt,
            dom: layero
          })
 
          this._viewcreate_okcount++
          if (this._viewcreate_okcount >= this._viewcreate_allcount) {
            this._startActivate(layero)
          }
 
          // 通知页面,页面需要定义initWidgetView方法
          if (viewWindow && viewWindow.initWidgetView) {
            if (this.config?.style) {
              jQuery(viewWindow.document.body).addClass(this.config.style)
            }
 
            viewWindow.initWidgetView(this)
          } else {
            mars3d.Log.logError(view_url + "页面没有定义function initWidgetView(widget)方法,无法初始化widget页面!")
          }
        }
      }
      if (viewopt._layerIdx && viewopt._layerIdx > 0) {
        layer.close(viewopt._layerIdx)
        viewopt._layerIdx = -1
      }
 
      viewopt._layerOpening = true
      viewopt._layerIdx = layer.open(this._getWinOpt(viewopt, opts))
    }
 
    _openDivWindow(viewopt) {
      const view_url = this._getUrl(viewopt.url)
      // div弹窗
      this.getHtml(view_url, (data) => {
        const opts = {
          type: 1,
          content: data,
          success: (layero, index) => {
            if (!this.isActivate) {
              layer.close(index)
              return
            }
            if (viewopt._layerIdx !== index) {
              layer.close(viewopt._layerIdx)
              viewopt._layerIdx = index
            }
 
            viewopt._layerOpening = false
            viewopt._dom = layero
 
            // 隐藏弹窗
            if (viewopt.windowOptions.hasOwnProperty("show") && !viewopt.windowOptions.show) {
              jQuery(layero).hide()
            }
 
            layer.setTop(layero)
            this.winCreateOK(viewopt, layero)
            eventTarget.fire(WidgetEventType.openView, {
              sourceTarget: this,
              view: viewopt,
              dom: layero
            })
 
            this._viewcreate_okcount++
            if (this._viewcreate_okcount >= this._viewcreate_allcount) {
              this._startActivate(layero)
            }
          }
        }
        viewopt._layerOpening = true
        viewopt._layerIdx = layer.open(this._getWinOpt(viewopt, opts))
      })
    }
 
    _getUrl(url) {
      url = this.addCacheVersion(url)
 
      if (url.startsWith("/") || url.startsWith(".") || url.startsWith("http")) {
        return url
      } else {
        return this.path + url
      }
    }
 
    _getWinOpt(viewopt, opts) {
      // 优先使用cofig中配置,覆盖js中的定义
      const def = getDefWindowOptions()
 
      const windowOptions = { ...def, ...viewopt.windowOptions, ...this.options.windowOptions }
      viewopt.windowOptions = windowOptions // 赋值
 
      const that = this
      const _size = this._getWinSize(windowOptions)
 
      let title = false
      if (!windowOptions.noTitle) {
        title = this.options.name || " "
        if (this.options.icon) {
          title = '<i class="' + this.options.icon + '" ></i>&nbsp;' + title
        }
      }
 
      // 默认值
      const defOpts = {
        title: title,
        area: _size.area,
        offset: _size.offset,
        shade: 0,
        maxmin: false,
        beforeEnd: function () {
          that.beforeDisable()
        },
        end: function () {
          // 销毁后触发的回调
          viewopt._layerIdx = -1
          viewopt._dom = null
          that.disableBase()
        },
        full: function (dom) {
          // 最大化后触发的回调
          that.winFull(dom)
        },
        min: function (dom) {
          // 最小化后触发的回调
          that.winMin(dom)
        },
        restore: function (dom) {
          // 还原 后触发的回调
          that.winRestore(dom)
        }
      }
      return { ...defOpts, ...windowOptions, ...opts }
    }
 
    // 计算弹窗大小和位置
    _getWinSize(windowOptions) {
      // 获取高宽
      let _width = this.bfb2Number(windowOptions.width, document.documentElement.clientWidth, windowOptions)
      let _height = this.bfb2Number(windowOptions.height, document.documentElement.clientHeight, windowOptions)
 
      // 计算位置offset
      let offset = ""
      const position = windowOptions.position
      if (position) {
        if (typeof position === "string") {
          // t顶部,b底部,r右边缘,l左边缘,lt左上角,lb左下角,rt右上角,rb右下角
          offset = position
        } else if (typeof position === "object") {
          let _top
          let _left
 
          if (position.hasOwnProperty("top") && position.top != null) {
            _top = this.bfb2Number(position.top, document.documentElement.clientHeight, windowOptions)
          }
          if (position.hasOwnProperty("bottom") && position.bottom != null) {
            windowOptions._hasresize = true
 
            const _bottom = this.bfb2Number(position.bottom, document.documentElement.clientHeight, windowOptions)
 
            if (_top != null) {
              _height = document.documentElement.clientHeight - _top - _bottom
            } else {
              _top = document.documentElement.clientHeight - _height - _bottom
            }
          }
 
          if (position.hasOwnProperty("left") && position.left != null) {
            _left = this.bfb2Number(position.left, document.documentElement.clientWidth, windowOptions)
          }
          if (position.hasOwnProperty("right") && position.right != null) {
            windowOptions._hasresize = true
            const _right = this.bfb2Number(position.right, document.documentElement.clientWidth, windowOptions)
 
            if (_left != null) {
              _width = document.documentElement.clientWidth - _left - _right
            } else {
              _left = document.documentElement.clientWidth - _width - _right
            }
          }
 
          if (_top == null || _top === undefined) {
            _top = (document.documentElement.clientHeight - _height) / 2
          }
          if (_left == null || _left === undefined) {
            _left = (document.documentElement.clientWidth - _width) / 2
          }
 
          offset = [_top + "px", _left + "px"]
        }
      }
 
      // 最大最小高度判断
      if (windowOptions.hasOwnProperty("minHeight") && _height < windowOptions.minHeight) {
        windowOptions._hasresize = true
        _height = windowOptions.minHeight
      }
      if (windowOptions.hasOwnProperty("maxHeight") && _height > windowOptions.maxHeight) {
        windowOptions._hasresize = true
        _height = windowOptions.maxHeight
      }
 
      // 最大最小宽度判断
      if (windowOptions.hasOwnProperty("minWidth") && _width < windowOptions.minWidth) {
        windowOptions._hasresize = true
        _width = windowOptions.minWidth
      }
      if (windowOptions.hasOwnProperty("maxWidth") && _width > windowOptions.maxWidth) {
        windowOptions._hasresize = true
        _width = windowOptions.maxWidth
      }
 
      let area
      if (_width && _height) {
        area = [_width + "px", _height + "px"]
      } else {
        area = _width + "px"
      }
 
      return { area: area, offset: offset }
    }
 
    /**
     * 更新窗口大小或位置,改变了主页面尺寸后需要调用(内部已自动调用)。
     * @return {void}  无
     */
    indexResize() {
      if (!this.isActivate) {
        return
      }
 
      const that = this
      this.eachView(function (viewopt) {
        if (!viewopt._layerIdx || viewopt._layerIdx === -1 || !viewopt.windowOptions || !viewopt.windowOptions._hasresize) {
          return
        }
        const tag = viewopt._dom.attr("maxmin")
        if (tag === "min") {
          return
        }
        if (tag === "full") {
          layer.full(viewopt._layerIdx, viewopt)
          return
        }
 
        const _size = that._getWinSize(viewopt.windowOptions)
 
        const _style = {}
        if (Array.isArray(_size.area)) {
          if (_size.area[0]) {
            _style.width = _size.area[0]
          }
          if (_size.area[1]) {
            _style.height = _size.area[1]
          }
        }
 
        if (Array.isArray(_size.offset)) {
          if (_size.offset[1]) {
            _style.top = _size.offset[0]
          }
          if (_size.offset[1]) {
            _style.left = _size.offset[1]
          }
        }
        jQuery(viewopt._dom).attr("myTopLeft", true)
        layer.style(viewopt._layerIdx, _style)
 
        if (viewopt.type === "divwindow") {
          layer.iframeAuto(viewopt._layerIdx)
        }
      })
    }
 
    //= =============直接添加dom节点=================
    _appendView(viewopt, html) {
      viewopt._dom = jQuery(html).appendTo(viewopt.parent || "body")
 
      // 设置css
      if (this.options.css) {
        jQuery(viewopt._dom).css(this.options.css)
      }
 
      this.winCreateOK(viewopt, html)
 
      this._viewcreate_okcount++
      if (this._viewcreate_okcount >= this._viewcreate_allcount) {
        this._startActivate(html)
      }
    }
 
    /**
     * 每个view窗口或页面创建完成后调用的钩子方法
     *
     * @param {object} opt 对应的view配置
     * @param {object|string} result 得到iframe页的窗口对象 或 view的html内容
     * @return {void}  无
     * @abstract
     */
    winCreateOK(opt, result) {}
 
    /**
     * 窗口最大化后触发后 的钩子方法
     * @return {void}  无
     * @abstract
     */
    winFull() {}
 
    /**
     * 窗口最小化后触发 的钩子方法
     * @return {void}  无
     * @abstract
     */
    winMin() {}
 
    /**
     * 最小化窗口
     * @return {void}  无
     */
    minView() {
      this.eachView(function (viewopt) {
        if (viewopt._layerIdx) {
          layer.min(viewopt._layerIdx, viewopt)
        }
      })
    }
 
    /**
     * 还原窗口
     * @return {void}  无
     */
    restoreView() {
      this.eachView(function (viewopt) {
        if (viewopt._layerIdx) {
          layer.restore(viewopt._layerIdx)
        }
      })
    }
 
    /**
     * 最大化窗口
     * @return {void}  无
     */
    fullView() {
      this.eachView(function (viewopt) {
        if (viewopt._layerIdx) {
          layer.full(viewopt._layerIdx, viewopt)
        }
      })
    }
 
    /**
     * 窗口还原后触发 的钩子方法
     * @return {void}  无
     * @abstract
     */
    winRestore() {}
 
    _startActivate(layero) {
      this.activate(layero)
      eventTarget.fire(WidgetEventType.activated, {
        sourceTarget: this
      })
 
      if (this.options.success) {
        this.options.success(this)
        delete this.options.success // 一次性的
      }
      if (!this.isActivate) {
        // 窗口打开中没加载完成时,被释放
        this.disableBase()
      }
    }
 
    /**
     * 激活模块之前 的钩子方法
     * @return {void}  无
     * @abstract
     */
    beforeActivate() {}
 
    /**
     * 激活模块【类内部实现方法】
     * @return {void}  无
     * @abstract
     */
    activate() {}
 
    //= =============释放插件=================
 
    /**
     * 释放插件,同 es5widget.disable方法
     * @return {void}  无
     */
    disableBase() {
      if (!this.isActivate) {
        return
      }
      this.isActivate = false
 
      this.beforeDisable()
      eventTarget.fire(WidgetEventType.beforeDisable, {
        sourceTarget: this
      })
 
      // 关闭所有窗口
      this.eachView(function (viewopt) {
        if (viewopt._layerIdx && viewopt._layerIdx > 0) {
          layer.close(viewopt._layerIdx)
 
          if (viewopt._layerOpening) {
            // 窗口还在加载中,success方法中去关闭
          } else {
            viewopt._layerIdx = -1
          }
          return true
        } else {
          if (viewopt.type === "append" && viewopt._dom) {
            viewopt._dom.remove()
            viewopt._dom = null
          }
          if (viewopt.type === "custom" && viewopt.close) {
            viewopt.close()
          }
          return false
        }
      })
 
      this.disable()
 
      // 还原配置为初始状态
      if (this.options.autoReset) {
        this.resetConfig()
      }
      eventTarget.fire(WidgetEventType.disabled, {
        sourceTarget: this
      })
    }
 
    /**
     * 释放模块前
     * @return {void}  无
     * @abstract
     */
    beforeDisable() {}
 
    /**
     * 释放模块【类内部实现方法】
     * @return {void}  无
     * @abstract
     */
    disable() {}
 
    //= =============其他方法=================
    bfb2Number(str, allnum, windowOptions) {
      if (typeof str === "string" && str.indexOf("%") !== -1) {
        windowOptions._hasresize = true
 
        return (allnum * Number(str.replace("%", ""))) / 100
      }
      return str
    }
 
    addCacheVersion(_resource) {
      if (!_resource) {
        return _resource
      }
 
      const cacheVersion = getCacheVersion()
      if (cacheVersion) {
        if (_resource.indexOf("?") === -1) {
          _resource += "?cache=" + cacheVersion
        } else if (_resource.indexOf("cache=" + cacheVersion) === -1) {
          _resource += "&cache=" + cacheVersion
        }
      }
      return _resource
    }
 
    /**
     * 还原配置为初始状态
     * @return {void}  无
     */
    resetConfig() {
      if (this.options._firstConfigBak) {
        const _backData = this.options._firstConfigBak
        for (const aa in _backData) {
          if (aa === "uri") {
            continue
          }
          this.options[aa] = _backData[aa]
        }
      }
    }
 
    /**
     * 设置view弹窗的显示和隐藏,基于修改css实现
     *
     * @param {boolean} show 是否显示
     * @param {number} [index] 当有多个view时,可以指定单个操作的view的index
     * @return {void}  无
     */
    setViewShow(show, index) {
      this.eachView(function (viewopt) {
        if (viewopt._layerIdx && viewopt._layerIdx > 0) {
          if (show) {
            jQuery("#layui-layer" + viewopt._layerIdx).show()
          } else {
            jQuery("#layui-layer" + viewopt._layerIdx).hide()
          }
        } else if (viewopt.type === "append" && viewopt._dom) {
          if (show) {
            jQuery(viewopt._dom).show()
          } else {
            jQuery(viewopt._dom).hide()
          }
        }
      }, index)
    }
 
    /**
     * 设置view弹窗的css
     *
     * @param {object} style css值
     * @param {number} [index] 当有多个view时,可以指定单个操作的view的index
     * @return {void}  无
     */
    setViewCss(style, index) {
      this.eachView(function (viewopt) {
        if (viewopt._layerIdx != null && viewopt._layerIdx > 0) {
          layer.style(viewopt._layerIdx, style)
        } else if (viewopt.type === "append" && viewopt._dom) {
          jQuery(viewopt._dom).css(style)
        }
      }, index)
    }
 
    /**
     * 设置view弹窗的标题
     *
     * @param {string} title css值
     * @param {number} [index] 当有多个view时,可以指定单个操作的view的index
     * @return {void}  无
     */
    setTitle(title, index) {
      this.eachView(function (viewopt) {
        if (viewopt._dom) {
          viewopt._dom.find(".layui-layer-title").html(title)
        }
      }, index)
    }
 
    /**
     * 读取html页面的内容
     *
     * @param {string} url html页面的url
     * @param {Function} callback 读取完成后的回调方法
     * @return {void}  无
     */
    getHtml(url, callback) {
      jQuery.ajax({
        url: url,
        type: "GET",
        dataType: "html",
        timeout: 0, // 永不超时
        success: function (data) {
          callback(data)
        }
      })
    }
  }
 
  //对外接口
  window.es5widget = {
    BaseWidget: BaseWidget,
    EventType: WidgetEventType,
    WidgetEventType: WidgetEventType,
    eventTarget,
    activate,
    bindClass,
    destroy,
    disable,
    disableAll,
    disableGroup,
    eachWidget,
    fire,
    getBasePath,
    getCacheVersion,
    getClass,
    getDefWindowOptions,
    getWidget,
    init,
    isActivate,
    listens,
    off,
    on,
    once,
    removeDebugeBar,
    setViewShow
  }
})()