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