|
| 1 | +/* Copyright (c) 2014 Paulo Sérgio Borges de Oliveira Filho |
| 2 | + * |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | + * copy of this software and associated documentation files (the "Software"), |
| 5 | + * to deal in the Software without restriction, including without limitation |
| 6 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 8 | + * Software is furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | + * DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +var FILES = { |
| 23 | + mapbox: { |
| 24 | + js: ['https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'], |
| 25 | + css: ['https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.css'], |
| 26 | + }, |
| 27 | + |
| 28 | + directions: { |
| 29 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/mapbox-directions.js/v0.0.1/mapbox.directions.js'], |
| 30 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/mapbox-directions.js/v0.0.1/mapbox.directions.css'] |
| 31 | + }, |
| 32 | + |
| 33 | + zoomslider: { |
| 34 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-zoomslider/v0.7.0/L.Control.Zoomslider.js'], |
| 35 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-zoomslider/v0.7.0/L.Control.Zoomslider.css'] |
| 36 | + }, |
| 37 | + |
| 38 | + pip: { |
| 39 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-pip/v0.0.2/leaflet-pip.js'], |
| 40 | + css: [] |
| 41 | + }, |
| 42 | + |
| 43 | + osm: { |
| 44 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-osm/v0.1.0/leaflet-osm.js'], |
| 45 | + css: [] |
| 46 | + }, |
| 47 | + |
| 48 | + omnivore: { |
| 49 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.2.0/leaflet-omnivore.min.js'], |
| 50 | + css: [] |
| 51 | + }, |
| 52 | + |
| 53 | + minimap: { |
| 54 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-minimap/v1.0.0/Control.MiniMap.js'], |
| 55 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-minimap/v1.0.0/Control.MiniMap.css'] |
| 56 | + }, |
| 57 | + |
| 58 | + markercluster: { |
| 59 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/leaflet.markercluster.js'], |
| 60 | + css: [ |
| 61 | + 'https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.css', |
| 62 | + 'https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.Default.css' |
| 63 | + ], |
| 64 | + }, |
| 65 | + |
| 66 | + // FIXME: Doesn't support IE<9 |
| 67 | + // https://www.mapbox.com/mapbox.js/example/v1.0.0/leaflet-locatecontrol/ |
| 68 | + locate: { |
| 69 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-locatecontrol/v0.24.0/L.Control.Locate.js'], |
| 70 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-locatecontrol/v0.24.0/L.Control.Locate.css'] |
| 71 | + }, |
| 72 | + |
| 73 | + label: { |
| 74 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-label/v0.2.1/leaflet.label.js'], |
| 75 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-label/v0.2.1/leaflet.label.css'] |
| 76 | + }, |
| 77 | + |
| 78 | + image: { |
| 79 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.4/leaflet-image.js'], |
| 80 | + css: [] |
| 81 | + }, |
| 82 | + |
| 83 | + heat: { |
| 84 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-heat/v0.1.0/leaflet-heat.js'], |
| 85 | + css: [] |
| 86 | + }, |
| 87 | + |
| 88 | + hash: { |
| 89 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-hash/v0.2.1/leaflet-hash.js'], |
| 90 | + css: [] |
| 91 | + }, |
| 92 | + |
| 93 | + geodesy: { |
| 94 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-geodesy/v0.1.0/leaflet-geodesy.js'], |
| 95 | + css: [] |
| 96 | + }, |
| 97 | + |
| 98 | + fullscreen: { |
| 99 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v0.0.3/Leaflet.fullscreen.min.js'], |
| 100 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v0.0.3/leaflet.fullscreen.css'] |
| 101 | + }, |
| 102 | + |
| 103 | + draw: { |
| 104 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js'], |
| 105 | + css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.css'] |
| 106 | + }, |
| 107 | + |
| 108 | + geojsonExtend: { |
| 109 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/geojson-extent/v0.0.1/geojson-extent.js'], |
| 110 | + css: [] |
| 111 | + }, |
| 112 | + |
| 113 | + geoViewport: { |
| 114 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/geo-viewport/v0.1.1/geo-viewport.js'], |
| 115 | + css: [] |
| 116 | + }, |
| 117 | + |
| 118 | + arc: { |
| 119 | + js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/arc.js/v0.1.0/arc.js'], |
| 120 | + css: [] |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +var deps = new Deps.Dependency; |
| 125 | +var loaded = false; |
| 126 | + |
| 127 | +var loadCount = 0; |
| 128 | +var loadCallback = null; |
| 129 | + |
| 130 | +var pluginCount = 0; |
| 131 | +var plugins = null; |
| 132 | + |
| 133 | +var onLoaded = function () { |
| 134 | + loaded = true; |
| 135 | + deps.changed(); |
| 136 | +}; |
| 137 | + |
| 138 | +var onPluginLoaded = function () { |
| 139 | + pluginCount--; |
| 140 | + |
| 141 | + if (pluginCount == 0) |
| 142 | + onLoaded(); |
| 143 | +}; |
| 144 | + |
| 145 | +var onMapboxLoaded = function () { |
| 146 | + pluginCount = _.size(plugins); |
| 147 | + |
| 148 | + if (pluginCount == 0) { |
| 149 | + onLoaded(); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + _.each(plugins, function (pluginName) { |
| 154 | + var plugin = FILES[pluginName]; |
| 155 | + |
| 156 | + if (plugin) |
| 157 | + loadFiles(plugin, onPluginLoaded); |
| 158 | + else |
| 159 | + console.log('Invalid MapBox plugin: "' + pluginName + '"'); |
| 160 | + }); |
| 161 | +}; |
| 162 | + |
| 163 | +var onLoadedFile = function (url) { |
| 164 | + if (Mapbox.debug) |
| 165 | + console.log('Loaded "' + url + '"'); |
| 166 | + |
| 167 | + loadCount--; |
| 168 | + |
| 169 | + if (loadCount == 0) |
| 170 | + loadCallback(); |
| 171 | +}; |
| 172 | + |
| 173 | +var loadScript = function (src) { |
| 174 | + var elem = document.createElement('script'); |
| 175 | + elem.type = 'text/javascript'; |
| 176 | + elem.src = src; |
| 177 | + elem.defer = true; |
| 178 | + |
| 179 | + elem.addEventListener('load', _.partial(onLoadedFile, src), false); |
| 180 | + |
| 181 | + var head = document.getElementsByTagName('head')[0]; |
| 182 | + head.appendChild(elem); |
| 183 | +}; |
| 184 | + |
| 185 | +var loadCss = function (href) { |
| 186 | + var elem = document.createElement('link'); |
| 187 | + elem.rel = 'stylesheet'; |
| 188 | + elem.href = href; |
| 189 | + |
| 190 | + elem.addEventListener('load', _.partial(onLoadedFile, href), false); |
| 191 | + |
| 192 | + var head = document.getElementsByTagName('head')[0]; |
| 193 | + head.appendChild(elem); |
| 194 | +}; |
| 195 | + |
| 196 | +var loadFiles = function (files, cb) { |
| 197 | + loadCount = _.size(files.js) + _.size(files.css); |
| 198 | + loadCallback = cb; |
| 199 | + |
| 200 | + _.each(files.js, function (url) { |
| 201 | + loadScript(url); |
| 202 | + }); |
| 203 | + |
| 204 | + _.each(files.css, function (url) { |
| 205 | + loadCss(url); |
| 206 | + }); |
| 207 | +}; |
| 208 | + |
| 209 | +Mapbox = { |
| 210 | + debug: false, |
| 211 | + |
| 212 | + load: function () { |
| 213 | + plugins = _.values(arguments); |
| 214 | + loadFiles(FILES.mapbox, onMapboxLoaded); |
| 215 | + }, |
| 216 | + |
| 217 | + loaded: function () { |
| 218 | + deps.depend(); |
| 219 | + return loaded; |
| 220 | + } |
| 221 | +}; |
0 commit comments