| 1 |
(function() { |
| 2 |
// holds a function queue to call once page is loaded |
| 3 |
function Main() { |
| 4 |
/** |
| 5 |
* this function mirrors the array appending function |
| 6 |
* so that we can at least append functions to a global array |
| 7 |
* before the maps are ready to be rendered |
| 8 |
* |
| 9 |
* All shortcodes should execute the following: |
| 10 |
* window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || []; |
| 11 |
* window.WPLeafletMapPlugin.push(function () { |
| 12 |
* |
| 13 |
* Think of it as a whenReady callback |
| 14 |
*/ |
| 15 |
this.push = function(fnc) { |
| 16 |
fnc(); |
| 17 |
}; |
| 18 |
|
| 19 |
/** |
| 20 |
* maps are created iteratively, so the last map is the current map |
| 21 |
*/ |
| 22 |
this.getCurrentMap = function () { |
| 23 |
return this.maps[this.maps.length - 1]; |
| 24 |
}; |
| 25 |
|
| 26 |
this.getCurrentMarkerGroup = function () { |
| 27 |
// marker groups are mapid -> feature group |
| 28 |
var mapid = this.maps.length; |
| 29 |
if (!this.markergroups[mapid]) { |
| 30 |
this.markergroups[mapid] = this.newMarkerGroup(this.maps[mapid - 1]); |
| 31 |
} |
| 32 |
return this.markergroups[mapid]; |
| 33 |
}; |
| 34 |
|
| 35 |
this.getGroup = function (map) { |
| 36 |
return new L.FeatureGroup().addTo(map); |
| 37 |
}; |
| 38 |
|
| 39 |
this.newMarkerGroup = function (map) { |
| 40 |
var mg = this.getGroup(map); |
| 41 |
|
| 42 |
mg.timeout = null; |
| 43 |
|
| 44 |
// custom attribute |
| 45 |
if (map.fit_markers) { |
| 46 |
mg.on('layeradd', function (d) { |
| 47 |
// needs a timeout so that it doesn't |
| 48 |
// opt out of a bound change |
| 49 |
window.clearTimeout(this.timeout); |
| 50 |
this.timeout = window.setTimeout(function() { |
| 51 |
map.fitBounds(mg.getBounds()); |
| 52 |
}, 100); |
| 53 |
}, mg); |
| 54 |
} |
| 55 |
|
| 56 |
return mg; |
| 57 |
}; |
| 58 |
|
| 59 |
this.unescape = function (str) { |
| 60 |
var div = document.createElement('div'); |
| 61 |
div.innerHTML = str; |
| 62 |
return div.innerText; |
| 63 |
}; |
| 64 |
|
| 65 |
// these accessible properties hold map objects |
| 66 |
this.maps = []; |
| 67 |
this.images = []; |
| 68 |
this.markergroups = {}; |
| 69 |
this.markers = []; |
| 70 |
this.lines = []; |
| 71 |
} |
| 72 |
|
| 73 |
function init () { |
| 74 |
/** |
| 75 |
* window.WPLeafletMapPlugin can be used, by saving arguments, |
| 76 |
* before it is officially initialized |
| 77 |
*/ |
| 78 |
var original = window.WPLeafletMapPlugin; |
| 79 |
window.WPLeafletMapPlugin = new Main(); |
| 80 |
|
| 81 |
if (!!original) { |
| 82 |
for (var i = 0, len = original.length; i < len; i++) { |
| 83 |
window.WPLeafletMapPlugin.push(original[i]); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// initialize the function |
| 89 |
if (window.addEventListener) { |
| 90 |
window.addEventListener('load', init, false); |
| 91 |
} else if (window.attachEvent) { |
| 92 |
window.attachEvent('onload', init); |
| 93 |
} |
| 94 |
})(); |
| 95 |
|