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