| 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 |
* execute all callbacks once page/Leaflet is loaded |
| 29 |
*/ |
| 30 |
this.init = function() { |
| 31 |
ready = true; |
| 32 |
for (var i = 0, len = callbacks.length; i < len; i++) { |
| 33 |
callbacks[i](); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* maps are created iteratively, so the last map is the current map |
| 39 |
*/ |
| 40 |
this.getCurrentMap = function () { |
| 41 |
return this.maps[this.maps.length - 1]; |
| 42 |
}; |
| 43 |
|
| 44 |
this.getCurrentMarkerGroup = function () { |
| 45 |
// marker groups are mapid -> feature group |
| 46 |
var mapid = this.maps.length; |
| 47 |
if (!this.markergroups[mapid]) { |
| 48 |
this.markergroups[mapid] = this.newMarkerGroup(this.maps[mapid - 1]); |
| 49 |
} |
| 50 |
return this.markergroups[mapid]; |
| 51 |
}; |
| 52 |
|
| 53 |
this.getGroup = function (map) { |
| 54 |
return new L.FeatureGroup().addTo(map); |
| 55 |
}; |
| 56 |
|
| 57 |
this.newMarkerGroup = function (map) { |
| 58 |
var mg = this.getGroup(map); |
| 59 |
|
| 60 |
mg.timeout = null; |
| 61 |
|
| 62 |
// custom attribute |
| 63 |
if (map.fit_markers) { |
| 64 |
mg.on('layeradd', function (d) { |
| 65 |
// needs a timeout so that it doesn't |
| 66 |
// opt out of a bound change |
| 67 |
window.clearTimeout(this.timeout); |
| 68 |
this.timeout = window.setTimeout(function() { |
| 69 |
map.fitBounds(mg.getBounds()); |
| 70 |
}, 100); |
| 71 |
}, mg); |
| 72 |
} |
| 73 |
|
| 74 |
return mg; |
| 75 |
}; |
| 76 |
|
| 77 |
this.unescape = function (str) { |
| 78 |
var div = document.createElement('div'); |
| 79 |
div.innerHTML = str; |
| 80 |
return div.innerText; |
| 81 |
}; |
| 82 |
|
| 83 |
// these accessible properties hold map objects |
| 84 |
this.maps = []; |
| 85 |
this.images = []; |
| 86 |
this.markergroups = {}; |
| 87 |
this.markers = []; |
| 88 |
this.lines = []; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* window.WPLeafletMapPlugin can be used, by saving arguments, |
| 93 |
* before it is officially initialized |
| 94 |
* |
| 95 |
* This is used to deal with the potential for deferred scripts |
| 96 |
*/ |
| 97 |
var original = window.WPLeafletMapPlugin; |
| 98 |
window.WPLeafletMapPlugin = new Main(); |
| 99 |
|
| 100 |
// check for functions to execute |
| 101 |
if (!!original) { |
| 102 |
for (var i = 0, len = original.length; i < len; i++) { |
| 103 |
window.WPLeafletMapPlugin.push(original[i]); |
| 104 |
} |
| 105 |
|
| 106 |
// empty the array |
| 107 |
original.splice(0); |
| 108 |
|
| 109 |
// re-add any methods that may have been added to the original |
| 110 |
for (var k in original) { |
| 111 |
if (original.hasOwnProperty(k)) { |
| 112 |
window.WPLeafletMapPlugin[k] = original[k]; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// onload waits for Leaflet to load |
| 118 |
if (window.addEventListener) { |
| 119 |
window.addEventListener('load', window.WPLeafletMapPlugin.init, false); |
| 120 |
} else if (window.attachEvent) { |
| 121 |
window.attachEvent('onload', window.WPLeafletMapPlugin.init); |
| 122 |
} |
| 123 |
})(); |
| 124 |
|