comparison src/djol/Map.js @ 8:f0035923ff3e

добавлена библиотека для работы с openlayers 3+
author cin
date Mon, 21 Aug 2017 17:47:00 +0300
parents
children
comparison
equal deleted inserted replaced
7:9c0943c68a90 8:f0035923ff3e
1 define([
2 "dijit/registry",
3 "dojo/_base/declare",
4 "dijit/_WidgetBase",
5 "dojo/dom-construct",
6 "./PopupContainer",
7 "dojo/_base/array",
8 "ol",
9 "ol3/listen",
10 "dojo/Deferred",
11 "implab/safe"
12 ], function (registry, declare, _WidgetBase, dom, PopupContainer, array, ol, listen, Deferred, safe) {
13
14 return declare([_WidgetBase], {
15 popupOverlays: null,
16 olMap: null,
17
18 _pending: null,
19
20 constructor: function () {
21 this._pending = {};
22 },
23
24 buildRendering: function () {
25 this.domNode = dom.create("div");
26 },
27
28 postCreate: function () {
29 this.inherited(arguments);
30 this.popupOverlays = {};
31 this.olMap = new ol.Map({
32 target: this.domNode,
33 layers: this.layers,
34 view: this.view,
35 controls: this.controls || [],
36 });
37 },
38
39 getProjection : function() {
40 return this.view.getProjection();
41 },
42
43 addInteraction: function (value) {
44 this.olMap.addInteraction(value);
45 },
46
47 addLayer: function (layer) {
48 if (layer) {
49 if (layer.get("layerType") == "base")
50 this.olMap.getLayers().insertAt(0, layer);
51 else
52 this.olMap.addLayer(layer);
53 }
54 },
55
56 removeLayer: function(layer) {
57 this.olMap.removeLayer(layer);
58 },
59
60 startup: function () {
61 this.inherited(arguments);
62
63 this.olMap.updateSize();
64 },
65
66 showPopup: function (contentWidget, position, opts) {
67 // Скрыть popups указанной в opts.role роли, если (opts.hint ==
68 // "replace")
69 // Если не задан opts или opta.hint скрывать все popup
70
71 var me = this;
72
73 if ((!opts) || (!opts.hint)) {
74 this.closeAllPopups();
75 } else if ((opts.hint) && (opts.hint == "replace")) {
76 if (opts.role) {
77 this.closePopupsByRole(opts.role);
78 } else {
79 this.closeAllPopups();
80 }
81 }
82 var overlay = new ol.Overlay({});
83
84 if (opts && (opts.role)) {
85 if (this.popupOverlays[opts.role]) {
86 this.popupOverlays[opts.role].push(overlay);
87 } else {
88 this.popupOverlays[opts.role] = [overlay];
89 }
90 }
91
92 // Отображение popUp start
93 this.olMap.addOverlay(overlay);
94 var popup = new PopupContainer({
95 overlay: overlay,
96 map: this.olMap,
97 onClose: function () {
98 // registry.byNode(overlay.getElement()).destroyRecursive();
99 array.forEach(me.popupOverlays[opts.role], function (o) {
100 if (o === overlay) {
101 var index = me.popupOverlays[opts.role].indexOf(o);
102 if (index > -1) {
103 me.popupOverlays[opts.role].splice(index, 1);
104 }
105 }
106 });
107 }
108 });
109 overlay.setElement(popup.domNode);
110 popup.show(contentWidget);
111 overlay.setPosition(position);
112 popup.hideOverlay();
113 // end
114 return popup;
115 },
116
117 closeAllPopups: function () {
118 var overlays = this.olMap.getOverlays();
119 overlays.forEach(function (elenemt, index) {
120 registry.byNode(elenemt.getElement()).destroyRecursive();
121 }, this);
122 this.popupOverlays = {};
123 },
124
125 closePopupsByRole: function (role) {
126 if (this.popupOverlays[role]) {
127 array.forEach(this.popupOverlays[role], function (overlay) {
128 registry.byNode(overlay.getElement()).destroyRecursive();
129 });
130 this.popupOverlays[role] = [];
131 }
132 },
133
134 /**
135 * Подписывается на событие карты
136 *
137 * @param {String}
138 * name Имя события
139 * @param {function(evt)}
140 * filter Фильтр того, что событие нужное
141 *
142 * @returns {ol.ObjectEvent | ol.MapBroeserEvent | ol.MapEvent} Событие
143 * на которое подписались.
144 */
145 awaitMapEvent: function (name, filter) {
146 safe.argumentNotEmptyString(name, "name");
147 var map = this.olMap,
148 handle, d, me = this;
149
150 if (me._pending[name])
151 throw new Error("An event is already pending: " + name);
152
153 me._pending[name] = d = new Deferred(function () {
154 handle.remove();
155 });
156
157 handle = listen.once(map, name, function (evt) {
158 if (!filter || filter(evt))
159 d.resolve(evt);
160 });
161
162 return d.then(function (evt) {
163 delete me._pending[name];
164 return evt;
165 }, function (err) {
166 delete me._pending[name];
167 throw err;
168 });
169 },
170
171 cancelPendingEvents: function () {
172 for (var name in this._pending)
173 this._pending[name].cancel();
174 }
175 });
176 });