-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathobject_to_mode.js
71 lines (62 loc) · 1.71 KB
/
object_to_mode.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
import ModeInterface from './mode_interface';
const eventMapper = {
drag: 'onDrag',
click: 'onClick',
mousemove: 'onMouseMove',
mousedown: 'onMouseDown',
mouseup: 'onMouseUp',
mouseout: 'onMouseOut',
keyup: 'onKeyUp',
keydown: 'onKeyDown',
touchstart: 'onTouchStart',
touchmove: 'onTouchMove',
touchend: 'onTouchEnd',
tap: 'onTap'
};
const eventKeys = Object.keys(eventMapper);
export default function(modeObject) {
const modeObjectKeys = Object.keys(modeObject);
return function(ctx, startOpts = {}) {
let state = {};
const mode = modeObjectKeys.reduce((m, k) => {
m[k] = modeObject[k];
return m;
}, new ModeInterface(ctx));
function wrapper(eh) {
return e => mode[eh](state, e);
}
return {
start() {
state = mode.onSetup(startOpts); // this should set ui buttons
// Adds event handlers for all event options
// add sets the selector to false for all
// handlers that are not present in the mode
// to reduce on render calls for functions that
// have no logic
eventKeys.forEach((key) => {
const modeHandler = eventMapper[key];
let selector = () => false;
if (modeObject[modeHandler]) {
selector = () => true;
}
this.on(key, selector, wrapper(modeHandler));
});
},
stop() {
mode.onStop(state);
},
trash() {
mode.onTrash(state);
},
combineFeatures() {
mode.onCombineFeatures(state);
},
uncombineFeatures() {
mode.onUncombineFeatures(state);
},
render(geojson, push) {
mode.toDisplayFeatures(state, geojson, push);
}
};
};
}