]> git.wh0rd.org - tt-rss.git/blame - js/feedlist.js
fix csrf_token for single user mode
[tt-rss.git] / js / 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 {
d36d86b9
AD
16 console.log("loadMoreHeadlines");
17
71317973
AD
18 var offset = 0;
19
20 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
21 var num_unread = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
22 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
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") {
33 offset = num_unread;
34 } else if (view_mode == "adaptive") {
35 if (num_unread > 0)
36 offset = num_unread;
37 else
38 offset = num_all;
39 } else {
40 offset = num_all;
41 }
2a8534a0 42
28364067 43 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset, false, true);
ac541432
AD
44
45 } catch (e) {
8b365031 46 exception_error("viewNextFeedPage", e);
ac541432
AD
47 }
48}
49
6a2034f9 50
f30ef1fa 51function viewfeed(feed, method, is_cat, offset, background, infscroll_req) {
7719618b 52 try {
78b2c6ce
AD
53 if (is_cat == undefined)
54 is_cat = false;
55 else
56 is_cat = !!is_cat;
57
f30ef1fa 58 if (method == undefined) method = '';
71317973 59 if (offset == undefined) offset = 0;
37c03d3a 60 if (background == undefined) background = false;
77c3c5f9 61 if (infscroll_req == undefined) infscroll_req = false;
c50e2b30 62
32f0c8b0
AD
63 last_requested_article = 0;
64
78b2c6ce
AD
65 var cached_headlines = false;
66
61834616 67 if (feed == getActiveFeedId()) {
78b2c6ce 68 cache_delete("feed:" + feed + ":" + is_cat);
61834616 69 } else {
78b2c6ce 70 cached_headlines = cache_get("feed:" + feed + ":" + is_cat);
b8e6acea 71
61834616
AD
72 // switching to a different feed, we might as well catchup stuff visible
73 // in headlines buffer (if any)
a469bc7e 74 if (!background && isCdmMode() && getInitParam("cdm_auto_catchup") == 1 && parseInt(getActiveFeedId()) > 0) {
61834616
AD
75
76 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
77 function(child) {
78 var hf = $("headlines-frame");
79
80 if (hf.scrollTop + hf.offsetHeight >=
81 child.offsetTop + child.offsetHeight) {
82
83 var id = child.id.replace("RROW-", "");
84
85 if (catchup_id_batch.indexOf(id) == -1)
86 catchup_id_batch.push(id);
87
88 }
89
90 if (catchup_id_batch.length > 0) {
91 window.clearTimeout(catchup_timeout_id);
92
93 if (!_infscroll_request_sent) {
94 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
95 2000);
96 }
97 }
98
99 });
100 }
101 }
102
17eb362b 103 if (offset == 0 && !background)
0612ce23
AD
104 dijit.byId("content-tabs").selectChild(
105 dijit.byId("content-tabs").getChildren()[0]);
f92d7c2b 106
37c03d3a
AD
107 if (!background) {
108 if (getActiveFeedId() != feed || offset == 0) {
109 active_post_id = 0;
110 _infscroll_disable = 0;
111 }
203de776 112
f30ef1fa 113 if (!offset && !method && cached_headlines && !background) {
37c03d3a
AD
114 try {
115 render_local_headlines(feed, is_cat, JSON.parse(cached_headlines));
116 return;
117 } catch (e) {
118 console.warn("render_local_headlines failed: " + e);
119 }
78b2c6ce 120 }
78b2c6ce 121
f30ef1fa 122 if (offset != 0 && !method) {
37c03d3a
AD
123 var date = new Date();
124 var timestamp = Math.round(date.getTime() / 1000);
82764a41 125
37c03d3a
AD
126 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
127 //console.log("infscroll request in progress, aborting");
128 return;
129 }
130
131 _infscroll_request_sent = timestamp;
82764a41
AD
132 }
133
37c03d3a 134 hideAuxDlg();
82764a41
AD
135 }
136
fd98576c
AD
137 Form.enable("main_toolbar_form");
138
fd98576c 139 var toolbar_query = Form.serialize("main_toolbar_form");
86b682ce 140
3f363052
AD
141 var query = "?op=feeds&method=view&feed=" + feed + "&" +
142 toolbar_query + "&m=" + param_escape(method);
37c03d3a
AD
143
144 if (!background) {
145 if (_search_query) {
52d7e7da 146 force_nocache = true;
37c03d3a
AD
147 query = query + "&" + _search_query;
148 _search_query = false;
52d7e7da 149 }
164f4738 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');
77c3c5f9 179 headlines_callback2(transport, offset, background, infscroll_req);
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 204 setTimeout("timeout()", 5000);
452e75cc 205 setTimeout("precache_headlines_idle()", 3000);
cd1bb36d 206
1b0809ae 207 } catch (e) {
bbf4d0b2 208 exception_error("feedlist/init", e);
1b0809ae 209 }
3745788e 210}
22e06406 211
997d9d7c 212function request_counters_real() {
5aaf9b34 213 try {
84c7b824 214 console.log("requesting counters...");
06c88b8d 215
f30ef1fa 216 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
5aaf9b34 217
9c99281f 218 query = query + "&omode=flc";
5aaf9b34 219
b4e75b2a
AD
220 new Ajax.Request("backend.php", {
221 parameters: query,
bd202c3f 222 onComplete: function(transport) {
5aaf9b34 223 try {
563b9c78 224 handle_rpc_json(transport);
5aaf9b34
AD
225 } catch (e) {
226 exception_error("viewfeed/getcounters", e);
227 }
228 } });
229
997d9d7c
AD
230 } catch (e) {
231 exception_error("request_counters_real", e);
232 }
233}
234
997d9d7c
AD
235
236function request_counters() {
237
238 try {
239
a598370d
AD
240 if (getInitParam("bw_limit") == "1") return;
241
997d9d7c
AD
242 var date = new Date();
243 var timestamp = Math.round(date.getTime() / 1000);
244
b39b57ac 245 if (timestamp - counters_last_request > 5) {
84c7b824 246 console.log("scheduling request of counters...");
5225d420
AD
247
248 window.clearTimeout(counter_timeout_id);
249 counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
250
997d9d7c
AD
251 counters_last_request = timestamp;
252 } else {
84c7b824 253 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
f55b0b12 254 }
997d9d7c 255
5aaf9b34
AD
256 } catch (e) {
257 exception_error("request_counters", e);
258 }
259}
a4b906ec 260
e69fb880
AD
261function displayNewContentPrompt(id) {
262 try {
263
a8328019 264 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
e69fb880
AD
265 __("New articles available in this feed (click to show)") + "</a>";
266
267 msg = msg.replace("%s", getFeedName(id));
268
269 $('auxDlg').innerHTML = msg;
270
271 new Effect.Appear('auxDlg', {duration : 0.5});
272
273 } catch (e) {
274 exception_error("displayNewContentPrompt", e);
275 }
276}
277
563b9c78 278function parse_counters(elems, scheduled_call) {
e69fb880 279 try {
e69fb880
AD
280 for (var l = 0; l < elems.length; l++) {
281
e331188f 282 var id = elems[l].id;
e69fb880 283 var kind = elems[l].kind;
e331188f 284 var ctr = parseInt(elems[l].counter);
e69fb880
AD
285 var error = elems[l].error;
286 var has_img = elems[l].has_img;
287 var updated = elems[l].updated;
563b9c78 288
e69fb880 289 if (id == "global-unread") {
e69fb880
AD
290 global_unread = ctr;
291 updateTitle();
292 continue;
293 }
294
295 if (id == "subscribed-feeds") {
296 feeds_found = ctr;
297 continue;
298 }
13e785e0 299
179db4a0
AD
300 // TODO: enable new content notification for categories
301
bd202c3f 302 if (!activeFeedIsCat() && id == getActiveFeedId()
179db4a0 303 && ctr > getFeedUnread(id) && scheduled_call) {
6e88da82
AD
304 displayNewContentPrompt(id);
305 }
306
78b2c6ce
AD
307 if (getFeedUnread(id, (kind == "cat")) != ctr)
308 cache_delete("feed:" + id + ":" + (kind == "cat"));
309
13e785e0 310 setFeedUnread(id, (kind == "cat"), ctr);
e69fb880 311
fcf70c51 312 if (kind != "cat") {
700f1426 313 setFeedValue(id, false, 'error', error);
fcf70c51 314 setFeedValue(id, false, 'updated', updated);
2ef5c21f
AD
315
316 if (id > 0) {
317 if (has_img) {
bd202c3f 318 setFeedIcon(id, false,
2ef5c21f
AD
319 getInitParam("icons_url") + "/" + id + ".ico");
320 } else {
321 setFeedIcon(id, false, 'images/blank_icon.gif');
322 }
323 }
e69fb880 324 }
fcf70c51 325 }
bd202c3f 326
e69fb880
AD
327 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
328
e69fb880
AD
329 } catch (e) {
330 exception_error("parse_counters", e);
331 }
332}
333
6e88da82 334function getFeedUnread(feed, is_cat) {
e69fb880 335 try {
e032e2e7
AD
336 var tree = dijit.byId("feedTree");
337
bd202c3f 338 if (tree && tree.model)
e032e2e7
AD
339 return tree.model.getFeedUnread(feed, is_cat);
340
e69fb880 341 } catch (e) {
fcf70c51 342 //
e69fb880 343 }
fcf70c51
AD
344
345 return -1;
e69fb880
AD
346}
347
e69fb880 348function hideOrShowFeeds(hide) {
fcf70c51
AD
349 var tree = dijit.byId("feedTree");
350
05f224a3
AD
351 if (tree)
352 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
e69fb880
AD
353}
354
bd202c3f 355function getFeedName(feed, is_cat) {
e032e2e7
AD
356 var tree = dijit.byId("feedTree");
357
bd202c3f 358 if (tree && tree.model)
e032e2e7 359 return tree.model.getFeedValue(feed, is_cat, 'name');
fcf70c51 360}
e69fb880 361
bd202c3f 362function getFeedValue(feed, is_cat, key) {
fcf70c51 363 try {
e032e2e7 364 var tree = dijit.byId("feedTree");
fcf70c51 365
bd202c3f 366 if (tree && tree.model)
e032e2e7 367 return tree.model.getFeedValue(feed, is_cat, key);
bd202c3f 368
fcf70c51
AD
369 } catch (e) {
370 //
e69fb880 371 }
fcf70c51 372 return '';
e69fb880
AD
373}
374
13e785e0
AD
375function setFeedUnread(feed, is_cat, unread) {
376 try {
e032e2e7
AD
377 var tree = dijit.byId("feedTree");
378
bd202c3f 379 if (tree && tree.model)
e032e2e7
AD
380 return tree.model.setFeedUnread(feed, is_cat, unread);
381
fcf70c51
AD
382 } catch (e) {
383 exception_error("setFeedUnread", e);
384 }
385}
386
387function setFeedValue(feed, is_cat, key, value) {
388 try {
e032e2e7 389 var tree = dijit.byId("feedTree");
13e785e0 390
bd202c3f 391 if (tree && tree.model)
e032e2e7 392 return tree.model.setFeedValue(feed, is_cat, key, value);
c8a9fe5b 393
13e785e0 394 } catch (e) {
e032e2e7 395 //
fcf70c51
AD
396 }
397}
398
fcf70c51
AD
399function selectFeed(feed, is_cat) {
400 try {
401 var tree = dijit.byId("feedTree");
402
e032e2e7 403 if (tree) return tree.selectFeed(feed, is_cat);
fcf70c51
AD
404
405 } catch (e) {
406 exception_error("selectFeed", e);
13e785e0
AD
407 }
408}
2ef5c21f
AD
409
410function setFeedIcon(feed, is_cat, src) {
411 try {
412 var tree = dijit.byId("feedTree");
413
e032e2e7 414 if (tree) return tree.setFeedIcon(feed, is_cat, src);
2ef5c21f
AD
415
416 } catch (e) {
417 exception_error("setFeedIcon", e);
418 }
419}
9c99281f
AD
420
421function setFeedExpandoIcon(feed, is_cat, src) {
422 try {
423 var tree = dijit.byId("feedTree");
424
e032e2e7 425 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
9c99281f
AD
426
427 } catch (e) {
428 exception_error("setFeedIcon", e);
429 }
f92d7c2b 430 return false;
9c99281f 431}
37c03d3a
AD
432
433function getNextUnreadFeed(feed, is_cat) {
434 try {
435 var tree = dijit.byId("feedTree");
436 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
437
438 if (nuf)
439 return tree.model.store.getValue(nuf, 'bare_id');
440
441 } catch (e) {
442 exception_error("getNextUnreadFeed", e);
443 }
444}
445
44245618
AD
446function catchupCurrentFeed() {
447 return catchupFeed(getActiveFeedId(), activeFeedIsCat());
448}
449
450function catchupFeedInGroup(id) {
451 try {
452
453 var title = getFeedName(id);
454
455 var str = __("Mark all articles in %s as read?").replace("%s", title);
456
457 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
458 return viewCurrentFeed('MarkAllReadGR:' + id);
459 }
460
461 } catch (e) {
462 exception_error("catchupFeedInGroup", e);
463 }
464}
465
e3d2c029
AD
466function catchupFeed(feed, is_cat) {
467 try {
a593cb62 468 var str = __("Mark all articles in %s as read?");
9dd22f1e 469 var fn = getFeedName(feed, is_cat);
a593cb62
AD
470
471 str = str.replace("%s", fn);
472
473 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
474 return;
475 }
e3d2c029 476
f30ef1fa 477 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
e3d2c029
AD
478 feed + "&is_cat=" + is_cat;
479
480 notify_progress("Loading, please wait...", true);
481
482 new Ajax.Request("backend.php", {
483 parameters: catchup_query,
484 onComplete: function(transport) {
485 handle_rpc_json(transport);
44245618
AD
486
487 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
488
489 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
490 function(child) {
491 child.removeClassName("Unread");
492 }
493 );
494 }
495
496 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
497
498 if (show_next_feed) {
499 var nuf = getNextUnreadFeed(feed, is_cat);
500
501 if (nuf) {
c86bf88d 502 viewfeed(nuf, '', is_cat);
44245618
AD
503 }
504 }
505
e3d2c029
AD
506 notify("");
507 } });
508
509 } catch (e) {
510 exception_error("catchupFeed", e);
511 }
512}