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