diff --git a/lib/tiles/offlineEnabler.js b/lib/tiles/offlineEnabler.js index afd1aca..7fd66d7 100644 --- a/lib/tiles/offlineEnabler.js +++ b/lib/tiles/offlineEnabler.js @@ -302,7 +302,8 @@ define([ handleAs: "text/plain; charset=x-user-defined", headers: { "X-Requested-With": "" //bypasses a dojo xhr bug - } + }, + timeout: 2000 }).then(function(response){ var img = Base64Utils.wordToBase64(Base64Utils.stringToWord(response)); callback(img.length + url.length); diff --git a/samples/tiles/tiles-indexed-db.html b/samples/tiles/tiles-indexed-db.html index a4f8eed..8060f61 100644 --- a/samples/tiles/tiles-indexed-db.html +++ b/samples/tiles/tiles-indexed-db.html @@ -151,7 +151,8 @@ var dojoConfig = { paths: { tiles: locationPath + "/../../lib/tiles", - vendor: locationPath + "/../../vendor" + vendor: locationPath + "/../../vendor", + utils: locationPath + "/../../utils" } } window.proxyPath = "../../lib/proxy.php"; diff --git a/samples/tiles/tiles-indexed-db.js b/samples/tiles/tiles-indexed-db.js index 9d121d6..f915b56 100644 --- a/samples/tiles/tiles-indexed-db.js +++ b/samples/tiles/tiles-indexed-db.js @@ -12,7 +12,7 @@ require(["esri/map", "dojo/dom", "dojo/on", "dojo/query", "vendor/bootstrap-map-js/src/js/bootstrapmap", "esri/urlUtils", "esri/geometry/webMercatorUtils", - "tiles/offlineEnabler", + "tiles/offlineEnabler","utils/debouncer", "dojo/dom-construct", "dojo/domReady!"], function(Map, GraphicsLayer, Graphic, SimpleFillSymbol, @@ -20,7 +20,7 @@ require(["esri/map", dom, on, query, BootstrapMap, urlUtils, webMercatorUtils, - offlineEnabler, + offlineEnabler,debouncer, domConstruct) { var scalebar; @@ -106,7 +106,9 @@ require(["esri/map", function initEvents() { - map.on('extent-change', updateTileCountEstimation ); + map.on('extent-change',debouncer.debounceMap(function(){ + updateTileCountEstimation(); + }) ); on(dojo.byId('minLevel'),'change', updateTileCountEstimation); on(dojo.byId('maxLevel'),'change', updateTileCountEstimation); @@ -117,6 +119,7 @@ require(["esri/map", function initOffline() { console.log("extending"); + offlineEnabler.extend(basemapLayer,function(success) { if(success) @@ -189,7 +192,8 @@ require(["esri/map", //NOTE: Number of tiles per zoom level will not change unless the map div changes size var levelEstimation; - try{basemapLayer.getLevelEstimation(map.extent,minLevel,function(result){ + + basemapLayer.getLevelEstimation(map.extent,minLevel,function(result){ levelEstimation = result; for(var level=minLevel; level<=maxLevel; level++) @@ -217,7 +221,7 @@ require(["esri/map", rowContent = "" + rowContent.join("") + ""; tr = domConstruct.place("", dojo.byId('tile-count-table-body'),'last') domConstruct.place(rowContent, tr,'last'); - }).bind(this);}catch(err){console.log("ERROR " + err.toString)} + }); } diff --git a/utils/debouncer.js b/utils/debouncer.js new file mode 100644 index 0000000..c66ded9 --- /dev/null +++ b/utils/debouncer.js @@ -0,0 +1,39 @@ +define([],function(){ + return { + + /** + * Activates the orientation listener and listens for native events. + * Handle orientation events to allow for resizing the map and working around + * jQuery mobile bugs related to how and when the view settles after such an event + */ + setOrientationListener: function(callback){ + var supportsOrientationChange = "onorientationchange" in window, + orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; + + window.addEventListener(orientationEvent, this.debounceMap(function(){ + callback(); + },this.DEBOUNCE_DELAY).bind(this), false); + }, + + /** + * Minimize the number of times window readjustment fires a function + * http://davidwalsh.name/javascript-debounce-function + * @param func + * @param wait + * @param immediate + * @returns {Function} + */ + debounceMap: function (func, wait, immediate) { + var timeout; + return function() { + var context = this, args = arguments; + clearTimeout(timeout); + timeout = setTimeout(function() { + timeout = null; + if (!immediate) func.apply(context, args); + }, wait); + if (immediate && !timeout) func.apply(context, args); + }; + } + } +}) \ No newline at end of file