This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
services.js
276 lines (214 loc) · 6.96 KB
/
services.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
var services = angular.module('wReader.services', []);
function Item() {
this.read = false;
this.starred = false;
this.selected = false;
this.item_id = null;
this.title = null;
this.pub_name = null;
this.pub_author = null;
this.pub_date = new Date(0);
this.short_desc = null;
this.content = null;
this.feed_link = null;
this.item_link = null;
}
// Create or open the data store where objects are stored for offline use
services.value('store', new Lawnchair({
name: 'entries',
record: 'entry',
adapter: 'indexed-db'
}, function() {
//TODO: this should probably go in the item store
this.toggleRead = function(key, value) {
this.get(key, function(entry) {
entry.read = value;
this.save(entry);
});
};
//TODO: this should probably go in the item store
this.toggleStar = function(key, value) {
this.get(key, function(entry) {
entry.starred = value;
this.save(entry);
});
};
}));
services.factory('items', ['$http', 'store', 'filterFilter', function($http, store, filter) {
var items = {
all: [],
filtered: [],
selected: null,
selectedIdx: null,
addItem: function(item) {
var exists = items.all.filter(function(val, i) {
return val.item_id == item.item_id;
}).length;
if (exists === 0) {
// If no results are returned, we insert the new item into the data
// controller in order of publication date
items.all.push(item);
return true;
} else {
// It's already in the data controller, so we won't re-add it.
return false;
}
},
getItemsFromDataStore: function() {
// Get all items from the local data store.
// We're using store.all because store.each returns async, and the
// method will return before we've pulled all the items out. Then
// there is a strong likelihood of getItemsFromServer stomping on
// local items.
store.all(function(arr) {
arr.forEach(function(entry) {
var item = new Item();
angular.extend(item, entry);
items.addItem(item);
});
console.log("Entries loaded from local data store:", arr.length);
//$scope.allItems = $scope.items;
// Load items from the server after we've loaded everything from the local
// data store.
items.getItemsFromServer();
//$scope.clearFilter(); // Show all items by default.
});
},
getItemsFromServer: function() {
var feedURL = 'http://blog.chromium.org/feeds/posts/default?alt=json';
var getLink = function(links, rel) {
for (var i = 0, link; link = links[i]; ++i) {
if (link.rel === rel) {
return link.href;
}
}
return null;
};
var successCallback = function(data, status, headers, config) {
items.all = [];
var entries = data.feed.entry;
// Use map to iterate through the items and create a new JSON object for
// each item
entries.forEach(function(entry) {
var item = {};
item.title = entry.title.$t;
item.item_id = entry.id.$t;
item.key = item.item_id; // For LawnChair.
item.pub_name = data.feed.title.$t; // Set the pub name to the feed's title.
item.pub_author = entry.author[0].name.$t;
item.pub_date = new Date(entry.published.$t);
// Set the link to the entry to it's original source if it exists
// or set it to the entry link
item.item_link = getLink(entry.link, 'alternate');
item.feed_link = getLink(data.feed.link, 'alternate');
item.content = entry.content.$t;
item.short_desc = item.content.substr(0, 128) + '...';
var tempItem = new Item();
angular.extend(tempItem, item);
// Try to add the item to the data controller, if it's successfully
// added, we get TRUE and add the item to the local data store,
// otherwise it's likely already in the local data store.
if (items.addItem(tempItem)) {
store.save(item);
}
});
items.filtered = items.all;
console.log('Entries loaded from server:', items.all.length);
};
$http.jsonp(feedURL + '&callback=JSON_CALLBACK').success(successCallback);
//$http.get(feedURL).success(successCallback).error(errorCallback);
},
prev: function() {
if (items.hasPrev()) {
items.selectItem(items.selected ? items.selectedIdx - 1 : 0);
}
},
next: function() {
if (items.hasNext()) {
items.selectItem(items.selected ? items.selectedIdx + 1 : 0);
}
},
hasPrev: function() {
if (!items.selected) {
return true;
}
return items.selectedIdx > 0;
},
hasNext: function() {
if (!items.selected) {
return true;
}
return items.selectedIdx < items.filtered.length - 1;
},
selectItem: function(opt_idx) {
// Unselect previous selection.
if (items.selected) {
items.selected.selected = false;
}
if (opt_idx != undefined) {
items.selected = items.filtered[opt_idx];
items.selectedIdx = opt_idx;
items.selected.selected = true;
} else {
// this.item.selected = true;
// selectedItem = this.item;
}
items.toggleRead(true);
//TODO: Update the address bar
//$location.hash(selectedItem.item_id)
//var url = location.origin + location.pathname + '';
//var item_url = "" + item.get('item_id');
//history.pushState(item.get('item_id'), 'title', url + item_url);
},
toggleRead: function(opt_read) {
var read = opt_read || !items.selected.read;
var item = items.selected;
item.read = read;
store.toggleRead(item.item_id, read);
},
toggleStar: function(opt_star) {
var star = opt_star || !items.selected.starred;
var item = items.selected;
item.starred = star;
store.toggleStar(item.item_id, star);
},
markAllRead: function() {
items.filtered.forEach(function(item) {
item.read = true;
store.toggleRead(item.item_id, true);
});
},
filterBy: function(key, value) {
items.filtered = filter(items.all, function(item) {
return item[key] === value;
});
},
clearFilter: function() {
items.filtered = items.all;
}
};
items.getItemsFromDataStore();
return items;
}]);
services.value('scroll', {
pageDown: function() {
var itemHeight = $('.entry.active').height() + 60;
var winHeight = $(window).height();
var curScroll = $('.entries').scrollTop();
var scroll = curScroll + winHeight;
if (scroll < itemHeight) {
$('.entries').scrollTop(scroll);
return true;
}
// already at the bottom
return false;
},
toCurrent: function() {
// Need the setTimeout to prevent race condition with item being selected.
window.setTimeout(function() {
var curScrollPos = $('.summaries').scrollTop();
var itemTop = $('.summary.active').offset().top - 60;
$('.summaries').animate({'scrollTop': curScrollPos + itemTop}, 200);
}, 0);
}
});