]>
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 | if (getInitParam("is_default_pw")) { | |
202 | console.warn("user password is at default value"); | |
203 | ||
204 | var dialog = new dijit.Dialog({ | |
205 | title: __("Your password is at default value"), | |
206 | href: "backend.php?op=dlg&method=defaultpasswordwarning", | |
207 | id: 'infoBox', | |
208 | style: "width: 600px", | |
209 | onCancel: function() { | |
210 | return true; | |
211 | }, | |
212 | onExecute: function() { | |
213 | return true; | |
214 | }, | |
215 | onClose: function() { | |
216 | return true; | |
217 | } | |
218 | }); | |
219 | ||
220 | dialog.show(); | |
221 | } | |
222 | ||
223 | // bw_limit disables timeout() so we request initial counters separately | |
224 | if (getInitParam("bw_limit") == "1") { | |
225 | request_counters(true); | |
226 | } else { | |
227 | setTimeout(timeout, 250); | |
228 | } | |
229 | } | |
230 | ||
231 | ||
232 | function request_counters(force) { | |
233 | var date = new Date(); | |
234 | var timestamp = Math.round(date.getTime() / 1000); | |
235 | ||
236 | if (force || timestamp - counters_last_request > 5) { | |
237 | console.log("scheduling request of counters..."); | |
238 | ||
239 | counters_last_request = timestamp; | |
240 | ||
241 | var query = "?op=rpc&method=getAllCounters&seq=" + next_seq(); | |
242 | ||
243 | if (!force) | |
244 | query = query + "&last_article_id=" + getInitParam("last_article_id"); | |
245 | ||
246 | console.log(query); | |
247 | ||
248 | new Ajax.Request("backend.php", { | |
249 | parameters: query, | |
250 | onComplete: function(transport) { | |
251 | handle_rpc_json(transport); | |
252 | } }); | |
253 | ||
254 | } else { | |
255 | console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request)); | |
256 | } | |
257 | } | |
258 | ||
259 | // NOTE: this implementation is incomplete | |
260 | // for general objects but good enough for counters | |
261 | // http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html | |
262 | function counter_is_equal(a, b) { | |
263 | // Create arrays of property names | |
264 | var aProps = Object.getOwnPropertyNames(a); | |
265 | var bProps = Object.getOwnPropertyNames(b); | |
266 | ||
267 | // If number of properties is different, | |
268 | // objects are not equivalent | |
269 | if (aProps.length != bProps.length) { | |
270 | return false; | |
271 | } | |
272 | ||
273 | for (var i = 0; i < aProps.length; i++) { | |
274 | var propName = aProps[i]; | |
275 | ||
276 | // If values of same property are not equal, | |
277 | // objects are not equivalent | |
278 | if (a[propName] !== b[propName]) { | |
279 | return false; | |
280 | } | |
281 | } | |
282 | ||
283 | // If we made it this far, objects | |
284 | // are considered equivalent | |
285 | return true; | |
286 | } | |
287 | ||
288 | ||
289 | function parse_counters(elems) { | |
290 | for (var l = 0; l < elems.length; l++) { | |
291 | ||
292 | if (_counters_prev[l] && counter_is_equal(elems[l], _counters_prev[l])) { | |
293 | continue; | |
294 | } | |
295 | ||
296 | var id = elems[l].id; | |
297 | var kind = elems[l].kind; | |
298 | var ctr = parseInt(elems[l].counter); | |
299 | var error = elems[l].error; | |
300 | var has_img = elems[l].has_img; | |
301 | var updated = elems[l].updated; | |
302 | var auxctr = parseInt(elems[l].auxcounter); | |
303 | ||
304 | if (id == "global-unread") { | |
305 | global_unread = ctr; | |
306 | updateTitle(); | |
307 | continue; | |
308 | } | |
309 | ||
310 | if (id == "subscribed-feeds") { | |
311 | /* feeds_found = ctr; */ | |
312 | continue; | |
313 | } | |
314 | ||
315 | /*if (getFeedUnread(id, (kind == "cat")) != ctr || | |
316 | (kind == "cat")) { | |
317 | }*/ | |
318 | ||
319 | setFeedUnread(id, (kind == "cat"), ctr); | |
320 | setFeedValue(id, (kind == "cat"), 'auxcounter', auxctr); | |
321 | ||
322 | if (kind != "cat") { | |
323 | setFeedValue(id, false, 'error', error); | |
324 | setFeedValue(id, false, 'updated', updated); | |
325 | ||
326 | if (id > 0) { | |
327 | if (has_img) { | |
328 | setFeedIcon(id, false, | |
329 | getInitParam("icons_url") + "/" + id + ".ico?" + has_img); | |
330 | } else { | |
331 | setFeedIcon(id, false, 'images/blank_icon.gif'); | |
332 | } | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | hideOrShowFeeds(getInitParam("hide_read_feeds") == 1); | |
338 | ||
339 | _counters_prev = elems; | |
340 | } | |
341 | ||
342 | function getFeedUnread(feed, is_cat) { | |
343 | try { | |
344 | var tree = dijit.byId("feedTree"); | |
345 | ||
346 | if (tree && tree.model) | |
347 | return tree.model.getFeedUnread(feed, is_cat); | |
348 | ||
349 | } catch (e) { | |
350 | // | |
351 | } | |
352 | ||
353 | return -1; | |
354 | } | |
355 | ||
356 | function getFeedCategory(feed) { | |
357 | try { | |
358 | var tree = dijit.byId("feedTree"); | |
359 | ||
360 | if (tree && tree.model) | |
361 | return tree.getFeedCategory(feed); | |
362 | ||
363 | } catch (e) { | |
364 | // | |
365 | } | |
366 | ||
367 | return false; | |
368 | } | |
369 | ||
370 | function hideOrShowFeeds(hide) { | |
371 | var tree = dijit.byId("feedTree"); | |
372 | ||
373 | if (tree) | |
374 | return tree.hideRead(hide, getInitParam("hide_read_shows_special")); | |
375 | } | |
376 | ||
377 | function getFeedName(feed, is_cat) { | |
378 | ||
379 | if (isNaN(feed)) return feed; // it's a tag | |
380 | ||
381 | var tree = dijit.byId("feedTree"); | |
382 | ||
383 | if (tree && tree.model) | |
384 | return tree.model.getFeedValue(feed, is_cat, 'name'); | |
385 | } | |
386 | ||
387 | function getFeedValue(feed, is_cat, key) { | |
388 | try { | |
389 | var tree = dijit.byId("feedTree"); | |
390 | ||
391 | if (tree && tree.model) | |
392 | return tree.model.getFeedValue(feed, is_cat, key); | |
393 | ||
394 | } catch (e) { | |
395 | // | |
396 | } | |
397 | return ''; | |
398 | } | |
399 | ||
400 | function setFeedUnread(feed, is_cat, unread) { | |
401 | var tree = dijit.byId("feedTree"); | |
402 | ||
403 | if (tree && tree.model) | |
404 | return tree.model.setFeedUnread(feed, is_cat, unread); | |
405 | } | |
406 | ||
407 | function setFeedValue(feed, is_cat, key, value) { | |
408 | try { | |
409 | var tree = dijit.byId("feedTree"); | |
410 | ||
411 | if (tree && tree.model) | |
412 | return tree.model.setFeedValue(feed, is_cat, key, value); | |
413 | ||
414 | } catch (e) { | |
415 | // | |
416 | } | |
417 | } | |
418 | ||
419 | function selectFeed(feed, is_cat) { | |
420 | var tree = dijit.byId("feedTree"); | |
421 | ||
422 | if (tree) return tree.selectFeed(feed, is_cat); | |
423 | } | |
424 | ||
425 | function setFeedIcon(feed, is_cat, src) { | |
426 | var tree = dijit.byId("feedTree"); | |
427 | ||
428 | if (tree) return tree.setFeedIcon(feed, is_cat, src); | |
429 | } | |
430 | ||
431 | function setFeedExpandoIcon(feed, is_cat, src) { | |
432 | var tree = dijit.byId("feedTree"); | |
433 | ||
434 | if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src); | |
435 | ||
436 | return false; | |
437 | } | |
438 | ||
439 | function getNextUnreadFeed(feed, is_cat) { | |
440 | var tree = dijit.byId("feedTree"); | |
441 | var nuf = tree.model.getNextUnreadFeed(feed, is_cat); | |
442 | ||
443 | if (nuf) | |
444 | return tree.model.store.getValue(nuf, 'bare_id'); | |
445 | } | |
446 | ||
447 | function catchupCurrentFeed(mode) { | |
448 | catchupFeed(getActiveFeedId(), activeFeedIsCat(), mode); | |
449 | } | |
450 | ||
451 | function catchupFeedInGroup(id) { | |
452 | var title = getFeedName(id); | |
453 | ||
454 | var str = __("Mark all articles in %s as read?").replace("%s", title); | |
455 | ||
456 | if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) { | |
457 | ||
458 | var rows = $$("#headlines-frame > div[id*=RROW][data-orig-feed-id='"+id+"']"); | |
459 | ||
460 | if (rows.length > 0) { | |
461 | ||
462 | rows.each(function (row) { | |
463 | row.removeClassName("Unread"); | |
464 | ||
465 | if (row.getAttribute("data-article-id") != getActiveArticleId()) { | |
466 | new Effect.Fade(row, {duration: 0.5}); | |
467 | } | |
468 | ||
469 | }); | |
470 | ||
471 | var feedTitles = $$("#headlines-frame > div[class='cdmFeedTitle']"); | |
472 | ||
473 | for (var i = 0; i < feedTitles.length; i++) { | |
474 | if (feedTitles[i].getAttribute("data-feed-id") == id) { | |
475 | ||
476 | if (i < feedTitles.length - 1) { | |
477 | new Effect.Fade(feedTitles[i], {duration: 0.5}); | |
478 | } | |
479 | ||
480 | break; | |
481 | } | |
482 | } | |
483 | ||
484 | updateFloatingTitle(true); | |
485 | } | |
486 | ||
487 | var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" + | |
488 | id + "&is_cat=false"; | |
489 | ||
490 | console.log(catchup_query); | |
491 | ||
492 | notify_progress("Loading, please wait...", true); | |
493 | ||
494 | new Ajax.Request("backend.php", { | |
495 | parameters: catchup_query, | |
496 | onComplete: function (transport) { | |
497 | handle_rpc_json(transport); | |
498 | } | |
499 | } ); | |
500 | ||
501 | //return viewCurrentFeed('MarkAllReadGR:' + id); | |
502 | } | |
503 | } | |
504 | ||
505 | function catchupFeed(feed, is_cat, mode) { | |
506 | if (is_cat == undefined) is_cat = false; | |
507 | ||
508 | var str = false; | |
509 | ||
510 | switch (mode) { | |
511 | case "1day": | |
512 | str = __("Mark %w in %s older than 1 day as read?"); | |
513 | break; | |
514 | case "1week": | |
515 | str = __("Mark %w in %s older than 1 week as read?"); | |
516 | break; | |
517 | case "2week": | |
518 | str = __("Mark %w in %s older than 2 weeks as read?"); | |
519 | break; | |
520 | default: | |
521 | str = __("Mark %w in %s as read?"); | |
522 | } | |
523 | ||
524 | var mark_what = last_search_query && last_search_query[0] ? __("search results") : __("all articles"); | |
525 | var fn = getFeedName(feed, is_cat); | |
526 | ||
527 | str = str.replace("%s", fn) | |
528 | .replace("%w", mark_what); | |
529 | ||
530 | if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) { | |
531 | return; | |
532 | } | |
533 | ||
534 | var catchup_query = {op: 'rpc', method: 'catchupFeed', feed_id: feed, | |
535 | is_cat: is_cat, mode: mode, search_query: last_search_query[0], | |
536 | search_lang: last_search_query[1]}; | |
537 | ||
538 | console.log(catchup_query); | |
539 | ||
540 | notify_progress("Loading, please wait...", true); | |
541 | ||
542 | new Ajax.Request("backend.php", { | |
543 | parameters: catchup_query, | |
544 | onComplete: function(transport) { | |
545 | handle_rpc_json(transport); | |
546 | ||
547 | var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1"; | |
548 | ||
549 | if (show_next_feed) { | |
550 | var nuf = getNextUnreadFeed(feed, is_cat); | |
551 | ||
552 | if (nuf) { | |
553 | viewfeed({feed: nuf, is_cat: is_cat}); | |
554 | } | |
555 | } else { | |
556 | if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) { | |
557 | viewCurrentFeed(); | |
558 | } | |
559 | } | |
560 | ||
561 | notify(""); | |
562 | } }); | |
563 | ||
564 | } | |
565 | ||
566 | function decrementFeedCounter(feed, is_cat) { | |
567 | var ctr = getFeedUnread(feed, is_cat); | |
568 | ||
569 | if (ctr > 0) { | |
570 | setFeedUnread(feed, is_cat, ctr - 1); | |
571 | global_unread = global_unread - 1; | |
572 | updateTitle(); | |
573 | ||
574 | if (!is_cat) { | |
575 | var cat = parseInt(getFeedCategory(feed)); | |
576 | ||
577 | if (!isNaN(cat)) { | |
578 | ctr = getFeedUnread(cat, true); | |
579 | ||
580 | if (ctr > 0) { | |
581 | setFeedUnread(cat, true, ctr - 1); | |
582 | } | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
587 | } | |
588 | ||
589 |