-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-list-markers.js
253 lines (225 loc) · 7.61 KB
/
leaflet-list-markers.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
(function () {
L.Control.ListMarkers = L.Control.extend(
{
includes: L.version[0] === '1' ? L.Evented.prototype : L.Mixin.Events,
options: {
layer: false,
maxItems: 20,
collapsed: false,
label: 'title',
itemIcon: L.Icon.Default.imagePath + '/marker-icon.png',
itemArrow: '➤', //visit: https://character-code.com/arrows-html-codes.php
maxZoom: 9,
position: 'bottomleft',
update: true,
maxheight: 0.7,
maxwidth: 0.5
//TODO autocollapse
},
initialize: function (options) {
L.Util.setOptions( this, options );
this._container = null;
this._list = null;
this._layer = this.options.layer || new L.LayerGroup();
this._greatest = 0;
},
onAdd: function (map) {
map.options._collapse = this.options.collapsed;
map.options._greatest = 0;
mapupdate = true;
map.on(
"update-end",
function (e) {
// console.log("update-end", map.options._collapse);
if ( ! map.options._collapse ) {
let greatest_before = map.options._greatest;
// console.log("greatest_before",greatest_before);
const createdlist = document.getElementsByClassName( 'list-markers-li' );
let length = createdlist.length;
for (let i = 0; i < length; i++) {
let rectwidth = createdlist[i].childNodes[0].childNodes[0].offsetWidth;
if ( rectwidth > map.options._greatest) {
map.options._greatest = rectwidth;
}
}
for (let i = 0; i < length; i++) {
createdlist[i].childNodes[0].style.width = map.options._greatest + 30 + "px";
}
// console.log("greatest_new",map.options._greatest);
if ( mapupdate ) {
mapupdate = false;
let center = map.getCenter();
// console.log( center );
let countlat = center.lat.toString().split( "." );
let countlng = center.lng.toString().split( "." );
let newlat = (( center.lat + 0.001 ) * 1000) / 1000;
let newlng = (( center.lng + 0.001 ) * 1000) / 1000;
// console.log( newlat, newlng );
map.setView( L.latLng( newlat, newlng ) );
map.once(
"update-end",
function (e) {
// console.log( "once update-end", mapupdate );
if (mapupdate) {
map.setView( center );
}
}
);
}
} else {
mapupdate = false;
}
}
);
this._map = map;
var container = this._container = L.DomUtil.create( 'div', 'list-markers-x-y list-markers' );
this._list = L.DomUtil.create( 'ul', 'list-markers-ul', container );
this._initToggle();
map.on( 'moveend', this._updateList, this );
var s = map.getSize();
var style = document.createElement( 'style' );
style.type = 'text/css';
style.innerHTML = '.list-markers-x-y { max-height: ' + (s.y) * this.options.maxheight + 'px; max-width: ' + (s.x * this.options.maxwidth) + 'px;}';
document.getElementsByTagName( 'head' )[0].appendChild( style );
this._updateList();
return container;
},
onRemove: function (map) {
map.off( 'moveend', this._updateList, this );
this._container = null;
this._list = null;
},
_createItem: function (layer) {
var li = L.DomUtil.create( 'li', 'list-markers-li' ),
a = L.DomUtil.create( 'a', '', li ),
icon = this.options.itemIcon ? '<img src="' + this.options.itemIcon + '" />' : '',
that = this;
a.href = '#';
L.DomEvent
.disableClickPropagation( a )
.on( a, 'click', L.DomEvent.stop, this )
.on(
a,
'click',
function (e) {
mapupdate = false;
//this._moveTo( layer.getLatLng() );
that.fire( 'item-click', {layer: layer } );
},
this
)
.on(
a,
'mouseover',
function (e) {
that.fire( 'item-mouseover', {layer: layer } );
},
this
)
.on(
a,
'mouseout',
function (e) {
that.fire( 'item-mouseout', {layer: layer } );
},
this
);
//console.log('_createItem',layer.options);
if ( layer.options.hasOwnProperty( this.options.label ) ) {
//a.innerHTML = icon + '<span>' + layer.options[this.options.label] + '</span> <b>' + this.options.itemArrow + '</b>';
a.innerHTML = '<span>' + layer.options[this.options.label] + '</span> <b>' + this.options.itemArrow + '</b>';
let thislength = layer.options[this.options.label].length;
// console.log(thislength);
if ( thislength > this._greatest ) {
this._greatest = thislength;
// console.log("li",this._greatest);
itemstyle = document.createElement( 'style' );
itemstyle.type = 'text/css';
thislength = 8.5 * thislength;
itemstyle.innerHTML = '.list-markers-li a { width: ' + thislength + 'px; }';
// console.log(itemstyle);
document.getElementsByTagName( 'head' )[0].appendChild( itemstyle );
}
//TODO use related marker icon!
//TODO use template for item
} else {
console.log( "propertyName '" + this.options.label + "' not found in marker" );
}
return li;
},
_updateList: function () {
// console.log("_updateList");
var that = this,
n = 0;
this._list.innerHTML = '';
this._layer.eachLayer(
function (layer) {
if (layer instanceof L.Marker) {
if ( that.options.update ) {
if ( that._map.getBounds().contains( layer.getLatLng() ) ) {
if (++n < that.options.maxItems) {
that._list.appendChild( that._createItem( layer ) );
}
}
} else {
if (++n < that.options.maxItems) {
that._list.appendChild( that._createItem( layer ) );
}
}
}
}
);
this._map.fire( 'update-end' );
},
_initToggle: function () {
/* inspired by L.Control.Layers */
var container = this._container;
//Makes this work on IE10 Touch devices by stopping it from firing a mouseout event when the touch is released
container.setAttribute( 'aria-haspopup', true );
if (this.options.collapsed) {
this._collapse();
L.DomEvent
.on( container, 'click', this._expand, this )
.on( container, 'click', this._collapse, this );
var link = this._button = L.DomUtil.create( 'a', 'leaflet-control-layers-toggle', container );
link.href = '#';
link.title = 'List Markers';
L.DomEvent
.on( link, 'click', L.DomEvent.stop )
.on( link, 'click', this._expand, this );
this._map.on( 'click', this._collapse, this );
// TODO keyboard accessibility
}
},
_expand: function () {
this._container.className = this._container.className.replace( ' list-markers-collapsed', '' );
L.DomUtil.addClass( this._container, 'list-markers-x-y' );
this._map.options._collapse = false;
this._map.fire( 'update-end' );
},
_collapse: function () {
L.DomUtil.addClass( this._container, 'list-markers-collapsed' );
this._container.className = this._container.className.replace( 'list-markers-x-y ', '' );
this._map.options._collapse = true;
},
_moveTo: function (latlng) {
if (this.options.maxZoom) {
this._map.setView( latlng, Math.min( this._map.getZoom(), this.options.maxZoom ) );
} else {
this._map.panTo( latlng );
}
}
}
);
L.control.listMarkers = function (options) {
return new L.Control.ListMarkers( options );
};
L.Map.addInitHook(
function () {
if (this.options.listMarkersControl) {
this.listMarkersControl = L.control.listMarkers( this.options.listMarkersControl );
this.addControl( this.listMarkersControl );
}
}
);
}).call( this );