widget.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use script"; //开发环境建议开启严格模式
  2. (function (window, mars3d) {
  3. //创建widget类,需要继承BaseWidget
  4. class MyWidget extends mars3d.widget.BaseWidget {
  5. //弹窗配置
  6. get view() {
  7. return {
  8. type: "window",
  9. url: "view.html",
  10. style: "dark",
  11. windowOptions: {
  12. skin: "layer-mars-dialog animation-scale-up",
  13. width: 250,
  14. position: {
  15. top: 10,
  16. right: 5,
  17. bottom: 30,
  18. },
  19. },
  20. };
  21. }
  22. //初始化[仅执行1次]
  23. create() {
  24. let that = this;
  25. $.getJSON(this.path + "config/attr.json", function (data) {
  26. that.attrConfig = data;
  27. that.setDefaultVal();
  28. that.attrConfig["curve"] = that.attrConfig["polyline"];
  29. that.startEditing();
  30. });
  31. }
  32. //获取所有可配置属性的默认值
  33. setDefaultVal() {
  34. let data = this.attrConfig;
  35. //标号默认样式
  36. let attrDefConfig = {};
  37. for (let i in data) {
  38. let defstyle = {};
  39. for (let idx = 0; idx < data[i].style.length; idx++) {
  40. let item = data[i].style[idx];
  41. defstyle[item.name] = item.defval;
  42. }
  43. attrDefConfig[i] = defstyle;
  44. }
  45. this.attrDefConfig = attrDefConfig;
  46. // let logInfo = JSON.stringify(attrDefConfig)
  47. // logInfo = logInfo.replaceAll('"diffHeight":0,', '').replaceAll('"hasShadows":false,', '')
  48. // console.log('标号默认样式', logInfo)
  49. }
  50. //每个窗口创建完成后调用
  51. winCreateOK(opt, result) {
  52. this.viewWindow = result;
  53. }
  54. //激活插件
  55. activate() {}
  56. //释放插件
  57. disable() {}
  58. getDefStyle(type) {
  59. let defStyle = this.attrDefConfig[type] || {};
  60. return mars3d.Util.clone(defStyle);
  61. }
  62. getMinPointNum() {
  63. let graphic = this.config.graphic;
  64. if (graphic && graphic._minPointNum) {
  65. return graphic._minPointNum;
  66. }
  67. return 3;
  68. }
  69. getMaxPointNum() {
  70. let graphic = this.config.graphic;
  71. if (graphic && graphic._maxPointNum) {
  72. return graphic._maxPointNum;
  73. }
  74. return 999;
  75. }
  76. get defaultAttrList() {
  77. return [
  78. { name: "id", label: "主键", type: "label", defval: "" },
  79. { name: "name", label: "名称", type: "text", defval: "" },
  80. { name: "remark", label: "备注", type: "textarea", defval: "" },
  81. ];
  82. }
  83. getAttrList() {
  84. return this.config.attrList || this.defaultAttrList;
  85. }
  86. getLayerName() {
  87. let graphic = this.config.graphic;
  88. return graphic?._layer?.name || "";
  89. }
  90. startEditing(graphic, lonlats) {
  91. if (graphic) {
  92. this.config.graphic = graphic;
  93. }
  94. if (lonlats) {
  95. this.config.lonlats = lonlats;
  96. }
  97. if (this.viewWindow == null) {
  98. return;
  99. }
  100. graphic = this.config.graphic;
  101. lonlats = this.config.lonlats;
  102. this.viewWindow.plotEdit.startEditing(graphic.options, lonlats);
  103. }
  104. //更新图上的属性
  105. updateAttr2map(attr) {
  106. console.log("更新属性", attr);
  107. var graphic = this.config.graphic; //当前编辑的graphic
  108. graphic.setOptions(attr);
  109. }
  110. //更新坐标
  111. updatePoints2map(points) {
  112. console.log("更新坐标", points);
  113. var graphic = this.config.graphic;
  114. graphic.positions = points;
  115. }
  116. centerCurrentEntity() {
  117. let graphic = this.config.graphic;
  118. graphic.flyTo();
  119. }
  120. deleteEntity() {
  121. let graphic = this.config.graphic;
  122. graphic.remove();
  123. this.disableBase();
  124. }
  125. //文件处理
  126. getGeoJson() {
  127. let graphic = this.config.graphic;
  128. let geojson = graphic.toGeoJSON();
  129. geojson.properties._layer = graphic._layer.name; //记录分组信息
  130. return geojson;
  131. }
  132. }
  133. //注册到widget管理器中。
  134. mars3d.widget.bindClass(MyWidget);
  135. //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
  136. })(window, mars3d);