deeplink.js | |
---|---|
deeplink - a small deeplink utilityhttp://draeton.github.com/deeplink Copyright 2013, Matthew Cobbs MIT licensed | /*global jQuery*/
var Deeplink = (function (window, $) {
"use strict";
var location = window.location;
var monitorDelay = 250;
|
hashchange monitoring for older browsers |
var monitorHash = function () {
var oldURL;
var newURL;
var timeout;
var monitor = function () {
newURL = window.location.toString();
if (newURL !== oldURL) {
$(window).trigger("hashchange");
oldURL = newURL;
}
timeout = setTimeout(monitor, monitorDelay);
};
monitor();
};
|
Start monitoring in browsers without window.onhashchange |
if (!("onhashchange" in window)) {
monitorHash();
}
|
public interface |
var methods = {
|
Should hashchange events fire on message bus |
triggering: true,
|
initBind hashchange event and set up monitoring for older browsers |
init: function (/* optional */ eq, sep) {
this.eq = eq || "=";
this.sep = sep || "&";
$(window).bind("hashchange", $.proxy(this.hashchange, this));
$(window).trigger("hashchange");
},
|
parseParse a string into a map of key-value pairs |
parse: function (hash) {
var list, map, i, l, pair;
list = hash.replace(/^\#/, "").split(this.sep);
map = {};
for (i = 0, l = list.length; i < l; i++) {
pair = list[i].split(this.eq);
map[pair[0]] = pair[1];
}
return map;
},
|
stringifyParse a map of key-value pairs into a string |
stringify: function (map) {
var hash, i;
hash = "";
for (i in map) {
if (map.hasOwnProperty(i)) {
if (typeof map[i] === "undefined") {
hash += this.sep + i;
} else {
hash += this.sep + i + this.eq + map[i];
}
}
}
return hash.replace(new RegExp("^\\" + this.sep), "");
},
|
hashchangeThe event handler. Parses the current hash into a map and trigger a deeplink event on the Deeplink instance |
hashchange: function () {
var hash, map;
hash = location.hash;
map = this.parse(hash);
if (this.triggering) {
$(this).trigger("deeplink", {
"hash": hash,
"map": map
});
}
this.triggering = true;
},
|
hashupdateUsed to update the current hash with values from a map object |
hashupdate: function (map, replace, fire) {
var hash, current;
if (!replace) {
hash = location.hash;
current = this.parse(hash);
map = $.extend(current, map);
}
this.triggering = fire;
location.hash = this.stringify(map);
}
};
|
constructor and prototype |
var Deeplink = function (/* optional */ eq, sep) {
this.init.apply(this, arguments);
};
Deeplink.prototype = methods;
|
Deeplink.setDelaySet the monitorDelay for monitorHash |
Deeplink.setDelay = function (ms) {
monitorDelay = ms;
};
|
return a reference to the constructor |
return Deeplink;
}(this, jQuery));
|