-
Notifications
You must be signed in to change notification settings - Fork 3
/
favorites.js
62 lines (50 loc) · 1.48 KB
/
favorites.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
var FavoritesCollection = Backbone.Model.extend({
initialize: function(options) {
_.bindAll(this, 'listChanged');
if (options.favorites) this._favorites = options.favorites;
else this._favorites = [];
},
sessions: function() {
return Database.sessions.filter().by(function(session) {
return session.isFavorite();
});
},
addFavorite: function(session) {
this._favorites.push(session.uniqueId());
this.listChanged();
},
removeFavorite: function(session) {
this._favorites = _.without(this._favorites, session.uniqueId());
this.listChanged();
},
containsSession: function(session) {
return _.indexOf(this._favorites, session.uniqueId()) !== -1;
},
listChanged: function() {
this.save();
this.trigger('change');
},
save: function() {
var favoritesString = JSON.stringify(this._favorites);
window.localStorage['favorites'] = favoritesString;
var transferUrl = [
location.protocol + "//",
location.host,
location.pathname,
"#load-" + escape(favoritesString)
];
$("a#saveLink").attr("href", transferUrl.join(''));
},
load: function() {
var data = window.localStorage['favorites'];
if (data) {
var parsed = JSON.parse(data);
if (parsed) this._favorites = parsed;
}
},
parse: function(hashId) {
var escaped = hashId.replace(/^load-/, '');
window.localStorage['favorites'] = unescape(escaped);
},
});
var Favorites = new FavoritesCollection();