-
Notifications
You must be signed in to change notification settings - Fork 5
/
backbone-spotify.js
280 lines (232 loc) · 7.99 KB
/
backbone-spotify.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// backbone-spotify 1.1.0
// vim: ts=2:sw=2:sts=2
(function(undefined) {
var BackboneSpotify = window.BackboneSpotify = {};
var Router = BackboneSpotify.Router = Backbone.Router.extend({
namedParam: /<\w+>/g,
splatParam: /\*\w+/g,
escapeRegExp: /[-[\]{}()+?.,\\^$|#\s]/g,
_routeToRegExp: function(route) {
route = route.replace(this.escapeRegExp, '\\$&')
.replace(this.namedParam, '([^:]+)')
.replace(this.splatParam, '(.*?)');
return new RegExp('^' + route + '$');
},
});
var History = BackboneSpotify.History = function(options) {
_.bindAll(this, 'checkUrl', 'onRoute');
options = options || {};
this.location = options.location || window.location;
this.application = options.application;
if (!this.application) {
throw new Error('options.application is required');
}
if (!this.application.uri || !this.application.arguments) {
throw new Error('options.application must have uri and arguments ' +
'properties (you might need to call options.' +
'application.load([\'arguments\', \'uri\']))');
}
if (options.debug) {
this.debug = _.bind(console.debug, console);
}
else {
this.debug = function() {};
}
this.handlers = [];
this.stack = [];
this.stackPosition = -1;
};
History.extend = Backbone.Model.extend;
_.extend(History.prototype, Backbone.Events, {
started: false,
MAX_STACK_SIZE: 25,
start: function(options) {
if (this.started) throw new Error("Backbone.history has already been started");
this.started = true;
this.options = options || {};
this.fragment = this.getFragment();
this.application.addEventListener(
'arguments',
this.checkUrl
);
this.on('route', this.onRoute)
if (!this.options.silent) {
return this.loadUrl();
}
},
stop: function() {
this.application.removeEventListener(
'arguments',
this.checkUrl
);
this.off('route', this.onRoute);
this.started = false;
},
route: Backbone.History.prototype.route,
getFragment: function(fragment) {
if (fragment) return fragment;
return this.application.arguments.join(':');
},
// Checks the current URL to see if it has changed, and if it has,
// calls `loadUrl`
checkUrl: function(e) {
var current = this.getFragment();
if (current == this.fragment) return false;
this.loadUrl();
},
loadUrl: function(fragmentOverride) {
this.fragment = this.getFragment(fragmentOverride);
this.debug('Loading', this.fragment);
var current = this.current();
// Forward button?
var next = this.next();
if (next && next.fragment == this.fragment) {
// Clear out a stale view
if (next.stale) {
// We clear at this point because it will get recreated
// immediately by loadUrl
this.clearIndex(this.stackPosition + 1);
return Backbone.History.prototype.loadUrl.apply(this, arguments);
}
this.debug('Forward button pressed, restoring', this.fragment);
this.trigger('route');
if (current.router.freeze) current.router.freeze(current.view);
if (current.view.freeze) current.view.freeze();
if (next.router.restore) next.router.restore(next.view);
if (next.view.restore) next.view.restore();
this.stackPosition += 1;
return true;
}
// Back button?
var previous = this.previous();
if (previous && previous.fragment == this.fragment) {
// Clear out a stale view
if (previous.stale) {
// We clear at this point because it will get recreated
// immediately by loadUrl
this.clearIndex(this.stackPosition - 1);
return Backbone.History.prototype.loadUrl.apply(this, arguments);
}
this.debug('Back button pressed, restoring', this.fragment);
this.trigger('route');
if (current.router.freeze) current.router.freeze(current.view);
if (current.view.freeze) current.view.freeze();
if (previous.router.restore) previous.router.restore(previous.view);
if (previous.view.restore) previous.view.restore();
this.stackPosition -= 1;
return true;
}
// This triggers onRoute, which will then add loaded view to the stack
return Backbone.History.prototype.loadUrl.apply(this, arguments);
},
onRoute: function(router, name, args) {
// Event has been triggered from going back/forward in stack, so ignore
if (!router) return;
// No view was rendered on last URL
if (!router.view) return;
// View raised an error, don't store in history
if (router.view.hasError && router.view.hasError()) return;
this.debug('Loaded', this.fragment);
this.clearNext();
var current = this.current();
if (current && current.view.freeze) {
if (current.router.freeze) current.router.freeze(current.view);
if (current.view.freeze) current.view.freeze();
}
this.stack.push({
fragment: this.fragment,
router: router,
view: router.view,
stale: false,
});
this.stackPosition += 1;
},
navigate: function(fragment) {
var l;
if (fragment) {
l = this.application.uri + ':' + fragment;
}
else {
l = this.application.uri;
}
this.location.assign(l);
},
current: function() {
return this.stack[this.stackPosition];
},
previous: function() {
return this.stack[this.stackPosition - 1];
},
next: function() {
return this.stack[this.stackPosition + 1];
},
// Close any views after stackPosition and prune any old views
clearNext: function() {
var closed = this.stack.slice(this.stackPosition + 1);
_.each(closed, function(item) {
item.view.close();
});
this.stack = this.stack.slice(0, this.stackPosition + 1);
if (this.stack.length > this.MAX_STACK_SIZE) {
var newStart = this.stack.length - this.MAX_STACK_SIZE;
_.each(this.stack.slice(0, newStart), function(item) {
item.view.close();
});
this.stack = this.stack.slice(newStart);
this.stackPosition = this.MAX_STACK_SIZE - 1;
}
},
// Clears history so all views are forced to reload
clear: function() {
this.debug('Clearing history')
_.each(this.stack, function(item) {
item.view.close();
});
this.stack = [];
this.stackPosition = -1;
},
// Clear a specific URL from the history
clearUrl: function(fragment) {
var history = this;
_.each(this.stack, function(item, i) {
if (item.fragment === fragment) {
history.clearIndex(i);
}
});
},
// Clear a specific index in the history
clearIndex: function(i) {
if (i == this.stackPosition) {
return;
}
if (i < this.stackPosition) {
this.stackPosition -= 1;
}
var item = this.stack.splice(i, 1)[0];
item.view.close();
},
// Mark a view to reload if we encounter it in the history
// We do this instead of clearing so stack position is maintained
//
// e.g. suppose this navigation:
// people -> people:john (stale) -> people:jane
// if we cleared people:john instead of marking it as stale, hitting back
// from people:jane wouldn't recognise the people:john view spotify send
// us, giving us this stack:
// people -> people:jane -> people:john
// and hitting back again would give
// people -> people:jane -> people:john -> people
// so we'd never get back to the original people view
//
markStale: function(view) {
_.each(this.stack, function(item) {
if (item.view === view) {
item.stale = true;
}
});
},
});
BackboneSpotify.init = function(options) {
Backbone.history = new History(options);
};
})();