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