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 | 1
30
30
12
12
12
12
12
11
11
2
2
10
10
10
10
10
30
10
10
10
10
10
10
10
10
10
1
1
26
26
3
3
26
1
1
1
2
2
2
2
1
20
| L.Toolbar = (L.Layer || L.Class).extend({
statics: {
baseClass: 'leaflet-toolbar'
},
includes: L.Mixin.Events,
options: {
className: '',
filter: function() { return true; },
actions: []
},
initialize: function(options) {
L.setOptions(this, options);
this._toolbar_type = this.constructor._toolbar_class_id;
},
addTo: function(map) {
this._arguments = [].slice.call(arguments);
map.addLayer(this);
return this;
},
onAdd: function(map) {
var currentToolbar = map._toolbars[this._toolbar_type];
if (this._calculateDepth() === 0) {
Iif (currentToolbar) { map.removeLayer(currentToolbar); }
map._toolbars[this._toolbar_type] = this;
}
},
onRemove: function(map) {
/*
* TODO: Cleanup event listeners.
* For some reason, this throws:
* "Uncaught TypeError: Cannot read property 'dragging' of null"
* on this._marker when a toolbar icon is clicked.
*/
// for (var i = 0, l = this._disabledEvents.length; i < l; i++) {
// L.DomEvent.off(this._ul, this._disabledEvents[i], L.DomEvent.stopPropagation);
// }
Eif (this._calculateDepth() === 0) {
delete map._toolbars[this._toolbar_type];
}
},
appendToContainer: function(container) {
var baseClass = this.constructor.baseClass + '-' + this._calculateDepth(),
className = baseClass + ' ' + this.options.className,
Action, action,
i, j, l, m;
this._container = container;
this._ul = L.DomUtil.create('ul', className, container);
/* Ensure that clicks, drags, etc. don't bubble up to the map. */
this._disabledEvents = ['click', 'mousemove', 'dblclick'];
for (j = 0, m = this._disabledEvents.length; j < m; j++) {
L.DomEvent.on(this._ul, this._disabledEvents[j], L.DomEvent.stopPropagation);
}
/* Instantiate each toolbar action and add its corresponding toolbar icon. */
for (i = 0, l = this.options.actions.length; i < l; i++) {
Action = this._getActionConstructor(this.options.actions[i]);
action = new Action();
console.log("new Action()", action);
action._createIcon(this, this._ul, this._arguments);
}
},
_getActionConstructor: function(Action) {
var args = this._arguments,
toolbar = this;
console.log("Action", Action.prototype);
return Action.extend({
initialize: function() {
Action.prototype.initialize.apply(this, args);
},
enable: function(e) {
/* Ensure that only one action in a toolbar will be active at a time. */
if (toolbar._active) { toolbar._active.disable(); }
toolbar._active = this;
Action.prototype.enable.call(this, e);
}
});
},
/* Used to hide subToolbars without removing them from the map. */
_hide: function() {
this._ul.style.display = 'none';
},
/* Used to show subToolbars without removing them from the map. */
_show: function() {
this._ul.style.display = 'block';
},
_calculateDepth: function() {
var depth = 0,
toolbar = this.parentToolbar;
while (toolbar) {
depth += 1;
toolbar = toolbar.parentToolbar;
}
return depth;
}
});
L.toolbar = {};
var toolbar_class_id = 0;
L.Toolbar.extend = function extend(props) {
var statics = L.extend({}, props.statics, {
"_toolbar_class_id": toolbar_class_id
});
toolbar_class_id += 1;
L.extend(props, { statics: statics });
return L.Class.extend.call(this, props);
};
L.Map.addInitHook(function() {
this._toolbars = {};
});
|