]>
Commit | Line | Data |
---|---|---|
1 | var _infscroll_disable = 0; | |
2 | var _infscroll_request_sent = 0; | |
3 | ||
4 | var _search_query = false; | |
5 | var _viewfeed_last = 0; | |
6 | var _viewfeed_timeout = false; | |
7 | ||
8 | var counters_last_request = 0; | |
9 | ||
10 | function viewCategory(cat) { | |
11 | viewfeed({feed: cat, is_cat: true}); | |
12 | return false; | |
13 | } | |
14 | ||
15 | function loadMoreHeadlines() { | |
16 | try { | |
17 | console.log("loadMoreHeadlines"); | |
18 | ||
19 | var offset = 0; | |
20 | ||
21 | var view_mode = document.forms["main_toolbar_form"].view_mode.value; | |
22 | var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length; | |
23 | var num_all = $$("#headlines-frame > div[id*=RROW]").length; | |
24 | var num_unread = getFeedUnread(getActiveFeedId(), activeFeedIsCat()); | |
25 | ||
26 | // TODO implement marked & published | |
27 | ||
28 | if (view_mode == "marked") { | |
29 | console.warn("loadMoreHeadlines: marked is not implemented, falling back."); | |
30 | offset = num_all; | |
31 | } else if (view_mode == "published") { | |
32 | console.warn("loadMoreHeadlines: published is not implemented, falling back."); | |
33 | offset = num_all; | |
34 | } else if (view_mode == "unread") { | |
35 | offset = unread_in_buffer; | |
36 | } else if (_search_query) { | |
37 | offset = num_all; | |
38 | } else if (view_mode == "adaptive" && !(getActiveFeedId() == -1 && !activeFeedIsCat())) { | |
39 | // ^ starred feed shows both unread & read articles in adaptive mode | |
40 | offset = num_unread > 0 ? unread_in_buffer : num_all; | |
41 | } else { | |
42 | offset = num_all; | |
43 | } | |
44 | ||
45 | console.log("offset: " + offset); | |
46 | ||
47 | viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), offset: offset, infscroll_req: true}); | |
48 | ||
49 | } catch (e) { | |
50 | exception_error("viewNextFeedPage", e); | |
51 | } | |
52 | } | |
53 | ||
54 | function cleanup_memory(root) { | |
55 | try { | |
56 | var dijits = dojo.query("[widgetid]", dijit.byId(root).domNode).map(dijit.byNode); | |
57 | ||
58 | dijits.each(function (d) { | |
59 | dojo.destroy(d.domNode); | |
60 | }); | |
61 | ||
62 | $$("#" + root + " *").each(function (i) { | |
63 | i.parentNode ? i.parentNode.removeChild(i) : true; | |
64 | }); | |
65 | } catch (e) { | |
66 | console.log("cleanup_memory: exception"); | |
67 | console.log(e); | |
68 | } | |
69 | } | |
70 | ||
71 | function viewfeed(params) { | |
72 | try { | |
73 | var feed = params.feed; | |
74 | var is_cat = params.is_cat; | |
75 | var offset = params.offset; | |
76 | var background = params.background; | |
77 | var infscroll_req = params.infscroll_req; | |
78 | var can_wait = params.can_wait; | |
79 | var viewfeed_debug = params.viewfeed_debug; | |
80 | var method = params.method; | |
81 | ||
82 | if (is_cat == undefined) | |
83 | is_cat = false; | |
84 | else | |
85 | is_cat = !!is_cat; | |
86 | ||
87 | if (offset == undefined) offset = 0; | |
88 | if (background == undefined) background = false; | |
89 | if (infscroll_req == undefined) infscroll_req = false; | |
90 | ||
91 | last_requested_article = 0; | |
92 | ||
93 | if (feed != getActiveFeedId() || activeFeedIsCat() != is_cat) { | |
94 | if (!background && _search_query) _search_query = false; | |
95 | } | |
96 | ||
97 | if (!background) { | |
98 | _viewfeed_last = get_timestamp(); | |
99 | ||
100 | if (getActiveFeedId() != feed || !infscroll_req) { | |
101 | setActiveArticleId(0); | |
102 | _infscroll_disable = 0; | |
103 | ||
104 | cleanup_memory("headlines-frame"); | |
105 | } | |
106 | ||
107 | if (infscroll_req) { | |
108 | var timestamp = get_timestamp(); | |
109 | ||
110 | if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) { | |
111 | //console.log("infscroll request in progress, aborting"); | |
112 | return; | |
113 | } | |
114 | ||
115 | _infscroll_request_sent = timestamp; | |
116 | } | |
117 | } | |
118 | ||
119 | Form.enable("main_toolbar_form"); | |
120 | ||
121 | var toolbar_query = Form.serialize("main_toolbar_form"); | |
122 | ||
123 | var query = "?op=feeds&method=view&feed=" + param_escape(feed) + "&" + | |
124 | toolbar_query; | |
125 | ||
126 | if (method) query += "&m=" + param_escape(method); | |
127 | ||
128 | if (offset > 0) { | |
129 | if (current_first_id) { | |
130 | query = query + "&fid=" + param_escape(current_first_id); | |
131 | } | |
132 | } | |
133 | ||
134 | if (!background) { | |
135 | if (_search_query) { | |
136 | force_nocache = true; | |
137 | query = query + "&" + _search_query; | |
138 | //_search_query = false; | |
139 | } | |
140 | ||
141 | if (offset != 0) { | |
142 | query = query + "&skip=" + offset; | |
143 | ||
144 | // to prevent duplicate feed titles when showing grouped vfeeds | |
145 | if (vgroup_last_feed) { | |
146 | query = query + "&vgrlf=" + param_escape(vgroup_last_feed); | |
147 | } | |
148 | } else { | |
149 | if (!is_cat && feed == getActiveFeedId() && !params.method) { | |
150 | query = query + "&m=ForceUpdate"; | |
151 | } | |
152 | } | |
153 | ||
154 | Form.enable("main_toolbar_form"); | |
155 | ||
156 | if (!setFeedExpandoIcon(feed, is_cat, | |
157 | (is_cat) ? 'images/indicator_tiny.gif' : 'images/indicator_white.gif')) | |
158 | notify_progress("Loading, please wait...", true); | |
159 | } | |
160 | ||
161 | query += "&cat=" + is_cat; | |
162 | ||
163 | console.log(query); | |
164 | ||
165 | if (can_wait && _viewfeed_timeout) { | |
166 | setFeedExpandoIcon(getActiveFeedId(), activeFeedIsCat(), 'images/blank_icon.gif'); | |
167 | clearTimeout(_viewfeed_timeout); | |
168 | } | |
169 | ||
170 | setActiveFeedId(feed, is_cat); | |
171 | ||
172 | if (viewfeed_debug) { | |
173 | window.open("backend.php" + query + "&debug=1&csrf_token=" + getInitParam("csrf_token")); | |
174 | } | |
175 | ||
176 | timeout_ms = can_wait ? 250 : 0; | |
177 | _viewfeed_timeout = setTimeout(function() { | |
178 | ||
179 | new Ajax.Request("backend.php", { | |
180 | parameters: query, | |
181 | onComplete: function(transport) { | |
182 | setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif'); | |
183 | headlines_callback2(transport, offset, background, infscroll_req); | |
184 | PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]); | |
185 | } }); | |
186 | }, timeout_ms); // Wait 250ms | |
187 | ||
188 | } catch (e) { | |
189 | exception_error("viewfeed", e); | |
190 | } | |
191 | } | |
192 | ||
193 | function feedlist_init() { | |
194 | try { | |
195 | console.log("in feedlist init"); | |
196 | ||
197 | loading_set_progress(50); | |
198 | ||
199 | document.onkeydown = hotkey_handler; | |
200 | setTimeout("hotkey_prefix_timeout()", 5*1000); | |
201 | ||
202 | if (!getActiveFeedId()) { | |
203 | viewfeed({feed: -3}); | |
204 | } else { | |
205 | viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat()}); | |
206 | } | |
207 | ||
208 | hideOrShowFeeds(getInitParam("hide_read_feeds") == 1); | |
209 | ||
210 | request_counters(true); | |
211 | timeout(); | |
212 | ||
213 | } catch (e) { | |
214 | exception_error("feedlist/init", e); | |
215 | } | |
216 | } | |
217 | ||
218 | ||
219 | function request_counters(force) { | |
220 | try { | |
221 | var date = new Date(); | |
222 | var timestamp = Math.round(date.getTime() / 1000); | |
223 | ||
224 | if (force || timestamp - counters_last_request > 5) { | |
225 | console.log("scheduling request of counters..."); | |
226 | ||
227 | counters_last_request = timestamp; | |
228 | ||
229 | var query = "?op=rpc&method=getAllCounters&seq=" + next_seq(); | |
230 | ||
231 | if (!force) | |
232 | query = query + "&last_article_id=" + getInitParam("last_article_id"); | |
233 | ||
234 | console.log(query); | |
235 | ||
236 | new Ajax.Request("backend.php", { | |
237 | parameters: query, | |
238 | onComplete: function(transport) { | |
239 | try { | |
240 | handle_rpc_json(transport); | |
241 | } catch (e) { | |
242 | exception_error("request_counters", e); | |
243 | } | |
244 | } }); | |
245 | ||
246 | } else { | |
247 | console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request)); | |
248 | } | |
249 | ||
250 | } catch (e) { | |
251 | exception_error("request_counters", e); | |
252 | } | |
253 | } | |
254 | ||
255 | function parse_counters(elems, scheduled_call) { | |
256 | try { | |
257 | for (var l = 0; l < elems.length; l++) { | |
258 | ||
259 | var id = elems[l].id; | |
260 | var kind = elems[l].kind; | |
261 | var ctr = parseInt(elems[l].counter); | |
262 | var error = elems[l].error; | |
263 | var has_img = elems[l].has_img; | |
264 | var updated = elems[l].updated; | |
265 | var auxctr = parseInt(elems[l].auxcounter); | |
266 | ||
267 | if (id == "global-unread") { | |
268 | global_unread = ctr; | |
269 | updateTitle(); | |
270 | continue; | |
271 | } | |
272 | ||
273 | if (id == "subscribed-feeds") { | |
274 | feeds_found = ctr; | |
275 | continue; | |
276 | } | |
277 | ||
278 | if (getFeedUnread(id, (kind == "cat")) != ctr || | |
279 | (kind == "cat")) { | |
280 | } | |
281 | ||
282 | setFeedUnread(id, (kind == "cat"), ctr); | |
283 | setFeedValue(id, (kind == "cat"), 'auxcounter', auxctr); | |
284 | ||
285 | if (kind != "cat") { | |
286 | setFeedValue(id, false, 'error', error); | |
287 | setFeedValue(id, false, 'updated', updated); | |
288 | ||
289 | if (id > 0) { | |
290 | if (has_img) { | |
291 | setFeedIcon(id, false, | |
292 | getInitParam("icons_url") + "/" + id + ".ico"); | |
293 | } else { | |
294 | setFeedIcon(id, false, 'images/blank_icon.gif'); | |
295 | } | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | hideOrShowFeeds(getInitParam("hide_read_feeds") == 1); | |
301 | ||
302 | } catch (e) { | |
303 | exception_error("parse_counters", e); | |
304 | } | |
305 | } | |
306 | ||
307 | function getFeedUnread(feed, is_cat) { | |
308 | try { | |
309 | var tree = dijit.byId("feedTree"); | |
310 | ||
311 | if (tree && tree.model) | |
312 | return tree.model.getFeedUnread(feed, is_cat); | |
313 | ||
314 | } catch (e) { | |
315 | // | |
316 | } | |
317 | ||
318 | return -1; | |
319 | } | |
320 | ||
321 | function getFeedCategory(feed) { | |
322 | try { | |
323 | var tree = dijit.byId("feedTree"); | |
324 | ||
325 | if (tree && tree.model) | |
326 | return tree.getFeedCategory(feed); | |
327 | ||
328 | } catch (e) { | |
329 | // | |
330 | } | |
331 | ||
332 | return false; | |
333 | } | |
334 | ||
335 | function hideOrShowFeeds(hide) { | |
336 | var tree = dijit.byId("feedTree"); | |
337 | ||
338 | if (tree) | |
339 | return tree.hideRead(hide, getInitParam("hide_read_shows_special")); | |
340 | } | |
341 | ||
342 | function getFeedName(feed, is_cat) { | |
343 | ||
344 | if (isNaN(feed)) return feed; // it's a tag | |
345 | ||
346 | var tree = dijit.byId("feedTree"); | |
347 | ||
348 | if (tree && tree.model) | |
349 | return tree.model.getFeedValue(feed, is_cat, 'name'); | |
350 | } | |
351 | ||
352 | function getFeedValue(feed, is_cat, key) { | |
353 | try { | |
354 | var tree = dijit.byId("feedTree"); | |
355 | ||
356 | if (tree && tree.model) | |
357 | return tree.model.getFeedValue(feed, is_cat, key); | |
358 | ||
359 | } catch (e) { | |
360 | // | |
361 | } | |
362 | return ''; | |
363 | } | |
364 | ||
365 | function setFeedUnread(feed, is_cat, unread) { | |
366 | try { | |
367 | var tree = dijit.byId("feedTree"); | |
368 | ||
369 | if (tree && tree.model) | |
370 | return tree.model.setFeedUnread(feed, is_cat, unread); | |
371 | ||
372 | } catch (e) { | |
373 | exception_error("setFeedUnread", e); | |
374 | } | |
375 | } | |
376 | ||
377 | function setFeedValue(feed, is_cat, key, value) { | |
378 | try { | |
379 | var tree = dijit.byId("feedTree"); | |
380 | ||
381 | if (tree && tree.model) | |
382 | return tree.model.setFeedValue(feed, is_cat, key, value); | |
383 | ||
384 | } catch (e) { | |
385 | // | |
386 | } | |
387 | } | |
388 | ||
389 | function selectFeed(feed, is_cat) { | |
390 | try { | |
391 | var tree = dijit.byId("feedTree"); | |
392 | ||
393 | if (tree) return tree.selectFeed(feed, is_cat); | |
394 | ||
395 | } catch (e) { | |
396 | exception_error("selectFeed", e); | |
397 | } | |
398 | } | |
399 | ||
400 | function setFeedIcon(feed, is_cat, src) { | |
401 | try { | |
402 | var tree = dijit.byId("feedTree"); | |
403 | ||
404 | if (tree) return tree.setFeedIcon(feed, is_cat, src); | |
405 | ||
406 | } catch (e) { | |
407 | exception_error("setFeedIcon", e); | |
408 | } | |
409 | } | |
410 | ||
411 | function setFeedExpandoIcon(feed, is_cat, src) { | |
412 | try { | |
413 | var tree = dijit.byId("feedTree"); | |
414 | ||
415 | if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src); | |
416 | ||
417 | } catch (e) { | |
418 | exception_error("setFeedIcon", e); | |
419 | } | |
420 | return false; | |
421 | } | |
422 | ||
423 | function getNextUnreadFeed(feed, is_cat) { | |
424 | try { | |
425 | var tree = dijit.byId("feedTree"); | |
426 | var nuf = tree.model.getNextUnreadFeed(feed, is_cat); | |
427 | ||
428 | if (nuf) | |
429 | return tree.model.store.getValue(nuf, 'bare_id'); | |
430 | ||
431 | } catch (e) { | |
432 | exception_error("getNextUnreadFeed", e); | |
433 | } | |
434 | } | |
435 | ||
436 | function catchupCurrentFeed(mode) { | |
437 | catchupFeed(getActiveFeedId(), activeFeedIsCat(), mode); | |
438 | } | |
439 | ||
440 | function catchupFeedInGroup(id) { | |
441 | try { | |
442 | ||
443 | var title = getFeedName(id); | |
444 | ||
445 | var str = __("Mark all articles in %s as read?").replace("%s", title); | |
446 | ||
447 | if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) { | |
448 | ||
449 | var rows = $$("#headlines-frame > div[id*=RROW][class*=Unread][orig-feed-id='"+id+"']"); | |
450 | ||
451 | if (rows.length > 0) { | |
452 | ||
453 | rows.each(function(row) { | |
454 | row.removeClassName("Unread"); | |
455 | }); | |
456 | ||
457 | updateFloatingTitle(true); | |
458 | ||
459 | var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" + | |
460 | id + "&is_cat=false"; | |
461 | ||
462 | console.log(catchup_query); | |
463 | ||
464 | notify_progress("Loading, please wait...", true); | |
465 | ||
466 | new Ajax.Request("backend.php", { | |
467 | parameters: catchup_query, | |
468 | onComplete: function (transport) { | |
469 | handle_rpc_json(transport); | |
470 | } | |
471 | } ); | |
472 | } | |
473 | ||
474 | //return viewCurrentFeed('MarkAllReadGR:' + id); | |
475 | } | |
476 | ||
477 | } catch (e) { | |
478 | exception_error("catchupFeedInGroup", e); | |
479 | } | |
480 | } | |
481 | ||
482 | function catchupFeed(feed, is_cat, mode) { | |
483 | try { | |
484 | if (is_cat == undefined) is_cat = false; | |
485 | ||
486 | var str = false; | |
487 | ||
488 | switch (mode) { | |
489 | case "1day": | |
490 | str = __("Mark all articles in %s older than 1 day as read?"); | |
491 | break; | |
492 | case "1week": | |
493 | str = __("Mark all articles in %s older than 1 week as read?"); | |
494 | break; | |
495 | case "2week": | |
496 | str = __("Mark all articles in %s older than 2 weeks as read?"); | |
497 | break; | |
498 | default: | |
499 | str = __("Mark all articles in %s as read?"); | |
500 | } | |
501 | ||
502 | var fn = getFeedName(feed, is_cat); | |
503 | ||
504 | str = str.replace("%s", fn); | |
505 | ||
506 | if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) { | |
507 | return; | |
508 | } | |
509 | ||
510 | var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" + | |
511 | feed + "&is_cat=" + is_cat + "&mode=" + mode; | |
512 | ||
513 | console.log(catchup_query); | |
514 | ||
515 | notify_progress("Loading, please wait...", true); | |
516 | ||
517 | new Ajax.Request("backend.php", { | |
518 | parameters: catchup_query, | |
519 | onComplete: function(transport) { | |
520 | handle_rpc_json(transport); | |
521 | ||
522 | var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1"; | |
523 | ||
524 | if (show_next_feed) { | |
525 | var nuf = getNextUnreadFeed(feed, is_cat); | |
526 | ||
527 | if (nuf) { | |
528 | viewfeed({feed: nuf, is_cat: is_cat}); | |
529 | } | |
530 | } else { | |
531 | if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) { | |
532 | viewCurrentFeed(); | |
533 | } | |
534 | } | |
535 | ||
536 | notify(""); | |
537 | } }); | |
538 | ||
539 | } catch (e) { | |
540 | exception_error("catchupFeed", e); | |
541 | } | |
542 | } | |
543 | ||
544 | function decrementFeedCounter(feed, is_cat) { | |
545 | try { | |
546 | var ctr = getFeedUnread(feed, is_cat); | |
547 | ||
548 | if (ctr > 0) { | |
549 | setFeedUnread(feed, is_cat, ctr - 1); | |
550 | global_unread = global_unread - 1; | |
551 | updateTitle(); | |
552 | ||
553 | if (!is_cat) { | |
554 | var cat = parseInt(getFeedCategory(feed)); | |
555 | ||
556 | if (!isNaN(cat)) { | |
557 | ctr = getFeedUnread(cat, true); | |
558 | ||
559 | if (ctr > 0) { | |
560 | setFeedUnread(cat, true, ctr - 1); | |
561 | } | |
562 | } | |
563 | } | |
564 | } | |
565 | ||
566 | } catch (e) { | |
567 | exception_error("decrement_feed_counter", e); | |
568 | } | |
569 | } | |
570 | ||
571 |