-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathleaflet.js
executable file
·518 lines (440 loc) · 20.7 KB
/
leaflet.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
CKEDITOR.dialog.add('leaflet', function(editor) {
var autocomplete;
var mapContainer = '';
// Access the current translation file.
var pluginTranslation = editor.lang.leaflet;
// Use the core translation file. Used mainly for the `Alignment` values.
var commonTranslation = editor.lang.common;
// Dialog's function callback for the Leaflet Map Widget.
return {
title: pluginTranslation.dialogTitle,
minWidth: 320,
minHeight: 125,
contents: [{
// Create a Location tab.
id: 'location_tab',
label: pluginTranslation.locationTabLabel,
elements: [
{
id: 'map_geocode',
className: 'geocode',
type: 'text',
label: pluginTranslation.googleSearchFieldLabel,
style: 'margin-top: -7px;',
setup: function(widget) {
this.setValue('');
},
onLoad: function (widget) {
// Get the DOM reference for the Search field.
var input = this.getInputElement().$;
// Set a diffused/default text for better user experience.
// This will override the Google's default placeholder text:
// 'Enter a location'.
jQuery('.geocode input').attr('placeholder', pluginTranslation.googleSearchFieldHint);
var config = editor.config;
// Default value, but eventually will reach its quota if many users
// will just utilize this key instead of creating their own.
var googleApiKey = 'AIzaSyA9ySM6msnGm0qQB1L1cLTMBdKEUKPySmQ';
if (typeof config.leaflet_maps_google_api_key != 'undefined' && config.leaflet_maps_google_api_key != '') {
googleApiKey = config.leaflet_maps_google_api_key;
}
// Execute only once, and not every dialog pop-up.
if (typeof google == 'undefined') {
// Load other needed external library.
// Wait for the script to finish loading before binding
// the autocomplete mechanism to prevent rendering issue.
CKEDITOR.scriptLoader.load('//maps.googleapis.com/maps/api/js?libraries=places&callback=dummy&key=' + googleApiKey, function() {
// Bind the Search field to the Autocomplete widget.
autocomplete = new google.maps.places.Autocomplete(input);
});
} else {
autocomplete = new google.maps.places.Autocomplete(input);
}
// Fix for the Google's type-ahead search displaying behind
// the widgets dialog window.
// Basically, we want to override the z-index of the
// Seach Autocomplete list, in which the styling is being set
// in real-time by Google.
// Make a new DOM element.
var stylesheet = jQuery('<style type="text/css" />');
// Set the inner HTML. Include also the vertical alignment
// adjustment for the MiniMap checkbox.
stylesheet.html('.pac-container { z-index: 100000 !important;} input.minimap { margin-top: 18px !important; }');
// Append to the main document's Head section.
jQuery('head').append(stylesheet);
},
},
{ // Dummy element serving as label/text container only.
type: 'html',
id: 'map_label',
className: 'label',
style: 'margin-bottom: -10px;',
html: '<p>' + pluginTranslation.manualCoordinatesFieldLabel + '</p>'
},
{
// Create a new horizontal group.
type: 'hbox',
// Set the relative widths of Latitude, Longitude and Zoom fields.
widths: [ '50%', '50%' ],
children: [
{
id: 'map_latitude',
className: 'latitude',
type: 'text',
label: pluginTranslation.manualLatitudeFieldLabel,
setup: function(widget) {
// Set the Lat values if widget has previous value.
if (widget.element.data('lat') !== '') {
// Update the data-lat based on the map lat in iframe.
// Make sure that mapContainer is set.
// Also avoids setting it again since zoom/longitude
// might already computed/set this object.
if (mapContainer === '') {
mapContainer = widget.element.getChild(0).$.contentDocument.getElementById('map_container');
}
var currentLat = mapContainer.getAttribute('data-lat');
this.setValue(currentLat);
}
},
},
{
id: 'map_longitude',
className: 'longitude',
type: 'text',
label: pluginTranslation.manualLongitudeFieldLabel,
setup: function(widget) {
// Set the Lon values if widget has previous value.
if (widget.element.data('lat') !== '') {
// Update the data-lon based on the map lon in iframe.
// Make sure that mapContainer is set.
// Also avoids setting it again since zoom/latitude
// might already computed/set this object.
if (mapContainer === '') {
mapContainer = widget.element.getChild(0).$.contentDocument.getElementById('map_container');
}
var currentLon = mapContainer.getAttribute('data-lon');
this.setValue(currentLon);
}
},
},
]
},
{
id: 'popup_text',
className: 'popup-text',
type: 'text',
label: pluginTranslation.popupTextFieldLabel,
style: 'margin-bottom: 8px;',
setup: function(widget) {
// Set the Lat values if widget has previous value.
if (widget.element.data('popup-text') != '') {
this.setValue(widget.element.data('popup-text'));
}
else {
// Set a diffused/default text for better user experience.
this.getInputElement().setAttribute('placeholder', pluginTranslation.popupTextFieldHint)
}
},
},
]
},
{
// Create an Options tab.
id: 'options_tab',
label: pluginTranslation.optionsTabLabel,
elements: [
{
// Create a new horizontal group.
type: 'hbox',
style: 'margin-top: -7px;',
// Set the relative widths of Latitude, Longitude and Zoom fields.
widths: [ '38%', '38%', '24%' ],
children: [
{
id: 'width',
className: 'map_width',
type: 'text',
label: pluginTranslation.mapWidthFieldLabel,
setup: function(widget) {
// Set a diffused/default text for better user experience.
this.getInputElement().setAttribute('placeholder', '400')
// Set the map width value if widget has a previous value.
if (widget.element.data('width') != '') {
this.setValue(widget.element.data('width'));
}
},
},
{
id: 'height',
className: 'map_height',
type: 'text',
label: pluginTranslation.mapHeightFieldLabel,
setup: function(widget) {
// Set a diffused/default text for better user experience.
this.getInputElement().setAttribute('placeholder', '400');
// Set the map height value if widget has a previous value.
if (widget.element.data('height') != '') {
this.setValue(widget.element.data('height'));
}
},
},
{
// Create a select list for Zoom Levels.
// 'className' attribute is used for targeting this element in jQuery.
id: 'map_zoom',
className: 'zoom',
type: 'select',
label: pluginTranslation.mapZoomLevelFieldLabel,
width: '70px',
items: [['1'], ['2'], ['3'], ['4'],['5'], ['6'], ['7'], ['8'], ['9'], ['10'], ['11'], ['12'], ['13'], ['14'], ['15'], ['16'], ['17'], ['18'], ['19'], ['20']],
// This will execute also every time you edit/double-click the widget.
setup: function(widget) {
// Set this Zoom Level's select list when
// the current location has been initialized and set previously.
if (widget.element.data('zoom') != '') {
// Update the data-zoom based on the map zoom level in iframe.
// Make sure that mapContainer is set.
// Also avoids setting it again since latitude/longitude
// might already computed/set this object.
if (mapContainer === '') {
mapContainer = widget.element.getChild(0).$.contentDocument.getElementById('map_container');
}
var currentZoom = mapContainer.getAttribute('data-zoom');
this.setValue(currentZoom);
}
// Set the Default Zoom Level value.
else {
this.setValue('10');
}
},
}
]
},
{
// Create a new horizontal group.
type: 'hbox',
// Set the relative widths for the tile and overview map fields.
widths: [ '50%', '50%' ],
children: [
{
// Create a select list for map tiles.
// 'className' attribute is used for targeting this element in jQuery.
type: 'select',
id: 'map_tile',
className: 'tile',
label: pluginTranslation.baseMapTileLabel,
items: [['OpenStreetMap.Mapnik'], ['OpenStreetMap.DE'], ['OpenStreetMap.HOT'], ['Esri.DeLorme'], ['Esri.NatGeoWorldMap'], ['Esri.WorldPhysical'], ['Esri.WorldTopoMap'], ['Thunderforest.OpenCycleMap'], ['Thunderforest.Landscape'], ['Stamen.Watercolor']],
// This will execute also every time you edit/double-click the widget.
setup: function(widget) {
var restrictedTiles = ['MapQuestOpen.Aerial', 'MapQuestOpen.OSM'];
var tile = widget.element.data('tile');
// Set the Tile data attribute.
// Must not be the restricted, MapQuest tiles.
if ( tile != '' && restrictedTiles.indexOf(tile) == -1 ) {
this.setValue(tile);
}
else {
// Set the default value.
// Includes the case for existing MapQuest tiles.
this.setValue('OpenStreetMap.Mapnik');
}
},
// This will execute every time you click the Dialog's OK button.
// It will inject a map iframe in the CKEditor page.
commit: function(widget) {
var dialog = this.getDialog();
// Remove the iframe if it has one.
widget.element.setHtml('');
// Retrieve the value in the Search field.
var geocode = dialog.getContentElement('location_tab','map_geocode').getValue();
var latitude, longitude;
if (geocode != '') {
// No need to call the encodeURIComponent().
var geocodingRequest = '//maps.googleapis.com/maps/api/geocode/json?address=' + geocode + '&sensor=false';
// Disable the asynchoronous behavior temporarily so that
// waiting for results will happen before proceeding
// to the next statements.
jQuery.ajaxSetup({
async: false
});
// Geocode the retrieved place name.
jQuery.getJSON(geocodingRequest, function(data) {
if (data['status'] != 'ZERO_RESULTS') {
// Get the Latitude and Longitude object in the
// returned JSON object.
latitude = data.results[0].geometry.location.lat;
longitude = data.results[0].geometry.location.lng;
}
// Handle queries with no results or have some
// malformed parameters.
else {
alert('The Place could not be Geocoded properly. Kindly choose another one.')
}
});
}
// Get the Lat/Lon values from the corresponding fields.
var latInput = dialog.getContentElement('location_tab','map_latitude').getValue();
var lonInput = dialog.getContentElement('location_tab','map_longitude').getValue();
// Get the data-lat and data-lon values.
// It is empty for yet to be created widgets.
var latSaved = widget.element.data('lat');
var lonSaved = widget.element.data('lon');
// Used the inputted values if it's not empty or
// not equal to the previously saved values.
// latSaved and lonSaved are initially empty also
// for widgets that are yet to be created.
// Or if the user edited an existing map, and did not edit
// the lat/lon fields, and the Search field is empty.
if ((latInput != '' && lonInput != '') && ((latInput != latSaved && lonInput != lonSaved) || geocode == '')) {
latitude = latInput;
longitude = lonInput;
}
var width = dialog.getContentElement('options_tab','width').getValue() || '400';
var height = dialog.getContentElement('options_tab','height').getValue() || '400';
var zoom = dialog.getContentElement('options_tab','map_zoom').getValue();
var popUpText = dialog.getContentElement('location_tab','popup_text').getValue();
var tile = dialog.getContentElement('options_tab','map_tile').getValue();
var alignment = dialog.getContentElement('options_tab','map_alignment').getValue();
// Returns 'on' or 'off'.
var minimap = dialog.getContentElement('options_tab','map_mini').getValue()?'on':'off';
// Get a unique timestamp:
var milliseconds = new Date().getTime();
// Set/Update the widget's data attributes.
widget.element.setAttribute('id', 'leaflet_div-' + milliseconds);
widget.element.data('lat', latitude);
widget.element.data('lon', longitude);
widget.element.data('width', width);
widget.element.data('height', height);
widget.element.data('zoom', zoom);
widget.element.data('popup-text', popUpText);
widget.element.data('tile', tile);
widget.element.data('minimap', minimap);
widget.element.data('alignment', alignment);
// Remove the previously set alignment class.
// Only one alignment class is set per map.
widget.element.removeClass('align-left');
widget.element.removeClass('align-right');
widget.element.removeClass('align-center');
// Set the alignment for this map.
widget.element.addClass('align-' + alignment);
// Returns 'on' or 'off'.
var responsive = dialog.getContentElement('options_tab','map_responsive').getValue()?'on':'off';
// Use 'off' if the Responsive checkbox is unchecked.
if (responsive === 'off') {
// Remove the previously set responsive map class,
// if there's any.
widget.element.removeClass('responsive-map');
}
else {
// Add a class for styling.
widget.element.addClass('responsive-map');
}
// Set the 'responsive' data attribute.
widget.element.data('responsive', responsive);
// Build the full path to the map renderer.
mapParserPathFull = mapParserPath + '?lat=' + latitude + '&lon=' + longitude + '&width=' + width + '&height=' + height + '&zoom=' + zoom + '&text=' + popUpText + '&tile=' + tile + '&minimap=' + minimap + '&responsive=' + responsive;
// Create a new CKEditor DOM's iFrame.
var iframe = new CKEDITOR.dom.element('iframe');
// Setup the iframe characteristics.
iframe.setAttributes({
'scrolling': 'no',
'id': 'leaflet_iframe-' + milliseconds,
'class': 'leaflet_iframe',
'width': width + 'px',
'height': height + 'px',
'frameborder': 0,
'allowTransparency': true,
'src': mapParserPathFull,
'data-cke-saved-src': mapParserPathFull
});
// If map is responsive.
if (responsive == 'on') {
// Add a class for styling.
iframe.setAttribute('class', 'leaflet_iframe responsive-map-iframe');
}
// Insert the iframe to the widget's DIV element.
widget.element.append(iframe);
},
},
{
type: 'checkbox',
id: 'map_mini',
className: 'minimap',
label: pluginTranslation.minimapCheckboxLabel,
// This will execute also every time you edit/double-click the widget.
setup: function(widget) {
// Set the MiniMap check button.
if (widget.element.data('minimap') != '' && widget.element.data('minimap') != 'on') {
this.setValue('');
}
else {
// Set the default value.
this.setValue('true');
}
},
}
]
},
{
// Create a new horizontal group.
type: 'hbox',
// Set the relative widths for alignment and responsive options.
widths: [ '20%', '80%' ],
children: [
{
// Create a select list for Map Alignment.
// 'className' attribute is used for targeting this element in jQuery.
id: 'map_alignment',
className: 'alignment',
type: 'select',
label: commonTranslation.align,
items: [[commonTranslation.alignLeft, 'left'], [commonTranslation.alignRight, 'right'], [commonTranslation.alignCenter, 'center']],
style: 'margin-bottom: 4px;',
// This will execute also every time you edit/double-click the widget.
setup: function(widget) {
// Set this map alignment's select list when
// the current map has been initialized and set previously.
if (widget.element.data('alignment') != '') {
// Set the alignment.
this.setValue(widget.element.data('alignment'));
}
// Set the Default alignment value.
else {
this.setValue('left');
}
},
},
{
type: 'checkbox',
id: 'map_responsive',
className: 'responsive',
label: pluginTranslation.responsiveMapCheckboxLabel,
style: 'margin-top: 18px;',
// This will execute also every time you edit/double-click the widget.
setup: function(widget) {
// Set the Responsive check button, when editing widget.
if (widget.element.data('responsive') != '') {
if (widget.element.data('responsive') == 'on') {
this.setValue('true');
}
else {
this.setValue('');
}
}
// Set the default value for new ones.
else {
this.setValue('');
}
},
}
]
}
]
}],
onHide: function(){
// Reset/clear the map iframe/DOM object reference.
// Fixes bug with lon/lat/zoom artifact values when closing dialog by pressing anything except ok.
mapContainer = '';
}
};
});