]> git.wh0rd.org - tt-rss.git/blame - js/feedlist.js
fix crash on feedlist collapse, do not use _COLLAPSED_FEEDLIST pref
[tt-rss.git] / js / feedlist.js
CommitLineData
29dfb258 1var _infscroll_disable = 0;
82764a41 2var _infscroll_request_sent = 0;
973fe3c6 3var _search_query = false;
4d5eed55 4var _viewfeed_last = 0;
ac541432 5
9922d56b 6var counters_last_request = 0;
91cdbe52 7
ff2c6e6a 8function viewCategory(cat) {
767e2486 9 viewfeed(cat, '', true);
0c3d1219 10 return false;
ff2c6e6a
AD
11}
12
71317973 13function loadMoreHeadlines() {
ac541432 14 try {
d36d86b9
AD
15 console.log("loadMoreHeadlines");
16
71317973
AD
17 var offset = 0;
18
19 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
3d2c9f5a 20 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
71317973 21 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
fe0a51ac 22 var num_unread = getFeedUnread(getActiveFeedId(), activeFeedIsCat());
71317973
AD
23
24 // TODO implement marked & published
25
26 if (view_mode == "marked") {
27 console.warn("loadMoreHeadlines: marked is not implemented, falling back.");
28 offset = num_all;
29 } else if (view_mode == "published") {
30 console.warn("loadMoreHeadlines: published is not implemented, falling back.");
31 offset = num_all;
32 } else if (view_mode == "unread") {
455e774a 33 offset = unread_in_buffer;
c67cd7ab
AD
34 } else if (_search_query) {
35 offset = num_all;
71317973 36 } else if (view_mode == "adaptive") {
fe0a51ac 37 if (num_unread > 0)
3d2c9f5a 38 offset = unread_in_buffer;
71317973
AD
39 else
40 offset = num_all;
41 } else {
42 offset = num_all;
43 }
2a8534a0 44
c67cd7ab
AD
45 console.log("offset: " + offset);
46
28364067 47 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset, false, true);
ac541432
AD
48
49 } catch (e) {
8b365031 50 exception_error("viewNextFeedPage", e);
ac541432
AD
51 }
52}
53
6a2034f9 54
f30ef1fa 55function viewfeed(feed, method, is_cat, offset, background, infscroll_req) {
7719618b 56 try {
78b2c6ce
AD
57 if (is_cat == undefined)
58 is_cat = false;
59 else
60 is_cat = !!is_cat;
61
f30ef1fa 62 if (method == undefined) method = '';
71317973 63 if (offset == undefined) offset = 0;
37c03d3a 64 if (background == undefined) background = false;
77c3c5f9 65 if (infscroll_req == undefined) infscroll_req = false;
c50e2b30 66
32f0c8b0
AD
67 last_requested_article = 0;
68
2bee32ff 69 if (feed != getActiveFeedId() || activeFeedIsCat() != is_cat) {
1e07a58a 70 if (!background && _search_query) _search_query = false;
61834616
AD
71 }
72
37c03d3a 73 if (!background) {
4d5eed55
AD
74 _viewfeed_last = get_timestamp();
75
37c03d3a 76 if (getActiveFeedId() != feed || offset == 0) {
18e1a773 77 setActiveArticleId(0);
37c03d3a
AD
78 _infscroll_disable = 0;
79 }
203de776 80
f30ef1fa 81 if (offset != 0 && !method) {
4d5eed55 82 var timestamp = get_timestamp();
82764a41 83
37c03d3a
AD
84 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
85 //console.log("infscroll request in progress, aborting");
86 return;
87 }
88
89 _infscroll_request_sent = timestamp;
82764a41 90 }
82764a41
AD
91 }
92
fd98576c
AD
93 Form.enable("main_toolbar_form");
94
fd98576c 95 var toolbar_query = Form.serialize("main_toolbar_form");
86b682ce 96
3f363052 97 var query = "?op=feeds&method=view&feed=" + feed + "&" +
c0c2abba
AD
98 toolbar_query;
99
100 if (method) {
101 query = query + "&m=" + param_escape(method);
102 }
37c03d3a
AD
103
104 if (!background) {
105 if (_search_query) {
52d7e7da 106 force_nocache = true;
37c03d3a 107 query = query + "&" + _search_query;
c67cd7ab 108 //_search_query = false;
52d7e7da 109 }
164f4738 110
37c03d3a
AD
111 if (offset != 0) {
112 query = query + "&skip=" + offset;
60d4df4d 113
37c03d3a
AD
114 // to prevent duplicate feed titles when showing grouped vfeeds
115 if (vgroup_last_feed) {
116 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
117 }
c0c2abba
AD
118 } else {
119 if (!method && !is_cat && feed == getActiveFeedId()) {
120 query = query + "&m=ForceUpdate";
121 }
60d4df4d 122 }
37c03d3a
AD
123
124 Form.enable("main_toolbar_form");
125
d5a8e864
AD
126 if (!setFeedExpandoIcon(feed, is_cat,
127 (is_cat) ? 'images/indicator_tiny.gif' : 'images/indicator_white.gif'))
37c03d3a 128 notify_progress("Loading, please wait...", true);
203de776
AD
129 }
130
37c03d3a 131 query += "&cat=" + is_cat;
6b4163cb 132
84c7b824 133 console.log(query);
081e527d 134
ba1c7d7c
AD
135 setActiveFeedId(feed, is_cat);
136
71317973 137 new Ajax.Request("backend.php", {
f6ba763d
AD
138 parameters: query,
139 onComplete: function(transport) {
140 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
141 headlines_callback2(transport, offset, background, infscroll_req);
142 PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]);
143 } });
6b4163cb 144
7719618b
AD
145 } catch (e) {
146 exception_error("viewfeed", e);
bd202c3f 147 }
1a66d16e
AD
148}
149
6b4163cb 150function feedlist_init() {
1b0809ae 151 try {
fcf70c51 152 console.log("in feedlist init");
bd202c3f 153
ea18ccea
AD
154 loading_set_progress(50);
155
1b0809ae 156 document.onkeydown = hotkey_handler;
164469f9
AD
157 setTimeout("hotkey_prefix_timeout()", 5*1000);
158
1b04a298
AD
159 if (!getActiveFeedId()) {
160 viewfeed(-3);
161 } else {
162 viewfeed(getActiveFeedId(), '', activeFeedIsCat());
163 }
7086277c 164
a73e6b97 165 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
782ddd70 166
5b55e9e2
AD
167 request_counters(true);
168 timeout();
cd1bb36d 169
1b0809ae 170 } catch (e) {
bbf4d0b2 171 exception_error("feedlist/init", e);
1b0809ae 172 }
3745788e 173}
22e06406 174
997d9d7c 175
5b55e9e2 176function request_counters(force) {
997d9d7c 177 try {
997d9d7c
AD
178 var date = new Date();
179 var timestamp = Math.round(date.getTime() / 1000);
180
5129d29d 181 if (force || timestamp - counters_last_request > 5) {
84c7b824 182 console.log("scheduling request of counters...");
5225d420 183
997d9d7c 184 counters_last_request = timestamp;
5b55e9e2
AD
185
186 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
187
0a7b1520 188 if (!force)
5b55e9e2
AD
189 query = query + "&last_article_id=" + getInitParam("last_article_id");
190
191 console.log(query);
192
193 new Ajax.Request("backend.php", {
194 parameters: query,
195 onComplete: function(transport) {
196 try {
197 handle_rpc_json(transport);
198 } catch (e) {
199 exception_error("request_counters", e);
200 }
201 } });
202
997d9d7c 203 } else {
84c7b824 204 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
f55b0b12 205 }
997d9d7c 206
5aaf9b34
AD
207 } catch (e) {
208 exception_error("request_counters", e);
209 }
210}
a4b906ec 211
563b9c78 212function parse_counters(elems, scheduled_call) {
e69fb880 213 try {
e69fb880
AD
214 for (var l = 0; l < elems.length; l++) {
215
e331188f 216 var id = elems[l].id;
e69fb880 217 var kind = elems[l].kind;
e331188f 218 var ctr = parseInt(elems[l].counter);
e69fb880
AD
219 var error = elems[l].error;
220 var has_img = elems[l].has_img;
221 var updated = elems[l].updated;
c594eca0 222 var auxctr = parseInt(elems[l].auxcounter);
563b9c78 223
e69fb880 224 if (id == "global-unread") {
e69fb880
AD
225 global_unread = ctr;
226 updateTitle();
227 continue;
228 }
229
230 if (id == "subscribed-feeds") {
231 feeds_found = ctr;
232 continue;
233 }
13e785e0 234
183ca8b9 235 if (getFeedUnread(id, (kind == "cat")) != ctr ||
0ef32f48 236 (kind == "cat")) {
183ca8b9 237 }
78b2c6ce 238
13e785e0 239 setFeedUnread(id, (kind == "cat"), ctr);
c594eca0 240 setFeedValue(id, (kind == "cat"), 'auxcounter', auxctr);
e69fb880 241
fcf70c51 242 if (kind != "cat") {
700f1426 243 setFeedValue(id, false, 'error', error);
fcf70c51 244 setFeedValue(id, false, 'updated', updated);
2ef5c21f
AD
245
246 if (id > 0) {
247 if (has_img) {
bd202c3f 248 setFeedIcon(id, false,
2ef5c21f
AD
249 getInitParam("icons_url") + "/" + id + ".ico");
250 } else {
251 setFeedIcon(id, false, 'images/blank_icon.gif');
252 }
253 }
e69fb880 254 }
fcf70c51 255 }
bd202c3f 256
e69fb880
AD
257 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
258
e69fb880
AD
259 } catch (e) {
260 exception_error("parse_counters", e);
261 }
262}
263
6e88da82 264function getFeedUnread(feed, is_cat) {
e69fb880 265 try {
e032e2e7
AD
266 var tree = dijit.byId("feedTree");
267
bd202c3f 268 if (tree && tree.model)
e032e2e7
AD
269 return tree.model.getFeedUnread(feed, is_cat);
270
e69fb880 271 } catch (e) {
fcf70c51 272 //
e69fb880 273 }
fcf70c51
AD
274
275 return -1;
e69fb880
AD
276}
277
6c8e8fbd
AD
278function getFeedCategory(feed) {
279 try {
280 var tree = dijit.byId("feedTree");
281
282 if (tree && tree.model)
283 return tree.getFeedCategory(feed);
284
285 } catch (e) {
286 //
287 }
288
289 return false;
290}
291
e69fb880 292function hideOrShowFeeds(hide) {
fcf70c51
AD
293 var tree = dijit.byId("feedTree");
294
05f224a3
AD
295 if (tree)
296 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
e69fb880
AD
297}
298
bd202c3f 299function getFeedName(feed, is_cat) {
e032e2e7
AD
300 var tree = dijit.byId("feedTree");
301
bd202c3f 302 if (tree && tree.model)
e032e2e7 303 return tree.model.getFeedValue(feed, is_cat, 'name');
fcf70c51 304}
e69fb880 305
bd202c3f 306function getFeedValue(feed, is_cat, key) {
fcf70c51 307 try {
e032e2e7 308 var tree = dijit.byId("feedTree");
fcf70c51 309
bd202c3f 310 if (tree && tree.model)
e032e2e7 311 return tree.model.getFeedValue(feed, is_cat, key);
bd202c3f 312
fcf70c51
AD
313 } catch (e) {
314 //
e69fb880 315 }
fcf70c51 316 return '';
e69fb880
AD
317}
318
13e785e0
AD
319function setFeedUnread(feed, is_cat, unread) {
320 try {
e032e2e7
AD
321 var tree = dijit.byId("feedTree");
322
bd202c3f 323 if (tree && tree.model)
e032e2e7
AD
324 return tree.model.setFeedUnread(feed, is_cat, unread);
325
fcf70c51
AD
326 } catch (e) {
327 exception_error("setFeedUnread", e);
328 }
329}
330
331function setFeedValue(feed, is_cat, key, value) {
332 try {
e032e2e7 333 var tree = dijit.byId("feedTree");
13e785e0 334
bd202c3f 335 if (tree && tree.model)
e032e2e7 336 return tree.model.setFeedValue(feed, is_cat, key, value);
c8a9fe5b 337
13e785e0 338 } catch (e) {
e032e2e7 339 //
fcf70c51
AD
340 }
341}
342
fcf70c51
AD
343function selectFeed(feed, is_cat) {
344 try {
345 var tree = dijit.byId("feedTree");
346
e032e2e7 347 if (tree) return tree.selectFeed(feed, is_cat);
fcf70c51
AD
348
349 } catch (e) {
350 exception_error("selectFeed", e);
13e785e0
AD
351 }
352}
2ef5c21f
AD
353
354function setFeedIcon(feed, is_cat, src) {
355 try {
356 var tree = dijit.byId("feedTree");
357
e032e2e7 358 if (tree) return tree.setFeedIcon(feed, is_cat, src);
2ef5c21f
AD
359
360 } catch (e) {
361 exception_error("setFeedIcon", e);
362 }
363}
9c99281f
AD
364
365function setFeedExpandoIcon(feed, is_cat, src) {
366 try {
367 var tree = dijit.byId("feedTree");
368
e032e2e7 369 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
9c99281f
AD
370
371 } catch (e) {
372 exception_error("setFeedIcon", e);
373 }
f92d7c2b 374 return false;
9c99281f 375}
37c03d3a
AD
376
377function getNextUnreadFeed(feed, is_cat) {
378 try {
379 var tree = dijit.byId("feedTree");
380 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
381
382 if (nuf)
383 return tree.model.store.getValue(nuf, 'bare_id');
384
385 } catch (e) {
386 exception_error("getNextUnreadFeed", e);
387 }
388}
389
e3ca32a8
AD
390function catchupCurrentFeed(mode) {
391 catchupFeed(getActiveFeedId(), activeFeedIsCat(), mode);
44245618
AD
392}
393
394function catchupFeedInGroup(id) {
395 try {
396
397 var title = getFeedName(id);
398
399 var str = __("Mark all articles in %s as read?").replace("%s", title);
400
401 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
402 return viewCurrentFeed('MarkAllReadGR:' + id);
403 }
404
405 } catch (e) {
406 exception_error("catchupFeedInGroup", e);
407 }
408}
409
c8b693cf 410function catchupFeed(feed, is_cat, mode) {
e3d2c029 411 try {
184f5195
AD
412 if (is_cat == undefined) is_cat = false;
413
c8b693cf
AD
414 var str = false;
415
416 switch (mode) {
417 case "1day":
418 str = __("Mark all articles in %s older than 1 day as read?");
419 break;
420 case "1week":
421 str = __("Mark all articles in %s older than 1 week as read?");
422 break;
7b32ee7e 423 case "2week":
c8b693cf
AD
424 str = __("Mark all articles in %s older than 2 weeks as read?");
425 break;
426 default:
427 str = __("Mark all articles in %s as read?");
428 }
429
9dd22f1e 430 var fn = getFeedName(feed, is_cat);
a593cb62
AD
431
432 str = str.replace("%s", fn);
433
434 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
435 return;
436 }
e3d2c029 437
f30ef1fa 438 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
c8b693cf 439 feed + "&is_cat=" + is_cat + "&mode=" + mode;
184f5195
AD
440
441 console.log(catchup_query);
e3d2c029
AD
442
443 notify_progress("Loading, please wait...", true);
444
445 new Ajax.Request("backend.php", {
446 parameters: catchup_query,
447 onComplete: function(transport) {
448 handle_rpc_json(transport);
44245618 449
44245618
AD
450 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
451
452 if (show_next_feed) {
453 var nuf = getNextUnreadFeed(feed, is_cat);
454
455 if (nuf) {
c86bf88d 456 viewfeed(nuf, '', is_cat);
44245618 457 }
c8b693cf
AD
458 } else {
459 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
460 viewCurrentFeed();
461 }
44245618
AD
462 }
463
e3d2c029
AD
464 notify("");
465 } });
466
467 } catch (e) {
468 exception_error("catchupFeed", e);
469 }
470}
de44b9a6
AD
471
472function decrementFeedCounter(feed, is_cat) {
473 try {
474 var ctr = getFeedUnread(feed, is_cat);
475
476 if (ctr > 0) {
477 setFeedUnread(feed, is_cat, ctr - 1);
5b55e9e2
AD
478 global_unread = global_unread - 1;
479 updateTitle();
de44b9a6
AD
480
481 if (!is_cat) {
482 var cat = parseInt(getFeedCategory(feed));
483
484 if (!isNaN(cat)) {
485 ctr = getFeedUnread(cat, true);
486
487 if (ctr > 0) {
488 setFeedUnread(cat, true, ctr - 1);
489 }
490 }
491 }
492 }
493
494 } catch (e) {
495 exception_error("decrement_feed_counter", e);
496 }
497}
2196b517
AD
498
499