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