1 |
|
2 | const ModeHandler = function(mode, DrawContext) {
|
3 |
|
4 | const handlers = {
|
5 | drag: [],
|
6 | click: [],
|
7 | mousemove: [],
|
8 | mousedown: [],
|
9 | mouseup: [],
|
10 | mouseout: [],
|
11 | keydown: [],
|
12 | keyup: []
|
13 | };
|
14 |
|
15 | const ctx = {
|
16 | on: function(event, selector, fn) {
|
17 | if (handlers[event] === undefined) {
|
18 | throw new Error(`Invalid event type: ${event}`);
|
19 | }
|
20 | handlers[event].push({
|
21 | selector: selector,
|
22 | fn: fn
|
23 | });
|
24 | },
|
25 | render: function(id) {
|
26 | DrawContext.store.featureChanged(id);
|
27 | }
|
28 | };
|
29 |
|
30 | const delegate = function (eventName, event) {
|
31 | const handles = handlers[eventName];
|
32 | let iHandle = handles.length;
|
33 | while (iHandle--) {
|
34 | const handle = handles[iHandle];
|
35 | if (handle.selector(event)) {
|
36 | handle.fn.call(ctx, event);
|
37 | DrawContext.store.render();
|
38 | DrawContext.ui.updateMapClasses();
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | break;
|
44 | }
|
45 | }
|
46 | };
|
47 |
|
48 | mode.start.call(ctx);
|
49 |
|
50 | return {
|
51 | render: mode.render,
|
52 | stop: function() {
|
53 | if (mode.stop) mode.stop();
|
54 | },
|
55 | trash: function() {
|
56 | if (mode.trash) {
|
57 | mode.trash();
|
58 | DrawContext.store.render();
|
59 | }
|
60 | },
|
61 | combineFeatures: function() {
|
62 | if (mode.combineFeatures) {
|
63 | mode.combineFeatures();
|
64 | }
|
65 | },
|
66 | uncombineFeatures: function() {
|
67 | if (mode.uncombineFeatures) {
|
68 | mode.uncombineFeatures();
|
69 | }
|
70 | },
|
71 | drag: function(event) {
|
72 | delegate('drag', event);
|
73 | },
|
74 | click: function(event) {
|
75 | delegate('click', event);
|
76 | },
|
77 | mousemove: function(event) {
|
78 | delegate('mousemove', event);
|
79 | },
|
80 | mousedown: function(event) {
|
81 | delegate('mousedown', event);
|
82 | },
|
83 | mouseup: function(event) {
|
84 | delegate('mouseup', event);
|
85 | },
|
86 | mouseout: function(event) {
|
87 | delegate('mouseout', event);
|
88 | },
|
89 | keydown: function(event) {
|
90 | delegate('keydown', event);
|
91 | },
|
92 | keyup: function(event) {
|
93 | delegate('keyup', event);
|
94 | }
|
95 | };
|
96 | };
|
97 |
|
98 | module.exports = ModeHandler;
|