Newer
Older
import LayerBaseVector from "../layers/LayerBaseVector";
import MapInteractionBase from './mapInteractionBase';
import * as checkDefined from '../util/checkDefined';
import provide from '../util/provide';
import makeGuid from '../util/makeGuid';
import {ol} from 'custom-ol'
import Timer = NodeJS.Timer;
const $ = require('jquery');
const nm = provide('olHelpers');
export interface extentObject{
minX: number;
minY: number;
maxX: number;
maxY: number;
}
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
export interface mapMoveCallbackFunction{
/**
*
* @param extent extent as predefined object minX, minX, maxX, maxY
* @param zoomLevel current zoom level
* @param evtType the event type 'change:center', 'change:resolution'
*/
(extent: extentObject, zoomLevel: number, evtType?: string): any
}
/**
* assists with map move interactions, trigger callback functions
* @augments MapInteractionBase
*/
export class MapMoveCls extends MapInteractionBase {
_mapExtent: extentObject;
_zoomLevel: number;
_lookupLayer: Object;
_arrLayer: Array<LayerBaseVector>;
_arrLyrTimeout: Array<Timer>;
_mapMoveCallbackTimeout: Array<Timer>;
_mapMoveCallbackDelays: Array<number>;
_mapMoveCallbacksLookup: Object;
_mapMoveCallbackContext: Array<Object>;
_mapMoveCallbacks: Array<mapMoveCallbackFunction>;
_arrLyrRequest: Array<any>;
/**
* constructor called implicitly
*/
constructor() {
super('map move');
this._arrLyrRequest = [];
this._arrLyrTimeout = [];
this._arrLayer = [];
this._lookupLayer = {};
this._mapMoveCallbacks = [];
this._mapMoveCallbacksLookup = {};
this._mapMoveCallbackDelays = [];
this._mapMoveCallbackContext = [];
this._mapMoveCallbackTimeout = [];
this._mapExtent = undefined;
this._zoomLevel = undefined;
}
/**
* initialize the map move object
* @param theMap - the ol map
*/
init(theMap: ol.Map){
super.init(theMap);
this.map.getView().on(['change:center', 'change:resolution'], (e) =>{
this._updateMapExtent();
// trigger the layer updates
for (let i = 0; i < this._arrLayer.length; i++) {
this.triggerLyrLoad(this._arrLayer[i], i, e.type);
}
// trigger the map callbacks
for (let i = 0; i < this._mapMoveCallbacks.length; i++) {
this.triggerMoveCallback(i, e.type);
}
});
}
_updateMapExtent() {
let theView = this.map.getView();
this._zoomLevel = theView.getZoom();
let extentArray = theView.calculateExtent(this.map.getSize());
this._mapExtent = {
minX: extentArray[0],
minY: extentArray[1],
maxX: extentArray[2],
maxY: extentArray[3]
};
}
/**
* return the map extent
*/
get mapExtent() {
if (!this._mapExtent) {
this._updateMapExtent();
}
return this._mapExtent;
}
/**
* Trigger the layer load
* @param lyr the layer being acted on
* @param index index of the layer
* @param eventType the event triggering the load, as 'change:center' or 'change:resolution'
*/
triggerLyrLoad(lyr: LayerBaseVector, index?: number, eventType?: string) {
if (checkDefined.undefinedOrNull(lyr) && checkDefined.undefinedOrNull(index)) {
throw 'need to define lyr or index';
} else if (checkDefined.definedAndNotNull(lyr) && checkDefined.undefinedOrNull(index)) {
index = this._arrLayer.indexOf(lyr);
} else if (checkDefined.undefinedOrNull(lyr) && checkDefined.definedAndNotNull(index)) {
lyr = this._arrLayer[index];
}
// clear the timeout
if (this._arrLyrTimeout[index] != null) {
clearTimeout(this._arrLyrTimeout[index]);
this._arrLyrTimeout[index] = null;
}
// abort if necessary and clear the request
if (this._arrLyrRequest[index] != null && this._arrLyrRequest[index] != 4) {
this._arrLyrRequest[index].abort();
this._arrLyrRequest[index] = null;
}
// dummy callback used if before load returns false
let callbackFunc = function () {};
if (lyr.mapMoveBefore(this._zoomLevel, eventType)) {
lyr.mapMoveMakeGetParams(this._mapExtent, this._zoomLevel);
let _this = this;
callbackFunc = function () {
function innerFunction(theLayer, theIndex) {
let _innerThis = this;
this._arrLyrRequest[theIndex] = $.get(
theLayer.url,
theLayer.mapMoveParams,
function (d) {
/**
* @type {LayerBaseVector}
*/
theLayer.mapMoveCallback(d);
theLayer.loadCallback();
}, 'json').fail(
function (jqXHR) {
if (jqXHR.statusText != 'abort') {
console.log('failed');
console.log(theLayer.url);
console.log(theLayer.mapMoveParams);
}
}).always(
function () {
_innerThis._arrLyrTimeout[theIndex] = null;
_innerThis._arrLyrRequest[theIndex] = null;
});
}
innerFunction.call(_this, lyr, index);
};
} else {
lyr.clear();
}
this._arrLyrTimeout[index] = setTimeout(callbackFunc, lyr.onDemandDelay);
}
/**
* trigger the map move call back at the given index
* @param ind - the index of the layer
* @param eventType=undefined the event triggering the load as 'change:center' or 'change:resolution'
* @param functionId=undefined the function id used to reference the added callback function
*/
triggerMoveCallback(ind: number, eventType?: string, functionId?: string) {
if (typeof ind == 'undefined' && typeof functionId == 'undefined'){
throw 'either the function index or the id must be defined';
}
if (typeof ind !== 'number'){
ind = this._mapMoveCallbacks.indexOf(this._mapMoveCallbacksLookup[functionId]);
}
if (ind < 0){
console.log('function not found');
return;
}
// clear the timeout
if (this._mapMoveCallbackTimeout[ind] != null) {
clearTimeout(this._mapMoveCallbackTimeout[ind]);
this._mapMoveCallbackTimeout[ind] = null;
}
let ctx = this._mapMoveCallbackContext[ind];
let theFunc = this._mapMoveCallbacks[ind];
let _this = this;
let f = function () {
if (ctx !== null) {
theFunc.call(ctx, _this._mapExtent, _this._zoomLevel, eventType);
} else {
theFunc(_this._mapExtent, _this._zoomLevel, eventType);
}
};
this._mapMoveCallbackTimeout[ind] = setTimeout(f, this._mapMoveCallbackDelays[ind]);
}
/**
* Add a layer to the interaction
* @param lyr - layer to add
* @param triggerOnAdd - if the layer should be loaded on add
*/
addVectorLayer(lyr: LayerBaseVector, triggerOnAdd: boolean = true) {
if (this._arrLayer.indexOf(lyr) > -1) {
console.log('already added ' + lyr.name + ' to map move');
return;
}
this._checkInit();
this._arrLyrRequest.push(null);
this._arrLyrTimeout.push(null);
this._arrLayer.push(lyr);
this._lookupLayer[lyr.id] = lyr;
triggerOnAdd = typeof triggerOnAdd == 'boolean' ? triggerOnAdd : true;
if (triggerOnAdd) {
if (this._mapExtent === undefined) {
this._updateMapExtent();
}
this.triggerLyrLoad(lyr, this._arrLayer.length - 1);
}
}
/**
* add a callback to the map move event
* @param func - callback function
* @param context - the context to use for this function
* @param delay=50 the delay before call load
* @param triggerOnAdd if the layer should be loaded on add to mapMove
* @param functionId optional id to reference the function later for outside triggering
*/
addCallback(func: mapMoveCallbackFunction, context: any, delay?: number, triggerOnAdd? : boolean, functionId?: string) {
if (this._mapMoveCallbacks.indexOf(func) > -1) {
console.log('this function already added to map move');
return;
}
this._checkInit();
if (!functionId){
functionId = makeGuid();
}
this._mapMoveCallbacks.push(func);
this._mapMoveCallbacksLookup[functionId] = functionId;
this._mapMoveCallbackDelays.push(typeof delay == 'number' ? delay : 50);
this._mapMoveCallbackContext.push(checkDefined.definedAndNotNull(context) ? context : null);
this._mapMoveCallbackTimeout.push(null);
triggerOnAdd = typeof triggerOnAdd == 'boolean' ? triggerOnAdd : true;
if (triggerOnAdd) {
if (this._mapExtent === undefined) {
this._updateMapExtent();
}
this.triggerMoveCallback(this._mapMoveCallbacks.length - 1);
}
}
}
nm.MapMoveCls = MapMoveCls;
export default MapMoveCls;