Lin_Ye_Fang_Huo/public/widgets/plot/widget.js

562 lines
16 KiB
JavaScript
Raw Normal View History

2023-07-18 09:04:15 +08:00
"use script"; //开发环境建议开启严格模式
(function (window, mars3d) {
2023-07-08 15:37:34 +08:00
//创建widget类需要继承BaseWidget
class MyWidget extends mars3d.widget.BaseWidget {
//外部资源配置
get resources() {
return [
2023-07-18 09:04:15 +08:00
"/widgets/plot/js/getGraphicDefStyle.js",
"./lib/kml/kml-geojson.js", //导出kml支持不用kml时可以删除
"./lib/dom2img/dom-to-image.js", //字体标号使用到的div转img库无此标号时可以删除
];
2023-07-08 15:37:34 +08:00
}
//弹窗配置
get view() {
return {
2023-07-18 09:04:15 +08:00
type: "window",
url: "view.html",
2023-07-08 15:37:34 +08:00
windowOptions: {
width: 250,
position: {
top: 50,
right: 5,
bottom: 5,
},
},
2023-07-18 09:04:15 +08:00
};
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
2023-07-08 15:37:34 +08:00
//初始化[仅执行1次]
create() {
2023-07-18 09:04:15 +08:00
this.storageName = "mars3d_plot";
2023-07-08 15:37:34 +08:00
this.graphicGroupLayer = new mars3d.layer.GraphicGroupLayer({
name: this.config.name,
pid: 99, //图层管理 中使用父节点id
2023-07-18 09:04:15 +08:00
defaultLayer: "默认分组",
isRestorePositions: true, //在标绘和编辑结束时是否将坐标还原为普通值true: 停止编辑时会有闪烁,但效率要好些。
2023-07-08 15:37:34 +08:00
hasEdit: false,
2023-07-18 09:04:15 +08:00
});
this.map.addLayer(this.graphicGroupLayer);
var that = this;
this.graphicGroupLayer.bindContextMenu([
{
text: "删除对象",
iconCls: "fa fa-trash-o",
callback: function (e) {
let graphic = e.graphic;
if (graphic) {
that.deleteEntity(graphic);
}
},
},
]);
2023-07-08 15:37:34 +08:00
//事件监听
this.graphicGroupLayer.on(mars3d.EventType.drawCreated, (e) => {
2023-07-18 09:04:15 +08:00
this.showLayerTree();
var graphic = e.graphic;
this.startEditing(graphic);
});
2023-07-08 15:37:34 +08:00
//模型加载完成后事件
this.graphicGroupLayer.on(mars3d.EventType.load, (e) => {
//console.log('gltf模型加载完成');
2023-07-18 09:04:15 +08:00
haoutil.loading.hide();
});
2023-07-08 15:37:34 +08:00
this.graphicGroupLayer.on(mars3d.EventType.editStart, (e) => {
2023-07-18 09:04:15 +08:00
var graphic = e.graphic;
this.startEditing(graphic);
});
2023-07-08 15:37:34 +08:00
this.graphicGroupLayer.on(mars3d.EventType.editMovePoint, (e) => {
2023-07-18 09:04:15 +08:00
var graphic = e.graphic;
this.startEditing(graphic);
});
2023-07-08 15:37:34 +08:00
this.graphicGroupLayer.on(mars3d.EventType.editRemovePoint, (e) => {
2023-07-18 09:04:15 +08:00
var graphic = e.graphic;
this.startEditing(graphic);
});
2023-07-08 15:37:34 +08:00
this.graphicGroupLayer.on(mars3d.EventType.editStop, (e) => {
2023-07-18 09:04:15 +08:00
var graphic = e.graphic;
this.stopEditing();
this.sendSaveEntity(graphic); //保存数据
this.showLayerTree();
});
this.graphicGroupLayer.on(mars3d.EventType.updateAttr, (e) => {
var graphic = e.graphic;
//更新了名称
if (e.attr.name && this.viewWindow) {
this.viewWindow.treeWork.updateNode(graphic);
}
});
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.sendGetList();
2023-07-08 15:37:34 +08:00
}
//每个窗口创建完成后调用
winCreateOK(opt, result) {
2023-07-18 09:04:15 +08:00
this.viewWindow = result;
2023-07-08 15:37:34 +08:00
}
//激活插件
activate() {
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.hasEdit = true;
2023-07-08 15:37:34 +08:00
}
//释放插件
disable() {
2023-07-18 09:04:15 +08:00
this.stopEditing();
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.stopDraw();
this.graphicGroupLayer.hasEdit = false;
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.viewWindow = null;
2023-07-08 15:37:34 +08:00
}
getDefStyle(type) {
2023-07-18 09:04:15 +08:00
return getGraphicDefStyle(type);
}
updateTemplateValues(url) {
if (this.map.options.templateValues) {
return mars3d.Util.template(url, this.map.options.templateValues);
} else {
return url;
}
2023-07-08 15:37:34 +08:00
}
hasEdit(val) {
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.hasEdit = val;
2023-07-08 15:37:34 +08:00
}
hasPopup(val) {
if (val) {
this.graphicGroupLayer.bindPopup(
(event) => {
2023-07-18 09:04:15 +08:00
let graphic = event.graphic;
2023-07-08 15:37:34 +08:00
var html = mars3d.Util.getTemplateHtml({
2023-07-18 09:04:15 +08:00
title: "属性编辑",
2023-07-08 15:37:34 +08:00
template: [
2023-07-18 09:04:15 +08:00
{ field: "name", name: "名称" },
{ field: "address", name: "地址" },
{ field: "phone", name: "电话" },
{ field: "remark", name: "备注", type: "textarea" },
{ name: "确定", type: "button" },
2023-07-08 15:37:34 +08:00
],
attr: graphic.attr,
edit: true,
width: 190,
2023-07-18 09:04:15 +08:00
});
return html;
2023-07-08 15:37:34 +08:00
},
{
onAdd: (event) => {
let eleId = event.id,
2023-07-18 09:04:15 +08:00
graphic = event.graphic;
2023-07-08 15:37:34 +08:00
//popup的DOM添加到页面的回调方法
2023-07-18 09:04:15 +08:00
$("#" + eleId + " .mars3d-popup-btn").click((e) => {
$("#" + eleId + " .mars3d-popup-edititem").each(function () {
var val = $(this).val();
var key = $(this).attr("data-type");
graphic.attr[key] = val;
});
this.graphicGroupLayer.closePopup();
});
2023-07-08 15:37:34 +08:00
},
onRemove: function (eleId, graphic) {
//popup的DOM从页面移除的回调方法
},
anchor: [0, -20],
}
2023-07-18 09:04:15 +08:00
);
2023-07-08 15:37:34 +08:00
} else {
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.unbindPopup();
2023-07-08 15:37:34 +08:00
}
}
//开始标记
startDraw(defval) {
2023-07-18 09:04:15 +08:00
haoutil.loading.hide();
2023-07-08 15:37:34 +08:00
switch (defval.type) {
default:
2023-07-18 09:04:15 +08:00
break;
case "model": //小模型
defval.drawShow = true; //模型点状对象绘制时是否显示本身对象可避免在3dtiles上拾取坐标存在问题。
haoutil.loading.show(); //LoadEnd后关闭
break;
case "point":
case "font-point":
case "billboard":
defval.drawShow = true; //模型点状对象绘制时是否显示本身对象可避免在3dtiles上拾取坐标存在问题。
break;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
console.log("开始绘制", defval);
this.graphicGroupLayer.startDraw(defval);
2023-07-08 15:37:34 +08:00
}
//结束绘制、等同双击完成绘制,比如手机端无法双击结束
endDraw() {
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.endDraw();
2023-07-08 15:37:34 +08:00
}
startEditingById(id) {
2023-07-18 09:04:15 +08:00
var graphic = this.graphicGroupLayer.getGraphicById(id);
2023-07-08 15:37:34 +08:00
if (graphic == null) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
graphic.flyTo();
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.startEditing(graphic);
2023-07-08 15:37:34 +08:00
}
startEditing(graphic) {
2023-07-18 09:04:15 +08:00
clearTimeout(this.timeTik);
2023-07-08 15:37:34 +08:00
if (this.viewWindow == null) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
2023-07-08 15:37:34 +08:00
if (plotAttr && plotAttr.isActivate) {
2023-07-18 09:04:15 +08:00
plotAttr.startEditing(graphic, graphic.coordinates);
2023-07-08 15:37:34 +08:00
} else {
mars3d.widget.activate({
2023-07-18 09:04:15 +08:00
uri: "widgets/plotAttr/widget.js",
2023-07-08 15:37:34 +08:00
graphic: graphic,
lonlats: graphic.coordinates,
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
}
}
stopEditing() {
this.timeTik = setTimeout(function () {
2023-07-18 09:04:15 +08:00
mars3d.widget.disable("widgets/plotAttr/widget.js");
}, 200);
2023-07-08 15:37:34 +08:00
}
flyTo(target) {
//参数解释线面数据scale控制边界的放大比例点数据radius控制视距距离
2023-07-18 09:04:15 +08:00
this.map.flyToGraphic(target, { scale: 1.9, radius: 1000 });
2023-07-08 15:37:34 +08:00
}
//文件处理
getGeoJson(target) {
if (target) {
2023-07-18 09:04:15 +08:00
return target.toGeoJSON();
2023-07-08 15:37:34 +08:00
} else {
2023-07-18 09:04:15 +08:00
return this.graphicGroupLayer.toGeoJSON();
2023-07-08 15:37:34 +08:00
}
}
downloadJson(filename, target) {
2023-07-18 09:04:15 +08:00
var data = this.getGeoJson(target);
2023-07-08 15:37:34 +08:00
if (data == null || (data.features && data.features.length == 0)) {
2023-07-18 09:04:15 +08:00
haoutil.msg("当前未标绘任何数据!");
2023-07-08 15:37:34 +08:00
} else {
2023-07-18 09:04:15 +08:00
var timestr = new Date().format("MMddHHmmss");
haoutil.file.downloadFile(filename + "_" + timestr + ".json", JSON.stringify(data));
2023-07-08 15:37:34 +08:00
}
}
2023-07-18 09:04:15 +08:00
toKml(target) {
var geojsonObject = this.getGeoJson(target);
if (geojsonObject == null) {
return null;
}
geojsonObject = haoutil.system.clone(geojsonObject);
var kml = kgUtil.toKml(geojsonObject, {
name: "Mars3D标绘数据",
documentName: "Mars3D标绘数据文件",
documentDescription: "标绘数据 by mars3d.cn",
simplestyle: true,
});
return kml;
}
downloadKml(filename, target) {
var data = this.toKml(target);
if (data == null) {
haoutil.msg("当前未标绘任何数据!");
} else {
var timestr = new Date().format("MMddHHmmss");
haoutil.file.downloadFile(filename + "_" + timestr + ".kml", data);
}
}
loadGeoJSON(json, options) {
2023-07-08 15:37:34 +08:00
if (json == null) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
var arr = this.graphicGroupLayer.loadGeoJSON(json, options);
//本地存储
var geojson = JSON.stringify(json);
haoutil.storage.add(this.storageName, geojson);
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.showLayerTree();
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
return arr;
2023-07-08 15:37:34 +08:00
}
deleteAll() {
2023-07-18 09:04:15 +08:00
this.stopEditing();
this.graphicGroupLayer.clear();
this.sendDeleteAll();
this.showLayerTree();
2023-07-08 15:37:34 +08:00
}
deleteEntity(graphic) {
2023-07-18 09:04:15 +08:00
graphic.remove();
this.sendDeleteEntity(graphic); //保存数据
this.showLayerTree();
2023-07-08 15:37:34 +08:00
}
//搜索
query(text, maxcount) {
2023-07-18 09:04:15 +08:00
var arrGraphic = this.graphicGroupLayer.getGraphics();
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
var arr = [];
var counts = 0;
2023-07-08 15:37:34 +08:00
for (var i = 0; i < arrGraphic.length; i++) {
2023-07-18 09:04:15 +08:00
var graphic = arrGraphic[i];
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
var name;
if (graphic.type == "label") {
name = graphic.text;
} else if (graphic.attr?.name) {
name = graphic.attr.name;
2023-07-08 15:37:34 +08:00
}
if (name == null || name.indexOf(text) == -1) {
2023-07-18 09:04:15 +08:00
continue;
2023-07-08 15:37:34 +08:00
}
arr.push({
name: name,
2023-07-18 09:04:15 +08:00
type: "标绘 - " + name,
_datatype: "plot",
2023-07-08 15:37:34 +08:00
_entity: graphic,
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
if (maxcount) {
2023-07-18 09:04:15 +08:00
counts++;
2023-07-08 15:37:34 +08:00
if (counts > maxcount) {
2023-07-18 09:04:15 +08:00
break;
2023-07-08 15:37:34 +08:00
}
}
}
2023-07-18 09:04:15 +08:00
return arr;
2023-07-08 15:37:34 +08:00
}
//弹窗属性编辑处理
// showEditAttrWindow(param) {
// this.last_window_param = param
// layer.open({
// type: 2,
// title: '选择数据',
// fix: true,
// shadeClose: false,
// maxmin: true,
// area: ['100%', '100%'],
// content: 'test.html?name=' + param.attrName + '&value=' + param.attrVal,
// skin: 'layer-mars-dialog animation-scale-up',
// success: function (layero) {},
// })
// }
// saveWindowEdit(attrVal) {
// this.viewWindow.plotEdit.updateAttr(this.last_window_param.parname, this.last_window_param.attrName, attrVal)
// layer.close(layer.index)
// }
//分组树相关方法
showLayerTree() {
if (!this.viewWindow || !this.isActivate) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.viewWindow.treeWork.loadData(this.graphicGroupLayer.getLayers());
this.sendSaveEntity(); //保存所有数据
2023-07-08 15:37:34 +08:00
}
checkRemoveGroup(layer) {
if (this.graphicGroupLayer.length < 2) {
2023-07-18 09:04:15 +08:00
haoutil.msg("不能删除所有图层需要至少保留1个图层");
return false;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
return true;
2023-07-08 15:37:34 +08:00
}
deleteLayer(layer) {
2023-07-18 09:04:15 +08:00
var result = this.graphicGroupLayer.deleteLayer(layer);
2023-07-08 15:37:34 +08:00
if (result) {
2023-07-18 09:04:15 +08:00
this.showLayerTree();
haoutil.msg("删除成功!");
return true;
2023-07-08 15:37:34 +08:00
} else {
2023-07-18 09:04:15 +08:00
haoutil.msg("删除失败!");
return false;
2023-07-08 15:37:34 +08:00
}
}
//删除所有非空数组
deleteEmptyLayer() {
if (this.graphicGroupLayer.length < 2) {
2023-07-18 09:04:15 +08:00
haoutil.msg("不能删除所有图层需要至少保留1个图层");
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.deleteEmptyLayer();
this.showLayerTree();
2023-07-08 15:37:34 +08:00
}
editGroupName(layer) {
mars3d.widget.activate({
2023-07-18 09:04:15 +08:00
uri: "widgets/plotGroupName/widget.js",
name: layer ? "重命名分组" : "新增分组",
data: layer ? layer.name : "",
2023-07-08 15:37:34 +08:00
checkName: (newname) => {
//校验分组是否有同名的
2023-07-18 09:04:15 +08:00
return this.graphicGroupLayer.hasLayer(newname, layer);
2023-07-08 15:37:34 +08:00
},
callback: (newname) => {
2023-07-18 09:04:15 +08:00
// console.log(newname)
2023-07-08 15:37:34 +08:00
if (layer) {
//修改
2023-07-18 09:04:15 +08:00
layer.name = newname;
2023-07-08 15:37:34 +08:00
} else {
//新增
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.createLayer(newname);
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.showLayerTree();
2023-07-08 15:37:34 +08:00
},
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
}
changeSelectedLayer(layer) {
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.selectedLayer = layer;
this.showLayerTree();
2023-07-08 15:37:34 +08:00
}
moveToLayer(graphic, group) {
2023-07-18 09:04:15 +08:00
this.graphicGroupLayer.moveToLayer(graphic, group);
this.showLayerTree();
2023-07-08 15:37:34 +08:00
}
//数据保存及加载处理
sendGetList() {
if (window.hasServer) {
//读取后台存储
// window.sendAjax({
// url: 'map/plot/list',
// type: 'get',
// success: (arr) => {
// var arrjson = []
// for (var i = 0; i < arr.length; i++) {
// var geojson = JSON.parse(arr[i].geojson)
// geojson.properties.id = arr[i].id
// arrjson.push(geojson)
// }
2023-07-18 09:04:15 +08:00
// this.loadGeoJSON({ type: 'FeatureCollection', features: arrjson }, { clear: true, flyTo: true })
2023-07-08 15:37:34 +08:00
// },
// })
} else {
//读取本地存储
2023-07-18 09:04:15 +08:00
var laststorage = haoutil.storage.get(this.storageName); //读取localStorage值
if (laststorage == null || laststorage == "null") {
this.loadDemoData();
2023-07-08 15:37:34 +08:00
} else {
2023-07-18 09:04:15 +08:00
var json = JSON.parse(laststorage);
console.log("加载历史缓存数据", json);
this.loadGeoJSON(json, { clear: true, flyTo: true });
2023-07-08 15:37:34 +08:00
}
}
}
sendSaveEntity(graphic) {
if (this.viewWindow == null) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
console.log("plot: 保存了数据");
2023-07-08 15:37:34 +08:00
if (window.hasServer) {
//服务端存储
// if (!graphic.attr.name) {
// haoutil.msg('名称不可为空!')
// return
// }
// let geojson = graphic.toGeoJSON()
// window.sendAjax({
// url: 'map/plot/update',
// type: 'post',
// data: {
// id: graphic.id,
// name: graphic.attr.name || '', //名称
// geojson: geojson || '', //geojson
// },
// contentType: 'application/json',
// success: function (data) {
// graphic.id = data.id
// },
// })
} else {
//本地存储
2023-07-18 09:04:15 +08:00
var geojson = JSON.stringify(this.getGeoJson());
haoutil.storage.add(this.storageName, geojson);
2023-07-08 15:37:34 +08:00
}
//测试的功能
//this.sendSocket(graphic)
}
sendDeleteEntity(graphic) {
2023-07-18 09:04:15 +08:00
// console.log('plot: 删除了数据')
2023-07-08 15:37:34 +08:00
if (window.hasServer) {
//后台删除
// window.sendAjax({
// url: 'map/plot/' + graphic.id,
// type: 'delete',
// contentType: 'application/json',
// success: function (result) {},
// })
} else {
2023-07-18 09:04:15 +08:00
var storagedata = JSON.stringify(this.getGeoJson());
haoutil.storage.add(this.storageName, storagedata);
2023-07-08 15:37:34 +08:00
}
}
sendDeleteAll() {
2023-07-18 09:04:15 +08:00
// console.log('plot: 删除了所有数据')
2023-07-08 15:37:34 +08:00
if (window.hasServer) {
//后台删除
// window.sendAjax({
// url: 'map/plot/deleteAll',
// contentType: 'application/json',
// success: function (result) {},
// })
} else {
//本地存储
2023-07-18 09:04:15 +08:00
haoutil.storage.del(this.storageName);
2023-07-08 15:37:34 +08:00
}
}
//websocket更新坐标
// socketConfig() {
// var that = this
// mars3d.widget.activate({
// uri: 'widgets/plotSocket/widget.js',
// updateAttr: function (json) {
2023-07-18 09:04:15 +08:00
// that.graphicGroupLayer.loadGeoJSON(json)
2023-07-08 15:37:34 +08:00
// },
// })
// }
// sendSocket(graphic) {
// var plotSocket = mars3d.widget.getClass('widgets/plotSocket/widget.js')
// if (plotSocket && plotSocket.isActivate) {
// plotSocket.sendSocket(graphic.toGeoJSON())
// }
// }
//实际系统时可以注释下面代码该代码是mars3d官网在线加载演示数据
loadDemoData() {
2023-07-18 09:04:15 +08:00
if (window.location.hostname.indexOf("mars") == -1) {
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
$.getJSON("//data.mars3d.cn/file/geojson/mars3d-draw.json", (result) => {
2023-07-08 15:37:34 +08:00
if (!this.isActivate) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.loadGeoJSON(result, { clear: true, flyTo: true });
});
2023-07-08 15:37:34 +08:00
}
}
//注册到widget管理器中。
2023-07-18 09:04:15 +08:00
mars3d.widget.bindClass(MyWidget);
2023-07-08 15:37:34 +08:00
//每个widet之间都是直接引入到index.html中会存在彼此命名冲突所以闭包处理下。
2023-07-18 09:04:15 +08:00
})(window, mars3d);