wangzhibo
2021-09-06 f4af2a4a11d73b79355e7bef7184eaaab4c383ed
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
"use script"; //开发环境建议开启严格模式
 
//对应widget.js中MyWidget实例化后的对象
var thisWidget;
 
//当前页面业务
function initWidgetView(_thisWidget) {
  thisWidget = _thisWidget;
 
  //清除所有标号
  $("#btn_plot_delall").click(function () {
    thisWidget.deleteAll();
    // tab2plot()
  });
  $("#btn_plot_end").click(function (e) {
    thisWidget.endDraw();
  });
 
  $("#btn_plot_socket").click(function (e) {
    thisWidget.socketConfig();
  });
 
  //是否可以编辑
  var isedit = true;
  $("#btn_plot_isedit").click(function () {
    isedit = !isedit;
 
    if (isedit) {
      $(this).removeClass("active");
      $(this).children().removeClass("fa-lock").addClass("fa-unlock");
    } else {
      $(this).addClass("active");
      $(this).children().removeClass("fa-unlock").addClass("fa-lock");
    }
    thisWidget.hasEdit(isedit);
  });
 
  var isPopup = false;
  $("#btn_plot_ispopup").click(function () {
    isPopup = !isPopup;
 
    if (isPopup) {
      $(this).removeClass("active");
      $(this).children().removeClass("fa-file-text").addClass("fa-file-text-o");
    } else {
      $(this).addClass("active");
      $(this).children().removeClass("fa-file-text-o").addClass("fa-file-text");
    }
    thisWidget.hasPopup(isPopup);
  });
 
  plotFile.initEvent();
  plotlist.bindSelList();
  treeWork.initEvent();
}
 
//文件处理
var plotFile = {
  initEvent: function () {
    var that = this;
 
    $("#btn_plot_openfile").click(function () {
      that.openPlotFile({ clear: true });
    });
 
    $("#btn_plot_openfile2").click(function () {
      that.openPlotFile({ clear: false });
    });
 
    $("#btn_plot_savefile").click(function () {
      thisWidget.downloadJson("标绘");
    });
 
    $("#input_plot_file").change(function (e) {
      var file = this.files[0];
 
      var fileName = file.name;
      var fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
 
      that.loadJsonOptions.symbol = null;
      if (fileType == "json" || fileType == "geojson") {
        let reader = new FileReader();
        reader.readAsText(file, "UTF-8");
        reader.onloadend = function (e) {
          let json = this.result;
          thisWidget.loadGeoJSON(json, that.loadJsonOptions);
          that.clearPlotFile();
        };
      } else if (fileType == "kml") {
        let reader = new FileReader();
        reader.readAsText(file, "UTF-8");
        reader.onloadend = function (e) {
          let strkml = this.result;
          parent.kgUtil.toGeoJSON(strkml).then((geojoson) => {
            console.log("kml2geojson", geojoson);
            that.loadJsonOptions.symbol = that.kmlDefSymbol;
            thisWidget.loadGeoJSON(geojoson, that.loadJsonOptions);
          });
          that.clearPlotFile();
        };
      } else if (fileType == "kmz") {
        //加载input文件控件的二进制流
        parent.kgUtil.toGeoJSON(file).then((geojoson) => {
          console.log("kmz2geojson", geojoson);
 
          that.loadJsonOptions.symbol = that.kmlDefSymbol;
          thisWidget.loadGeoJSON(geojoson, that.loadJsonOptions);
          that.clearPlotFile();
        });
      } else {
        toastr.error("暂不支持 " + fileType + " 文件类型的数据!");
        that.clearPlotFile();
      }
    });
  },
  openPlotFile: function (options) {
    this.loadJsonOptions = options;
    this.loadJsonOptions.flyTo = true;
 
    $("#input_plot_file").click();
  },
  clearPlotFile: function () {
    if (!window.addEventListener) {
      document.getElementById("input_plot_file").outerHTML += ""; //IE
    } else {
      document.getElementById("input_plot_file").value = ""; //FF
    }
  },
  //kml的默认样式处理
  kmlDefSymbol: function (attr, style, featue) {
    style.clampToGround = true; //贴地
 
    let geoType = featue.geometry?.type;
    if (geoType == "Point") {
      return {
        image: "img/marker/di3.png",
        verticalOrigin: 1,
        scale: 0.4,
        clampToGround: true,
        label: {
          text: attr.name,
          font_size: 18,
          color: "#ffffff",
          outline: true,
          outlineColor: "#000000",
          pixelOffsetY: -20,
          scaleByDistance: true,
          scaleByDistance_far: 990000,
          scaleByDistance_farValue: 0.3,
          scaleByDistance_near: 10000,
          scaleByDistance_nearValue: 1,
        },
      };
    }
    return style;
  },
};
 
