UNPKG

ngx.leaflet.offline

Version:

A Leaflet library that downloads map tiles and uses them offline.

1 lines 4.52 kB
"use strict";!function(e,t){if("function"==typeof define&&define.amd)define(["leaflet"],e);else if("object"==typeof exports&&module.exports)module.exports=e(require("leaflet"));else if(void 0!==t){if(void 0===t.L)throw"Leaflet must be loaded first!";e(t.L)}}(function(e){e.TileLayer.Offline=e.TileLayer.extend({initialize:function(t,i,o){this._url=t,this._tilesDb=i,(o=e.Util.setOptions(this,o)).detectRetina&&e.Browser.retina&&o.maxZoom>0&&(o.tileSize=Math.floor(o.tileSize/2),o.zoomReverse?(o.zoomOffset--,o.minZoom++):(o.zoomOffset++,o.maxZoom--),o.minZoom=Math.max(0,o.minZoom)),"string"==typeof o.subdomains&&(o.subdomains=o.subdomains.split("")),e.Browser.android||this.on("tileunload",this._onTileRemove)},createTile:function(t,i){var o=document.createElement("img");return e.DomEvent.on(o,"load",e.bind(this._tileOnLoad,this,i,o)),e.DomEvent.on(o,"error",e.bind(this._tileOnError,this,i,o)),this.options.crossOrigin&&(o.crossOrigin=""),o.alt="",o.setAttribute("role","presentation"),this.getTileUrl(t).then(function(e){o.src=e}).catch(function(e){throw e}),o},getTileUrl:function(t){var i=e.TileLayer.prototype.getTileUrl.call(this,t),o=this._getStorageKey(i);return this._tilesDb.getItem(o).then(function(e){return e&&"object"==typeof e?URL.createObjectURL(e):i}).catch(function(e){throw e})},getTileUrls:function(t,i){var o=[],n=this._url;this.setUrl(this._url.replace("{z}",i),!0);for(var r=e.bounds(t.min.divideBy(this.getTileSize().x).floor(),t.max.divideBy(this.getTileSize().x).floor()),l=r.min.x;l<=r.max.x;l++)for(var s=r.min.y;s<=r.max.y;s++){var a=new e.Point(l,s),f=e.TileLayer.prototype.getTileUrl.call(this,a);o.push({key:this._getStorageKey(f),url:f})}return this.setUrl(n,!0),o},_getStorageKey:function(e){var t=null;if(e.indexOf("{s}")){var i=new RegExp("["+this.options.subdomains.join("|")+"].");t=e.replace(i,this.options.subdomains[0]+".")}return t||e}}),e.tileLayer.offline=function(t,i,o){return new e.TileLayer.Offline(t,i,o)}},window),function(e,t){if("function"==typeof define&&define.amd)define(["leaflet"],e);else if("object"==typeof exports&&module.exports)module.exports=e(require("leaflet"));else if(void 0!==t){if(void 0===t.L)throw"Leaflet must be loaded first!";e(t.L)}}(function(e){e.Control.Offline=e.Control.extend({options:{position:"topleft",saveButtonHtml:"S",saveButtonTitle:"Save tiles",removeButtonHtml:"R",removeButtonTitle:"Remove tiles",minZoom:0,maxZoom:19,confirmSavingCallback:null,confirmRemovalCallback:null},initialize:function(t,i,o){this._baseLayer=t,this._tilesDb=i,e.Util.setOptions(this,o)},onAdd:function(t){var i=e.DomUtil.create("div","leaflet-control-offline leaflet-bar");return this._createButton(this.options.saveButtonHtml,this.options.saveButtonTitle,"save-tiles-button",i,this._saveTiles),this._createButton(this.options.removeButtonHtml,this.options.removeButtonTitle,"remove-tiles-button",i,this._removeTiles),i},_createButton:function(t,i,o,n,r){var l=e.DomUtil.create("a",o,n);return l.innerHTML=t,l.href="#",l.title=i,e.DomEvent.disableClickPropagation(l),e.DomEvent.on(l,"click",e.DomEvent.stop),e.DomEvent.on(l,"click",r,this),e.DomEvent.on(l,"click",this._refocusOnMap,this),l},_saveTiles:function(){var t=this,i=null,o=[],n=[],r=this._map.getZoom(),l=this._map.getBounds();if(r<this.options.minZoom)t._baseLayer.fire("offline:below-min-zoom-error");else{for(var s=r;s<=this.options.maxZoom;s++)o.push(s);for(var a=0;a<o.length;a++)i=e.bounds(this._map.project(l.getNorthWest(),o[a]),this._map.project(l.getSouthEast(),o[a])),n=n.concat(this._baseLayer.getTileUrls(i,o[a]));var f=function(){t._baseLayer.fire("offline:save-start",{nTilesToSave:n.length}),t._tilesDb.saveTiles(n).then(function(){t._baseLayer.fire("offline:save-end")}).catch(function(e){t._baseLayer.fire("offline:save-error",{error:e})})};this.options.confirmSavingCallback?this.options.confirmSavingCallback(n.length,f):f()}},_removeTiles:function(){var e=this,t=function(){e._baseLayer.fire("offline:remove-start"),e._tilesDb.clear().then(function(){e._baseLayer.fire("offline:remove-end")}).catch(function(t){e._baseLayer.fire("offline:remove-error",{error:t})})};e.options.confirmRemovalCallback?e.options.confirmRemovalCallback(t):t()}}),e.control.offline=function(t,i,o){return new e.Control.Offline(t,i,o)}},window),function(e){"function"==typeof define&&define.amd?define(["./TileLayer.Offline","./Control.Offline"],e):"object"==typeof exports&&module.exports&&(module.exports=e(require("./TileLayer.Offline"),require("./Control.Offline")))}(function(e,t){});