PluginProbe
Leaflet Map / 2.17.2
Leaflet Map v2.17.2
3.4.7 3.4.6 trunk 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.11.5 2.12.0 2.13.0 2.14.0 2.15.0 2.16.0 2.16.1 2.16.2 2.17.0 2.17.1 2.17.2 2.17.3 2.18.0 2.19.0 2.19.1 All 49 releases
leaflet-map / scripts / construct-leaflet-map.js

construct-leaflet-map.js in Leaflet Map 2.17.2, at scripts/construct-leaflet-map.js

255 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ;(function () {
2 // holds a function queue to call once page is loaded
3 function Main() {
4 var ready = false
5 var callbacks = []
6
7 /**
8 * this function mirrors the array appending function
9 * so that we can at least append functions to a global array
10 * before the maps are ready to be rendered
11 *
12 * All shortcodes should execute the following:
13 * window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [];
14 * window.WPLeafletMapPlugin.push(function () {
15 *
16 * Think of it as a whenReady callback
17 */
18 this.push = function (fnc) {
19 if (ready) {
20 fnc()
21 } else {
22 callbacks.push(fnc)
23 }
24 }
25
26 /**
27 * Same as above, but what if someone wants to execute a function
28 * before other functions?
29 */
30 this.unshift = function (fnc) {
31 if (ready) {
32 fnc()
33 } else {
34 callbacks.unshift(fnc)
35 }
36 }
37
38 /**
39 * execute all callbacks once page/Leaflet is loaded
40 */
41 this.init = function () {
42 ready = true
43 for (var i = 0, len = callbacks.length; i < len; i++) {
44 callbacks[i]()
45 }
46 }
47
48 /**
49 * maps are created iteratively, so the last map is the current map
50 */
51 this.getCurrentMap = function () {
52 return this.maps[this.maps.length - 1]
53 }
54
55 /**
56 * Get/Create L.FeatureGroup for ALL shapes; used for `fitbounds`
57 * @since 2.13.0
58 */
59 this.getCurrentGroup = function () {
60 // marker groups are mapid -> feature group
61 var mapid = this.maps.length
62 if (!this.markergroups[mapid]) {
63 this.markergroups[mapid] = this.newMarkerGroup(this.maps[mapid - 1])
64 }
65 return this.markergroups[mapid]
66 }
67
68 /**
69 * backwards-compatible getCurrentGroup
70 * @deprecated 2.13.0
71 */
72 this.getCurrentMarkerGroup = this.getCurrentGroup
73
74 /**
75 * get FeatureGroup and add to map
76 *
77 * ! This is extracted so that it can be overwritten by plugins
78 */
79 this.getGroup = function (map) {
80 return new L.FeatureGroup().addTo(map)
81 }
82
83 /**
84 * group is created and event is added
85 */
86 this.newMarkerGroup = function (map) {
87 var mg = this.getGroup(map)
88
89 mg.timeout = null
90
91 // custom attribute
92 if (map._shouldFitBounds) {
93 mg.on(
94 'layeradd',
95 function (event) {
96 // needs a timeout so that it doesn't
97 // opt out of a bound change
98 if (event.layer instanceof L.FeatureGroup) {
99 // wait for featuregroup/ajax-geojson to be ready
100 event.layer.on('ready', function () {
101 map.fitBounds(mg.getBounds())
102 })
103 }
104
105 window.clearTimeout(this.timeout)
106 this.timeout = window.setTimeout(function () {
107 try {
108 map.fitBounds(mg.getBounds())
109 } catch (e) {
110 // ajax-geojson might not have valid bounds yet
111 }
112 }, 100)
113 },
114 mg
115 )
116 }
117
118 return mg
119 }
120
121 var unescape = (this.unescape = function (str) {
122 var div = document.createElement('div')
123 div.innerHTML = str
124 return div.innerText
125 })
126
127 var templateRe = /\{ *(.*?) *\}/g
128
129 /**
130 * It interpolates variables in curly brackets (regex above)
131 *
132 * ex: "Property Value: {property_key}"
133 *
134 * @param {string} str
135 * @param {object} data e.g. feature.properties
136 */
137 this.template = function (str, data) {
138 return str.replace(templateRe, function (match, key) {
139 var value = parseKey(data, key)
140 if (value === undefined) {
141 return match
142 }
143 return value
144 })
145 }
146
147 var strToPathRe = /[.‘’'“”"\[\]]+/g
148
149 /**
150 * Converts nested object keys to array
151 *
152 * ex: `this.that['and'].theOther[4]` ->
153 * ['this', 'that', 'and', 'theOther', '4']
154 * @param {string} key
155 */
156 function strToPath(key) {
157 var input = key.split(strToPathRe)
158 var output = []
159
160 // failsafe for all empty strings;
161 // mostly catches brackets at the end of a string
162 for (var i = 0, len = input.length; i < len; i++) {
163 if (input[i] !== '') {
164 output.push(input[i])
165 }
166 }
167
168 return output
169 }
170
171 /**
172 * It uses strToPath to access a possibly nested path value
173 *
174 * @param {object} obj
175 * @param {string} key
176 */
177 function parseKey(obj, key) {
178 var arr = strToPath(unescape(key))
179 var value = obj
180
181 for (var i = 0, len = arr.length; i < len; i++) {
182 value = value[arr[i]]
183 if (!value) {
184 return undefined
185 }
186 }
187
188 return value
189 }
190
191 function waitFor(prop, cb) {
192 if (typeof L !== 'undefined' && typeof L[prop] !== 'undefined') {
193 cb()
194 } else {
195 setTimeout(function () {
196 waitFor(prop, cb)
197 }, 100)
198 }
199 }
200
201 /** wait for leaflet-svg-icon (if deferred) */
202 this.waitForSVG = function (cb) {
203 waitFor('SVGIcon', cb)
204 }
205
206 /** wait for leaflet-ajax-geojson (if deferred) */
207 this.waitForAjax = function (cb) {
208 waitFor('AjaxGeoJSON', cb)
209 }
210
211 // these accessible properties hold map objects
212 this.maps = []
213 this.images = []
214 this.markergroups = {}
215 this.markers = []
216 this.lines = []
217 this.polygons = []
218 this.circles = []
219 this.geojsons = []
220 }
221
222 /**
223 * window.WPLeafletMapPlugin can be used, by saving arguments,
224 * before it is officially initialized
225 *
226 * This is used to deal with the potential for deferred scripts
227 */
228 var original = window.WPLeafletMapPlugin
229 window.WPLeafletMapPlugin = new Main()
230
231 // check for functions to execute
232 if (!!original) {
233 for (var i = 0, len = original.length; i < len; i++) {
234 window.WPLeafletMapPlugin.push(original[i])
235 }
236
237 // empty the array
238 original.splice(0)
239
240 // re-add any methods that may have been added to the original
241 for (var k in original) {
242 if (original.hasOwnProperty(k)) {
243 window.WPLeafletMapPlugin[k] = original[k]
244 }
245 }
246 }
247
248 // onload waits for Leaflet to load
249 if (window.addEventListener) {
250 window.addEventListener('load', window.WPLeafletMapPlugin.init, false)
251 } else if (window.attachEvent) {
252 window.attachEvent('onload', window.WPLeafletMapPlugin.init)
253 }
254 })()
255