Skip to content

Instantly share code, notes, and snippets.

@dmitrig01
Created August 8, 2011 20:54
Show Gist options
  • Save dmitrig01/1132716 to your computer and use it in GitHub Desktop.
Save dmitrig01/1132716 to your computer and use it in GitHub Desktop.
diff --git a/control/g/bwdetect.js b/control/g/bwdetect.js
index 37c1f8c..62c6759 100644
--- a/control/g/bwdetect.js
+++ b/control/g/bwdetect.js
@@ -40,18 +40,36 @@ wax.g.bwdetect = function(map, options) {
}
function bwTest() {
+ wax.bw = -1;
var im = new Image();
im.src = testImage;
+ var first = true;
var timeout = setTimeout(function() {
- detector.bw(0);
+ if (first) {
+ detector.bw(0);
+ first = false;
+ }
}, threshold);
im.onload = function() {
- clearTimeout(timeout);
+ if (first) {
+ clearTimeout(timeout);
+ detector.bw(1);
+ first = false;
+ }
};
}
detector.bw = function(x) {
if (!arguments.length) return bw;
+ if (wax.bwlisteners && wax.bwlisteners.length) (function () {
+ wax.bw = x;
+ listeners = wax.bwlisteners;
+ wax.bwlisteners = [];
+ for (i = 0; i < listeners; i++) {
+ listeners[i](x);
+ }
+ })();
+
if (bw != (bw = x)) map.setMapTypeId(bw ? 'mb' : 'mb-low');
};
@@ -60,5 +78,14 @@ wax.g.bwdetect = function(map, options) {
return this;
};
- return detector.add(map);
+ if (wax.bw == -1) {
+ wax.bwlisteners = wax.bwlisteners || [];
+ wax.bwlisteners.push(detector.bw);
+ }
+ else if (wax.bw != undefined) {
+ detector.bw(wax.bw);
+ }
+ else {
+ return detector.add(map);
+ }
};
diff --git a/control/mm/bwdetect.js b/control/mm/bwdetect.js
index c536c70..a5d59b7 100644
--- a/control/mm/bwdetect.js
+++ b/control/mm/bwdetect.js
@@ -34,18 +34,36 @@ wax.mm.bwdetect = function(map, options) {
}
function bwTest() {
+ wax.bw = -1;
var im = new Image();
im.src = testImage;
+ var first = true;
var timeout = setTimeout(function() {
- detector.bw(0);
+ if (first) {
+ detector.bw(0);
+ first = false;
+ }
}, threshold);
mm.addEvent(im, 'load', function() {
- clearTimeout(timeout);
+ if (first) {
+ clearTimeout(timeout);
+ detector.bw(1);
+ first = false;
+ }
});
}
detector.bw = function(x) {
if (!arguments.length) return bw;
+ if (wax.bwlisteners && wax.bwlisteners.length) (function () {
+ wax.bw = x;
+ listeners = wax.bwlisteners;
+ wax.bwlisteners = [];
+ for (i = 0; i < listeners; i++) {
+ listeners[i](x);
+ }
+ })();
+
if (bw != (bw = x)) setProvider(map.provider);
};
@@ -55,5 +73,14 @@ wax.mm.bwdetect = function(map, options) {
return this;
};
- return detector.add(map);
+ if (wax.bw == -1) {
+ wax.bwlisteners = wax.bwlisteners || [];
+ wax.bwlisteners.push(detector.bw);
+ }
+ else if (wax.bw != undefined) {
+ detector.bw(wax.bw);
+ }
+ else {
+ return detector.add(map);
+ }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment