]>
Commit | Line | Data |
---|---|---|
1 | /* global dijit, __ */ | |
2 | ||
3 | let _active_article_id = 0; | |
4 | ||
5 | let vgroup_last_feed = false; | |
6 | let post_under_pointer = false; | |
7 | ||
8 | let last_requested_article = 0; | |
9 | ||
10 | let catchup_id_batch = []; | |
11 | //let catchup_timeout_id = false; | |
12 | ||
13 | //let cids_requested = []; | |
14 | let loaded_article_ids = []; | |
15 | let _last_headlines_update = 0; | |
16 | let _headlines_scroll_offset = 0; | |
17 | let current_first_id = 0; | |
18 | let last_search_query; | |
19 | ||
20 | let _catchup_request_sent = false; | |
21 | ||
22 | let has_storage = 'sessionStorage' in window && window['sessionStorage'] !== null; | |
23 | ||
24 | function headlines_callback2(transport, offset, background, infscroll_req) { | |
25 | const reply = handle_rpc_json(transport); | |
26 | ||
27 | console.log("headlines_callback2 [offset=" + offset + "] B:" + background + " I:" + infscroll_req); | |
28 | ||
29 | if (background) | |
30 | return; | |
31 | ||
32 | let is_cat = false; | |
33 | let feed_id = false; | |
34 | ||
35 | if (reply) { | |
36 | ||
37 | is_cat = reply['headlines']['is_cat']; | |
38 | feed_id = reply['headlines']['id']; | |
39 | last_search_query = reply['headlines']['search_query']; | |
40 | ||
41 | if (feed_id != -7 && (feed_id != getActiveFeedId() || is_cat != activeFeedIsCat())) | |
42 | return; | |
43 | ||
44 | try { | |
45 | if (infscroll_req == false) { | |
46 | $("headlines-frame").scrollTop = 0; | |
47 | ||
48 | $("floatingTitle").style.visibility = "hidden"; | |
49 | $("floatingTitle").setAttribute("data-article-id", 0); | |
50 | $("floatingTitle").innerHTML = ""; | |
51 | } | |
52 | } catch (e) { } | |
53 | ||
54 | $("headlines-frame").removeClassName("cdm"); | |
55 | $("headlines-frame").removeClassName("normal"); | |
56 | ||
57 | $("headlines-frame").addClassName(isCdmMode() ? "cdm" : "normal"); | |
58 | ||
59 | const headlines_count = reply['headlines-info']['count']; | |
60 | ||
61 | vgroup_last_feed = reply['headlines-info']['vgroup_last_feed']; | |
62 | ||
63 | if (parseInt(headlines_count) < 30) { | |
64 | _infscroll_disable = 1; | |
65 | } else { | |
66 | _infscroll_disable = 0; | |
67 | } | |
68 | ||
69 | current_first_id = reply['headlines']['first_id']; | |
70 | const counters = reply['counters']; | |
71 | const articles = reply['articles']; | |
72 | ||
73 | if (infscroll_req == false) { | |
74 | loaded_article_ids = []; | |
75 | ||
76 | dojo.html.set($("headlines-toolbar"), | |
77 | reply['headlines']['toolbar'], | |
78 | {parseContent: true}); | |
79 | ||
80 | $("headlines-frame").innerHTML = ''; | |
81 | ||
82 | let tmp = document.createElement("div"); | |
83 | tmp.innerHTML = reply['headlines']['content']; | |
84 | dojo.parser.parse(tmp); | |
85 | ||
86 | while (tmp.hasChildNodes()) { | |
87 | var row = tmp.removeChild(tmp.firstChild); | |
88 | ||
89 | if (loaded_article_ids.indexOf(row.id) == -1 || row.hasClassName("feed-title")) { | |
90 | dijit.byId("headlines-frame").domNode.appendChild(row); | |
91 | ||
92 | loaded_article_ids.push(row.id); | |
93 | } | |
94 | } | |
95 | ||
96 | let hsp = $("headlines-spacer"); | |
97 | if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"}); | |
98 | dijit.byId('headlines-frame').domNode.appendChild(hsp); | |
99 | ||
100 | initHeadlinesMenu(); | |
101 | ||
102 | if (_infscroll_disable) | |
103 | hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" + | |
104 | __("Click to open next unread feed.") + "</a>"; | |
105 | ||
106 | if (_search_query) { | |
107 | $("feed_title").innerHTML += "<span id='cancel_search'>" + | |
108 | " (<a href='#' onclick='cancelSearch()'>" + __("Cancel search") + "</a>)" + | |
109 | "</span>"; | |
110 | } | |
111 | ||
112 | } else if (headlines_count > 0 && feed_id == getActiveFeedId() && is_cat == activeFeedIsCat()) { | |
113 | console.log("adding some more headlines: " + headlines_count); | |
114 | ||
115 | const c = dijit.byId("headlines-frame"); | |
116 | //const ids = getSelectedArticleIds2(); | |
117 | ||
118 | let hsp = $("headlines-spacer"); | |
119 | ||
120 | if (hsp) | |
121 | c.domNode.removeChild(hsp); | |
122 | ||
123 | let tmp = document.createElement("div"); | |
124 | tmp.innerHTML = reply['headlines']['content']; | |
125 | dojo.parser.parse(tmp); | |
126 | ||
127 | while (tmp.hasChildNodes()) { | |
128 | let row = tmp.removeChild(tmp.firstChild); | |
129 | ||
130 | if (loaded_article_ids.indexOf(row.id) == -1 || row.hasClassName("feed-title")) { | |
131 | dijit.byId("headlines-frame").domNode.appendChild(row); | |
132 | ||
133 | loaded_article_ids.push(row.id); | |
134 | } | |
135 | } | |
136 | ||
137 | if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"}); | |
138 | c.domNode.appendChild(hsp); | |
139 | ||
140 | if (headlines_count < 30) _infscroll_disable = true; | |
141 | ||
142 | /* console.log("restore selected ids: " + ids); | |
143 | ||
144 | for (let i = 0; i < ids.length; i++) { | |
145 | markHeadline(ids[i]); | |
146 | } */ | |
147 | ||
148 | initHeadlinesMenu(); | |
149 | ||
150 | if (_infscroll_disable) { | |
151 | hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" + | |
152 | __("Click to open next unread feed.") + "</a>"; | |
153 | } | |
154 | ||
155 | } else { | |
156 | console.log("no new headlines received"); | |
157 | ||
158 | const first_id_changed = reply['headlines']['first_id_changed']; | |
159 | console.log("first id changed:" + first_id_changed); | |
160 | ||
161 | let hsp = $("headlines-spacer"); | |
162 | ||
163 | if (hsp) { | |
164 | if (first_id_changed) { | |
165 | hsp.innerHTML = "<a href='#' onclick='viewCurrentFeed()'>" + | |
166 | __("New articles found, reload feed to continue.") + "</a>"; | |
167 | } else { | |
168 | hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" + | |
169 | __("Click to open next unread feed.") + "</a>"; | |
170 | } | |
171 | ||
172 | } | |
173 | ||
174 | } | |
175 | ||
176 | } else { | |
177 | console.error("Invalid object received: " + transport.responseText); | |
178 | dijit.byId("headlines-frame").attr('content', "<div class='whiteBox'>" + | |
179 | __('Could not update headlines (invalid object received - see error console for details)') + | |
180 | "</div>"); | |
181 | } | |
182 | ||
183 | _infscroll_request_sent = 0; | |
184 | _last_headlines_update = new Date().getTime(); | |
185 | ||
186 | unpackVisibleHeadlines(); | |
187 | ||
188 | // if we have some more space in the buffer, why not try to fill it | |
189 | ||
190 | if (!_infscroll_disable && $("headlines-spacer") && | |
191 | $("headlines-spacer").offsetTop < $("headlines-frame").offsetHeight) { | |
192 | ||
193 | window.setTimeout(function() { | |
194 | loadMoreHeadlines(); | |
195 | }, 250); | |
196 | } | |
197 | ||
198 | notify(""); | |
199 | } | |
200 | ||
201 | function render_article(article) { | |
202 | cleanup_memory("content-insert"); | |
203 | ||
204 | dijit.byId("headlines-wrap-inner").addChild( | |
205 | dijit.byId("content-insert")); | |
206 | ||
207 | const c = dijit.byId("content-insert"); | |
208 | ||
209 | try { | |
210 | c.domNode.scrollTop = 0; | |
211 | } catch (e) { } | |
212 | ||
213 | c.attr('content', article); | |
214 | PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED, c.domNode); | |
215 | ||
216 | correctHeadlinesOffset(getActiveArticleId()); | |
217 | ||
218 | try { | |
219 | c.focus(); | |
220 | } catch (e) { } | |
221 | } | |
222 | ||
223 | /* | |
224 | function showArticleInHeadlines(id, noexpand) { | |
225 | const row = $("RROW-" + id); | |
226 | if (!row) return; | |
227 | ||
228 | if (!noexpand) | |
229 | row.removeClassName("Unread"); | |
230 | ||
231 | row.addClassName("active"); | |
232 | ||
233 | selectArticles('none'); | |
234 | ||
235 | markHeadline(id); | |
236 | } | |
237 | ||
238 | function article_callback2(transport, id) { | |
239 | console.log("article_callback2 " + id); | |
240 | ||
241 | const reply = handle_rpc_json(transport); | |
242 | ||
243 | if (reply) { | |
244 | ||
245 | reply.each(function(article) { | |
246 | if (getActiveArticleId() == article['id']) { | |
247 | render_article(article['content']); | |
248 | } | |
249 | cids_requested.remove(article['id']); | |
250 | ||
251 | cache_set("article:" + article['id'], article['content']); | |
252 | }); | |
253 | ||
254 | } else { | |
255 | console.error("Invalid object received: " + transport.responseText); | |
256 | ||
257 | render_article("<div class='whiteBox'>" + | |
258 | __('Could not display article (invalid object received - see error console for details)') + "</div>"); | |
259 | } | |
260 | ||
261 | const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length; | |
262 | request_counters(unread_in_buffer == 0); | |
263 | ||
264 | notify(""); | |
265 | } | |
266 | */ | |
267 | function view(id, noexpand) { | |
268 | setActiveArticleId(id); | |
269 | ||
270 | if (!noexpand) { | |
271 | console.log("loading article", id); | |
272 | ||
273 | const neighbor_ids = getRelativePostIds(id); | |
274 | const cids = []; | |
275 | ||
276 | /* only request uncached articles */ | |
277 | ||
278 | neighbor_ids.each((n) => { | |
279 | if (!cache_get("article:" + n)) | |
280 | cids.push(n); | |
281 | }); | |
282 | ||
283 | const cached_article = cache_get("article:" + id); | |
284 | ||
285 | if (cached_article) { | |
286 | console.log('rendering cached', id); | |
287 | render_article(cached_article); | |
288 | return false; | |
289 | } | |
290 | ||
291 | xhrPost("backend.php", {op: "article", method: "view", id: id, cids: cids.toString()}, (transport) => { | |
292 | try { | |
293 | const reply = handle_rpc_json(transport); | |
294 | ||
295 | if (reply) { | |
296 | ||
297 | reply.each(function(article) { | |
298 | if (getActiveArticleId() == article['id']) { | |
299 | render_article(article['content']); | |
300 | } | |
301 | //cids_requested.remove(article['id']); | |
302 | ||
303 | cache_set("article:" + article['id'], article['content']); | |
304 | }); | |
305 | ||
306 | } else { | |
307 | console.error("Invalid object received: " + transport.responseText); | |
308 | ||
309 | render_article("<div class='whiteBox'>" + | |
310 | __('Could not display article (invalid object received - see error console for details)') + "</div>"); | |
311 | } | |
312 | ||
313 | //const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length; | |
314 | //request_counters(unread_in_buffer == 0); | |
315 | ||
316 | notify(""); | |
317 | ||
318 | } catch (e) { | |
319 | exception_error(e); | |
320 | } | |
321 | }) | |
322 | } | |
323 | ||
324 | return false; | |
325 | ||
326 | /* const oldrow = $("RROW-" + getActiveArticleId()); | |
327 | if (oldrow) oldrow.removeClassName("active"); | |
328 | ||
329 | const crow = $("RROW-" + id); | |
330 | ||
331 | if (!crow) return; | |
332 | if (noexpand) { | |
333 | setActiveArticleId(id); | |
334 | showArticleInHeadlines(id, noexpand); | |
335 | return; | |
336 | } | |
337 | ||
338 | console.log("loading article: " + id); | |
339 | ||
340 | const cached_article = cache_get("article:" + id); | |
341 | ||
342 | console.log("cache check result: " + (cached_article != false)); | |
343 | ||
344 | const query = {op: "article", method: "view", id: id}; | |
345 | ||
346 | const neighbor_ids = getRelativePostIds(id); | |
347 | ||
348 | /* only request uncached articles */ | |
349 | ||
350 | /* const cids_to_request = []; | |
351 | ||
352 | for (let i = 0; i < neighbor_ids.length; i++) { | |
353 | if (cids_requested.indexOf(neighbor_ids[i]) == -1) | |
354 | if (!cache_get("article:" + neighbor_ids[i])) { | |
355 | cids_to_request.push(neighbor_ids[i]); | |
356 | cids_requested.push(neighbor_ids[i]); | |
357 | } | |
358 | } | |
359 | ||
360 | console.log("additional ids: " + cids_to_request.toString()); | |
361 | ||
362 | query.cids = cids_to_request.toString(); | |
363 | ||
364 | const article_is_unread = crow.hasClassName("Unread"); | |
365 | ||
366 | setActiveArticleId(id); | |
367 | showArticleInHeadlines(id); | |
368 | ||
369 | if (cached_article && article_is_unread) { | |
370 | query.mode = "prefetch"; | |
371 | render_article(cached_article); | |
372 | } else if (cached_article) { | |
373 | query.mode = "prefetch_old"; | |
374 | render_article(cached_article); | |
375 | ||
376 | // if we don't need to request any relative ids, we might as well skip | |
377 | // the server roundtrip altogether | |
378 | if (cids_to_request.length == 0) { | |
379 | return; | |
380 | } | |
381 | } | |
382 | ||
383 | last_requested_article = id; | |
384 | ||
385 | console.log(query); | |
386 | ||
387 | if (article_is_unread) { | |
388 | decrementFeedCounter(getActiveFeedId(), activeFeedIsCat()); | |
389 | } | |
390 | ||
391 | xhrPost("backend.php", query, (transport) => { | |
392 | article_callback2(transport, id); | |
393 | }) | |
394 | ||
395 | return false; | |
396 | */ | |
397 | } | |
398 | ||
399 | function toggleMark(id, client_only) { | |
400 | const query = { op: "rpc", id: id, method: "mark" }; | |
401 | ||
402 | const row = $("RROW-" + id); | |
403 | if (!row) return; | |
404 | ||
405 | const imgs = []; | |
406 | ||
407 | const row_imgs = row.getElementsByClassName("markedPic"); | |
408 | ||
409 | for (let i = 0; i < row_imgs.length; i++) | |
410 | imgs.push(row_imgs[i]); | |
411 | ||
412 | const ft = $("floatingTitle"); | |
413 | ||
414 | if (ft && ft.getAttribute("data-article-id") == id) { | |
415 | const fte = ft.getElementsByClassName("markedPic"); | |
416 | ||
417 | for (var i = 0; i < fte.length; i++) | |
418 | imgs.push(fte[i]); | |
419 | } | |
420 | ||
421 | for (i = 0; i < imgs.length; i++) { | |
422 | const img = imgs[i]; | |
423 | ||
424 | if (!row.hasClassName("marked")) { | |
425 | img.src = img.src.replace("mark_unset", "mark_set"); | |
426 | query.mark = 1; | |
427 | } else { | |
428 | img.src = img.src.replace("mark_set", "mark_unset"); | |
429 | query.mark = 0; | |
430 | } | |
431 | } | |
432 | ||
433 | row.toggleClassName("marked"); | |
434 | ||
435 | if (!client_only) | |
436 | xhrPost("backend.php", query, (transport) => { | |
437 | handle_rpc_json(transport); | |
438 | }); | |
439 | } | |
440 | ||
441 | function togglePub(id, client_only, no_effects, note) { | |
442 | const query = { op: "rpc", id: id, method: "publ" }; | |
443 | ||
444 | if (note != undefined) { | |
445 | query.note = note; | |
446 | } else { | |
447 | query.note = "undefined"; | |
448 | } | |
449 | ||
450 | const row = $("RROW-" + id); | |
451 | if (!row) return; | |
452 | ||
453 | const imgs = []; | |
454 | ||
455 | const row_imgs = row.getElementsByClassName("pubPic"); | |
456 | ||
457 | for (let i = 0; i < row_imgs.length; i++) | |
458 | imgs.push(row_imgs[i]); | |
459 | ||
460 | const ft = $("floatingTitle"); | |
461 | ||
462 | if (ft && ft.getAttribute("data-article-id") == id) { | |
463 | const fte = ft.getElementsByClassName("pubPic"); | |
464 | ||
465 | for (let i = 0; i < fte.length; i++) | |
466 | imgs.push(fte[i]); | |
467 | } | |
468 | ||
469 | for (let i = 0; i < imgs.length; i++) { | |
470 | const img = imgs[i]; | |
471 | ||
472 | if (!row.hasClassName("published") || note != undefined) { | |
473 | img.src = img.src.replace("pub_unset", "pub_set"); | |
474 | query.pub = 1; | |
475 | } else { | |
476 | img.src = img.src.replace("pub_set", "pub_unset"); | |
477 | query.pub = 0; | |
478 | } | |
479 | } | |
480 | ||
481 | if (note != undefined) | |
482 | row.addClassName("published"); | |
483 | else | |
484 | row.toggleClassName("published"); | |
485 | ||
486 | if (!client_only) | |
487 | xhrPost("backend.php", query, (transport) => { | |
488 | handle_rpc_json(transport); | |
489 | }); | |
490 | } | |
491 | ||
492 | function moveToPost(mode, noscroll, noexpand) { | |
493 | const rows = getLoadedArticleIds(); | |
494 | ||
495 | let prev_id = false; | |
496 | let next_id = false; | |
497 | ||
498 | if (!$('RROW-' + getActiveArticleId())) { | |
499 | setActiveArticleId(0); | |
500 | } | |
501 | ||
502 | if (!getActiveArticleId()) { | |
503 | next_id = rows[0]; | |
504 | prev_id = rows[rows.length-1] | |
505 | } else { | |
506 | for (let i = 0; i < rows.length; i++) { | |
507 | if (rows[i] == getActiveArticleId()) { | |
508 | ||
509 | // Account for adjacent identical article ids. | |
510 | if (i > 0) prev_id = rows[i-1]; | |
511 | ||
512 | for (let j = i+1; j < rows.length; j++) { | |
513 | if (rows[j] != getActiveArticleId()) { | |
514 | next_id = rows[j]; | |
515 | break; | |
516 | } | |
517 | } | |
518 | break; | |
519 | } | |
520 | } | |
521 | } | |
522 | ||
523 | console.log("cur: " + getActiveArticleId() + " next: " + next_id); | |
524 | ||
525 | if (mode == "next") { | |
526 | if (next_id || getActiveArticleId()) { | |
527 | if (isCdmMode()) { | |
528 | ||
529 | const article = $("RROW-" + getActiveArticleId()); | |
530 | const ctr = $("headlines-frame"); | |
531 | ||
532 | if (!noscroll && article && article.offsetTop + article.offsetHeight > | |
533 | ctr.scrollTop + ctr.offsetHeight) { | |
534 | ||
535 | scrollArticle(ctr.offsetHeight/4); | |
536 | ||
537 | } else if (next_id) { | |
538 | cdmScrollToArticleId(next_id, true); | |
539 | setActiveArticleId(next_id); | |
540 | } | |
541 | ||
542 | } else if (next_id) { | |
543 | correctHeadlinesOffset(next_id); | |
544 | view(next_id, noexpand); | |
545 | } | |
546 | } | |
547 | } | |
548 | ||
549 | if (mode == "prev") { | |
550 | if (prev_id || getActiveArticleId()) { | |
551 | if (isCdmMode()) { | |
552 | ||
553 | const article = $("RROW-" + getActiveArticleId()); | |
554 | const prev_article = $("RROW-" + prev_id); | |
555 | const ctr = $("headlines-frame"); | |
556 | ||
557 | if (!noscroll && article && article.offsetTop < ctr.scrollTop) { | |
558 | scrollArticle(-ctr.offsetHeight/3); | |
559 | } else if (!noscroll && prev_article && | |
560 | prev_article.offsetTop < ctr.scrollTop) { | |
561 | scrollArticle(-ctr.offsetHeight/4); | |
562 | } else if (prev_id) { | |
563 | cdmScrollToArticleId(prev_id, noscroll); | |
564 | setActiveArticleId(prev_id); | |
565 | } | |
566 | ||
567 | } else if (prev_id) { | |
568 | correctHeadlinesOffset(prev_id); | |
569 | view(prev_id, noexpand); | |
570 | } | |
571 | } | |
572 | } | |
573 | ||
574 | } | |
575 | ||
576 | /* function toggleSelected(id, force_on) { | |
577 | const row = $("RROW-" + id); | |
578 | ||
579 | if (row) { | |
580 | const cb = dijit.getEnclosingWidget( | |
581 | row.getElementsByClassName("rchk")[0]); | |
582 | ||
583 | if (row.hasClassName('Selected') && !force_on) { | |
584 | row.removeClassName('Selected'); | |
585 | if (cb) cb.attr("checked", false); | |
586 | } else { | |
587 | row.addClassName('Selected'); | |
588 | if (cb) cb.attr("checked", true); | |
589 | } | |
590 | } | |
591 | ||
592 | updateSelectedPrompt(); | |
593 | } */ | |
594 | ||
595 | function updateSelectedPrompt() { | |
596 | const count = getSelectedArticleIds2().length; | |
597 | const elem = $("selected_prompt"); | |
598 | ||
599 | if (elem) { | |
600 | elem.innerHTML = ngettext("%d article selected", | |
601 | "%d articles selected", count).replace("%d", count); | |
602 | ||
603 | if (count > 0) | |
604 | Element.show(elem); | |
605 | else | |
606 | Element.hide(elem); | |
607 | } | |
608 | ||
609 | } | |
610 | ||
611 | function toggleUnread(id, cmode) { | |
612 | const row = $("RROW-" + id); | |
613 | ||
614 | if (row) { | |
615 | const tmpClassName = row.className; | |
616 | ||
617 | if (cmode == undefined || cmode == 2) { | |
618 | if (row.hasClassName("Unread")) { | |
619 | row.removeClassName("Unread"); | |
620 | ||
621 | } else { | |
622 | row.addClassName("Unread"); | |
623 | } | |
624 | ||
625 | } else if (cmode == 0) { | |
626 | row.removeClassName("Unread"); | |
627 | } else if (cmode == 1) { | |
628 | row.addClassName("Unread"); | |
629 | } | |
630 | ||
631 | if (tmpClassName != row.className) { | |
632 | if (cmode == undefined) cmode = 2; | |
633 | ||
634 | const query = {op: "rpc", method: "catchupSelected", | |
635 | cmode: cmode, ids: id}; | |
636 | ||
637 | xhrPost("backend.php", query, (transport) => { | |
638 | handle_rpc_json(transport); | |
639 | }); | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
644 | function selectionRemoveLabel(id, ids) { | |
645 | if (!ids) ids = getSelectedArticleIds2(); | |
646 | ||
647 | if (ids.length == 0) { | |
648 | alert(__("No articles are selected.")); | |
649 | return; | |
650 | } | |
651 | ||
652 | const query = { op: "article", method: "removeFromLabel", | |
653 | ids: ids.toString(), lid: id }; | |
654 | ||
655 | xhrPost("backend.php", query, (transport) => { | |
656 | handle_rpc_json(transport); | |
657 | show_labels_in_headlines(transport); | |
658 | }); | |
659 | } | |
660 | ||
661 | function selectionAssignLabel(id, ids) { | |
662 | if (!ids) ids = getSelectedArticleIds2(); | |
663 | ||
664 | if (ids.length == 0) { | |
665 | alert(__("No articles are selected.")); | |
666 | return; | |
667 | } | |
668 | ||
669 | const query = { op: "article", method: "assignToLabel", | |
670 | ids: ids.toString(), lid: id }; | |
671 | ||
672 | xhrPost("backend.php", query, (transport) => { | |
673 | handle_rpc_json(transport); | |
674 | show_labels_in_headlines(transport); | |
675 | }); | |
676 | } | |
677 | ||
678 | function selectionToggleUnread(set_state, callback, no_error, ids) { | |
679 | const rows = ids ? ids : getSelectedArticleIds2(); | |
680 | ||
681 | if (rows.length == 0 && !no_error) { | |
682 | alert(__("No articles are selected.")); | |
683 | return; | |
684 | } | |
685 | ||
686 | for (let i = 0; i < rows.length; i++) { | |
687 | const row = $("RROW-" + rows[i]); | |
688 | if (row) { | |
689 | if (set_state == undefined) { | |
690 | if (row.hasClassName("Unread")) { | |
691 | row.removeClassName("Unread"); | |
692 | } else { | |
693 | row.addClassName("Unread"); | |
694 | } | |
695 | } | |
696 | ||
697 | if (set_state == false) { | |
698 | row.removeClassName("Unread"); | |
699 | } | |
700 | ||
701 | if (set_state == true) { | |
702 | row.addClassName("Unread"); | |
703 | } | |
704 | } | |
705 | } | |
706 | ||
707 | updateFloatingTitle(true); | |
708 | ||
709 | if (rows.length > 0) { | |
710 | ||
711 | let cmode = ""; | |
712 | ||
713 | if (set_state == undefined) { | |
714 | cmode = "2"; | |
715 | } else if (set_state == true) { | |
716 | cmode = "1"; | |
717 | } else if (set_state == false) { | |
718 | cmode = "0"; | |
719 | } | |
720 | ||
721 | const query = {op: "rpc", method: "catchupSelected", | |
722 | cmode: cmode, ids: rows.toString() }; | |
723 | ||
724 | notify_progress("Loading, please wait..."); | |
725 | ||
726 | xhrPost("backend.php", query, (transport) => { | |
727 | handle_rpc_json(transport); | |
728 | if (callback) callback(transport); | |
729 | }); | |
730 | ||
731 | } | |
732 | } | |
733 | ||
734 | // sel_state ignored | |
735 | function selectionToggleMarked(sel_state, callback, no_error, ids) { | |
736 | const rows = ids ? ids : getSelectedArticleIds2(); | |
737 | ||
738 | if (rows.length == 0 && !no_error) { | |
739 | alert(__("No articles are selected.")); | |
740 | return; | |
741 | } | |
742 | ||
743 | for (let i = 0; i < rows.length; i++) { | |
744 | toggleMark(rows[i], true, true); | |
745 | } | |
746 | ||
747 | if (rows.length > 0) { | |
748 | const query = { op: "rpc", method: "markSelected", | |
749 | ids: rows.toString(), cmode: 2 }; | |
750 | ||
751 | xhrPost("backend.php", query, (transport) => { | |
752 | handle_rpc_json(transport); | |
753 | if (callback) callback(transport); | |
754 | }); | |
755 | } | |
756 | } | |
757 | ||
758 | // sel_state ignored | |
759 | function selectionTogglePublished(sel_state, callback, no_error, ids) { | |
760 | const rows = ids ? ids : getSelectedArticleIds2(); | |
761 | ||
762 | if (rows.length == 0 && !no_error) { | |
763 | alert(__("No articles are selected.")); | |
764 | return; | |
765 | } | |
766 | ||
767 | for (let i = 0; i < rows.length; i++) { | |
768 | togglePub(rows[i], true, true); | |
769 | } | |
770 | ||
771 | if (rows.length > 0) { | |
772 | const query = { op: "rpc", method: "publishSelected", | |
773 | ids: rows.toString(), cmode: 2 }; | |
774 | ||
775 | xhrPost("backend.php", query, (transport) => { | |
776 | handle_rpc_json(transport); | |
777 | if (callback) callback(transport); | |
778 | }); | |
779 | } | |
780 | } | |
781 | ||
782 | function getSelectedArticleIds2() { | |
783 | ||
784 | const rv = []; | |
785 | ||
786 | $$("#headlines-frame > div[id*=RROW][class*=Selected]").each( | |
787 | function(child) { | |
788 | rv.push(child.getAttribute("data-article-id")); | |
789 | }); | |
790 | ||
791 | // i wonder if this is a good idea: consider active article a honorary member | |
792 | // of selected articles | |
793 | if (getActiveArticleId()) | |
794 | rv.push(getActiveArticleId()); | |
795 | ||
796 | return rv; | |
797 | } | |
798 | ||
799 | function getLoadedArticleIds() { | |
800 | const rv = []; | |
801 | ||
802 | const children = $$("#headlines-frame > div[id*=RROW-]"); | |
803 | ||
804 | children.each(function(child) { | |
805 | if (Element.visible(child)) { | |
806 | rv.push(child.getAttribute("data-article-id")); | |
807 | } | |
808 | }); | |
809 | ||
810 | return rv; | |
811 | ||
812 | } | |
813 | ||
814 | // mode = all,none,unread,invert,marked,published | |
815 | function selectArticles(mode) { | |
816 | let query = "#headlines-frame > div[id*=RROW]"; | |
817 | ||
818 | switch (mode) { | |
819 | case "none": | |
820 | case "all": | |
821 | case "invert": | |
822 | break; | |
823 | case "marked": | |
824 | query += "[class*=marked]"; | |
825 | break; | |
826 | case "published": | |
827 | query += "[class*=published]"; | |
828 | break; | |
829 | case "unread": | |
830 | query += "[class*=Unread]"; | |
831 | break; | |
832 | default: | |
833 | console.warn("selectArticles: unknown mode", mode); | |
834 | } | |
835 | ||
836 | const rows = $$(query); | |
837 | ||
838 | for (let i = 0; i < rows.length; i++) { | |
839 | const row = rows[i]; | |
840 | const cb = dijit.getEnclosingWidget(row.select(".rchk")[0]); | |
841 | ||
842 | switch (mode) { | |
843 | case "none": | |
844 | row.removeClassName("Selected"); | |
845 | ||
846 | if (!row.hasClassName("active")) | |
847 | cb.attr("checked", false); | |
848 | break; | |
849 | case "invert": | |
850 | if (row.hasClassName("Selected")) { | |
851 | row.removeClassName("Selected"); | |
852 | ||
853 | if (!row.hasClassName("active")) | |
854 | cb.attr("checked", false); | |
855 | } else { | |
856 | row.addClassName("Selected"); | |
857 | cb.attr("checked", true); | |
858 | } | |
859 | break; | |
860 | default: | |
861 | row.addClassName("Selected"); | |
862 | cb.attr("checked", true); | |
863 | } | |
864 | ||
865 | updateSelectedPrompt(); | |
866 | } | |
867 | } | |
868 | ||
869 | // mode = all,none,unread,invert,marked,published | |
870 | /* function selectArticles(mode, query) { | |
871 | if (!query) query = "#headlines-frame > div[id*=RROW]"; | |
872 | ||
873 | const children = $$(query); | |
874 | ||
875 | children.each(function(child) { | |
876 | //const id = child.getAttribute("data-article-id"); | |
877 | ||
878 | const cb = dijit.getEnclosingWidget( | |
879 | child.getElementsByClassName("rchk")[0]); | |
880 | ||
881 | if (mode == "all") { | |
882 | child.addClassName("Selected"); | |
883 | if (cb) cb.attr("checked", true); | |
884 | } else if (mode == "unread") { | |
885 | if (child.hasClassName("Unread")) { | |
886 | child.addClassName("Selected"); | |
887 | if (cb) cb.attr("checked", true); | |
888 | } else { | |
889 | child.removeClassName("Selected"); | |
890 | if (cb) cb.attr("checked", false); | |
891 | } | |
892 | } else if (mode == "marked") { | |
893 | if (child.hasClassName("marked")) { | |
894 | child.addClassName("Selected"); | |
895 | if (cb) cb.attr("checked", true); | |
896 | } else { | |
897 | child.removeClassName("Selected"); | |
898 | if (cb) cb.attr("checked", false); | |
899 | } | |
900 | } else if (mode == "published") { | |
901 | if (child.hasClassName("published")) { | |
902 | child.addClassName("Selected"); | |
903 | if (cb) cb.attr("checked", true); | |
904 | } else { | |
905 | child.removeClassName("Selected"); | |
906 | if (cb) cb.attr("checked", false); | |
907 | } | |
908 | ||
909 | } else if (mode == "invert") { | |
910 | if (child.hasClassName("Selected")) { | |
911 | child.removeClassName("Selected"); | |
912 | if (cb) cb.attr("checked", false); | |
913 | } else { | |
914 | child.addClassName("Selected"); | |
915 | if (cb) cb.attr("checked", true); | |
916 | } | |
917 | ||
918 | } else { | |
919 | child.removeClassName("Selected"); | |
920 | if (cb) cb.attr("checked", false); | |
921 | } | |
922 | }); | |
923 | ||
924 | updateSelectedPrompt(); | |
925 | } */ | |
926 | ||
927 | function deleteSelection() { | |
928 | ||
929 | const rows = getSelectedArticleIds2(); | |
930 | ||
931 | if (rows.length == 0) { | |
932 | alert(__("No articles are selected.")); | |
933 | return; | |
934 | } | |
935 | ||
936 | const fn = getFeedName(getActiveFeedId(), activeFeedIsCat()); | |
937 | let str; | |
938 | ||
939 | if (getActiveFeedId() != 0) { | |
940 | str = ngettext("Delete %d selected article in %s?", "Delete %d selected articles in %s?", rows.length); | |
941 | } else { | |
942 | str = ngettext("Delete %d selected article?", "Delete %d selected articles?", rows.length); | |
943 | } | |
944 | ||
945 | str = str.replace("%d", rows.length); | |
946 | str = str.replace("%s", fn); | |
947 | ||
948 | if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) { | |
949 | return; | |
950 | } | |
951 | ||
952 | const query = { op: "rpc", method: "delete", ids: rows.toString() }; | |
953 | ||
954 | xhrPost("backend.php", query, (transport) => { | |
955 | handle_rpc_json(transport); | |
956 | viewCurrentFeed(); | |
957 | }); | |
958 | } | |
959 | ||
960 | function archiveSelection() { | |
961 | ||
962 | const rows = getSelectedArticleIds2(); | |
963 | ||
964 | if (rows.length == 0) { | |
965 | alert(__("No articles are selected.")); | |
966 | return; | |
967 | } | |
968 | ||
969 | const fn = getFeedName(getActiveFeedId(), activeFeedIsCat()); | |
970 | let str; | |
971 | let op; | |
972 | ||
973 | if (getActiveFeedId() != 0) { | |
974 | str = ngettext("Archive %d selected article in %s?", "Archive %d selected articles in %s?", rows.length); | |
975 | op = "archive"; | |
976 | } else { | |
977 | str = ngettext("Move %d archived article back?", "Move %d archived articles back?", rows.length); | |
978 | ||
979 | str += " " + __("Please note that unstarred articles might get purged on next feed update."); | |
980 | ||
981 | op = "unarchive"; | |
982 | } | |
983 | ||
984 | str = str.replace("%d", rows.length); | |
985 | str = str.replace("%s", fn); | |
986 | ||
987 | if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) { | |
988 | return; | |
989 | } | |
990 | ||
991 | for (let i = 0; i < rows.length; i++) { | |
992 | cache_delete("article:" + rows[i]); | |
993 | } | |
994 | ||
995 | const query = {op: "rpc", method: op, ids: rows.toString()}; | |
996 | ||
997 | xhrPost("backend.php", query, (transport) => { | |
998 | handle_rpc_json(transport); | |
999 | viewCurrentFeed(); | |
1000 | }); | |
1001 | } | |
1002 | ||
1003 | function catchupSelection() { | |
1004 | ||
1005 | const rows = getSelectedArticleIds2(); | |
1006 | ||
1007 | if (rows.length == 0) { | |
1008 | alert(__("No articles are selected.")); | |
1009 | return; | |
1010 | } | |
1011 | ||
1012 | const fn = getFeedName(getActiveFeedId(), activeFeedIsCat()); | |
1013 | ||
1014 | let str = ngettext("Mark %d selected article in %s as read?", "Mark %d selected articles in %s as read?", rows.length); | |
1015 | ||
1016 | str = str.replace("%d", rows.length); | |
1017 | str = str.replace("%s", fn); | |
1018 | ||
1019 | if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) { | |
1020 | return; | |
1021 | } | |
1022 | ||
1023 | selectionToggleUnread(false, 'viewCurrentFeed()', true); | |
1024 | } | |
1025 | ||
1026 | function editArticleTags(id) { | |
1027 | const query = "backend.php?op=article&method=editArticleTags¶m=" + param_escape(id); | |
1028 | ||
1029 | if (dijit.byId("editTagsDlg")) | |
1030 | dijit.byId("editTagsDlg").destroyRecursive(); | |
1031 | ||
1032 | const dialog = new dijit.Dialog({ | |
1033 | id: "editTagsDlg", | |
1034 | title: __("Edit article Tags"), | |
1035 | style: "width: 600px", | |
1036 | execute: function() { | |
1037 | if (this.validate()) { | |
1038 | const query = dojo.objectToQuery(this.attr('value')); | |
1039 | ||
1040 | notify_progress("Saving article tags...", true); | |
1041 | ||
1042 | xhrPost("backend.php", this.attr('value'), (transport) => { | |
1043 | try { | |
1044 | notify(''); | |
1045 | dialog.hide(); | |
1046 | ||
1047 | const data = JSON.parse(transport.responseText); | |
1048 | ||
1049 | if (data) { | |
1050 | const id = data.id; | |
1051 | ||
1052 | const tags = $("ATSTR-" + id); | |
1053 | const tooltip = dijit.byId("ATSTRTIP-" + id); | |
1054 | ||
1055 | if (tags) tags.innerHTML = data.content; | |
1056 | if (tooltip) tooltip.attr('label', data.content_full); | |
1057 | } | |
1058 | } catch (e) { | |
1059 | exception_error(e); | |
1060 | } | |
1061 | }); | |
1062 | } | |
1063 | }, | |
1064 | href: query | |
1065 | }); | |
1066 | ||
1067 | var tmph = dojo.connect(dialog, 'onLoad', function() { | |
1068 | dojo.disconnect(tmph); | |
1069 | ||
1070 | new Ajax.Autocompleter('tags_str', 'tags_choices', | |
1071 | "backend.php?op=article&method=completeTags", | |
1072 | { tokens: ',', paramName: "search" }); | |
1073 | }); | |
1074 | ||
1075 | dialog.show(); | |
1076 | ||
1077 | } | |
1078 | ||
1079 | function cdmScrollToArticleId(id, force) { | |
1080 | const ctr = $("headlines-frame"); | |
1081 | const e = $("RROW-" + id); | |
1082 | ||
1083 | if (!e || !ctr) return; | |
1084 | ||
1085 | if (force || e.offsetTop+e.offsetHeight > (ctr.scrollTop+ctr.offsetHeight) || | |
1086 | e.offsetTop < ctr.scrollTop) { | |
1087 | ||
1088 | // expanded cdm has a 4px margin now | |
1089 | ctr.scrollTop = parseInt(e.offsetTop) - 4; | |
1090 | ||
1091 | /*setActiveArticleId(id); | |
1092 | ||
1093 | // article is selected manually, set it read | |
1094 | toggleUnread(id, 0); */ | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | // for the time being active article does not affect buffer selection (we still re/set the checkbox | |
1099 | // because of getSelectedArticleIds2() hack | |
1100 | function setActiveArticleId(id) { | |
1101 | console.log("setActiveArticleId", id); | |
1102 | ||
1103 | $$("div[id*=RROW][class*=active]").each((e) => { | |
1104 | e.removeClassName("active"); | |
1105 | ||
1106 | if (!e.hasClassName("Selected")) { | |
1107 | const cb = dijit.getEnclosingWidget(e.select(".rchk")[0]); | |
1108 | if (cb) cb.attr("checked", false); | |
1109 | } | |
1110 | }) | |
1111 | ||
1112 | _active_article_id = id; | |
1113 | ||
1114 | const row = $("RROW-" + id); | |
1115 | ||
1116 | if (row) { | |
1117 | if (row.hasClassName("Unread")) { | |
1118 | toggleUnread(id, 0); | |
1119 | ||
1120 | decrementFeedCounter(getActiveFeedId(), activeFeedIsCat()); | |
1121 | updateFloatingTitle(true); | |
1122 | } | |
1123 | ||
1124 | row.addClassName("active"); | |
1125 | ||
1126 | if (!row.hasClassName("Selected")) { | |
1127 | const cb = dijit.getEnclosingWidget(row.select(".rchk")[0]); | |
1128 | if (cb) cb.attr("checked", true); | |
1129 | } | |
1130 | ||
1131 | PluginHost.run(PluginHost.HOOK_ARTICLE_SET_ACTIVE, _active_article_id); | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | function getActiveArticleId() { | |
1136 | return _active_article_id; | |
1137 | } | |
1138 | ||
1139 | function postMouseIn(e, id) { | |
1140 | post_under_pointer = id; | |
1141 | } | |
1142 | ||
1143 | function postMouseOut(id) { | |
1144 | post_under_pointer = false; | |
1145 | } | |
1146 | ||
1147 | function unpackVisibleHeadlines() { | |
1148 | if (!isCdmMode()) return; | |
1149 | ||
1150 | const rows = $$("#headlines-frame div[id*=RROW][data-content]"); | |
1151 | const threshold = $("headlines-frame").scrollTop + $("headlines-frame").offsetHeight + 300; | |
1152 | ||
1153 | for (let i = 0; i < rows.length; i++) { | |
1154 | const row = rows[i]; | |
1155 | ||
1156 | if (row.offsetTop <= threshold) { | |
1157 | console.log("unpacking: " + row.id); | |
1158 | ||
1159 | row.select(".content-inner")[0].innerHTML = row.getAttribute("data-content"); | |
1160 | row.removeAttribute("data-content"); | |
1161 | ||
1162 | PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row); | |
1163 | ||
1164 | // i wonder if this is a good idea? | |
1165 | if (!getActiveArticleId() && !row.hasClassName("Unread")) | |
1166 | setActiveArticleId(row.getAttribute("data-article-id")); | |
1167 | ||
1168 | } else { | |
1169 | break; | |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | function headlines_scroll_handler(e) { | |
1175 | try { | |
1176 | ||
1177 | // rate-limit in case of smooth scrolling and similar abominations | |
1178 | if (Math.max(e.scrollTop, _headlines_scroll_offset) - Math.min(e.scrollTop, _headlines_scroll_offset) < 25) { | |
1179 | return; | |
1180 | } | |
1181 | ||
1182 | _headlines_scroll_offset = e.scrollTop; | |
1183 | ||
1184 | unpackVisibleHeadlines(); | |
1185 | ||
1186 | // set topmost child in the buffer as active | |
1187 | if (isCdmMode() && getInitParam("cdm_auto_catchup") == 1) { | |
1188 | ||
1189 | const rows = $$("#headlines-frame > div[id*=RROW]"); | |
1190 | ||
1191 | for (let i = 0; i < rows.length; i++) { | |
1192 | const row = rows[i]; | |
1193 | ||
1194 | if ($("headlines-frame").scrollTop <= row.offsetTop && | |
1195 | row.offsetTop - $("headlines-frame").scrollTop < 100 && | |
1196 | row.getAttribute("data-article-id") != getActiveArticleId()) { | |
1197 | ||
1198 | /* if (_active_article_id) { | |
1199 | const row = $("RROW-" + _active_article_id); | |
1200 | if (row) row.removeClassName("active"); | |
1201 | } | |
1202 | ||
1203 | _active_article_id = row.getAttribute("data-article-id"); | |
1204 | showArticleInHeadlines(_active_article_id, true); | |
1205 | updateSelectedPrompt(); */ | |
1206 | ||
1207 | setActiveArticleId(row.getAttribute("data-article-id")); | |
1208 | ||
1209 | break; | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | if (!_infscroll_disable) { | |
1215 | const hsp = $("headlines-spacer"); | |
1216 | ||
1217 | if (hsp && hsp.offsetTop - 250 <= e.scrollTop + e.offsetHeight) { | |
1218 | ||
1219 | hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " + | |
1220 | __("Loading, please wait...") + "</span>"; | |
1221 | ||
1222 | loadMoreHeadlines(); | |
1223 | return; | |
1224 | ||
1225 | } | |
1226 | } | |
1227 | ||
1228 | if (isCdmMode()) { | |
1229 | updateFloatingTitle(); | |
1230 | } | |
1231 | ||
1232 | if (getInitParam("cdm_auto_catchup") == 1) { | |
1233 | ||
1234 | let rows = $$("#headlines-frame > div[id*=RROW][class*=Unread]"); | |
1235 | ||
1236 | for (let i = 0; i < rows.length; i++) { | |
1237 | const row = rows[i]; | |
1238 | ||
1239 | if ($("headlines-frame").scrollTop > (row.offsetTop + row.offsetHeight/2)) { | |
1240 | ||
1241 | const id = row.getAttribute("data-article-id") | |
1242 | ||
1243 | if (catchup_id_batch.indexOf(id) == -1) | |
1244 | catchup_id_batch.push(id); | |
1245 | ||
1246 | //console.log("auto_catchup_batch: " + catchup_id_batch.toString()); | |
1247 | } else { | |
1248 | break; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | if (_infscroll_disable) { | |
1253 | const row = $$("#headlines-frame div[id*=RROW]").last(); | |
1254 | ||
1255 | if (row && $("headlines-frame").scrollTop > | |
1256 | (row.offsetTop + row.offsetHeight - 50)) { | |
1257 | ||
1258 | console.log("we seem to be at an end"); | |
1259 | ||
1260 | if (getInitParam("on_catchup_show_next_feed") == "1") { | |
1261 | openNextUnreadFeed(); | |
1262 | } | |
1263 | } | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | } catch (e) { | |
1268 | console.warn("headlines_scroll_handler: " + e); | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | function openNextUnreadFeed() { | |
1273 | const is_cat = activeFeedIsCat(); | |
1274 | const nuf = getNextUnreadFeed(getActiveFeedId(), is_cat); | |
1275 | if (nuf) viewfeed({feed: nuf, is_cat: is_cat}); | |
1276 | } | |
1277 | ||
1278 | function catchupBatchedArticles() { | |
1279 | if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) { | |
1280 | ||
1281 | console.log("catchupBatchedArticles, size=", catchup_id_batch.length); | |
1282 | ||
1283 | // make a copy of the array | |
1284 | const batch = catchup_id_batch.slice(); | |
1285 | const query = { op: "rpc", method: "catchupSelected", | |
1286 | cmode: 0, ids: batch.toString() }; | |
1287 | ||
1288 | _catchup_request_sent = true; | |
1289 | ||
1290 | xhrPost("backend.php", query, (transport) => { | |
1291 | const reply = handle_rpc_json(transport); | |
1292 | ||
1293 | _catchup_request_sent = false; | |
1294 | ||
1295 | if (reply) { | |
1296 | const batch = reply.ids; | |
1297 | ||
1298 | batch.each(function (id) { | |
1299 | const elem = $("RROW-" + id); | |
1300 | if (elem) elem.removeClassName("Unread"); | |
1301 | catchup_id_batch.remove(id); | |
1302 | }); | |
1303 | } | |
1304 | ||
1305 | updateFloatingTitle(true); | |
1306 | }); | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | function catchupRelativeToArticle(below, id) { | |
1311 | ||
1312 | if (!id) id = getActiveArticleId(); | |
1313 | ||
1314 | if (!id) { | |
1315 | alert(__("No article is selected.")); | |
1316 | return; | |
1317 | } | |
1318 | ||
1319 | const visible_ids = getLoadedArticleIds(); | |
1320 | ||
1321 | const ids_to_mark = []; | |
1322 | ||
1323 | if (!below) { | |
1324 | for (let i = 0; i < visible_ids.length; i++) { | |
1325 | if (visible_ids[i] != id) { | |
1326 | const e = $("RROW-" + visible_ids[i]); | |
1327 | ||
1328 | if (e && e.hasClassName("Unread")) { | |
1329 | ids_to_mark.push(visible_ids[i]); | |
1330 | } | |
1331 | } else { | |
1332 | break; | |
1333 | } | |
1334 | } | |
1335 | } else { | |
1336 | for (let i = visible_ids.length - 1; i >= 0; i--) { | |
1337 | if (visible_ids[i] != id) { | |
1338 | const e = $("RROW-" + visible_ids[i]); | |
1339 | ||
1340 | if (e && e.hasClassName("Unread")) { | |
1341 | ids_to_mark.push(visible_ids[i]); | |
1342 | } | |
1343 | } else { | |
1344 | break; | |
1345 | } | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | if (ids_to_mark.length == 0) { | |
1350 | alert(__("No articles found to mark")); | |
1351 | } else { | |
1352 | const msg = ngettext("Mark %d article as read?", "Mark %d articles as read?", ids_to_mark.length).replace("%d", ids_to_mark.length); | |
1353 | ||
1354 | if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) { | |
1355 | ||
1356 | for (var i = 0; i < ids_to_mark.length; i++) { | |
1357 | var e = $("RROW-" + ids_to_mark[i]); | |
1358 | e.removeClassName("Unread"); | |
1359 | } | |
1360 | ||
1361 | const query = { op: "rpc", method: "catchupSelected", | |
1362 | cmode: 0, ids: ids_to_mark.toString() }; | |
1363 | ||
1364 | xhrPost("backend.php", query, (transport) => { | |
1365 | handle_rpc_json(transport); | |
1366 | }); | |
1367 | } | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | function getArticleUnderPointer() { | |
1372 | return post_under_pointer; | |
1373 | } | |
1374 | ||
1375 | function scrollArticle(offset) { | |
1376 | if (!isCdmMode()) { | |
1377 | const ci = $("content-insert"); | |
1378 | if (ci) { | |
1379 | ci.scrollTop += offset; | |
1380 | } | |
1381 | } else { | |
1382 | const hi = $("headlines-frame"); | |
1383 | if (hi) { | |
1384 | hi.scrollTop += offset; | |
1385 | } | |
1386 | ||
1387 | } | |
1388 | } | |
1389 | ||
1390 | function show_labels_in_headlines(transport) { | |
1391 | const data = JSON.parse(transport.responseText); | |
1392 | ||
1393 | if (data) { | |
1394 | data['info-for-headlines'].each(function (elem) { | |
1395 | $$(".HLLCTR-" + elem.id).each(function (ctr) { | |
1396 | ctr.innerHTML = elem.labels; | |
1397 | }); | |
1398 | }); | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | function cdmClicked(event, id, in_body) { | |
1403 | if (event.ctrlKey && !in_body) { | |
1404 | openArticleInNewWindow(id); | |
1405 | } | |
1406 | ||
1407 | setActiveArticleId(id); | |
1408 | ||
1409 | //var shift_key = event.shiftKey; | |
1410 | ||
1411 | /* if (!event.ctrlKey && !event.metaKey) { | |
1412 | ||
1413 | let elem = $("RROW-" + getActiveArticleId()); | |
1414 | ||
1415 | if (elem) elem.removeClassName("active"); | |
1416 | ||
1417 | selectArticles("none"); | |
1418 | toggleSelected(id); | |
1419 | ||
1420 | elem = $("RROW-" + id); | |
1421 | const article_is_unread = elem.hasClassName("Unread"); | |
1422 | ||
1423 | elem.removeClassName("Unread"); | |
1424 | elem.addClassName("active"); | |
1425 | ||
1426 | setActiveArticleId(id); | |
1427 | ||
1428 | if (article_is_unread) { | |
1429 | decrementFeedCounter(getActiveFeedId(), activeFeedIsCat()); | |
1430 | updateFloatingTitle(true); | |
1431 | ||
1432 | const query = { | |
1433 | op: "rpc", method: "catchupSelected", | |
1434 | cmode: 0, ids: id | |
1435 | }; | |
1436 | ||
1437 | xhrPost("backend.php", query, (transport) => { | |
1438 | handle_rpc_json(transport); | |
1439 | }); | |
1440 | } | |
1441 | ||
1442 | return !event.shiftKey; | |
1443 | ||
1444 | } else if (!in_body) { | |
1445 | ||
1446 | toggleSelected(id, true); | |
1447 | ||
1448 | let elem = $("RROW-" + id); | |
1449 | const article_is_unread = elem.hasClassName("Unread"); | |
1450 | ||
1451 | if (article_is_unread) { | |
1452 | decrementFeedCounter(getActiveFeedId(), activeFeedIsCat()); | |
1453 | } | |
1454 | ||
1455 | toggleUnread(id, 0, false); | |
1456 | ||
1457 | openArticleInNewWindow(id); | |
1458 | } else { | |
1459 | return true; | |
1460 | } | |
1461 | ||
1462 | const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length | |
1463 | request_counters(unread_in_buffer == 0); */ | |
1464 | ||
1465 | return false; | |
1466 | } | |
1467 | ||
1468 | function hlClicked(event, id) { | |
1469 | if (event.ctrlKey) { | |
1470 | openArticleInNewWindow(id); | |
1471 | setActiveArticleId(id); | |
1472 | } else { | |
1473 | view(id); | |
1474 | } | |
1475 | ||
1476 | return false; | |
1477 | ||
1478 | /* if (event.which == 2) { | |
1479 | view(id); | |
1480 | return true; | |
1481 | } else if (event.ctrlKey || event.metaKey) { | |
1482 | openArticleInNewWindow(id); | |
1483 | return false; | |
1484 | } else { | |
1485 | view(id); | |
1486 | return false; | |
1487 | } */ | |
1488 | } | |
1489 | ||
1490 | function openArticleInNewWindow(id) { | |
1491 | const w = window.open(""); | |
1492 | w.opener = null; | |
1493 | w.location = "backend.php?op=article&method=redirect&id=" + id; | |
1494 | } | |
1495 | ||
1496 | function isCdmMode() { | |
1497 | return getInitParam("combined_display_mode"); | |
1498 | } | |
1499 | ||
1500 | /* function markHeadline(id, marked) { | |
1501 | if (marked == undefined) marked = true; | |
1502 | ||
1503 | const row = $("RROW-" + id); | |
1504 | if (row) { | |
1505 | const check = dijit.getEnclosingWidget( | |
1506 | row.getElementsByClassName("rchk")[0]); | |
1507 | ||
1508 | if (check) { | |
1509 | check.attr("checked", marked); | |
1510 | } | |
1511 | ||
1512 | if (marked) | |
1513 | row.addClassName("Selected"); | |
1514 | else | |
1515 | row.removeClassName("Selected"); | |
1516 | } | |
1517 | } */ | |
1518 | ||
1519 | function getRelativePostIds(id, limit) { | |
1520 | ||
1521 | const tmp = []; | |
1522 | ||
1523 | if (!limit) limit = 6; //3 | |
1524 | ||
1525 | const ids = getLoadedArticleIds(); | |
1526 | ||
1527 | for (let i = 0; i < ids.length; i++) { | |
1528 | if (ids[i] == id) { | |
1529 | for (let k = 1; k <= limit; k++) { | |
1530 | //if (i > k-1) tmp.push(ids[i-k]); | |
1531 | if (i < ids.length - k) tmp.push(ids[i + k]); | |
1532 | } | |
1533 | break; | |
1534 | } | |
1535 | } | |
1536 | ||
1537 | return tmp; | |
1538 | } | |
1539 | ||
1540 | function correctHeadlinesOffset(id) { | |
1541 | const container = $("headlines-frame"); | |
1542 | const row = $("RROW-" + id); | |
1543 | ||
1544 | if (!container || !row) return; | |
1545 | ||
1546 | const viewport = container.offsetHeight; | |
1547 | ||
1548 | const rel_offset_top = row.offsetTop - container.scrollTop; | |
1549 | const rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop; | |
1550 | ||
1551 | //console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom); | |
1552 | //console.log("Vport: " + viewport); | |
1553 | ||
1554 | if (rel_offset_top <= 0 || rel_offset_top > viewport) { | |
1555 | container.scrollTop = row.offsetTop; | |
1556 | } else if (rel_offset_bottom > viewport) { | |
1557 | container.scrollTop = row.offsetTop + row.offsetHeight - viewport; | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | function headlineActionsChange(elem) { | |
1562 | eval(elem.value); | |
1563 | elem.attr('value', 'false'); | |
1564 | } | |
1565 | ||
1566 | function closeArticlePanel() { | |
1567 | if (dijit.byId("content-insert")) | |
1568 | dijit.byId("headlines-wrap-inner").removeChild( | |
1569 | dijit.byId("content-insert")); | |
1570 | } | |
1571 | ||
1572 | function initFloatingMenu() { | |
1573 | if (!dijit.byId("floatingMenu")) { | |
1574 | ||
1575 | const menu = new dijit.Menu({ | |
1576 | id: "floatingMenu", | |
1577 | targetNodeIds: ["floatingTitle"] | |
1578 | }); | |
1579 | ||
1580 | headlinesMenuCommon(menu); | |
1581 | ||
1582 | menu.startup(); | |
1583 | } | |
1584 | } | |
1585 | ||
1586 | function headlinesMenuCommon(menu) { | |
1587 | ||
1588 | menu.addChild(new dijit.MenuItem({ | |
1589 | label: __("Open original article"), | |
1590 | onClick: function (event) { | |
1591 | openArticleInNewWindow(this.getParent().currentTarget.getAttribute("data-article-id")); | |
1592 | } | |
1593 | })); | |
1594 | ||
1595 | menu.addChild(new dijit.MenuItem({ | |
1596 | label: __("Display article URL"), | |
1597 | onClick: function (event) { | |
1598 | displayArticleUrl(this.getParent().currentTarget.getAttribute("data-article-id")); | |
1599 | } | |
1600 | })); | |
1601 | ||
1602 | menu.addChild(new dijit.MenuSeparator()); | |
1603 | ||
1604 | menu.addChild(new dijit.MenuItem({ | |
1605 | label: __("Toggle unread"), | |
1606 | onClick: function () { | |
1607 | ||
1608 | let ids = getSelectedArticleIds2(); | |
1609 | // cast to string | |
1610 | const id = (this.getParent().currentTarget.getAttribute("data-article-id")) + ""; | |
1611 | ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id]; | |
1612 | ||
1613 | selectionToggleUnread(undefined, false, true, ids); | |
1614 | } | |
1615 | })); | |
1616 | ||
1617 | menu.addChild(new dijit.MenuItem({ | |
1618 | label: __("Toggle starred"), | |
1619 | onClick: function () { | |
1620 | let ids = getSelectedArticleIds2(); | |
1621 | // cast to string | |
1622 | const id = (this.getParent().currentTarget.getAttribute("data-article-id")) + ""; | |
1623 | ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id]; | |
1624 | ||
1625 | selectionToggleMarked(undefined, false, true, ids); | |
1626 | } | |
1627 | })); | |
1628 | ||
1629 | menu.addChild(new dijit.MenuItem({ | |
1630 | label: __("Toggle published"), | |
1631 | onClick: function () { | |
1632 | let ids = getSelectedArticleIds2(); | |
1633 | // cast to string | |
1634 | const id = (this.getParent().currentTarget.getAttribute("data-article-id")) + ""; | |
1635 | ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id]; | |
1636 | ||
1637 | selectionTogglePublished(undefined, false, true, ids); | |
1638 | } | |
1639 | })); | |
1640 | ||
1641 | menu.addChild(new dijit.MenuSeparator()); | |
1642 | ||
1643 | menu.addChild(new dijit.MenuItem({ | |
1644 | label: __("Mark above as read"), | |
1645 | onClick: function () { | |
1646 | catchupRelativeToArticle(0, this.getParent().currentTarget.getAttribute("data-article-id")); | |
1647 | } | |
1648 | })); | |
1649 | ||
1650 | menu.addChild(new dijit.MenuItem({ | |
1651 | label: __("Mark below as read"), | |
1652 | onClick: function () { | |
1653 | catchupRelativeToArticle(1, this.getParent().currentTarget.getAttribute("data-article-id")); | |
1654 | } | |
1655 | })); | |
1656 | ||
1657 | ||
1658 | const labels = getInitParam("labels"); | |
1659 | ||
1660 | if (labels && labels.length) { | |
1661 | ||
1662 | menu.addChild(new dijit.MenuSeparator()); | |
1663 | ||
1664 | const labelAddMenu = new dijit.Menu({ownerMenu: menu}); | |
1665 | const labelDelMenu = new dijit.Menu({ownerMenu: menu}); | |
1666 | ||
1667 | labels.each(function (label) { | |
1668 | const bare_id = label.id; | |
1669 | const name = label.caption; | |
1670 | ||
1671 | labelAddMenu.addChild(new dijit.MenuItem({ | |
1672 | label: name, | |
1673 | labelId: bare_id, | |
1674 | onClick: function () { | |
1675 | ||
1676 | let ids = getSelectedArticleIds2(); | |
1677 | // cast to string | |
1678 | const id = (this.getParent().ownerMenu.currentTarget.getAttribute("data-article-id")) + ""; | |
1679 | ||
1680 | ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id]; | |
1681 | ||
1682 | selectionAssignLabel(this.labelId, ids); | |
1683 | } | |
1684 | })); | |
1685 | ||
1686 | labelDelMenu.addChild(new dijit.MenuItem({ | |
1687 | label: name, | |
1688 | labelId: bare_id, | |
1689 | onClick: function () { | |
1690 | let ids = getSelectedArticleIds2(); | |
1691 | // cast to string | |
1692 | const id = (this.getParent().ownerMenu.currentTarget.getAttribute("data-article-id")) + ""; | |
1693 | ||
1694 | ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id]; | |
1695 | ||
1696 | selectionRemoveLabel(this.labelId, ids); | |
1697 | } | |
1698 | })); | |
1699 | ||
1700 | }); | |
1701 | ||
1702 | menu.addChild(new dijit.PopupMenuItem({ | |
1703 | label: __("Assign label"), | |
1704 | popup: labelAddMenu | |
1705 | })); | |
1706 | ||
1707 | menu.addChild(new dijit.PopupMenuItem({ | |
1708 | label: __("Remove label"), | |
1709 | popup: labelDelMenu | |
1710 | })); | |
1711 | ||
1712 | } | |
1713 | } | |
1714 | ||
1715 | function initHeadlinesMenu() { | |
1716 | if (!dijit.byId("headlinesMenu")) { | |
1717 | ||
1718 | const menu = new dijit.Menu({ | |
1719 | id: "headlinesMenu", | |
1720 | targetNodeIds: ["headlines-frame"], | |
1721 | selector: ".hlMenuAttach" | |
1722 | }); | |
1723 | ||
1724 | headlinesMenuCommon(menu); | |
1725 | ||
1726 | menu.startup(); | |
1727 | } | |
1728 | ||
1729 | /* vgroup feed title menu */ | |
1730 | ||
1731 | if (!dijit.byId("headlinesFeedTitleMenu")) { | |
1732 | ||
1733 | const menu = new dijit.Menu({ | |
1734 | id: "headlinesFeedTitleMenu", | |
1735 | targetNodeIds: ["headlines-frame"], | |
1736 | selector: "div.cdmFeedTitle" | |
1737 | }); | |
1738 | ||
1739 | menu.addChild(new dijit.MenuItem({ | |
1740 | label: __("Select articles in group"), | |
1741 | onClick: function (event) { | |
1742 | selectArticles("all", | |
1743 | "#headlines-frame > div[id*=RROW]" + | |
1744 | "[data-orig-feed-id='" + this.getParent().currentTarget.getAttribute("data-feed-id") + "']"); | |
1745 | ||
1746 | } | |
1747 | })); | |
1748 | ||
1749 | menu.addChild(new dijit.MenuItem({ | |
1750 | label: __("Mark group as read"), | |
1751 | onClick: function () { | |
1752 | selectArticles("none"); | |
1753 | selectArticles("all", | |
1754 | "#headlines-frame > div[id*=RROW]" + | |
1755 | "[data-orig-feed-id='" + this.getParent().currentTarget.getAttribute("data-feed-id") + "']"); | |
1756 | ||
1757 | catchupSelection(); | |
1758 | } | |
1759 | })); | |
1760 | ||
1761 | menu.addChild(new dijit.MenuItem({ | |
1762 | label: __("Mark feed as read"), | |
1763 | onClick: function () { | |
1764 | catchupFeedInGroup(this.getParent().currentTarget.getAttribute("data-feed-id")); | |
1765 | } | |
1766 | })); | |
1767 | ||
1768 | menu.addChild(new dijit.MenuItem({ | |
1769 | label: __("Edit feed"), | |
1770 | onClick: function () { | |
1771 | editFeed(this.getParent().currentTarget.getAttribute("data-feed-id")); | |
1772 | } | |
1773 | })); | |
1774 | ||
1775 | menu.startup(); | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | function cache_set(id, obj) { | |
1780 | //console.log("cache_set: " + id); | |
1781 | if (has_storage) | |
1782 | try { | |
1783 | sessionStorage[id] = obj; | |
1784 | } catch (e) { | |
1785 | sessionStorage.clear(); | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | function cache_get(id) { | |
1790 | if (has_storage) | |
1791 | return sessionStorage[id]; | |
1792 | } | |
1793 | ||
1794 | function cache_clear() { | |
1795 | if (has_storage) | |
1796 | sessionStorage.clear(); | |
1797 | } | |
1798 | ||
1799 | function cache_delete(id) { | |
1800 | if (has_storage) | |
1801 | sessionStorage.removeItem(id); | |
1802 | } | |
1803 | ||
1804 | function cancelSearch() { | |
1805 | _search_query = ""; | |
1806 | viewCurrentFeed(); | |
1807 | } | |
1808 | ||
1809 | function setSelectionScore() { | |
1810 | const ids = getSelectedArticleIds2(); | |
1811 | ||
1812 | if (ids.length > 0) { | |
1813 | console.log(ids); | |
1814 | ||
1815 | const score = prompt(__("Please enter new score for selected articles:")); | |
1816 | ||
1817 | if (score != undefined) { | |
1818 | const query = { op: "article", method: "setScore", id: ids.toString(), | |
1819 | score: score }; | |
1820 | ||
1821 | xhrJson("backend.php", query, (reply) => { | |
1822 | if (reply) { | |
1823 | reply.id.each((id) => { | |
1824 | const row = $("RROW-" + id); | |
1825 | ||
1826 | if (row) { | |
1827 | const pic = row.getElementsByClassName("score-pic")[0]; | |
1828 | ||
1829 | if (pic) { | |
1830 | pic.src = pic.src.replace(/score_.*?\.png/, | |
1831 | reply["score_pic"]); | |
1832 | pic.setAttribute("score", reply["score"]); | |
1833 | } | |
1834 | } | |
1835 | }); | |
1836 | } | |
1837 | }); | |
1838 | } | |
1839 | ||
1840 | } else { | |
1841 | alert(__("No articles are selected.")); | |
1842 | } | |
1843 | } | |
1844 | ||
1845 | function changeScore(id, pic) { | |
1846 | const score = pic.getAttribute("score"); | |
1847 | ||
1848 | const new_score = prompt(__("Please enter new score for this article:"), score); | |
1849 | ||
1850 | if (new_score != undefined) { | |
1851 | const query = { op: "article", method: "setScore", id: id, score: new_score }; | |
1852 | ||
1853 | xhrJson("backend.php", query, (reply) => { | |
1854 | if (reply) { | |
1855 | pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]); | |
1856 | pic.setAttribute("score", new_score); | |
1857 | pic.setAttribute("title", new_score); | |
1858 | } | |
1859 | }); | |
1860 | } | |
1861 | } | |
1862 | ||
1863 | function displayArticleUrl(id) { | |
1864 | const query = { op: "rpc", method: "getlinktitlebyid", id: id }; | |
1865 | ||
1866 | xhrJson("backend.php", query, (reply) => { | |
1867 | if (reply && reply.link) { | |
1868 | prompt(__("Article URL:"), reply.link); | |
1869 | } | |
1870 | }); | |
1871 | ||
1872 | } | |
1873 | ||
1874 | // floatingTitle goto button uses this | |
1875 | function scrollToRowId(id) { | |
1876 | const row = $(id); | |
1877 | ||
1878 | if (row) | |
1879 | $("headlines-frame").scrollTop = row.offsetTop - 4; | |
1880 | } | |
1881 | ||
1882 | function updateFloatingTitle(unread_only) { | |
1883 | if (!isCdmMode()) return; | |
1884 | ||
1885 | const hf = $("headlines-frame"); | |
1886 | const elems = $$("#headlines-frame > div[id*=RROW]"); | |
1887 | ||
1888 | for (let i = 0; i < elems.length; i++) { | |
1889 | ||
1890 | const child = elems[i]; | |
1891 | ||
1892 | if (child && child.offsetTop + child.offsetHeight > hf.scrollTop) { | |
1893 | ||
1894 | const header = child.select(".header")[0]; | |
1895 | ||
1896 | if (unread_only || child.getAttribute("data-article-id") != $("floatingTitle").getAttribute("data-article-id")) { | |
1897 | if (child.getAttribute("data-article-id") != $("floatingTitle").getAttribute("data-article-id")) { | |
1898 | ||
1899 | $("floatingTitle").setAttribute("data-article-id", child.getAttribute("data-article-id")); | |
1900 | $("floatingTitle").innerHTML = header.innerHTML; | |
1901 | $("floatingTitle").firstChild.innerHTML = "<img class='anchor markedPic' src='images/page_white_go.png' onclick=\"scrollToRowId('" + child.id + "')\">" + $("floatingTitle").firstChild.innerHTML; | |
1902 | ||
1903 | initFloatingMenu(); | |
1904 | ||
1905 | const cb = $$("#floatingTitle .dijitCheckBox")[0]; | |
1906 | ||
1907 | if (cb) | |
1908 | cb.parentNode.removeChild(cb); | |
1909 | } | |
1910 | ||
1911 | if (child.hasClassName("Unread")) | |
1912 | $("floatingTitle").addClassName("Unread"); | |
1913 | else | |
1914 | $("floatingTitle").removeClassName("Unread"); | |
1915 | ||
1916 | PluginHost.run(PluginHost.HOOK_FLOATING_TITLE, child); | |
1917 | } | |
1918 | ||
1919 | $("floatingTitle").style.marginRight = hf.offsetWidth - child.offsetWidth + "px"; | |
1920 | if (header.offsetTop + header.offsetHeight < hf.scrollTop + $("floatingTitle").offsetHeight - 5 && | |
1921 | child.offsetTop + child.offsetHeight >= hf.scrollTop + $("floatingTitle").offsetHeight - 5) | |
1922 | $("floatingTitle").style.visibility = "visible"; | |
1923 | else | |
1924 | $("floatingTitle").style.visibility = "hidden"; | |
1925 | ||
1926 | return; | |
1927 | ||
1928 | } | |
1929 | } | |
1930 | } |