| 1 |
(function() { |
| 2 |
// holds a function queue to call once page is loaded |
| 3 |
function Main() { |
| 4 |
var init_functions = []; |
| 5 |
|
| 6 |
// this function iterates all map creation functions |
| 7 |
this.init = function() { |
| 8 |
for (var i = 0, len = init_functions.length; i < len; i++) { |
| 9 |
var fnc = init_functions[i]; |
| 10 |
fnc(); |
| 11 |
} |
| 12 |
}; |
| 13 |
|
| 14 |
// this is the function we use to create all map objects |
| 15 |
this.add = function(fnc) { |
| 16 |
init_functions.push(fnc); |
| 17 |
}; |
| 18 |
|
| 19 |
this.getCurrentMap = function() { |
| 20 |
// maps are created iteratively, so the last map is the current map |
| 21 |
return this.maps[this.maps.length - 1]; |
| 22 |
}; |
| 23 |
|
| 24 |
this.getCurrentMarkerGroup = function() { |
| 25 |
// marker groups are mapid -> feature group |
| 26 |
var mapid = this.maps.length; |
| 27 |
if (!this.markergroups[mapid]) { |
| 28 |
this.markergroups[mapid] = this.newMarkerGroup(this.maps[mapid - 1]); |
| 29 |
} |
| 30 |
return this.markergroups[mapid]; |
| 31 |
}; |
| 32 |
|
| 33 |
this.newMarkerGroup = function(map) { |
| 34 |
var mg = new L.FeatureGroup().addTo(map); |
| 35 |
|
| 36 |
mg.timeout = null; |
| 37 |
|
| 38 |
// custom attribute |
| 39 |
if (map.fit_markers) { |
| 40 |
mg.on('layeradd', function(d) { |
| 41 |
// needs a timeout so that it doesn't |
| 42 |
// opt out of a bound change |
| 43 |
window.clearTimeout(this.timeout); |
| 44 |
this.timeout = window.setTimeout(function() { |
| 45 |
map.fitBounds(mg.getBounds()); |
| 46 |
}, 100); |
| 47 |
}, mg); |
| 48 |
} |
| 49 |
|
| 50 |
return mg; |
| 51 |
}; |
| 52 |
|
| 53 |
// these accessible properties hold map objects |
| 54 |
this.maps = []; |
| 55 |
this.images = []; |
| 56 |
this.markergroups = {}; |
| 57 |
this.markers = []; |
| 58 |
this.lines = []; |
| 59 |
} |
| 60 |
|
| 61 |
Main.prototype.unescape = function(str) { |
| 62 |
var div = document.createElement('div'); |
| 63 |
div.innerHTML = str; |
| 64 |
return div.innerText; |
| 65 |
}; |
| 66 |
|
| 67 |
window.WPLeafletMapPlugin = new Main(); |
| 68 |
|
| 69 |
// initialize the function |
| 70 |
if (window.addEventListener) { |
| 71 |
window.addEventListener('load', window.WPLeafletMapPlugin.init, false); |
| 72 |
} else if (window.attachEvent) { |
| 73 |
window.attachEvent('onload', function() { |
| 74 |
// hopefully this helps any references to `this` |
| 75 |
window.WPLeafletMapPlugin.init(); |
| 76 |
}); |
| 77 |
} |
| 78 |
})(); |
| 79 |
|