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