97 lines
2.1 KiB
JavaScript
97 lines
2.1 KiB
JavaScript
/*jslint devel:true, browser:true, indent:2, maxlen: 70 */
|
|
(function (window) {
|
|
"use strict";
|
|
var Condition, Listener, watcher;
|
|
|
|
// Requires window.matchMedia:
|
|
// https://developer.mozilla.org/en/DOM/window.matchMedia
|
|
if (typeof window.matchMedia === "undefined") { return; }
|
|
|
|
Condition = function (mq, callback) {
|
|
this.mq = window.matchMedia(mq);
|
|
this.callback = function () {
|
|
callback.call();
|
|
};
|
|
};
|
|
Condition.prototype.matches = function () {
|
|
return this.mq.matches;
|
|
};
|
|
|
|
Listener = function () {
|
|
this.callbacks = [];
|
|
this.cbLen = 0;
|
|
this.runned = 0;
|
|
};
|
|
Listener.prototype.waitFor = function (condition) {
|
|
this.callbacks.push(condition);
|
|
this.cbLen += 1;
|
|
};
|
|
Listener.prototype.walk = function () {
|
|
var toRun = [],
|
|
i = this.cbLen,
|
|
cb = this.callbacks;
|
|
|
|
while (i) {
|
|
i -= 1;
|
|
if (cb[i]) {
|
|
if (cb[i].matches()) {
|
|
toRun.push(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
return toRun;
|
|
};
|
|
Listener.prototype.execute = function (toRun) {
|
|
var i = toRun.length;
|
|
|
|
while (i) {
|
|
i -= 1;
|
|
this.callbacks[toRun[i]].callback.call();
|
|
this.callbacks[toRun[i]] = '';
|
|
this.runned += 1;
|
|
}
|
|
};
|
|
Listener.prototype.investigate = function () {
|
|
var toRun;
|
|
toRun = this.walk();
|
|
|
|
if (toRun.length) { this.execute(toRun); }
|
|
|
|
if (this.runned === this.cbLen) {
|
|
window.onresize = "";
|
|
}
|
|
};
|
|
Listener.prototype.install = function () {
|
|
var callback, wait, that = this;
|
|
|
|
callback = function () {
|
|
that.investigate();
|
|
};
|
|
|
|
window.onresize = function () {
|
|
clearTimeout(wait);
|
|
wait = setTimeout(callback, 333);
|
|
};
|
|
|
|
this.investigate();
|
|
};
|
|
|
|
watcher = new Listener();
|
|
|
|
watcher.waitFor(new Condition(
|
|
"all and (min-width: 801px)",
|
|
function () {
|
|
console.log("load delicicous bookmarks (we have room!)");
|
|
}
|
|
));
|
|
|
|
watcher.waitFor(new Condition(
|
|
"all and (min-width: 1281px)",
|
|
function () {
|
|
console.log("load heavy pictures (we are on desktop!)");
|
|
}
|
|
));
|
|
|
|
watcher.install();
|
|
}(window));
|