//标号列表相关
var plotlist = {
  //绑定标号列表切换下拉框
  bindSelList: function () {
    var that = this;
    var $sel_plot_list = $("#sel_plot_list");
    $.getJSON("config/plotlist.json", function (plotlist) {
      var inhtml = "";
      var defval;
      var count = 0;
      for (var i in plotlist) {
        inhtml += '<option value="' + i + '">' + i + "(" + plotlist[i].length + ")</option>";
        if (defval == null) {
          defval = i;
        }
        count++;
      }
      localforage.getItem("plot_list").then((historyval) => {
        if (historyval) {
          defval = historyval;
        }
        if (defval && plotlist[defval]) {
          that.showPlotList(plotlist[defval]);
          $sel_plot_list.attr("data-value", defval);
        }
 
        if (count > 1) {
          $sel_plot_list.html(inhtml);
          $sel_plot_list.select();
          $sel_plot_list.change(function () {
            var val = $(this).attr("data-value");
            var list = plotlist[val];
            that.showPlotList(list);
            //记录历史值
            localforage.setItem("plot_list", val);
          });
        } else {
          $sel_plot_list.hide();
          $(".mp_mark").css({ "margin-top": "10px" });
        }
      });
    });
  },
  _listData: null,
  showPlotList: function (list) {
    this._listData = list;
 
    var inhtml = "";
    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      if (item.hide) {
        continue;
      }
 
      //处理模型url
      if (item.style && item.style.url) {
        item.style.url = thisWidget.updateTemplateValues(item.style.url);
      }
 
      var defStyle = thisWidget.getDefStyle(item.edittype || item.type);
 
      //使用图片图标
      var image;
      if (defStyle) {
        image = defStyle.image;
      }
      if (item.style && item.style.image) {
        image = item.style.image;
      }
      if (item.image) {
        image = item.image;
      }
 
      if (image) {
        image = thisWidget.updateTemplateValues(image);
 
        if (image.startsWith("http") || image.startsWith("//")) {
          //不用特殊处理
        } else if (image.startsWith("{plotPath}")) {
          image = image.replace("{plotPath}", "."); //是模块内部本级图片
        } else {
          image = "../../" + image; //相对于父级index页面的图片
        }
        inhtml +=
          ' <li onclick="plotlist.startPlot(' +
          i +
          ',this)"> <i title="' +
          item.name +
          '"  > <img src="' +
          image +
          '"  onerror="plotlist.imgerrorfun();"/></i></li>';
      }
      // else if (item.style && item.style.html) {
      //     inhtml += ' <li onclick="plotlist.startPlot(' + i + ',this)"> <i title="' + item.name + '"  > ' + item.style.html + '</i></li>';
      // }
      else {
        //使用字体图标
        var icon;
        var clr = "#000000";
        if (defStyle) {
          icon = defStyle.iconClass;
          clr = defStyle.color;
        }
        if (item.iconClass) {
          icon = item.iconClass;
        }
        if (item.style && item.style.iconClass) {
          icon = item.style.iconClass;
        }
 
        if (item.color) {
          clr = item.color;
        }
        if (item.style && item.style.color) {
          clr = item.style.color;
        }
        if (icon) {
          inhtml +=
            '<li onclick="plotlist.startPlot(' + i + ',this)"><i title="' + item.name + '"  class="' + icon + '" style="color:' + clr + '"></i></li>';
        } else {
          inhtml +=
            '<li onclick="plotlist.startPlot(' + i + ',this)"><i title="' + item.name + '" style="font-size: 13px;">' + item.name + "</i></li>";
        }
      }
    }
    $("#plotlist").html(inhtml);
  },
  imgerrorfun: function () {
    var img = event.srcElement;
    img.src = "../../../img/favicon/app-icon72x72@2x.png";
    img.onerror = null;
  },
  //激活标绘
  _lastLi: null,
  //开始绘制
  startPlot: function (idx, li) {
    var _thisli = $(li);
    _thisli.addClass("markon");
    if (this._lastLi) {
      this._lastLi.removeClass("markon");
    }
    this._lastLi = _thisli;
 
    var item = haoutil.system.clone(this._listData[idx] || {});
    delete item.image;
 
    //赋值默认样式
    var defStyle = thisWidget.getDefStyle(item.edittype || item.type);
    if (defStyle) {
      item.style = item.style || {};
      for (var i in defStyle) {
        if (item.style[i] == null) {
          item.style[i] = defStyle[i];
        }
      }
    }
 
    //赋值默认属性
    item.attr = {
      id: "",
      name: "",
      remark: "",
    };
 
    thisWidget.startDraw(item);
  },
  //绘制结束
  plotEnd: function () {
    //取消选中状态
    if (this._lastLi) {
      this._lastLi.removeClass("markon");
    }
  },
};
 
//标号树处理
var objFeature = {};
var treeObj;
 
var treeWork = {
  initEvent: function () {
    $("#btn_plot_addgroup").click(function (e) {
      thisWidget.editGroupName();
    });
    $("#btn_plot_delAllGroup").click(function (e) {
      thisWidget.deleteEmptyLayer();
    });
 
    bindRightMenuEvnet(); //右键
    thisWidget.showLayerTree();
  },
  loadData: function (arrGroup) {
    //初始化树 http://www.treejs.cn/v3/api.php
    var setting = {
      check: {
        enable: true,
      },
      edit: {
        drag: {
          isMove: true,
        },
        showRemoveBtn: false,
        showRenameBtn: false,
        enable: true,
      },
      data: {
        simpleData: {
          enable: true,
        },
      },
      callback: {
        beforeDrag: treeOverlays_beforeDrag,
        beforeDrop: treeOverlays_beforeDrop,
        onDrop: treeOverlays_onDrop,
        onCheck: treeOverlays_onCheck,
        onRightClick: treeOverlays_OnRightClick,
        onClick: treeOverlays_onClick,
        onRemove: treeOverlays_onRemove,
      },
    };
 
    //构造树节点
    objFeature = {};
 
    var zNodes = [];
    for (var i = 0; i < arrGroup.length; i++) {
      var layer = arrGroup[i];
      var arrGraphic = layer.getGraphics();
 
      var name = layer.name + "(" + arrGraphic.length + "个)";
      if (layer.isActivate) {
        name += "-激活";
      }
      var oldNode = treeObj && treeObj.getNodeByParam("id", layer.uuid);
 
      //添加分组
      var groupNode = {
        id: layer.uuid,
        pId: layer.pid,
        name: name,
        icon: "img/tree/folder.png",
        isGroup: true,
        checked: layer.show,
        open: oldNode && oldNode.open, //是否展开分组
      };
 
      zNodes.push(groupNode);
 
      objFeature[groupNode.id] = layer;
 
      for (var j = 0, len = arrGraphic.length; j < len; j++) {
        var graphic = arrGraphic[j];
 
        //添加标号
        var plotNode = {
          id: graphic.uuid,
          pId: groupNode.id,
          name: graphic.attr?.name || graphic.name || "未命名",
          checked: graphic.show,
          icon: "img/tree/plot.png",
        };
        zNodes.push(plotNode);
 
        objFeature[plotNode.id] = graphic;
      }
    }
 
    $.fn.zTree.destroy();
    treeObj = $.fn.zTree.init($("#treeOverlays"), setting, zNodes);
  },
  updateNode: function (graphic) {
    var treeObj = $.fn.zTree.getZTreeObj("treeOverlays");
 
    var node = treeObj.getNodeByParam("id", graphic.uuid, null);
    if (node) {
      node.name = graphic.attr?.name || graphic.name || "未命名";
 
      var show = graphic.isAdded && graphic.show;
      if (node.checked != show) {
        node.checkedOld = node.checked;
        node.checked = show;
      }
 
      treeObj.updateNode(node);
    }
  },
};
 
//===================================单击定位图层====================================
function treeOverlays_onClick(event, treeId, treeNode) {
  if (treeNode == null || treeNode.id == null) {
    return;
  }
 
  var layer = objFeature[treeNode.id];
  if (layer == null) {
    return;
  }
 
  thisWidget.flyTo(layer);
}
 
//===================================勾选显示隐藏图层====================================
 
function treeOverlays_onCheck(e, treeId, setreeNode) {
  //获得所有改变check状态的节点
  var changedNodes = treeObj.getChangeCheckedNodes();
  for (var i = 0; i < changedNodes.length; i++) {
    var treeNode = changedNodes[i];
    treeNode.checkedOld = treeNode.checked;
 
    // if (treeNode.check_Child_State == 1) {
    //   // 0:无子节点被勾选,  1:部分子节点被勾选,  2:全部子节点被勾选, -1:不存在子节点 或 子节点全部设置为 nocheck = true
    //   continue
    // }
 
    var layer = objFeature[treeNode.id];
    if (layer == null) {
      continue;
    }
 
    layer.show = treeNode.checked;
  }
}
 
