XWWitPark/witpark_szjsc/xw_szjsc_web/public/widgets/plot/widget.js
2024-09-12 08:10:00 +08:00

2 lines
7.3 KiB
JavaScript

/* 2020-9-24 14:48:23 | 版权所有 军懋国兴 */
mars3d.widget.bindClass(mars3d.widget.BaseWidget.extend({options:{resources:["./lib/dom2img/dom-to-image.js"],view:{type:"window",url:"view.html",windowOptions:{width:250,position:{top:50,right:5,bottom:5}}}},drawControl:null,getServerURL:function(){return this.viewer.mars.config.serverURL},getGltfServerURL:function(){return serverURL_gltf},create:function(){var i=this;this.drawControl=new mars3d.Draw(this.viewer,{hasEdit:!1,nameTooltip:!0}),this.drawControl.on(mars3d.draw.event.DrawCreated,function(t){var e=t.entity;i.startEditing(e)}),this.drawControl.on(mars3d.draw.event.EditStart,function(t){var e=t.entity;i.startEditing(e)}),this.drawControl.on(mars3d.draw.event.EditMovePoint,function(t){var e=t.entity;i.startEditing(e)}),this.drawControl.on(mars3d.draw.event.EditRemovePoint,function(t){var e=t.entity;i.startEditing(e)}),this.drawControl.on(mars3d.draw.event.EditStop,function(t){var e=t.entity;i.stopEditing(e),i.sendSaveEntity(e),i.showTable()}),this.drawControl.on(mars3d.draw.event.Delete,function(t){i.sendDeleteEntity(t.entity),i.showTable()}),window.bindToLayerControl&&(this.layerWork=bindToLayerControl({pid:0,name:"标绘",visible:!0,onAdd:function(){i.drawControl.setVisible(!0)},onRemove:function(){i.drawControl.setVisible(!1)},onCenterAt:function(t){var e=i.drawControl.getEntitys();i.viewer.flyTo(e,{duration:t})},compare:{onCreate:function(t){this.drawControlEx=new mars3d.Draw(t,{hasEdit:!1,nameTooltip:!0})},onAdd:function(){this.drawControlEx.loadJson(i.getGeoJson()),this.drawControlEx.setVisible(!0)},onRemove:function(){this.drawControlEx.setVisible(!1)}}})),this.sendGetList()},viewWindow:null,winCreateOK:function(t,e){this.viewWindow=e},activate:function(){this.drawControl.hasEdit(!0)},disable:function(){this.stopEditing(),this.viewWindow=null,this.drawControl.stopDraw(),this.drawControl.hasEdit(!1)},getDefStyle:function(t){return mars3d.draw.util.getDefStyle(t)},startDraw:function(t){t&&"model"===t.type&&haoutil.msg("模型根据其大小需要一点加载时间,请稍等片刻。"),this.drawControl.startDraw(t)},endDraw:function(){this.drawControl.endDraw()},startEditingById:function(t){var e=this.drawControl.getEntityById(t);null!=e&&(this.viewer.mars.flyTo(e),this.drawControl.startEditing(e))},startEditing:function(t){var e,i,n=this;null!=this.viewWindow&&(e=this.drawControl.getCoordinates(t),clearTimeout(this.timeTik),(i=mars3d.widget.getClass("widgets/plotAttr/widget.js"))&&i.isActivate?i.startEditing(t,e):mars3d.widget.activate({uri:"widgets/plotAttr/widget.js",entity:t,lonlats:e,deleteEntity:function(t){n.deleteCurrentEntity(t)},updateAttr:function(t){n.updateAttr2map(t)},updateGeo:function(t){n.updateGeo2map(t)},centerAt:function(t){n.centerCurrentEntity(t)}}))},stopEditing:function(){this.timeTik=setTimeout(function(){mars3d.widget.disable("widgets/plotAttr/widget.js")},200)},updateAttr2map:function(t){this.drawControl.updateAttribute(t)},updateGeo2map:function(t){return this.drawControl.setPositions(t),t},centerCurrentEntity:function(t){this.viewer.mars.flyTo(t,{scale:.5,radius:1e3})},getGeoJson:function(){return this.drawControl.toGeoJSON()},getCurrentEntityGeoJson:function(){var t=this.drawControl.getCurrentEntity();if(null!=t)return this.drawControl.toGeoJSON(t)},jsonToLayer:function(t,e,i){if(null!=t)return this.showTable(t),this.drawControl.loadJson(t,{clear:e,flyTo:i})},deleteAll:function(){this.drawControl.deleteAll(),this.sendDeleteAll(),this.showTable()},deleteEntity:function(t){var e=this.drawControl.getEntityById(t);null!=e&&this.drawControl.deleteEntity(e)},isOnDraw:!1,deleteCurrentEntity:function(t){null!=(t=t||this.drawControl.getCurrentEntity())&&this.drawControl.deleteEntity(t)},hasEdit:function(t){this.drawControl.hasEdit(t)},query:function(t,e){for(var i=this.drawControl.getEntitys(),n=[],o=0,r=0;r<i.length;r++){var a,s=i[r];if("label"===s.attribute.type?a=s.attribute.style.text:s.attribute.attr&&(a=s.attribute.attr.name),null!=a&&-1!=a.indexOf(t)&&(n.push({name:a,type:"标绘 - "+s.attribute.name,_datatype:"plot",_entity:s}),e&&e<++o))break}return n},last_window_param:null,showEditAttrWindow:function(t){this.last_window_param=t},saveWindowEdit:function(t){this.viewWindow.plotEdit.updateAttr(this.last_window_param.parname,this.last_window_param.attrName,t),layer.close(layer.index)},showTable:function(t){var e=[];if((t=t||this.getGeoJson())&&t.features)for(var i=0,n=t.features.length;i<n;i++){var o,r,a=t.features[i];a.properties&&(o=a.properties.attr&&a.properties.attr.name||a.properties.name||"未命名",r=a.properties.type),e.push({index:i,name:o,type:r})}this.arrList=e,this.viewWindow&&this.viewWindow.tableWork.loadData(e)},showTableItem:function(t){var e=this.drawControl.getEntitys(!0)[t.index];this.viewer.mars.flyTo(e)},delTableItem:function(t){var e=this.drawControl.getEntitys()[t.index];this.drawControl.deleteEntity(e)},editTableItem:function(t){var e=this.drawControl.getEntitys(!0)[t.index];null!=e&&(this.viewer.mars.flyTo(e),this.drawControl.startEditing(e))},storageName:"marsgis_plot",sendGetList:function(){var t,e,r=this;window.hasServer?sendAjax({url:"map/plot/list",type:"get",success:function(t){for(var e=[],i=0;i<t.length;i++){var n=JSON.parse(t[i].geojson);n.properties.attr.id=t[i].id,e.push(n)}var o={type:"FeatureCollection",features:e};r.drawControl.loadJson(o,{clear:!0,flyTo:!1});r.showTable(o)}}):null==(t=haoutil.storage.get(this.storageName))||"null"==t?"127.0.0.1"!=location.hostname&&"localhost"!=location.hostname&&$.getJSON(serverURL_file+"/geojson/draw-demo.json",function(t){r.isActivate&&r.jsonToLayer(t,!0,!0)}):(e=JSON.parse(t),r.jsonToLayer(e,!0,!0))},sendSaveEntity:function(e){if(null!=this.viewWindow)if(this.isOnDraw)this.isOnDraw=!1;else{if(console.log("plot: 保存了数据"),window.hasServer){e.attribute.attr=e.attribute.attr||{};var t=e.attribute.attr.id,i=this.drawControl.toGeoJSON(e);if(!e.attribute.attr.name)return void haoutil.msg("名称不可为空!");t?sendAjax({url:"map/plot/update",data:JSON.stringify({id:t,geojson:i}),contentType:"application/json",success:function(){}}):sendAjax({url:"map/plot/add",type:"post",data:JSON.stringify({name:e.attribute.attr.name||"",geojson:i||"",type:e.attribute.type,remark:e.attribute.attr.remark||""}),contentType:"application/json",success:function(t){e.attribute.attr.id=t.id}})}else{e.attribute.attr.id=e.attribute.attr.id||(new Date).format("yyyyMMddHHmmss");i=JSON.stringify(this.getGeoJson());haoutil.storage.add(this.storageName,i)}this.sendSocket(e)}},sendDeleteEntity:function(t){if(console.log("plot: 删除了数据"),window.hasServer){var e=t.attribute.attr.id;if(!e)return void(this.isOnDraw=!0);sendAjax({url:"map/plot/"+e,data:JSON.stringify({}),type:"delete",contentType:"application/json",success:function(){}})}else{var i=JSON.stringify(this.getGeoJson());haoutil.storage.add(this.storageName,i)}},sendDeleteAll:function(){console.log("plot: 删除了所有数据"),window.hasServer?sendAjax({url:"map/plot/deleteAll",contentType:"application/json",success:function(){}}):haoutil.storage.del(this.storageName)},socketConfig:function(){var e=this;mars3d.widget.activate({uri:"widgets/plotSocket/widget.js",updateAttr:function(t){e.drawControl.loadJson(t,{clear:!1,flyTo:!1})}})},sendSocket:function(t){var e,i=mars3d.widget.getClass("widgets/plotSocket/widget.js");i&&i.isActivate&&(e=this.drawControl.toGeoJSON(t),i.sendSocket(e))}}));