1 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
2 |
|
3 | function _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); } }
|
4 |
|
5 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
6 |
|
7 |
|
8 | import cloneDeep from 'lodash/cloneDeep';
|
9 |
|
10 | var DrawSource = function () {
|
11 | function DrawSource(data) {
|
12 | _classCallCheck(this, DrawSource);
|
13 |
|
14 | this.data = data || this.getDefaultData();
|
15 | }
|
16 |
|
17 | _createClass(DrawSource, [{
|
18 | key: "addFeature",
|
19 | value: function addFeature(feature) {
|
20 | this.data.features.push(feature);
|
21 | }
|
22 | }, {
|
23 | key: "getData",
|
24 | value: function getData() {
|
25 | var features = cloneDeep(this.data.features).map(function (feature) {
|
26 | var _feature$properties, _feature$properties2;
|
27 |
|
28 | feature.properties = {
|
29 | id: feature === null || feature === void 0 ? void 0 : (_feature$properties = feature.properties) === null || _feature$properties === void 0 ? void 0 : _feature$properties.id,
|
30 | type: feature === null || feature === void 0 ? void 0 : (_feature$properties2 = feature.properties) === null || _feature$properties2 === void 0 ? void 0 : _feature$properties2.type
|
31 | };
|
32 | return feature;
|
33 | });
|
34 | return {
|
35 | type: 'FeatureCollection',
|
36 | features: features
|
37 | };
|
38 | }
|
39 | }, {
|
40 | key: "getFeature",
|
41 | value: function getFeature(id) {
|
42 | var result = this.data.features.find(function (fe) {
|
43 | var _fe$properties;
|
44 |
|
45 | return (fe === null || fe === void 0 ? void 0 : (_fe$properties = fe.properties) === null || _fe$properties === void 0 ? void 0 : _fe$properties.id) === id;
|
46 | });
|
47 | return result;
|
48 | }
|
49 | }, {
|
50 | key: "removeAllFeatures",
|
51 | value: function removeAllFeatures() {
|
52 | this.data = this.getDefaultData();
|
53 | }
|
54 | }, {
|
55 | key: "removeFeature",
|
56 | value: function removeFeature(feature) {
|
57 | var index = this.getFeatureIndex(feature);
|
58 |
|
59 | if (index !== undefined) {
|
60 | this.data.features.splice(index, 1);
|
61 | }
|
62 | }
|
63 | }, {
|
64 | key: "setFeatureActive",
|
65 | value: function setFeatureActive(feature) {
|
66 | var _feature$properties3;
|
67 |
|
68 | var fe = this.getFeature(feature === null || feature === void 0 ? void 0 : (_feature$properties3 = feature.properties) === null || _feature$properties3 === void 0 ? void 0 : _feature$properties3.id);
|
69 |
|
70 | if (fe && fe.properties) {
|
71 | fe.properties.active = true;
|
72 | }
|
73 | }
|
74 | }, {
|
75 | key: "setFeatureUnActive",
|
76 | value: function setFeatureUnActive(feature) {
|
77 | var _feature$properties4;
|
78 |
|
79 | var fe = this.getFeature(feature === null || feature === void 0 ? void 0 : (_feature$properties4 = feature.properties) === null || _feature$properties4 === void 0 ? void 0 : _feature$properties4.id);
|
80 |
|
81 | if (fe && fe.properties) {
|
82 | fe.properties.active = false;
|
83 | }
|
84 | }
|
85 | }, {
|
86 | key: "clearFeatureActive",
|
87 | value: function clearFeatureActive() {
|
88 | this.data.features.forEach(function (fe) {
|
89 | if (fe && fe.properties) {
|
90 | fe.properties.active = false;
|
91 | }
|
92 | });
|
93 | }
|
94 | }, {
|
95 | key: "updateFeature",
|
96 | value: function updateFeature(feature) {
|
97 | this.removeFeature(feature);
|
98 | this.addFeature(feature);
|
99 | }
|
100 | }, {
|
101 | key: "destroy",
|
102 | value: function destroy() {
|
103 | this.data = this.getDefaultData();
|
104 | }
|
105 | }, {
|
106 | key: "getDefaultData",
|
107 | value: function getDefaultData() {
|
108 | return {
|
109 | type: 'FeatureCollection',
|
110 | features: []
|
111 | };
|
112 | }
|
113 | }, {
|
114 | key: "getFeatureIndex",
|
115 | value: function getFeatureIndex(feature) {
|
116 | return this.data.features.findIndex(function (fe) {
|
117 | var _fe$properties2, _feature$properties5;
|
118 |
|
119 | return (fe === null || fe === void 0 ? void 0 : (_fe$properties2 = fe.properties) === null || _fe$properties2 === void 0 ? void 0 : _fe$properties2.id) === (feature === null || feature === void 0 ? void 0 : (_feature$properties5 = feature.properties) === null || _feature$properties5 === void 0 ? void 0 : _feature$properties5.id);
|
120 | });
|
121 | }
|
122 | }]);
|
123 |
|
124 | return DrawSource;
|
125 | }();
|
126 |
|
127 | export { DrawSource as default }; |
\ | No newline at end of file |