]> git.wh0rd.org - tt-rss.git/blame_incremental - viewfeed.js
move print_label_select to functions.php
[tt-rss.git] / viewfeed.js
... / ...
CommitLineData
1var active_post_id = false;
2var last_article_view = false;
3var active_real_feed_id = false;
4
5// FIXME: kludges, needs proper implementation
6var _reload_feedlist_after_view = false;
7
8var _cdm_wd_timeout = false;
9var _cdm_wd_vishist = new Array();
10
11var article_cache = new Array();
12
13var vgroup_last_feed = false;
14var post_under_pointer = false;
15
16var last_requested_article = false;
17
18function catchup_callback2(transport, callback) {
19 try {
20 console.log("catchup_callback2 " + transport + ", " + callback);
21 notify("");
22 all_counters_callback2(transport);
23 if (callback) {
24 setTimeout(callback, 10);
25 }
26 } catch (e) {
27 exception_error("catchup_callback2", e, transport);
28 }
29}
30
31function clean_feed_selections() {
32 try {
33 var feeds = $("feedList").getElementsByTagName("LI");
34
35 for (var i = 0; i < feeds.length; i++) {
36 if (feeds[i].id && feeds[i].id.match("FEEDR-")) {
37 feeds[i].className = feeds[i].className.replace("Selected", "");
38 }
39 if (feeds[i].id && feeds[i].id.match("FCAT-")) {
40 feeds[i].className = feeds[i].className.replace("Selected", "");
41 }
42 }
43 } catch (e) {
44 exception_error("clean_feed_selections", e);
45 }
46}
47
48function headlines_callback2(transport, feed_cur_page) {
49 try {
50
51 if (!transport.responseText && db) {
52 offlineConfirmModeChange();
53 return;
54 }
55
56 loading_set_progress(100);
57
58 console.log("headlines_callback2 [page=" + feed_cur_page + "]");
59
60 if (!transport_error_check(transport)) return;
61
62 clean_feed_selections();
63
64 var is_cat = false;
65 var feed_id = false;
66
67 if (transport.responseXML) {
68 var headlines = transport.responseXML.getElementsByTagName("headlines")[0];
69 if (headlines) {
70 is_cat = headlines.getAttribute("is_cat");
71 feed_id = headlines.getAttribute("id");
72 setActiveFeedId(feed_id, is_cat);
73 }
74 }
75
76 var ll = $('FLL-' + feed_id);
77
78 if (!is_cat) {
79 var feedr = $("FEEDR-" + feed_id);
80 if (feedr && !feedr.className.match("Selected")) {
81 feedr.className = feedr.className + "Selected";
82 }
83 if (feedr && ll) feedr.removeChild(ll);
84 } else {
85 var feedr = $("FCAT-" + feed_id);
86 if (feedr && !feedr.className.match("Selected")) {
87 feedr.className = feedr.className + "Selected";
88 }
89
90 var fcap = $("FCAP-" + feed_id);
91 if (fcap && ll) fcap.removeChild(ll);
92
93 }
94
95 var img = $('FIMG-' + feed_id);
96
97 if (img && !is_cat) {
98 img.src = img.alt;
99 }
100
101 var f = $("headlines-frame");
102 try {
103 if (feed_cur_page == 0) {
104 console.log("resetting headlines scrollTop");
105 f.scrollTop = 0;
106 }
107 } catch (e) { };
108
109 if (transport.responseXML) {
110 var response = transport.responseXML;
111
112 var headlines = response.getElementsByTagName("headlines")[0];
113 var headlines_info = response.getElementsByTagName("headlines-info")[0];
114
115 if (headlines_info)
116 headlines_info = JSON.parse(headlines_info.firstChild.nodeValue);
117 else
118 console.log("didn't find headlines-info object in response");
119
120 var headlines_count = headlines_info.count;
121 var headlines_unread = headlines_info.unread;
122 var disable_cache = headlines_info.disable_cache;
123
124 vgroup_last_feed = headlines_info.vgroup_last_feed;
125
126 if (headlines_count == 0) {
127 _infscroll_disable = 1;
128 } else {
129 _infscroll_disable = 0;
130 }
131
132 var counters = response.getElementsByTagName("counters")[0];
133 var articles = response.getElementsByTagName("article");
134 var runtime_info = response.getElementsByTagName("runtime-info");
135
136 if (feed_cur_page == 0) {
137 if (headlines) {
138 f.innerHTML = headlines.firstChild.nodeValue;
139
140 var cache_prefix = "";
141
142 if (is_cat) {
143 cache_prefix = "C:";
144 } else {
145 cache_prefix = "F:";
146 }
147
148 cache_invalidate(cache_prefix + feed_id);
149
150 if (!disable_cache) {
151 cache_inject(cache_prefix + feed_id,
152 headlines.firstChild.nodeValue, headlines_unread);
153 }
154
155 } else {
156 console.log("headlines_callback: returned no data");
157 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
158
159 }
160 } else {
161 if (headlines) {
162 if (headlines_count > 0) {
163 console.log("adding some more headlines...");
164
165 var c = $("headlinesList");
166
167 if (!c) {
168 c = $("headlinesInnerContainer");
169 }
170
171 var ids = getSelectedArticleIds2();
172
173 c.innerHTML = c.innerHTML + headlines.firstChild.nodeValue;
174
175 console.log("restore selected ids: " + ids);
176
177 for (var i = 0; i < ids.length; i++) {
178 markHeadline(ids[i]);
179 }
180
181 } else {
182 console.log("no new headlines received");
183 }
184 } else {
185 console.log("headlines_callback: returned no data");
186 notify_error("Error while trying to load more headlines");
187 }
188
189 }
190
191 if (articles) {
192 for (var i = 0; i < articles.length; i++) {
193 var a_id = articles[i].getAttribute("id");
194 console.log("found id: " + a_id);
195 cache_inject(a_id, articles[i].firstChild.nodeValue);
196 }
197 } else {
198 console.log("no cached articles received");
199 }
200
201 if (counters) {
202 console.log("parsing piggybacked counters: " + counters);
203 parse_counters(counters, false);
204 } else {
205 console.log("counters container not found in reply, requesting...");
206 request_counters();
207 }
208
209 if (runtime_info) {
210 console.log("parsing runtime info: " + runtime_info[0]);
211 parse_runtime_info(runtime_info[0]);
212 } else {
213 console.log("counters container not found in reply");
214 }
215
216 } else {
217 console.log("headlines_callback: returned no XML object");
218 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
219 }
220
221 if (typeof correctPNG != 'undefined') {
222 correctPNG();
223 }
224
225 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
226
227 if (!$("headlinesList") &&
228 getActiveFeedId() != -3 &&
229 getInitParam("cdm_auto_catchup") == 1) {
230 console.log("starting CDM watchdog");
231 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
232 _cdm_wd_vishist = new Array();
233 } else {
234 console.log("not in CDM mode or watchdog disabled");
235 }
236
237 _feed_cur_page = feed_cur_page;
238 _infscroll_request_sent = 0;
239
240 notify("");
241
242 remove_splash();
243
244 } catch (e) {
245 exception_error("headlines_callback2", e, transport);
246 }
247}
248
249function render_article(article) {
250 try {
251 var f = $("content-frame");
252 try {
253 f.scrollTop = 0;
254 } catch (e) { };
255
256 var fi = $("content-insert");
257
258 try {
259 fi.scrollTop = 0;
260 } catch (e) { };
261
262 fi.innerHTML = article;
263
264// article.evalScripts();
265
266 } catch (e) {
267 exception_error("render_article", e);
268 }
269}
270
271function showArticleInHeadlines(id) {
272
273 try {
274
275 cleanSelected("headlinesList");
276
277 var crow = $("RROW-" + id);
278
279 if (!crow) return;
280
281 var article_is_unread = crow.className.match("Unread");
282
283 crow.className = crow.className.replace("Unread", "");
284
285 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
286
287 var upd_img_pic = $("FUPDPIC-" + id);
288
289 var cache_prefix = "";
290
291 if (activeFeedIsCat()) {
292 cache_prefix = "C:";
293 } else {
294 cache_prefix = "F:";
295 }
296
297 var view_mode = false;
298
299 try {
300 view_mode = document.forms['main_toolbar_form'].view_mode;
301 view_mode = view_mode[view_mode.selectedIndex].value;
302 } catch (e) {
303 //
304 }
305
306 if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
307 upd_img_pic.src.match("fresh_sign.png"))) {
308
309 upd_img_pic.src = "images/blank_icon.gif";
310
311 cache_invalidate(cache_prefix + getActiveFeedId());
312
313 cache_inject(cache_prefix + getActiveFeedId(),
314 $("headlines-frame").innerHTML,
315 get_feed_unread(getActiveFeedId()));
316
317 } else if (article_is_unread && view_mode == "all_articles") {
318
319 cache_invalidate(cache_prefix + getActiveFeedId());
320
321 cache_inject(cache_prefix + getActiveFeedId(),
322 $("headlines-frame").innerHTML,
323 get_feed_unread(getActiveFeedId())-1);
324
325 } else if (article_is_unread) {
326 cache_invalidate(cache_prefix + getActiveFeedId());
327 }
328
329 markHeadline(id);
330
331 } catch (e) {
332 exception_error("showArticleInHeadlines", e);
333 }
334}
335
336function article_callback2(transport, id) {
337 try {
338 console.log("article_callback2 " + id);
339
340 if (!transport.responseText && db) {
341 offlineConfirmModeChange();
342 return;
343 }
344
345 if (transport.responseXML) {
346
347 if (!transport_error_check(transport)) return;
348
349/* var ll = $('LL-' + id);
350 var content = $('HLC-' + id);
351
352 if (ll && content) content.removeChild(ll); */
353
354 var upic = $('FUPDPIC-' + id);
355
356 if (upic) {
357 upic.src = 'images/blank_icon.gif';
358 }
359
360 if (id != last_requested_article) {
361 console.log("requested article id is out of sequence, aborting");
362 return;
363 }
364
365 active_post_id = id;
366
367 console.log("looking for articles to cache...");
368
369 var articles = transport.responseXML.getElementsByTagName("article");
370
371 for (var i = 0; i < articles.length; i++) {
372 var a_id = articles[i].getAttribute("id");
373
374 console.log("found id: " + a_id);
375
376 if (a_id == active_post_id) {
377 console.log("active article, rendering...");
378 render_article(articles[i].firstChild.nodeValue);
379 }
380
381 cache_inject(a_id, articles[i].firstChild.nodeValue);
382 }
383
384
385 showArticleInHeadlines(id);
386
387 if (db) {
388 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id]);
389 }
390
391 var reply = transport.responseXML.firstChild.firstChild;
392
393 } else {
394 console.log("article_callback: returned no XML object");
395 //var f = $("content-frame");
396 //f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
397 }
398
399 var date = new Date();
400 last_article_view = date.getTime() / 1000;
401
402 if (typeof correctPNG != 'undefined') {
403 correctPNG();
404 }
405
406 if (_reload_feedlist_after_view) {
407 setTimeout('updateFeedList(false, false)', 50);
408 _reload_feedlist_after_view = false;
409 } else {
410 if (transport.responseXML) {
411 var counters = transport.responseXML.getElementsByTagName("counters")[0];
412
413 if (counters) {
414 console.log("parsing piggybacked counters: " + counters);
415 parse_counters(counters, false);
416 } else {
417 console.log("counters container not found in reply, requesting...");
418 request_counters();
419 }
420 }
421 }
422
423 notify("");
424 } catch (e) {
425 exception_error("article_callback2", e, transport);
426 }
427}
428
429function view(id) {
430 try {
431 console.log("loading article: " + id);
432
433 if (offline_mode) return view_offline(id);
434
435 var cached_article = cache_find(id);
436
437 console.log("cache check result: " + (cached_article != false));
438
439 enableHotkeys();
440 hideAuxDlg();
441
442 var query = "?op=view&id=" + param_escape(id);
443
444 var neighbor_ids = getRelativePostIds(active_post_id);
445
446 /* only request uncached articles */
447
448 var cids_to_request = Array();
449
450 for (var i = 0; i < neighbor_ids.length; i++) {
451 if (!cache_check(neighbor_ids[i])) {
452 cids_to_request.push(neighbor_ids[i]);
453 }
454 }
455
456 console.log("additional ids: " + cids_to_request.toString());
457
458 /* additional info for piggyback counters */
459
460 if (tagsAreDisplayed()) {
461 query = query + "&omode=lt";
462 } else {
463 query = query + "&omode=flc";
464 }
465
466 query = query + "&cids=" + cids_to_request.toString();
467
468 var crow = $("RROW-" + id);
469 var article_is_unread = crow.className.match("Unread");
470
471 if (!async_counters_work) {
472 query = query + "&csync=true";
473 }
474
475 showArticleInHeadlines(id);
476
477 if (!cached_article) {
478
479 var upic = $('FUPDPIC-' + id);
480
481 if (upic) {
482 upic.src = getInitParam("sign_progress");
483 }
484
485 } else if (cached_article && article_is_unread) {
486
487 query = query + "&mode=prefetch";
488
489 render_article(cached_article);
490
491 } else if (cached_article) {
492
493 query = query + "&mode=prefetch_old";
494 render_article(cached_article);
495
496 }
497
498 cache_expire();
499
500 last_requested_article = id;
501
502 new Ajax.Request("backend.php", {
503 parameters: query,
504 onComplete: function(transport) {
505 article_callback2(transport, id);
506 } });
507
508 return false;
509
510 } catch (e) {
511 exception_error("view", e);
512 }
513}
514
515function tMark(id) {
516 return toggleMark(id);
517}
518
519function tPub(id) {
520 return togglePub(id);
521}
522
523function tMark_afh_off(effect) {
524 try {
525 var elem = effect.effects[0].element;
526
527 console.log("tMark_afh_off : " + elem.id);
528
529 if (elem) {
530 elem.src = elem.src.replace("mark_set", "mark_unset");
531 elem.alt = __("Star article");
532 Element.show(elem);
533 }
534
535 } catch (e) {
536 exception_error("tMark_afh_off", e);
537 }
538}
539
540function tPub_afh_off(effect) {
541 try {
542 var elem = effect.effects[0].element;
543
544 console.log("tPub_afh_off : " + elem.id);
545
546 if (elem) {
547 elem.src = elem.src.replace("pub_set", "pub_unset");
548 elem.alt = __("Publish article");
549 Element.show(elem);
550 }
551
552 } catch (e) {
553 exception_error("tPub_afh_off", e);
554 }
555}
556
557function toggleMark(id, client_only, no_effects) {
558
559 try {
560
561 var query = "?op=rpc&id=" + id + "&subop=mark";
562
563 query = query + "&afid=" + getActiveFeedId();
564
565 if (tagsAreDisplayed()) {
566 query = query + "&omode=tl";
567 } else {
568 query = query + "&omode=flc";
569 }
570
571 var mark_img = $("FMPIC-" + id);
572
573 if (!mark_img) return;
574
575 var vfeedu = $("FEEDU--1");
576 var crow = $("RROW-" + id);
577
578 if (mark_img.src.match("mark_unset")) {
579 mark_img.src = mark_img.src.replace("mark_unset", "mark_set");
580 mark_img.alt = __("Unstar article");
581 query = query + "&mark=1";
582
583 if (db) {
584 db.execute("UPDATE articles SET marked = 1 WHERE id = ?", [id]);
585 }
586
587 } else {
588 mark_img.alt = __("Please wait...");
589 query = query + "&mark=0";
590
591 if ($("headlinesList") && !no_effects) {
592 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tMark_afh_off});
593 } else {
594 mark_img.src = mark_img.src.replace("mark_set", "mark_unset");
595 mark_img.alt = __("Star article");
596 }
597
598 if (db) {
599 db.execute("UPDATE articles SET marked = 0 WHERE id = ?", [id]);
600 }
601
602 }
603
604 if (!no_effects) update_local_feedlist_counters();
605
606 if (!client_only) {
607 console.log(query);
608
609 new Ajax.Request("backend.php", {
610 parameters: query,
611 onComplete: function(transport) {
612 all_counters_callback2(transport);
613 } });
614
615 }
616
617 } catch (e) {
618 exception_error("toggleMark", e);
619 }
620}
621
622function togglePub(id, client_only, no_effects, note) {
623
624 try {
625
626 var query = "?op=rpc&id=" + id + "&subop=publ";
627
628 query = query + "&afid=" + getActiveFeedId();
629
630 if (note != undefined) {
631 query = query + "&note=" + param_escape(note);
632 } else {
633 query = query + "&note=undefined";
634 }
635
636 if (tagsAreDisplayed()) {
637 query = query + "&omode=tl";
638 } else {
639 query = query + "&omode=flc";
640 }
641
642 var mark_img = $("FPPIC-" + id);
643
644 if (!mark_img) return;
645
646 var vfeedu = $("FEEDU--2");
647 var crow = $("RROW-" + id);
648
649 if (mark_img.src.match("pub_unset") || note != undefined) {
650 mark_img.src = mark_img.src.replace("pub_unset", "pub_set");
651 mark_img.alt = __("Unpublish article");
652 query = query + "&pub=1";
653
654 } else {
655 mark_img.alt = __("Please wait...");
656 query = query + "&pub=0";
657
658 if ($("headlinesList") && !no_effects) {
659 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tPub_afh_off});
660 } else {
661 mark_img.src = mark_img.src.replace("pub_set", "pub_unset");
662 mark_img.alt = __("Publish article");
663 }
664 }
665
666 if (!client_only) {
667 new Ajax.Request("backend.php", {
668 parameters: query,
669 onComplete: function(transport) {
670 all_counters_callback2(transport);
671
672 var note = transport.responseXML.getElementsByTagName("note")[0];
673
674 if (note) {
675 var note_id = note.getAttribute("id");
676 var note_size = note.getAttribute("size");
677 var note_content = note.firstChild.nodeValue;
678
679 var container = $('POSTNOTE-' + note_id);
680
681 cache_invalidate(note_id);
682
683 if (container) {
684 if (note_size == "0") {
685 Element.hide(container);
686 } else {
687 container.innerHTML = note_content;
688 Element.show(container);
689 }
690 }
691 }
692
693 } });
694 }
695
696 } catch (e) {
697 exception_error("togglePub", e);
698 }
699}
700
701function correctHeadlinesOffset(id) {
702
703 try {
704
705 var hlist = $("headlinesList");
706 var container = $("headlinesInnerContainer");
707 var row = $("RROW-" + id);
708
709 var viewport = container.offsetHeight;
710
711 var rel_offset_top = row.offsetTop - container.scrollTop;
712 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
713
714 console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
715 console.log("Vport: " + viewport);
716
717 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
718 container.scrollTop = row.offsetTop;
719 } else if (rel_offset_bottom > viewport) {
720
721 /* doesn't properly work with Opera in some cases because
722 Opera fucks up element scrolling */
723
724 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
725 }
726
727 } catch (e) {
728 exception_error("correctHeadlinesOffset", e);
729 }
730
731}
732
733function moveToPost(mode) {
734
735 try {
736
737 var rows;
738
739 if (isCdmMode()) {
740 rows = cdmGetVisibleArticles();
741 } else {
742 rows = getVisibleHeadlineIds();
743 }
744
745 var prev_id = false;
746 var next_id = false;
747
748 if (!$('RROW-' + active_post_id)) {
749 active_post_id = false;
750 }
751
752 if (active_post_id == false) {
753 next_id = getFirstVisibleHeadlineId();
754 prev_id = getLastVisibleHeadlineId();
755 } else {
756 for (var i = 0; i < rows.length; i++) {
757 if (rows[i] == active_post_id) {
758 prev_id = rows[i-1];
759 next_id = rows[i+1];
760 }
761 }
762 }
763
764 if (mode == "next") {
765 if (next_id) {
766 if (isCdmMode()) {
767
768 if (!cdmArticleIsActuallyVisible(next_id)) {
769 cdmScrollToArticleId(next_id);
770 }
771 cdmSelectArticles("none");
772 toggleUnread(next_id, 0, true);
773 toggleSelected(next_id);
774
775 } else {
776 correctHeadlinesOffset(next_id);
777 view(next_id, getActiveFeedId());
778 }
779 }
780 }
781
782 if (mode == "prev") {
783 if (prev_id) {
784 if (isCdmMode()) {
785 cdmScrollToArticleId(prev_id);
786 cdmSelectArticles("none");
787 toggleUnread(prev_id, 0, true);
788 toggleSelected(prev_id);
789 } else {
790 correctHeadlinesOffset(prev_id);
791 view(prev_id, getActiveFeedId());
792 }
793 }
794 }
795
796 } catch (e) {
797 exception_error("moveToPost", e);
798 }
799}
800
801function toggleSelected(id) {
802 try {
803
804 var cb = $("RCHK-" + id);
805
806 var row = $("RROW-" + id);
807 if (row) {
808 var nc = row.className;
809
810 if (!nc.match("Selected")) {
811 nc = nc + "Selected";
812 if (cb) {
813 cb.checked = true;
814 }
815
816 // In CDM basically last selected article == active article
817 if (isCdmMode()) active_post_id = id;
818 } else {
819 nc = nc.replace("Selected", "");
820 if (cb) {
821 cb.checked = false;
822 }
823
824 }
825
826 row.className = nc;
827 }
828 } catch (e) {
829 exception_error("toggleSelected", e);
830 }
831}
832
833function toggleUnread_afh(effect) {
834 try {
835
836 var elem = effect.element;
837 elem.style.backgroundColor = "";
838
839 } catch (e) {
840 exception_error("toggleUnread_afh", e);
841 }
842}
843
844function toggleUnread(id, cmode, effect) {
845 try {
846
847 var row = $("RROW-" + id);
848 if (row) {
849 var nc = row.className;
850 var is_selected = row.className.match("Selected");
851 nc = nc.replace("Unread", "");
852 nc = nc.replace("Selected", "");
853
854 // since we are removing selection from the object, uncheck
855 // corresponding checkbox
856
857 var cb = $("RCHK-" + id);
858 if (cb) {
859 cb.checked = false;
860 }
861
862 // NOTE: I'm not sure that resetting selection here is a feature -fox
863
864 if (cmode == undefined || cmode == 2) {
865 if (row.className.match("Unread")) {
866 row.className = nc;
867
868 if (effect) {
869 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
870 afterFinish: toggleUnread_afh,
871 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
872 }
873
874 } else {
875 row.className = nc + "Unread";
876 }
877
878 if (db) {
879 db.execute("UPDATE articles SET unread = not unread "+
880 "WHERE id = ?", [id]);
881 }
882
883 } else if (cmode == 0) {
884 row.className = nc;
885
886 if (effect) {
887 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
888 afterFinish: toggleUnread_afh,
889 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
890 }
891
892 if (db) {
893 db.execute("UPDATE articles SET unread = 0 "+
894 "WHERE id = ?", [id]);
895 }
896
897 } else if (cmode == 1) {
898 row.className = nc + "Unread";
899
900 if (db) {
901 db.execute("UPDATE articles SET unread = 1 "+
902 "WHERE id = ?", [id]);
903 }
904
905 }
906
907 update_local_feedlist_counters();
908
909 // Disable unmarking as selected for the time being (16.05.08) -fox
910 if (is_selected) row.className = row.className + "Selected";
911
912 if (cmode == undefined) cmode = 2;
913
914 var query = "?op=rpc&subop=catchupSelected" +
915 "&cmode=" + param_escape(cmode) + "&ids=" + param_escape(id);
916
917// notify_progress("Loading, please wait...");
918
919 new Ajax.Request("backend.php", {
920 parameters: query,
921 onComplete: function(transport) {
922 all_counters_callback2(transport);
923 } });
924
925 }
926
927 } catch (e) {
928 exception_error("toggleUnread", e);
929 }
930}
931
932function selectionRemoveLabel(id) {
933 try {
934
935 var ids = getSelectedArticleIds2();
936
937 if (ids.length == 0) {
938 alert(__("No articles are selected."));
939 return;
940 }
941
942// var ok = confirm(__("Remove selected articles from label?"));
943
944// if (ok) {
945
946 var query = "?op=rpc&subop=removeFromLabel&ids=" +
947 param_escape(ids.toString()) + "&lid=" + param_escape(id);
948
949// notify_progress("Loading, please wait...");
950
951 cache_invalidate("F:" + (-11 - id));
952
953 new Ajax.Request("backend.php", {
954 parameters: query,
955 onComplete: function(transport) {
956 show_labels_in_headlines(transport);
957 all_counters_callback2(transport);
958 } });
959
960// }
961
962 } catch (e) {
963 exception_error("selectionAssignLabel", e);
964
965 }
966}
967
968function selectionAssignLabel(id) {
969 try {
970
971 var ids = getSelectedArticleIds2();
972
973 if (ids.length == 0) {
974 alert(__("No articles are selected."));
975 return;
976 }
977
978// var ok = confirm(__("Assign selected articles to label?"));
979
980// if (ok) {
981
982 cache_invalidate("F:" + (-11 - id));
983
984 var query = "?op=rpc&subop=assignToLabel&ids=" +
985 param_escape(ids.toString()) + "&lid=" + param_escape(id);
986
987// notify_progress("Loading, please wait...");
988
989 new Ajax.Request("backend.php", {
990 parameters: query,
991 onComplete: function(transport) {
992 show_labels_in_headlines(transport);
993 all_counters_callback2(transport);
994 } });
995
996// }
997
998 } catch (e) {
999 exception_error("selectionAssignLabel", e);
1000
1001 }
1002}
1003
1004function selectionToggleUnread(set_state, callback_func, no_error) {
1005 try {
1006 var rows = getSelectedArticleIds2();
1007
1008 if (rows.length == 0 && !no_error) {
1009 alert(__("No articles are selected."));
1010 return;
1011 }
1012
1013 for (i = 0; i < rows.length; i++) {
1014 var row = $("RROW-" + rows[i]);
1015 if (row) {
1016 var nc = row.className;
1017 nc = nc.replace("Unread", "");
1018 nc = nc.replace("Selected", "");
1019
1020 if (set_state == undefined) {
1021 if (row.className.match("Unread")) {
1022 row.className = nc + "Selected";
1023 } else {
1024 row.className = nc + "UnreadSelected";
1025 }
1026 if (db) {
1027 db.execute("UPDATE articles SET unread = NOT unread WHERE id = ?",
1028 [rows[i]]);
1029 }
1030 }
1031
1032 if (set_state == false) {
1033 row.className = nc + "Selected";
1034 if (db) {
1035 db.execute("UPDATE articles SET unread = 0 WHERE id = ?",
1036 [rows[i]]);
1037 }
1038 }
1039
1040 if (set_state == true) {
1041 row.className = nc + "UnreadSelected";
1042 if (db) {
1043 db.execute("UPDATE articles SET unread = 1 WHERE id = ?",
1044 [rows[i]]);
1045 }
1046 }
1047 }
1048 }
1049
1050 if (rows.length > 0) {
1051
1052 update_local_feedlist_counters();
1053
1054 var cmode = "";
1055
1056 if (set_state == undefined) {
1057 cmode = "2";
1058 } else if (set_state == true) {
1059 cmode = "1";
1060 } else if (set_state == false) {
1061 cmode = "0";
1062 }
1063
1064 var query = "?op=rpc&subop=catchupSelected" +
1065 "&cmode=" + cmode + "&ids=" + param_escape(rows.toString());
1066
1067 notify_progress("Loading, please wait...");
1068
1069 new Ajax.Request("backend.php", {
1070 parameters: query,
1071 onComplete: function(transport) {
1072 catchup_callback2(transport, callback_func);
1073 } });
1074
1075 }
1076
1077 } catch (e) {
1078 exception_error("selectionToggleUnread", e);
1079 }
1080}
1081
1082function selectionToggleMarked() {
1083 try {
1084
1085 var rows = getSelectedArticleIds2();
1086
1087 if (rows.length == 0) {
1088 alert(__("No articles are selected."));
1089 return;
1090 }
1091
1092 for (i = 0; i < rows.length; i++) {
1093 toggleMark(rows[i], true, true);
1094 }
1095
1096 update_local_feedlist_counters();
1097
1098 if (rows.length > 0) {
1099
1100 var query = "?op=rpc&subop=markSelected&ids=" +
1101 param_escape(rows.toString()) + "&cmode=2";
1102
1103 query = query + "&afid=" + getActiveFeedId();
1104
1105 query = query + "&omode=lc";
1106
1107 new Ajax.Request("backend.php", {
1108 parameters: query,
1109 onComplete: function(transport) {
1110 all_counters_callback2(transport);
1111 } });
1112
1113 }
1114
1115 } catch (e) {
1116 exception_error("selectionToggleMarked", e);
1117 }
1118}
1119
1120function selectionTogglePublished() {
1121 try {
1122
1123 var rows = getSelectedArticleIds2();
1124
1125 if (rows.length == 0) {
1126 alert(__("No articles are selected."));
1127 return;
1128 }
1129
1130 for (i = 0; i < rows.length; i++) {
1131 togglePub(rows[i], true, true);
1132 }
1133
1134 if (rows.length > 0) {
1135
1136 var query = "?op=rpc&subop=publishSelected&ids=" +
1137 param_escape(rows.toString()) + "&cmode=2";
1138
1139 query = query + "&afid=" + getActiveFeedId();
1140
1141 query = query + "&omode=lc";
1142
1143 new Ajax.Request("backend.php", {
1144 parameters: query,
1145 onComplete: function(transport) {
1146 all_counters_callback2(transport);
1147 } });
1148
1149 }
1150
1151 } catch (e) {
1152 exception_error("selectionToggleMarked", e);
1153 }
1154}
1155
1156function cdmGetSelectedArticles() {
1157 var sel_articles = new Array();
1158 var container = $("headlinesInnerContainer");
1159
1160 for (i = 0; i < container.childNodes.length; i++) {
1161 var child = container.childNodes[i];
1162
1163 if (child.id && child.id.match("RROW-") && child.className.match("Selected")) {
1164 var c_id = child.id.replace("RROW-", "");
1165 sel_articles.push(c_id);
1166 }
1167 }
1168
1169 return sel_articles;
1170}
1171
1172function cdmGetVisibleArticles() {
1173 var sel_articles = new Array();
1174 var container = $("headlinesInnerContainer");
1175
1176 if (!container) return sel_articles;
1177
1178 for (i = 0; i < container.childNodes.length; i++) {
1179 var child = container.childNodes[i];
1180
1181 if (child.id && child.id.match("RROW-")) {
1182 var c_id = child.id.replace("RROW-", "");
1183 sel_articles.push(c_id);
1184 }
1185 }
1186
1187 return sel_articles;
1188}
1189
1190function cdmGetUnreadArticles() {
1191 var sel_articles = new Array();
1192 var container = $("headlinesInnerContainer");
1193
1194 for (i = 0; i < container.childNodes.length; i++) {
1195 var child = container.childNodes[i];
1196
1197 if (child.id && child.id.match("RROW-") && child.className.match("Unread")) {
1198 var c_id = child.id.replace("RROW-", "");
1199 sel_articles.push(c_id);
1200 }
1201 }
1202
1203 return sel_articles;
1204}
1205
1206
1207// mode = all,none,unread
1208function cdmSelectArticles(mode) {
1209 var container = $("headlinesInnerContainer");
1210
1211 for (i = 0; i < container.childNodes.length; i++) {
1212 var child = container.childNodes[i];
1213
1214 if (child.id && child.id.match("RROW-")) {
1215 var aid = child.id.replace("RROW-", "");
1216
1217 var cb = $("RCHK-" + aid);
1218
1219 if (mode == "all") {
1220 if (!child.className.match("Selected")) {
1221 child.className = child.className + "Selected";
1222 cb.checked = true;
1223 }
1224 } else if (mode == "unread") {
1225 if (child.className.match("Unread") && !child.className.match("Selected")) {
1226 child.className = child.className + "Selected";
1227 cb.checked = true;
1228 }
1229 } else {
1230 child.className = child.className.replace("Selected", "");
1231 cb.checked = false;
1232 }
1233 }
1234 }
1235}
1236
1237function catchupPage() {
1238
1239 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1240
1241 var str = __("Mark all visible articles in %s as read?");
1242
1243 str = str.replace("%s", fn);
1244
1245 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1246 return;
1247 }
1248
1249 if ($("headlinesList")) {
1250 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
1251 selectionToggleUnread(false, 'viewCurrentFeed()', true);
1252 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
1253 } else {
1254 cdmSelectArticles('all');
1255 selectionToggleUnread(false, 'viewCurrentFeed()', true)
1256 cdmSelectArticles('none');
1257 }
1258}
1259
1260function deleteSelection() {
1261
1262 try {
1263
1264 var rows = getSelectedArticleIds2();
1265
1266 if (rows.length == 0) {
1267 alert(__("No articles are selected."));
1268 return;
1269 }
1270
1271 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1272 var str;
1273 var op;
1274
1275 if (getActiveFeedId() != 0) {
1276 str = __("Delete %d selected articles in %s?");
1277 } else {
1278 str = __("Delete %d selected articles?");
1279 }
1280
1281 str = str.replace("%d", rows.length);
1282 str = str.replace("%s", fn);
1283
1284 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1285 return;
1286 }
1287
1288 query = "?op=rpc&subop=delete&ids=" + param_escape(rows);
1289
1290 console.log(query);
1291
1292 new Ajax.Request("backend.php", {
1293 parameters: query,
1294 onComplete: function(transport) {
1295 viewCurrentFeed();
1296 } });
1297
1298 } catch (e) {
1299 exception_error("deleteSelection", e);
1300 }
1301}
1302
1303function archiveSelection() {
1304
1305 try {
1306
1307 var rows = getSelectedArticleIds2();
1308
1309 if (rows.length == 0) {
1310 alert(__("No articles are selected."));
1311 return;
1312 }
1313
1314 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1315 var str;
1316 var op;
1317
1318 if (getActiveFeedId() != 0) {
1319 str = __("Archive %d selected articles in %s?");
1320 op = "archive";
1321 } else {
1322 str = __("Move %d archived articles back?");
1323 op = "unarchive";
1324 }
1325
1326 str = str.replace("%d", rows.length);
1327 str = str.replace("%s", fn);
1328
1329 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1330 return;
1331 }
1332
1333 query = "?op=rpc&subop="+op+"&ids=" + param_escape(rows);
1334
1335 console.log(query);
1336
1337 for (var i = 0; i < rows.length; i++) {
1338 cache_invalidate(rows[i]);
1339 }
1340
1341 new Ajax.Request("backend.php", {
1342 parameters: query,
1343 onComplete: function(transport) {
1344 viewCurrentFeed();
1345 } });
1346
1347 } catch (e) {
1348 exception_error("archiveSelection", e);
1349 }
1350}
1351
1352function catchupSelection() {
1353
1354 try {
1355
1356 var rows = getSelectedArticleIds2();
1357
1358 if (rows.length == 0) {
1359 alert(__("No articles are selected."));
1360 return;
1361 }
1362
1363 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1364
1365 var str = __("Mark %d selected articles in %s as read?");
1366
1367 str = str.replace("%d", rows.length);
1368 str = str.replace("%s", fn);
1369
1370 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1371 return;
1372 }
1373
1374 if ($("headlinesList")) {
1375 selectionToggleUnread(false, 'viewCurrentFeed()', true);
1376 } else {
1377 selectionToggleUnread(false, 'viewCurrentFeed()', true)
1378 }
1379
1380 } catch (e) {
1381 exception_error("catchupSelection", e);
1382 }
1383}
1384
1385function editArticleTags(id, feed_id, cdm_enabled) {
1386 displayDlg('editArticleTags', id,
1387 function () {
1388 $("tags_str").focus();
1389
1390 new Ajax.Autocompleter('tags_str', 'tags_choices',
1391 "backend.php?op=rpc&subop=completeTags",
1392 { tokens: ',', paramName: "search" });
1393 });
1394}
1395
1396function editTagsSave() {
1397
1398 notify_progress("Saving article tags...");
1399
1400 var form = document.forms["tag_edit_form"];
1401
1402 var query = Form.serialize("tag_edit_form");
1403
1404 query = "?op=rpc&subop=setArticleTags&" + query;
1405
1406 console.log(query);
1407
1408 new Ajax.Request("backend.php", {
1409 parameters: query,
1410 onComplete: function(transport) {
1411 try {
1412 console.log("tags saved...");
1413
1414 closeInfoBox();
1415 notify("");
1416
1417 if (tagsAreDisplayed()) {
1418 _reload_feedlist_after_view = true;
1419 }
1420
1421 if (transport.responseXML) {
1422 var tags_str = transport.responseXML.getElementsByTagName("tags-str")[0];
1423
1424 if (tags_str) {
1425 var id = tags_str.getAttribute("id");
1426
1427 if (id) {
1428 var tags = $("ATSTR-" + id);
1429 if (tags) {
1430 tags.innerHTML = tags_str.firstChild.nodeValue;
1431 }
1432
1433 cache_invalidate(id);
1434 }
1435 }
1436 }
1437
1438 } catch (e) {
1439 exception_error("editTagsSave", e);
1440 }
1441 } });
1442}
1443
1444function editTagsInsert() {
1445 try {
1446
1447 var form = document.forms["tag_edit_form"];
1448
1449 var found_tags = form.found_tags;
1450 var tags_str = form.tags_str;
1451
1452 var tag = found_tags[found_tags.selectedIndex].value;
1453
1454 if (tags_str.value.length > 0 &&
1455 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
1456
1457 tags_str.value = tags_str.value + ", ";
1458 }
1459
1460 tags_str.value = tags_str.value + tag + ", ";
1461
1462 found_tags.selectedIndex = 0;
1463
1464 } catch (e) {
1465 exception_error("editTagsInsert", e);
1466 }
1467}
1468
1469function cdmScrollViewport(where) {
1470 console.log("cdmScrollViewport: " + where);
1471
1472 var ctr = $("headlinesInnerContainer");
1473
1474 if (!ctr) return;
1475
1476 if (where == "bottom") {
1477 ctr.scrollTop = ctr.scrollHeight;
1478 } else {
1479 ctr.scrollTop = where;
1480 }
1481}
1482
1483function cdmArticleIsBelowViewport(id) {
1484 try {
1485 var ctr = $("headlinesInnerContainer");
1486 var e = $("RROW-" + id);
1487
1488 if (!e || !ctr) return;
1489
1490 // article starts below viewport
1491
1492 if (ctr.scrollTop < e.offsetTop) {
1493 return true;
1494 } else {
1495 return false;
1496 }
1497
1498 } catch (e) {
1499 exception_error("cdmArticleIsVisible", e);
1500 }
1501}
1502
1503function cdmArticleIsAboveViewport(id) {
1504 try {
1505 var ctr = $("headlinesInnerContainer");
1506 var e = $("RROW-" + id);
1507
1508 if (!e || !ctr) return;
1509
1510 // article starts above viewport
1511
1512 if (ctr.scrollTop > e.offsetTop + e.offsetHeight) {
1513 return true;
1514 } else {
1515 return false;
1516 }
1517
1518 } catch (e) {
1519 exception_error("cdmArticleIsVisible", e);
1520 }
1521}
1522
1523function cdmScrollToArticleId(id) {
1524 try {
1525 var ctr = $("headlinesInnerContainer");
1526 var e = $("RROW-" + id);
1527
1528 if (!e || !ctr) return;
1529
1530 ctr.scrollTop = e.offsetTop;
1531
1532 } catch (e) {
1533 exception_error("cdmScrollToArticleId", e);
1534 }
1535}
1536
1537function cdmArticleIsActuallyVisible(id) {
1538 try {
1539 var ctr = $("headlinesInnerContainer");
1540 var e = $("RROW-" + id);
1541
1542 if (!e || !ctr) return;
1543
1544 // article fits in viewport OR article is longer than viewport and
1545 // its bottom is visible
1546
1547 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1548 ctr.scrollTop + ctr.offsetHeight) {
1549
1550 return true;
1551
1552 } else if (e.offsetHeight > ctr.offsetHeight &&
1553 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1554 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1555
1556 return true;
1557
1558 }
1559
1560 return false;
1561
1562 } catch (e) {
1563 exception_error("cdmArticleIsVisible", e);
1564 }
1565}
1566
1567function cdmWatchdog() {
1568
1569 try {
1570
1571 var ctr = $("headlinesInnerContainer");
1572
1573 if (!ctr) return;
1574
1575 var ids = new Array();
1576
1577 var e = ctr.firstChild;
1578
1579 while (e) {
1580 if (e.className && e.className == "cdmArticleUnread" && e.id &&
1581 e.id.match("RROW-")) {
1582
1583 // article fits in viewport OR article is longer than viewport and
1584 // its bottom is visible
1585
1586 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1587 ctr.scrollTop + ctr.offsetHeight) {
1588
1589// console.log(e.id + " is visible " + e.offsetTop + "." +
1590// (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
1591// (ctr.scrollTop + ctr.offsetHeight));
1592
1593 ids.push(e.id.replace("RROW-", ""));
1594
1595 } else if (e.offsetHeight > ctr.offsetHeight &&
1596 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1597 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1598
1599 ids.push(e.id.replace("RROW-", ""));
1600
1601 }
1602
1603 // method 2: article bottom is visible and is in upper 1/2 of the viewport
1604
1605/* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1606 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
1607
1608 ids.push(e.id.replace("RROW-", ""));
1609
1610 } */
1611
1612 }
1613
1614 e = e.nextSibling;
1615 }
1616
1617 console.log("cdmWatchdog, ids= " + ids.toString());
1618
1619 if (ids.length > 0) {
1620
1621 for (var i = 0; i < ids.length; i++) {
1622 var e = $("RROW-" + ids[i]);
1623 if (e) {
1624 e.className = e.className.replace("Unread", "");
1625 }
1626 }
1627
1628 var query = "?op=rpc&subop=catchupSelected" +
1629 "&cmode=0" + "&ids=" + param_escape(ids.toString());
1630
1631 new Ajax.Request("backend.php", {
1632 parameters: query,
1633 onComplete: function(transport) {
1634 all_counters_callback2(transport);
1635 } });
1636
1637 }
1638
1639 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
1640
1641 } catch (e) {
1642 exception_error("cdmWatchdog", e);
1643 }
1644
1645}
1646
1647
1648function cache_inject(id, article, param) {
1649 try {
1650 if (!cache_check_param(id, param)) {
1651 console.log("cache_article: miss: " + id + " [p=" + param + "]");
1652
1653 if (db) {
1654
1655 var date = new Date();
1656 var ts = Math.round(date.getTime() / 1000);
1657
1658 db.execute("INSERT INTO cache (id, article, param, added) VALUES (?, ?, ?, ?)",
1659 [id, article, param, ts]);
1660 } else {
1661
1662 var cache_obj = {};
1663
1664 cache_obj["id"] = id;
1665 cache_obj["data"] = article;
1666 cache_obj["param"] = param;
1667 cache_obj["added"] = new Date();
1668
1669 if (param) id = id + ":" + param;
1670
1671 if (has_local_storage())
1672 localStorage.setItem(id, JSON.stringify(cache_obj));
1673 else
1674 article_cache.push(cache_obj);
1675 }
1676
1677 } else {
1678 console.log("cache_article: hit: " + id + " [p=" + param + "]");
1679 }
1680 } catch (e) {
1681 exception_error("cache_inject", e);
1682 }
1683}
1684
1685function cache_find(id) {
1686
1687 if (db) {
1688 var rs = db.execute("SELECT article FROM cache WHERE id = ?", [id]);
1689 var a = false;
1690
1691 if (rs.isValidRow()) {
1692 var a = rs.field(0);
1693 }
1694
1695 rs.close();
1696
1697 return a;
1698
1699 } else {
1700
1701 if (has_local_storage()) {
1702 var cache_obj = localStorage.getItem(id);
1703
1704 if (cache_obj) {
1705 cache_obj = JSON.parse(cache_obj);
1706
1707 if (cache_obj)
1708 return cache_obj['data'];
1709 }
1710
1711 } else {
1712 for (var i = 0; i < article_cache.length; i++) {
1713 if (article_cache[i]["id"] == id) {
1714 return article_cache[i]["data"];
1715 }
1716 }
1717 }
1718 }
1719 return false;
1720}
1721
1722function cache_find_param(id, param) {
1723
1724 if (db) {
1725 var rs = db.execute("SELECT article FROM cache WHERE id = ? AND param = ?",
1726 [id, param]);
1727 var a = false;
1728
1729 if (rs.isValidRow()) {
1730 a = rs.field(0);
1731 }
1732
1733 rs.close();
1734
1735 return a;
1736
1737 } else {
1738
1739 if (has_local_storage()) {
1740 var cache_obj = localStorage.getItem(id + ":" + param);
1741
1742 if (cache_obj) {
1743 cache_obj = JSON.parse(cache_obj);
1744
1745 if (cache_obj)
1746 return cache_obj['data'];
1747 }
1748
1749 } else {
1750 for (var i = 0; i < article_cache.length; i++) {
1751 if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1752 return article_cache[i]["data"];
1753 }
1754 }
1755 }
1756 }
1757 return false;
1758}
1759
1760function cache_check(id) {
1761
1762 if (db) {
1763 var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ?",
1764 [id]);
1765 var a = false;
1766
1767 if (rs.isValidRow()) {
1768 a = rs.field(0) != "0";
1769 }
1770
1771 rs.close();
1772
1773 return a;
1774
1775 } else {
1776 if (has_local_storage) {
1777 if (localStorage.getItem(id))
1778 return true;
1779 } else {
1780 for (var i = 0; i < article_cache.length; i++) {
1781 if (article_cache[i]["id"] == id) {
1782 return true;
1783 }
1784 }
1785 }
1786 }
1787 return false;
1788}
1789
1790function cache_check_param(id, param) {
1791
1792 if (db) {
1793 var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ? AND param = ?",
1794 [id, param]);
1795 var a = false;
1796
1797 if (rs.isValidRow()) {
1798 a = rs.field(0) != "0";
1799 }
1800
1801 rs.close();
1802
1803 return a;
1804
1805 } else {
1806
1807 if (has_local_storage) {
1808 if (localStorage.getItem(id + ':' + param))
1809 return true;
1810 } else {
1811 for (var i = 0; i < article_cache.length; i++) {
1812 if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1813 return true;
1814 }
1815 }
1816 }
1817 }
1818 return false;
1819}
1820
1821function cache_expire() {
1822 if (db) {
1823 var date = new Date();
1824 var ts = Math.round(date.getTime() / 1000);
1825
1826 db.execute("DELETE FROM cache WHERE added < ? - 1800 AND id LIKE 'FEEDLIST'", [ts]);
1827 db.execute("DELETE FROM cache WHERE added < ? - 600 AND (id LIKE 'F:%' OR id LIKE 'C:%')", [ts]);
1828 db.execute("DELETE FROM cache WHERE added < ? - 86400", [ts]);
1829
1830
1831 } else {
1832 if (has_local_storage()) {
1833 while (localStorage.length > 25) {
1834 localStorage.removeItem(localStorage.key(0));
1835 }
1836 } else {
1837 while (article_cache.length > 25) {
1838 article_cache.shift();
1839 }
1840 }
1841 }
1842}
1843
1844function cache_flush() {
1845 article_cache = new Array();
1846}
1847
1848function cache_invalidate(id) {
1849 try {
1850
1851 if (db) {
1852 rs = db.execute("DELETE FROM cache WHERE id = ?", [id]);
1853 return rs.rowsAffected != 0;
1854 } else {
1855
1856 if (has_local_storage()) {
1857 for (var i = 0; i < localStorage.length; i++) {
1858 var key = localStorage.key(i);
1859
1860 if (key == id || key.indexOf(id + ":") == 0)
1861 localStorage.removeItem(key);
1862
1863 }
1864 } else {
1865 var i = 0
1866
1867 while (i < article_cache.length) {
1868 if (article_cache[i]["id"] == id) {
1869 console.log("cache_invalidate: removed id " + id);
1870 article_cache.splice(i, 1);
1871 return true;
1872 }
1873 i++;
1874 }
1875 }
1876 }
1877
1878 console.log("cache_invalidate: id not found: " + id);
1879 return false;
1880 } catch (e) {
1881 exception_error("cache_invalidate", e);
1882 }
1883}
1884
1885function getActiveArticleId() {
1886 return active_post_id;
1887}
1888
1889function cdmClicked(id) {
1890 try {
1891 var elem = $("RROW-" + id);
1892
1893 if (elem) {
1894 var id = elem.id.replace("RROW-", "");
1895 active_post_id = id;
1896
1897// cdmSelectArticles("none");
1898 toggleUnread(id, 0, true);
1899// toggleSelected(id);
1900
1901 }
1902 } catch (e) {
1903 exception_error("cdmClicked", e);
1904 }
1905}
1906
1907function preloadArticleUnderPointer(id) {
1908 try {
1909 if (getInitParam("bw_limit") == "1") return;
1910
1911 if (post_under_pointer == id && !cache_check(id)) {
1912
1913 console.log("trying to preload article " + id);
1914
1915 var neighbor_ids = getRelativePostIds(id, 1);
1916
1917 /* only request uncached articles */
1918
1919 var cids_to_request = Array();
1920
1921 for (var i = 0; i < neighbor_ids.length; i++) {
1922 if (!cache_check(neighbor_ids[i])) {
1923 cids_to_request.push(neighbor_ids[i]);
1924 }
1925 }
1926 console.log("additional ids: " + cids_to_request.toString());
1927
1928 cids_to_request.push(id);
1929
1930 var query = "?op=rpc&subop=getArticles&ids=" +
1931 cids_to_request.toString();
1932
1933 new Ajax.Request("backend.php", {
1934 parameters: query,
1935 onComplete: function(transport) {
1936 var articles = transport.responseXML.getElementsByTagName("article");
1937
1938 for (var i = 0; i < articles.length; i++) {
1939 var id = articles[i].getAttribute("id");
1940 if (!cache_check(id)) {
1941 cache_inject(id, articles[i].firstChild.nodeValue);
1942 console.log("preloaded article: " + id);
1943 }
1944 }
1945 } });
1946 }
1947 } catch (e) {
1948 exception_error("preloadArticleUnderPointer", e);
1949 }
1950}
1951
1952function postMouseIn(id) {
1953 try {
1954 if (post_under_pointer != id) {
1955 post_under_pointer = id;
1956 if (!isCdmMode()) {
1957 window.setTimeout("preloadArticleUnderPointer(" + id + ")", 250);
1958 }
1959 }
1960
1961 } catch (e) {
1962 exception_error("postMouseIn", e);
1963 }
1964}
1965
1966function postMouseOut(id) {
1967 try {
1968 post_under_pointer = false;
1969 } catch (e) {
1970 exception_error("postMouseOut", e);
1971 }
1972}
1973
1974function headlines_scroll_handler() {
1975 try {
1976
1977 var e = $("headlinesInnerContainer");
1978
1979 var toolbar_form = document.forms["main_toolbar_form"];
1980
1981 console.log((e.scrollTop + e.offsetHeight) + " vs " + e.scrollHeight + " dis? " +
1982 _infscroll_disable);
1983
1984 if (e.scrollTop + e.offsetHeight > e.scrollHeight - 100) {
1985 if (!_infscroll_disable) {
1986 console.log("more cowbell!");
1987 viewNextFeedPage();
1988 }
1989 }
1990
1991 } catch (e) {
1992 exception_error("headlines_scroll_handler", e);
1993 }
1994}
1995
1996function catchupRelativeToArticle(below) {
1997
1998 try {
1999
2000
2001 if (!getActiveArticleId()) {
2002 alert(__("No article is selected."));
2003 return;
2004 }
2005
2006 var visible_ids;
2007
2008 if ($("headlinesList")) {
2009 visible_ids = getVisibleHeadlineIds();
2010 } else {
2011 visible_ids = cdmGetVisibleArticles();
2012 }
2013
2014 var ids_to_mark = new Array();
2015
2016 if (!below) {
2017 for (var i = 0; i < visible_ids.length; i++) {
2018 if (visible_ids[i] != getActiveArticleId()) {
2019 var e = $("RROW-" + visible_ids[i]);
2020
2021 if (e && e.className.match("Unread")) {
2022 ids_to_mark.push(visible_ids[i]);
2023 }
2024 } else {
2025 break;
2026 }
2027 }
2028 } else {
2029 for (var i = visible_ids.length-1; i >= 0; i--) {
2030 if (visible_ids[i] != getActiveArticleId()) {
2031 var e = $("RROW-" + visible_ids[i]);
2032
2033 if (e && e.className.match("Unread")) {
2034 ids_to_mark.push(visible_ids[i]);
2035 }
2036 } else {
2037 break;
2038 }
2039 }
2040 }
2041
2042 if (ids_to_mark.length == 0) {
2043 alert(__("No articles found to mark"));
2044 } else {
2045 var msg = __("Mark %d article(s) as read?").replace("%d", ids_to_mark.length);
2046
2047 if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) {
2048
2049 for (var i = 0; i < ids_to_mark.length; i++) {
2050 var e = $("RROW-" + ids_to_mark[i]);
2051 e.className = e.className.replace("Unread", "");
2052 }
2053
2054 var query = "?op=rpc&subop=catchupSelected" +
2055 "&cmode=0" + "&ids=" + param_escape(ids_to_mark.toString());
2056
2057 new Ajax.Request("backend.php", {
2058 parameters: query,
2059 onComplete: function(transport) {
2060 catchup_callback2(transport);
2061 } });
2062
2063 }
2064 }
2065
2066 } catch (e) {
2067 exception_error("catchupRelativeToArticle", e);
2068 }
2069}
2070
2071function cdmExpandArticle(a_id) {
2072 try {
2073 var id = 'CICD-' + a_id;
2074
2075 try {
2076 Element.hide("CEXC-" + a_id);
2077 } catch (e) { }
2078
2079 Effect.Appear(id, {duration : 0.5,
2080 beforeStart: function(effect) {
2081 var h_id = 'CICH-' + a_id;
2082 var h_elem = $(h_id);
2083 if (h_elem) { h_elem.style.display = "none"; }
2084
2085 toggleUnread(a_id, 0);
2086 }});
2087
2088
2089 } catch (e) {
2090 exception_error("cdmExpandArticle", e);
2091 }
2092
2093}
2094
2095function fixHeadlinesOrder(ids) {
2096 try {
2097 for (var i = 0; i < ids.length; i++) {
2098 var e = $("RROW-" + ids[i]);
2099
2100 if (e) {
2101 if (i % 2 == 0) {
2102 e.className = e.className.replace("even", "odd");
2103 } else {
2104 e.className = e.className.replace("odd", "even");
2105 }
2106 }
2107 }
2108 } catch (e) {
2109 exception_error("fixHeadlinesOrder", e);
2110 }
2111}
2112
2113function hideReadHeadlines() {
2114 try {
2115
2116 var ids = false;
2117 var vis_ids = new Array();
2118
2119 if ($("headlinesList")) {
2120 ids = getVisibleHeadlineIds();
2121 } else {
2122 ids = cdmGetVisibleArticles();
2123 }
2124
2125 var read_headlines_visible = true;
2126
2127 for (var i = 0; i < ids.length; i++) {
2128 var row = $("RROW-" + ids[i]);
2129
2130 if (row && row.className) {
2131 if (read_headlines_visible) {
2132 if (row.className.match("Unread") || row.className.match("Selected")) {
2133 Element.show(row);
2134 vis_ids.push(ids[i]);
2135 } else {
2136 //Effect.Fade(row, {duration : 0.3});
2137 Element.hide(row);
2138 }
2139 } else {
2140 Element.show(row);
2141 vis_ids.push(ids[i]);
2142 }
2143 }
2144 }
2145
2146 fixHeadlinesOrder(vis_ids);
2147
2148 read_headlines_visible = !read_headlines_visible;
2149
2150 } catch (e) {
2151 exception_error("hideReadHeadlines", e);
2152 }
2153}
2154
2155function invertHeadlineSelection() {
2156 try {
2157 var rows = new Array();
2158 var r = false;
2159
2160 if (!isCdmMode()) {
2161 r = document.getElementsByTagName("TR");
2162 } else {
2163 r = document.getElementsByTagName("DIV");
2164 }
2165
2166 for (var i = 0; i < r.length; i++) {
2167 if (r[i].id && r[i].id.match("RROW-")) {
2168 rows.push(r[i]);
2169 }
2170 }
2171
2172 for (var i = 0; i < rows.length; i++) {
2173 var nc = rows[i].className;
2174 var id = rows[i].id.replace("RROW-", "");
2175 var cb = $("RCHK-" + id);
2176
2177 if (!rows[i].className.match("Selected")) {
2178 nc = nc + "Selected";
2179 cb.checked = true;
2180 } else {
2181 nc = nc.replace("Selected", "");
2182 cb.checked = false;
2183 }
2184
2185 rows[i].className = nc;
2186
2187 }
2188
2189 } catch (e) {
2190 exception_error("invertHeadlineSelection", e);
2191 }
2192}
2193
2194function getArticleUnderPointer() {
2195 return post_under_pointer;
2196}
2197
2198function zoomToArticle(id) {
2199 try {
2200 var w = window.open("backend.php?op=view&mode=zoom&id=" + param_escape(id),
2201 "ttrss_zoom_" + id,
2202 "status=0,toolbar=0,location=0,width=450,height=300,scrollbars=1,menubar=0");
2203
2204 } catch (e) {
2205 exception_error("zoomToArticle", e);
2206 }
2207}
2208
2209function scrollArticle(offset) {
2210 try {
2211 if (!isCdmMode()) {
2212 var ci = $("content-insert");
2213 if (ci) {
2214 ci.scrollTop += offset;
2215 }
2216 } else {
2217 var hi = $("headlinesInnerContainer");
2218 if (hi) {
2219 hi.scrollTop += offset;
2220 }
2221
2222 }
2223 } catch (e) {
2224 exception_error("scrollArticle", e);
2225 }
2226}
2227
2228function show_labels_in_headlines(transport) {
2229 try {
2230 if (transport.responseXML) {
2231 var info = transport.responseXML.getElementsByTagName("info-for-headlines")[0];
2232
2233 var elems = info.getElementsByTagName("entry");
2234
2235 for (var l = 0; l < elems.length; l++) {
2236 var e_id = elems[l].getAttribute("id");
2237
2238 if (e_id) {
2239
2240 var ctr = $("HLLCTR-" + e_id);
2241
2242 if (ctr) {
2243 ctr.innerHTML = elems[l].firstChild.nodeValue;
2244 }
2245 }
2246
2247 }
2248
2249 }
2250 } catch (e) {
2251 exception_error("show_labels_in_headlines", e);
2252
2253 }
2254}
2255
2256function toggleHeadlineActions() {
2257 try {
2258 var e = $("headlineActionsBody");
2259 var p = $("headlineActionsDrop");
2260
2261 if (!Element.visible(e)) {
2262 Element.show(e);
2263 } else {
2264 Element.hide(e);
2265 }
2266
2267 e.scrollTop = 0;
2268 e.style.left = (p.offsetLeft + 1) + "px";
2269 e.style.top = (p.offsetTop + p.offsetHeight + 2) + "px";
2270
2271 } catch (e) {
2272 exception_error("toggleHeadlineActions", e);
2273 }
2274}
2275
2276function publishWithNote(id, def_note) {
2277 try {
2278 if (!def_note) def_note = '';
2279
2280 var note = prompt(__("Please enter a note for this article:"), def_note);
2281
2282 if (note != undefined) {
2283 togglePub(id, false, false, note);
2284 }
2285
2286 } catch (e) {
2287 exception_error("publishWithNote", e);
2288 }
2289}
2290
2291function emailArticle(id) {
2292 try {
2293 if (!id) {
2294 var ids = getSelectedArticleIds2();
2295
2296 if (ids.length == 0) {
2297 alert(__("No articles are selected."));
2298 return;
2299 }
2300
2301 id = ids.toString();
2302 }
2303
2304 displayDlg('emailArticle', id,
2305 function () {
2306 document.forms['article_email_form'].destination.focus();
2307
2308 new Ajax.Autocompleter('destination', 'destination_choices',
2309 "backend.php?op=rpc&subop=completeEmails",
2310 { tokens: '', paramName: "search" });
2311
2312 });
2313
2314 } catch (e) {
2315 exception_error("emailArticle", e);
2316 }
2317}
2318
2319function emailArticleDo() {
2320 try {
2321 var f = document.forms['article_email_form'];
2322
2323 if (f.destination.value == "") {
2324 alert("Please fill in the destination email.");
2325 return;
2326 }
2327
2328 if (f.subject.value == "") {
2329 alert("Please fill in the subject.");
2330 return;
2331 }
2332
2333 var query = Form.serialize("article_email_form");
2334
2335// console.log(query);
2336
2337 new Ajax.Request("backend.php", {
2338 parameters: query,
2339 onComplete: function(transport) {
2340 try {
2341
2342 var error = transport.responseXML.getElementsByTagName('error')[0];
2343
2344 if (error) {
2345 alert(__('Error sending email:') + ' ' + error.firstChild.nodeValue);
2346 } else {
2347 notify_info('Your message has been sent.');
2348 closeInfoBox();
2349 }
2350
2351 } catch (e) {
2352 exception_error("sendEmailDo", e);
2353 }
2354
2355 } });
2356
2357 } catch (e) {
2358 exception_error("emailArticleDo", e);
2359 }
2360}