]> git.wh0rd.org - tt-rss.git/blame - feedlist.js
improve headline caching, update cache after toggleMark/togglePub()
[tt-rss.git] / feedlist.js
CommitLineData
29dfb258 1var _infscroll_disable = 0;
82764a41 2var _infscroll_request_sent = 0;
973fe3c6 3var _search_query = false;
ac541432 4
5225d420
AD
5var counter_timeout_id = false;
6
9922d56b 7var counters_last_request = 0;
91cdbe52 8
ff2c6e6a 9function viewCategory(cat) {
767e2486 10 viewfeed(cat, '', true);
0c3d1219 11 return false;
ff2c6e6a
AD
12}
13
71317973 14function loadMoreHeadlines() {
ac541432 15 try {
71317973
AD
16 var offset = 0;
17
18 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
19 var num_unread = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
20 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
21
22 // TODO implement marked & published
23
24 if (view_mode == "marked") {
25 console.warn("loadMoreHeadlines: marked is not implemented, falling back.");
26 offset = num_all;
27 } else if (view_mode == "published") {
28 console.warn("loadMoreHeadlines: published is not implemented, falling back.");
29 offset = num_all;
30 } else if (view_mode == "unread") {
31 offset = num_unread;
32 } else if (view_mode == "adaptive") {
33 if (num_unread > 0)
34 offset = num_unread;
35 else
36 offset = num_all;
37 } else {
38 offset = num_all;
39 }
2a8534a0 40
71317973 41 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset);
ac541432
AD
42
43 } catch (e) {
8b365031 44 exception_error("viewNextFeedPage", e);
ac541432
AD
45 }
46}
47
6a2034f9 48
37c03d3a 49function viewfeed(feed, subop, is_cat, offset, background) {
7719618b 50 try {
78b2c6ce
AD
51 if (is_cat == undefined)
52 is_cat = false;
53 else
54 is_cat = !!is_cat;
55
cd1bb36d 56 if (subop == undefined) subop = '';
71317973 57 if (offset == undefined) offset = 0;
37c03d3a 58 if (background == undefined) background = false;
c50e2b30 59
32f0c8b0
AD
60 last_requested_article = 0;
61
78b2c6ce
AD
62 var cached_headlines = false;
63
64 if (feed == getActiveFeedId())
65 cache_delete("feed:" + feed + ":" + is_cat);
66 else
67 cached_headlines = cache_get("feed:" + feed + ":" + is_cat);
b8e6acea 68
f92d7c2b
AD
69 dijit.byId("content-tabs").selectChild(
70 dijit.byId("content-tabs").getChildren()[0]);
71
52d7e7da
AD
72 var force_nocache = false;
73
37c03d3a
AD
74 if (!background) {
75 if (getActiveFeedId() != feed || offset == 0) {
76 active_post_id = 0;
77 _infscroll_disable = 0;
78 }
203de776 79
37c03d3a
AD
80 if (!offset && !subop && cached_headlines && !background) {
81 try {
82 render_local_headlines(feed, is_cat, JSON.parse(cached_headlines));
83 return;
84 } catch (e) {
85 console.warn("render_local_headlines failed: " + e);
86 }
78b2c6ce 87 }
78b2c6ce 88
37c03d3a
AD
89 if (offset != 0 && !subop) {
90 var date = new Date();
91 var timestamp = Math.round(date.getTime() / 1000);
82764a41 92
37c03d3a
AD
93 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
94 //console.log("infscroll request in progress, aborting");
95 return;
96 }
97
98 _infscroll_request_sent = timestamp;
82764a41
AD
99 }
100
37c03d3a 101 hideAuxDlg();
82764a41
AD
102 }
103
fd98576c
AD
104 Form.enable("main_toolbar_form");
105
fd98576c 106 var toolbar_query = Form.serialize("main_toolbar_form");
86b682ce 107
37c03d3a
AD
108 var query = "?op=viewfeed&feed=" + feed + "&" +
109 toolbar_query + "&subop=" + param_escape(subop);
110
111 if (!background) {
112 if (_search_query) {
52d7e7da 113 force_nocache = true;
37c03d3a
AD
114 query = query + "&" + _search_query;
115 _search_query = false;
52d7e7da 116 }
164f4738 117
37c03d3a 118 if (subop == "MarkAllRead") {
86b682ce 119
37c03d3a 120 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
86b682ce 121
37c03d3a
AD
122 if (show_next_feed) {
123 var nuf = getNextUnreadFeed(feed, is_cat);
e0998414 124
37c03d3a
AD
125 if (nuf) {
126 var cached_nuf = cache_get("feed:" + nuf + ":false");
3ac2b520 127
37c03d3a 128 if (cached_nuf) {
692de159 129
37c03d3a 130 render_local_headlines(nuf, false, JSON.parse(cached_nuf));
692de159 131
37c03d3a
AD
132 var catchup_query = "?op=rpc&subop=catchupFeed&feed_id=" +
133 feed + "&is_cat=" + is_cat;
134
135 console.log(catchup_query);
136
137 new Ajax.Request("backend.php", {
138 parameters: catchup_query,
139 onComplete: function(transport) {
140 handle_rpc_json(transport);
141 } });
142
143 return;
144 } else {
145 query += "&nuf=" + param_escape(nuf);
146 }
147 }
692de159 148 }
e0998414 149 }
ff2c6e6a 150
37c03d3a
AD
151 if (offset != 0) {
152 query = query + "&skip=" + offset;
60d4df4d 153
37c03d3a
AD
154 // to prevent duplicate feed titles when showing grouped vfeeds
155 if (vgroup_last_feed) {
156 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
157 }
60d4df4d 158 }
37c03d3a
AD
159
160 Form.enable("main_toolbar_form");
161
162 if (!offset)
163 if (!is_cat) {
164 if (!setFeedExpandoIcon(feed, is_cat, 'images/indicator_white.gif'))
165 notify_progress("Loading, please wait...", true);
166 } else {
167 notify_progress("Loading, please wait...", true);
168 }
203de776
AD
169 }
170
37c03d3a 171 query += "&cat=" + is_cat;
6b4163cb 172
84c7b824 173 console.log(query);
081e527d 174
71317973
AD
175 new Ajax.Request("backend.php", {
176 parameters: query,
177 onComplete: function(transport) {
178 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
37c03d3a 179 headlines_callback2(transport, offset, background);
71317973 180 } });
6b4163cb 181
7719618b
AD
182 } catch (e) {
183 exception_error("viewfeed", e);
bd202c3f 184 }
1a66d16e
AD
185}
186
6b4163cb 187function feedlist_init() {
1b0809ae 188 try {
fcf70c51 189 console.log("in feedlist init");
bd202c3f 190
60ea2377 191 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
1b0809ae 192 document.onkeydown = hotkey_handler;
164469f9
AD
193 setTimeout("hotkey_prefix_timeout()", 5*1000);
194
6e88da82 195 if (!getActiveFeedId()) {
bd202c3f
AD
196 setTimeout("viewfeed(-3)", 100);
197 }
fcf70c51 198
bd202c3f 199 console.log("T:" +
6e88da82 200 getInitParam("cdm_auto_catchup") + " " + getFeedUnread(-3));
7086277c 201
a73e6b97 202 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
782ddd70 203
cd1bb36d
AD
204 setTimeout("timeout()", 5000);
205
1b0809ae 206 } catch (e) {
bbf4d0b2 207 exception_error("feedlist/init", e);
1b0809ae 208 }
3745788e 209}
22e06406 210
997d9d7c 211function request_counters_real() {
5aaf9b34 212 try {
84c7b824 213 console.log("requesting counters...");
06c88b8d 214
6237ea05 215 var query = "?op=rpc&subop=getAllCounters&seq=" + next_seq();
5aaf9b34 216
9c99281f 217 query = query + "&omode=flc";
5aaf9b34 218
b4e75b2a
AD
219 new Ajax.Request("backend.php", {
220 parameters: query,
bd202c3f 221 onComplete: function(transport) {
5aaf9b34 222 try {
563b9c78 223 handle_rpc_json(transport);
5aaf9b34
AD
224 } catch (e) {
225 exception_error("viewfeed/getcounters", e);
226 }
227 } });
228
997d9d7c
AD
229 } catch (e) {
230 exception_error("request_counters_real", e);
231 }
232}
233
997d9d7c
AD
234
235function request_counters() {
236
237 try {
238
a598370d
AD
239 if (getInitParam("bw_limit") == "1") return;
240
997d9d7c
AD
241 var date = new Date();
242 var timestamp = Math.round(date.getTime() / 1000);
243
b39b57ac 244 if (timestamp - counters_last_request > 5) {
84c7b824 245 console.log("scheduling request of counters...");
5225d420
AD
246
247 window.clearTimeout(counter_timeout_id);
248 counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
249
997d9d7c
AD
250 counters_last_request = timestamp;
251 } else {
84c7b824 252 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
f55b0b12 253 }
997d9d7c 254
5aaf9b34
AD
255 } catch (e) {
256 exception_error("request_counters", e);
257 }
258}
a4b906ec 259
e69fb880
AD
260function displayNewContentPrompt(id) {
261 try {
262
a8328019 263 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
e69fb880
AD
264 __("New articles available in this feed (click to show)") + "</a>";
265
266 msg = msg.replace("%s", getFeedName(id));
267
268 $('auxDlg').innerHTML = msg;
269
270 new Effect.Appear('auxDlg', {duration : 0.5});
271
272 } catch (e) {
273 exception_error("displayNewContentPrompt", e);
274 }
275}
276
563b9c78 277function parse_counters(elems, scheduled_call) {
e69fb880
AD
278 try {
279
280 var feeds_found = 0;
281
e69fb880
AD
282 for (var l = 0; l < elems.length; l++) {
283
284 var id = elems[l].id
285 var kind = elems[l].kind;
286 var ctr = parseInt(elems[l].counter)
287 var error = elems[l].error;
288 var has_img = elems[l].has_img;
289 var updated = elems[l].updated;
290 var title = elems[l].title;
563b9c78 291
e69fb880 292 if (id == "global-unread") {
e69fb880
AD
293 global_unread = ctr;
294 updateTitle();
295 continue;
296 }
297
298 if (id == "subscribed-feeds") {
299 feeds_found = ctr;
300 continue;
301 }
13e785e0
AD
302
303 var treeItem;
304
179db4a0
AD
305 // TODO: enable new content notification for categories
306
bd202c3f 307 if (!activeFeedIsCat() && id == getActiveFeedId()
179db4a0 308 && ctr > getFeedUnread(id) && scheduled_call) {
6e88da82
AD
309 displayNewContentPrompt(id);
310 }
311
78b2c6ce
AD
312 if (getFeedUnread(id, (kind == "cat")) != ctr)
313 cache_delete("feed:" + id + ":" + (kind == "cat"));
314
13e785e0 315 setFeedUnread(id, (kind == "cat"), ctr);
e69fb880 316
fcf70c51 317 if (kind != "cat") {
700f1426 318 setFeedValue(id, false, 'error', error);
fcf70c51 319 setFeedValue(id, false, 'updated', updated);
2ef5c21f
AD
320
321 if (id > 0) {
322 if (has_img) {
bd202c3f 323 setFeedIcon(id, false,
2ef5c21f
AD
324 getInitParam("icons_url") + "/" + id + ".ico");
325 } else {
326 setFeedIcon(id, false, 'images/blank_icon.gif');
327 }
328 }
e69fb880 329 }
fcf70c51 330 }
bd202c3f 331
e69fb880
AD
332 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
333
e69fb880
AD
334 } catch (e) {
335 exception_error("parse_counters", e);
336 }
337}
338
6e88da82 339function getFeedUnread(feed, is_cat) {
e69fb880 340 try {
e032e2e7
AD
341 var tree = dijit.byId("feedTree");
342
bd202c3f 343 if (tree && tree.model)
e032e2e7
AD
344 return tree.model.getFeedUnread(feed, is_cat);
345
e69fb880 346 } catch (e) {
fcf70c51 347 //
e69fb880 348 }
fcf70c51
AD
349
350 return -1;
e69fb880
AD
351}
352
e69fb880 353function resort_feedlist() {
6e88da82 354 console.warn("resort_feedlist: function not implemented");
e69fb880
AD
355}
356
357function hideOrShowFeeds(hide) {
fcf70c51
AD
358 var tree = dijit.byId("feedTree");
359
05f224a3
AD
360 if (tree)
361 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
e69fb880
AD
362}
363
bd202c3f 364function getFeedName(feed, is_cat) {
e032e2e7
AD
365 var tree = dijit.byId("feedTree");
366
bd202c3f 367 if (tree && tree.model)
e032e2e7 368 return tree.model.getFeedValue(feed, is_cat, 'name');
fcf70c51 369}
e69fb880 370
bd202c3f 371function getFeedValue(feed, is_cat, key) {
fcf70c51 372 try {
e032e2e7 373 var tree = dijit.byId("feedTree");
fcf70c51 374
bd202c3f 375 if (tree && tree.model)
e032e2e7 376 return tree.model.getFeedValue(feed, is_cat, key);
bd202c3f 377
fcf70c51
AD
378 } catch (e) {
379 //
e69fb880 380 }
fcf70c51 381 return '';
e69fb880
AD
382}
383
13e785e0
AD
384function setFeedUnread(feed, is_cat, unread) {
385 try {
e032e2e7
AD
386 var tree = dijit.byId("feedTree");
387
bd202c3f 388 if (tree && tree.model)
e032e2e7
AD
389 return tree.model.setFeedUnread(feed, is_cat, unread);
390
fcf70c51
AD
391 } catch (e) {
392 exception_error("setFeedUnread", e);
393 }
394}
395
396function setFeedValue(feed, is_cat, key, value) {
397 try {
e032e2e7 398 var tree = dijit.byId("feedTree");
13e785e0 399
bd202c3f 400 if (tree && tree.model)
e032e2e7 401 return tree.model.setFeedValue(feed, is_cat, key, value);
c8a9fe5b 402
13e785e0 403 } catch (e) {
e032e2e7 404 //
fcf70c51
AD
405 }
406}
407
fcf70c51
AD
408function selectFeed(feed, is_cat) {
409 try {
410 var tree = dijit.byId("feedTree");
411
e032e2e7 412 if (tree) return tree.selectFeed(feed, is_cat);
fcf70c51
AD
413
414 } catch (e) {
415 exception_error("selectFeed", e);
13e785e0
AD
416 }
417}
2ef5c21f
AD
418
419function setFeedIcon(feed, is_cat, src) {
420 try {
421 var tree = dijit.byId("feedTree");
422
e032e2e7 423 if (tree) return tree.setFeedIcon(feed, is_cat, src);
2ef5c21f
AD
424
425 } catch (e) {
426 exception_error("setFeedIcon", e);
427 }
428}
9c99281f
AD
429
430function setFeedExpandoIcon(feed, is_cat, src) {
431 try {
432 var tree = dijit.byId("feedTree");
433
e032e2e7 434 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
9c99281f
AD
435
436 } catch (e) {
437 exception_error("setFeedIcon", e);
438 }
f92d7c2b 439 return false;
9c99281f 440}
37c03d3a
AD
441
442function getNextUnreadFeed(feed, is_cat) {
443 try {
444 var tree = dijit.byId("feedTree");
445 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
446
447 if (nuf)
448 return tree.model.store.getValue(nuf, 'bare_id');
449
450 } catch (e) {
451 exception_error("getNextUnreadFeed", e);
452 }
453}
454