Skip to content

Instantly share code, notes, and snippets.

@dcsan
Created April 17, 2014 19:11
Show Gist options
  • Save dcsan/11005456 to your computer and use it in GitHub Desktop.
Save dcsan/11005456 to your computer and use it in GitHub Desktop.
define("famous/scene/Router", ["require", "exports", "module", "./Controller", "./Route"], function (t, e, i) {
function o() {
c.apply(this, arguments), this.connected = [], this._routes = [], this._redirects = [], this.handleStateChange = this._handleStateChange.bind(this), this.bindListener()
}
function s(t) {
this.updateConnected(t.getEvent())
}
function n(t) {
this.pushState(t.eventKey)
}
function r(t, e, i) {
var o = e[t.length()];
if (!o) return !1;
for (var s = 0; s < o.length; s++) {
var n = o[s].isMatch(t);
if (n) return i && i(o[s]), !0
}
return !1
}
function a(t, e) {
for (var i in t) {
var o = new p(i, t[i]),
s = o.length();
e[s] || (e[s] = []), e[s].push(o)
}
}
function u(t, e) {
for (item in t)
for (var i = 0; i < e.length; i++)
for (var o = 0; o < e[i].length; o++) {
var s = e[i][o];
s.route == t[item] && e[i].splice(o, 1)
}
}
var c = t("./Controller"),
p = t("./Route");
o.protoype = Object.create(c.prototype), o.prototype.constructor = o, o.prototype.bindListener = function () {
History.Adapter.bind(window, "statechange", this.handleStateChange)
}, o.prototype.pushState = function (t) {
return History.pushState(null, null, t)
}, o.prototype._handleStateChange = function () {
return this.state = this.getState(), this._eventOutput.emit("statechange", this.state), this.updateRouteListeners()
}, o.prototype.updateRouteListeners = function () {
var t = this.getParsedHash(),
e = r.call(this, t, this._redirects, n.bind(this));
return e ? e : r.call(this, t, this._routes, s.bind(this))
}, o.prototype.isValidRoute = function () {
this.state = this.getState();
var t = this.getParsedHash();
return r.call(this, t, this._routes)
}, o.prototype.getHash = function () {
return this.state.hash
}, o.prototype.getParsedHash = function () {
return new p(this.getHash())
}, o.prototype.updateConnected = function (t) {
for (var e = 0; e < this.connected.length; e++) this.connected[e].trigger("set", t)
}, o.prototype.getState = function () {
return History.getState()
}, o.prototype.registerLink = function (t, e) {
t.on("click", function (t, e) {
e.preventDefault();
for (var i = $(e.srcElement), o = i.attr("href"), s = i.attr("title") || null, n = i; null === o;) n = n.parent(), o = n.attr("href"), s = n.attr("title") || null;
null !== o && (t && t(s, o), History.pushState(null, s, o))
}.bind({}, e))
}, o.prototype.addRoutes = function (t) {
a.call(this, t, this._routes)
}, o.prototype.removeRoutes = function (t) {
u.call(this, t, this._routes)
}, o.prototype.addRedirects = function (t) {
a.call(this, t, this._redirects)
}, o.prototype.removeRedirects = function (t) {
u.call(this, t, this._redirects)
}, o.prototype.connect = function (t) {
this.connected.push(t)
}, i.exports = o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment