UNPKG

7.93 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.DrawControl = void 0;
7
8var _l = require("@antv/l7");
9
10require("./css/draw.css");
11
12var _modes = require("./modes");
13
14var _helpers = require("@turf/helpers");
15
16var _constant = require("./util/constant");
17
18function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
19
20function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21
22function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
23
24function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
25
26function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
27
28function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
29
30function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
31
32function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
33
34function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
35
36function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
37
38function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
39
40var DrawType = {
41 point: _modes.DrawPoint,
42 line: _modes.DrawLine,
43 polygon: _modes.DrawPolygon,
44 circle: _modes.DrawCircle,
45 rect: _modes.DrawRect
46};
47
48var DrawControl = /*#__PURE__*/function (_Control) {
49 _inherits(DrawControl, _Control);
50
51 var _super = _createSuper(DrawControl);
52
53 function DrawControl(scene, options) {
54 var _this;
55
56 _classCallCheck(this, DrawControl);
57
58 _this = _super.call(this, options);
59 _this.draw = {};
60
61 _this.onButtonClick = function (type, e) {
62 for (var draw in _this.draw) {
63 if (draw === type) {
64 _this.draw[draw].enable();
65 } else {
66 _this.draw[draw].disable();
67 }
68 }
69 };
70
71 _this.onDeleteMode = function (type, e) {
72 e.stopPropagation();
73
74 if (!_this.currentDraw) {
75 return;
76 }
77
78 _this.currentDraw.deleteMode.enable();
79
80 return false;
81 };
82
83 _this.onModeChange = function (type, mode) {
84 if (mode === _constant.DrawModes.SIMPLE_SELECT) {
85 _this.currentDraw = _this.draw[type];
86 }
87 };
88
89 _this.scene = scene;
90 return _this;
91 }
92
93 _createClass(DrawControl, [{
94 key: "getDefault",
95 value: function getDefault() {
96 return {
97 position: _l.PositionType.TOPLEFT,
98 controls: {
99 point: true,
100 line: true,
101 polygon: true,
102 rect: true,
103 circle: true,
104 delete: true
105 },
106 name: 'draw'
107 };
108 }
109 }, {
110 key: "onAdd",
111 value: function onAdd() {
112 var layout = this.controlOption.layout;
113 var controlClass = 'l7-control-draw' + ' ' + layout;
114 var controls = this.controlOption.controls;
115
116 var container = _l.DOM.create('div', controlClass);
117
118 this.addControls(controls, container); // 代理每个绘制组件的事件
119
120 this.addControlEvent(); // 监听组件 选中, 编辑
121
122 return container;
123 }
124 }, {
125 key: "onRemove",
126 value: function onRemove() {
127 for (var draw in this.draw) {
128 if (this.draw[draw]) {
129 this.draw[draw].destroy();
130 }
131 }
132 }
133 }, {
134 key: "getDraw",
135 value: function getDraw(type) {
136 var controls = this.controlOption.controls;
137
138 if (controls[type]) {
139 return this.draw[type];
140 }
141
142 return null;
143 }
144 }, {
145 key: "getAllData",
146 value: function getAllData() {
147 var res = {};
148
149 for (var draw in this.draw) {
150 if (this.draw[draw]) {
151 res[draw] = this.draw[draw].getData();
152 }
153 }
154
155 return res;
156 }
157 }, {
158 key: "removeAllData",
159 value: function removeAllData() {
160 for (var draw in this.draw) {
161 if (this.draw[draw]) {
162 this.draw[draw].removeAllData();
163 }
164 }
165 }
166 }, {
167 key: "addControls",
168 value: function addControls(controls, container) {
169 var style = this.controlOption.style;
170
171 for (var type in controls) {
172 if (DrawType[type] && controls[type] !== false) {
173 var drawOption = (0, _helpers.isObject)(controls[type]) ? controls[type] : {};
174
175 if (style) {
176 drawOption.style = style;
177 }
178
179 var draw = new DrawType[type](this.scene, drawOption);
180 draw.on(_constant.DrawEvent.MODE_CHANGE, this.onModeChange.bind(null, type));
181 this.draw[type] = draw;
182 this.createButton(draw.title, 'draw-' + type, container, 'click', this.onButtonClick.bind(null, type));
183 } else if (type === 'delete' && controls[type] !== false) {
184 var _draw = new _modes.DrawDelete(this.scene);
185
186 _draw.on(_constant.DrawEvent.MODE_CHANGE, this.onModeChange.bind(null, type));
187
188 this.createButton(_draw.title, 'draw-' + type, container, 'mousedown', this.onDeleteMode.bind(null, type));
189 }
190 }
191 }
192 }, {
193 key: "addControlEvent",
194 value: function addControlEvent() {
195 var _this2 = this;
196
197 var _loop = function _loop(draw) {
198 if (_this2.draw[draw]) {
199 ['draw.create', 'draw.update', 'draw.delete'].forEach(function (type) {
200 _this2.draw[draw].on(type, function (feature) {
201 _this2.emit(type, {
202 drawType: draw,
203 feature: feature
204 });
205 });
206 });
207 }
208 };
209
210 for (var draw in this.draw) {
211 _loop(draw);
212 }
213 }
214 }, {
215 key: "createButton",
216 value: function createButton(tile, className, container, eventType, fn) {
217 var link = _l.DOM.create('button', className, container);
218
219 link.title = tile;
220 link.addEventListener(eventType, fn, false);
221 return link;
222 }
223 }]);
224
225 return DrawControl;
226}(_l.Control);
227
228exports.DrawControl = DrawControl;
\No newline at end of file