/
ModeSelector.js
339 lines (301 loc) · 8.9 KB
/
ModeSelector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// The MIT License (MIT)
//
// Copyright (c) 2019-2022 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
import ngeoQueryAction from 'ngeo/query/Action';
import ngeoQueryMode from 'ngeo/query/Mode';
import {isEventUsingCtrlKey} from 'ngeo/utils';
import {listen as olEventsListen} from 'ol/events';
/**
* @hidden
*/
export class QueryModeSelector {
/**
* Service that provides the query "mode", i.e. how queries on the
* map should be made by the user.
*
* It also provides the query "action", i.e. what to do with the
* query results.
*
* Both the "mode" and "action" can be temporarily set if a key is
* pressed on the keyboard by the user. Therefore, this service
* listens to the keyboard `keydown` and `keyup` events to
* temporarily set them accordingly depending on the key that was
* pressed.
*
* @param {angular.IScope} $rootScope .
*/
constructor($rootScope) {
// Constants
/**
* The key to press to temporarily set the action to "ADD"
*
* @type {string}
* @private
*/
this.keyActionAdd_ = 'a';
/**
* The key to press to temporarily set the action to "REMOVE"
*
* @type {string}
* @private
*/
this.keyActionRemove_ = 'x';
/**
* @type {string[]}
* @private
*/
this.keysAction_ = [this.keyActionAdd_, this.keyActionRemove_];
// Variables
/**
* The action currently active.
*
* @type {string}
* @private
*/
this.action_ = ngeoQueryAction.REPLACE;
/**
* A flag that determines whether a request is currently pending
* in the query component.
*
* While this flag is set, the mode can't change due to keyboard
* keys being pressed.
*
* @type {boolean}
* @private
*/
this.pending_ = false;
/**
* A flag than handles the change of mode while a request was
* still pending.
*
* @type {boolean}
* @private
*/
this.wasPending_ = false;
/**
* If a key is pressed, it can temporarily change the currently
* active action. When that happens, the currently active action
* is stored here to be restored later, after the key has been
* released.
*
* @type {?string}
* @private
*/
this.previousAction_ = null;
/**
* The mode currently active.
*
* @type {string}
* @private
*/
this.mode_ = ngeoQueryMode.CLICK;
/**
* If a key is pressed, it can temporarily change the currently
* active mode. When that happens, the currently active mode is
* stored here to be restored later, after the key has been
* released.
*
* @type {?string}
* @private
*/
this.previousMode_ = null;
/**
* The action key currently being pressed. Only those registered
* in `this.keysAction_` can be active.
*
* @type {?string}
* @private
*/
this.activeActionKey_ = null;
/**
* @type {angular.IScope}
* @private
*/
this.rootScope_ = $rootScope;
// Event listeners
olEventsListen(document, 'keydown', this.handleKeyDown_, this);
olEventsListen(document, 'keyup', this.handleKeyUp_, this);
setInterval(this.checkPageFocus_.bind(this), 200);
}
/**
* @returns {string} The query action currently active
*/
get action() {
return this.action_;
}
/**
* @param {string} action The query action to set as active
*/
set action(action) {
this.action_ = action;
}
/**
* @returns {string} The query mode currently active
*/
get mode() {
return this.mode_;
}
/**
* @param {string} mode The query mode to set as active
*/
set mode(mode) {
this.mode_ = mode;
}
/**
* @returns {boolean} Whether a request is currently pending or not.
*/
get pending() {
return this.pending_;
}
/**
* @param {boolean} pending Whether a request is currently pending or not.
*/
set pending(pending) {
this.pending_ = pending;
// If we're no longer pending, but we were pending while a change
// of mode tried to occur, then do it here.
if (!pending && this.wasPending_ && this.previousMode_) {
this.wasPending_ = false;
this.mode = this.previousMode_;
this.previousMode_ = null;
}
}
// Handlers
/**
* Handle key down events.
* Watch out, some computer or OS (not browser) trigger a pressed key event only once, but
* some others trigger it infinitely every x milliseconds.
*
* @param {Event|import('ol/events/Event').default} evt Event.
* @private
*/
handleKeyDown_(evt) {
if (!(evt instanceof KeyboardEvent)) {
return;
}
// No need to do anything on "keydown" if a request is already pending
if (this.pending) {
return;
}
const key = evt.key;
let updateScope = false;
if (this.keysAction_.includes(key) && !this.previousAction_) {
// An 'action' key was pressed and none were already previously
// pressed. In other words, only the first 'action key' is handled.
this.previousAction_ = this.action;
let newAction;
switch (key) {
case this.keyActionAdd_:
newAction = ngeoQueryAction.ADD;
break;
case this.keyActionRemove_:
newAction = ngeoQueryAction.REMOVE;
break;
default:
break;
}
if (newAction) {
this.action = newAction;
this.activeActionKey_ = key;
updateScope = true;
}
} else if (isEventUsingCtrlKey(evt) && !this.previousMode_) {
// The 'ctrl' (or 'meta' key) on mac was pressed
this.previousMode_ = this.mode;
this.mode = ngeoQueryMode.DRAW_BOX;
updateScope = true;
} else if (!isEventUsingCtrlKey(evt) && !this.keysAction_.includes(key) && this.previousMode_) {
// If the ctrl key was pressed and now another key (but not an action key) is pressed,
// reset to the default mode. (For instance to avoid to keep a previous mode
// after a ctrl+p.)
updateScope = this.reset_();
}
if (updateScope) {
this.rootScope_.$apply();
}
}
/**
* Handle key up events.
* Watch out, some computer or OS (not browser) trigger a pressed key event only once, but
* some others trigger it infinitely every x milliseconds.
*
* @param {Event|import('ol/events/Event').default} evt Event.
* @private
*/
handleKeyUp_(evt) {
if (!(evt instanceof KeyboardEvent)) {
return;
}
let updateScope = false;
// On any 'keyup', if it comes from a 'ctrl' (or 'meta' on mac) release and
// there is a previous mode set, then set it as new active mode.
if (isEventUsingCtrlKey(evt) && this.previousMode_) {
updateScope = this.reset_();
}
// If the active action key was released, then restore the
// previous action.
const key = evt.key;
if (this.activeActionKey_ === key && this.previousAction_) {
this.action = this.previousAction_;
this.previousAction_ = null;
this.activeActionKey_ = null;
updateScope = true;
}
if (updateScope) {
this.rootScope_.$apply();
}
}
/**
* Check the focus on the page determined by the browser.
* If the page is not anymore focus then reset the mode.
*
* @private
*/
checkPageFocus_() {
if (!document.hasFocus() && this.previousMode_) {
if (this.reset_()) {
this.rootScope_.$apply();
}
}
}
/**
* Reset the mode to the default one.
*
* @returns {boolean} true if the scope should be updated.
* @private
*/
reset_() {
if (this.pending) {
this.wasPending_ = true;
return false;
}
this.mode = this.previousMode_;
this.previousMode_ = null;
return true;
}
}
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoQueryModeSelector', []);
myModule.service('ngeoQueryModeSelector', QueryModeSelector);
export default myModule;