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