//===================================右键菜单====================================
var lastRightClickTreeId;
var lastRightClickTreeNode;
 
function treeOverlays_OnRightClick(event, treeId, treeNode) {
  if (treeNode == null || objFeature[treeNode.id] == null) {
    return;
  }
 
  //右击时的节点
  lastRightClickTreeId = treeId;
  lastRightClickTreeNode = treeNode;
 
  var top = event.clientY;
  var left = event.clientX;
  var maxtop = document.body.offsetHeight - 100;
  var maxleft = document.body.offsetWidth - 100;
 
  if (top > maxtop) {
    top = maxtop;
  }
  if (left > maxleft) {
    left = maxleft;
  }
 
  if (treeNode.isGroup) {
    $("#plot_rMenu_group .group").show();
    $("#plot_rMenu_group .plot").hide();
  } else {
    $("#plot_rMenu_group .group").hide();
    $("#plot_rMenu_group .plot").show();
  }
 
  $("#plot_rMenu_group")
    .css({
      top: top + "px",
      left: left + "px",
    })
    .show();
  $("body").bind("mousedown", onBodyMouseDown);
}
 
function bindRightMenuEvnet() {
  $("#plot_rMenu_group li").on("click", function () {
    hideRMenu();
 
    var type = $(this).attr("data-type");
    moveNodeAndLayer(type);
  });
}
 
function onBodyMouseDown(event) {
  if (!(event.target.id == "plot_rMenu_group" || $(event.target).parents("#plot_rMenu_group").length > 0)) {
    hideRMenu();
  }
}
 
function hideRMenu() {
  $("body").unbind("mousedown", onBodyMouseDown);
  $("#plot_rMenu_group").hide();
}
 
//移动节点及图层位置
function moveNodeAndLayer(type) {
  var thisNode = lastRightClickTreeNode;
  var thisLayer = objFeature[thisNode.id];
 
  switch (type) {
    default:
      break;
    case "g_act": //置为激活分组
      thisWidget.changeSelectedLayer(thisLayer);
      break;
    case "g_name": //重命名分组
      thisWidget.editGroupName(thisLayer);
      break;
    case "g_del": //删除分组
      // if (thisWidget.checkRemoveGroup(thisLayer)) {
      treeObj.removeNode(thisNode);
      thisWidget.deleteLayer(thisLayer);
      // }
      break;
    case "p_del": //删除标号
      treeObj.removeNode(thisNode);
      thisWidget.deleteEntity(thisLayer);
      break;
    case "g_json": //导出GeoJson
      thisWidget.downloadJson("标绘分组_" + thisNode.name, thisLayer);
      break;
    case "g_kml":
      thisWidget.downloadKml("标绘分组_" + thisNode.name, thisLayer);
      break;
    case "p_json": //导出GeoJson
      thisWidget.downloadJson(thisNode.name, thisLayer);
      break;
    case "p_kml":
      thisWidget.downloadKml(thisNode.name, thisLayer);
      break;
    case "g_imp": //导入GeoJson到分组中
      plotFile.openPlotFile({ layer: thisLayer.name });
      break;
  }
}
 
//===================================删除处理====================================
 
function treeOverlays_onRemove(event, treeId, treeNode) {
  if (treeNode == null || treeNode.id == null) {
    return;
  }
 
  var layer = objFeature[treeNode.id];
  if (layer == null) {
    return;
  }
 
  if (treeNode.isGroup) {
    thisWidget.deleteLayer(layer);
  } else {
    thisWidget.deleteEntity(layer);
  }
}
 
//===================================拖拽调整层次处理====================================
 
function treeOverlays_beforeDrag(treeId, treeNodes) {
  for (var i = 0, l = treeNodes.length; i < l; i++) {
    if (treeNodes[i].isGroup) {
      return false; //禁止拖拽
    }
  }
  return true;
}
function treeOverlays_beforeDrop(treeId, treeNodes, targetNode, moveType) {
  if (targetNode && targetNode.isGroup) {
    return true;
  } else {
    return false;
  } //禁止拖拽到我身上
}
 
function treeOverlays_onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
  if (!targetNode) {
    return;
  }
 
  // var group = targetNode.name //分组名称
  var layerGroup = objFeature[targetNode.id]; //分组
  var graphic = objFeature[treeNodes[0].id];
 
  thisWidget.moveToLayer(graphic, layerGroup);
}