-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.superrouter.js
169 lines (136 loc) · 4.79 KB
/
backbone.superrouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var Backbone = require('backbone');
var SuperRouter = {};
var _ = require('underscore');
var fromQuery = require('querystring/decode');
Backbone.history.routeObjects = [];
var pathStripper = /#.*$/;
Backbone.history.navigate = function(fragment, options) {
// We have to override Backbone's existing navigate by copying it :(
if (!Backbone.History.started) return false;
if (!options || options === true) options = {trigger: !!options};
// Normalize the fragment.
fragment = this.getFragment(fragment || '');
// Don't include a trailing slash on the root.
var root = this.root;
if (fragment === '' || fragment.charAt(0) === '?') {
root = root.slice(0, -1) || '/';
}
var url = root + fragment;
// Strip the hash and decode for matching.
fragment = this.decodeFragment(fragment.replace(pathStripper, ''));
if (this.fragment === fragment) return;
this.fragment = fragment;
// If pushState is available, we use it to set the fragment as a real URL.
if (this._usePushState) {
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
// If hash changes haven't been explicitly disabled, update the hash
// fragment to store history.
} else if (this._wantsHashChange) {
this._updateHash(this.location, fragment, options.replace);
if (this.iframe && (fragment !== this.getHash(this.iframe.contentWindow))) {
var iWindow = this.iframe.contentWindow;
// Opening and closing the iframe tricks IE7 and earlier to push a
// history entry on hash-tag change. When replace is true, we don't
// want this.
if (!options.replace) {
iWindow.document.open();
iWindow.document.close();
}
this._updateHash(iWindow.location, fragment, options.replace);
}
// If you've told us that you explicitly don't want fallback hashchange-
// based history, then `navigate` becomes a page refresh.
} else {
return this.location.assign(url);
}
if (options.trigger) return this.loadUrl(fragment, options);
};
Backbone.history.loadUrl = function(fragment, options){
fragment = this.fragment = this.getFragment(fragment);
return _.some(Backbone.history.routeObjects, function(route){
if(route.matches(fragment)){
if(this.currentRoute){
this.currentRoute.unroute();
}
route.run(fragment, options);
this.currentRoute = route;
return true;
}
return false;
}, this);
};
Backbone.history.currentRoute = null;
var Route = SuperRouter.Route = function(){};
var optionalParam = /\((.*?)\)/g;
var namedParam = /(\(\?)?:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
_.extend(Route.prototype, {
route: function(){
// This function is called when the route was matched and
// executed
throw new Exception("Route.route() was not implemented");
},
unroute: function(){
// This function is called when the route is being changed
// away from this one
// Defaults to doing nothing
},
url: null,
regex: null,
matches: function(fragment){
// This function returns true if the route was matched
if(this.url == null || this.regex == null){
console.warn("Route.url was not overriden!");
return false;
};
var matches = this.regex.test(fragment);
if(matches){
this.matchedURL = fragment;
}
return matches;
},
run: function(fragment, options){
// Run executes the route
this.options = options || {};
var params = this.regex.exec(fragment).slice(1);
var query = params.pop();
this.query = {};
if(query != undefined){
// Querystring was passed
this.query = fromQuery(query);
this.options = _.extend(fromQuery(query), this.options);
}
params = _.map(params, function(param, i){
if (i === params.length - 1) return param || null;
return param ? decodeURIComponent(param) : null;
});
this.route.apply(this, params);
},
initialize: function(){
// Initialize. Defaults to creating the regex
if(this.url != null){
var route = this.url.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, function(match, optional) {
return optional ? match : '([^/?]+)';
})
.replace(splatParam, '([^?]*?)');
this.regex = new RegExp('^' + route + '(?:\\?([\\s\\S]*))?$');
}
}
});
Route.extend = Backbone.Model.extend; // Use backbone's extend function
// Create a new Route
Route.create = function(opts){
var route = new this();
_.extend(route, opts);
Backbone.history.routeObjects.push(route);
route.initialize();
return route;
};
// Clear all routes
Route.clear = SuperRouter.clear = function(){
Backbone.history.routeObjects = [];
};
module.exports = SuperRouter;