]> git.wh0rd.org Git - tt-rss.git/blob - viewfeed.js
add _PREFS_ACTIVE_TAB hidden option, tweak update.php, move SCHEMA_VERSION definition...
[tt-rss.git] / viewfeed.js
1 var active_post_id = false;
2 var _catchup_callback_func = false;
3 var last_article_view = false;
4 var active_real_feed_id = false;
5
6 var _tag_active_post_id = false;
7 var _tag_active_feed_id = false;
8 var _tag_active_cdm = false;
9
10 // FIXME: kludge, to restore scrollTop after tag editor terminates
11 var _tag_cdm_scroll = false;
12
13 // FIXME: kludges, needs proper implementation
14 var _reload_feedlist_after_view = false;
15
16 var _cdm_wd_timeout = false;
17 var _cdm_wd_vishist = new Array();
18
19 function catchup_callback() {
20         if (xmlhttp_rpc.readyState == 4) {
21                 try {
22                         debug("catchup_callback");
23                         if (_catchup_callback_func) {
24                                 setTimeout(_catchup_callback_func, 100);        
25                         }
26                         notify("");                     
27                         all_counters_callback();
28                 } catch (e) {
29                         exception_error("catchup_callback", e);
30                 }
31         }
32 }
33
34 function headlines_callback() {
35         if (xmlhttp.readyState == 4) {
36                 debug("headlines_callback");
37                 var f = document.getElementById("headlines-frame");
38                 try {
39                         f.scrollTop = 0;
40                 } catch (e) { };
41                 f.innerHTML = xmlhttp.responseText;
42                 update_all_counters();
43                 if (typeof correctPNG != 'undefined') {
44                         correctPNG();
45                 }
46
47                 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
48
49                 if (!document.getElementById("headlinesList") && 
50                                 getInitParam("cdm_auto_catchup") == 1) {
51                         debug("starting CDM watchdog");
52                         _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
53                         _cdm_wd_vishist = new Array();
54                 } else {
55                         debug("not in CDM mode or watchdog disabled");
56                 }
57
58                 if (_tag_cdm_scroll) {
59                         try {
60                                 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
61                                 _tag_cdm_scroll = false;
62                         } catch (e) { }
63                 }
64
65                 notify("");
66         }
67 }
68
69 function article_callback() {
70         if (xmlhttp.readyState == 4) {
71                 debug("article_callback");
72                 var f = document.getElementById("content-frame");
73                 try {
74                         f.scrollTop = 0;
75                 } catch (e) { };
76                 f.innerHTML = xmlhttp.responseText;
77
78                 var date = new Date();
79                 last_article_view = date.getTime() / 1000;
80
81                 if (typeof correctPNG != 'undefined') {
82                         correctPNG();
83                 }
84
85                 if (_reload_feedlist_after_view) {
86                         setTimeout('updateFeedList(false, false)', 50);                 
87                         _reload_feedlist_after_view = false;
88                 } else {
89                         update_all_counters();
90                 }
91
92                 notify("");
93         }
94 }
95
96 function view(id, feed_id, skip_history) {
97         
98         try {
99                 debug("loading article: " + id + "/" + feed_id);
100
101                 active_real_feed_id = feed_id;
102
103                 if (!skip_history) {
104                         history_push("ARTICLE:" + id + ":" + feed_id);
105                 }
106         
107                 enableHotkeys();
108         
109                 active_post_id = id; 
110                 //setActiveFeedId(feed_id);
111
112                 var query = "backend.php?op=view&id=" + param_escape(id) +
113                         "&feed=" + param_escape(feed_id);
114
115                 var date = new Date();
116
117                 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
118                         debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
119                         xmlhttp.abort();
120                 }
121
122                 if (xmlhttp_ready(xmlhttp)) {
123
124                         cleanSelected("headlinesList");
125
126                         var crow = document.getElementById("RROW-" + active_post_id);
127                         crow.className = crow.className.replace("Unread", "");
128
129                         var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
130
131                         if (upd_img_pic) {
132                                 upd_img_pic.src = "images/blank_icon.gif";
133                         }
134
135                         selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
136                         markHeadline(active_post_id);
137
138                         var date = new Date();
139                         var timestamp = Math.round(date.getTime() / 1000);
140                         query = query + "&ts=" + timestamp;
141
142                         notify_progress("Loading, please wait...");
143
144                         xmlhttp.open("GET", query, true);
145                         xmlhttp.onreadystatechange=article_callback;
146                         xmlhttp.send(null);
147                 } else {
148                         debug("xmlhttp busy (@view)");
149                         printLockingError();
150                 }  
151
152         } catch (e) {
153                 exception_error("view", e);
154         }
155 }
156
157 function toggleMark(id) {
158
159         if (!xmlhttp_ready(xmlhttp_rpc)) {
160                 printLockingError();
161                 return;
162         }
163
164         var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
165
166         var mark_img = document.getElementById("FMARKPIC-" + id);
167         var vfeedu = document.getElementById("FEEDU--1");
168         var crow = document.getElementById("RROW-" + id);
169
170         if (mark_img.alt != "Reset mark") {
171                 mark_img.src = "images/mark_set.png";
172                 mark_img.alt = "Reset mark";
173                 query = query + "&mark=1";
174
175                 if (vfeedu && crow.className.match("Unread")) {
176                         vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
177                 }
178
179         } else {
180                 mark_img.src = "images/mark_unset.png";
181                 mark_img.alt = "Set mark";
182                 query = query + "&mark=0";
183
184                 if (vfeedu && crow.className.match("Unread")) {
185                         vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
186                 }
187
188         }
189
190         var vfeedctr = document.getElementById("FEEDCTR--1");
191         var vfeedr = document.getElementById("FEEDR--1");
192
193         if (vfeedu && vfeedctr) {
194                 if ((+vfeedu.innerHTML) > 0) {
195                         if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
196                                 vfeedr.className = vfeedr.className + "Unread";
197                                 vfeedctr.className = "odd";
198                         }
199                 } else {
200                         vfeedctr.className = "invisible";
201                         vfeedr.className = vfeedr.className.replace("Unread", "");
202                 }
203         }
204
205         debug("toggle starred for aid " + id);
206
207         new Ajax.Request(query);
208
209 }
210
211 function moveToPost(mode) {
212
213         // check for combined mode
214         if (!document.getElementById("headlinesList"))
215                 return;
216
217         var rows = getVisibleHeadlineIds();
218
219         var prev_id;
220         var next_id;
221
222         if (!document.getElementById('RROW-' + active_post_id)) {
223                 active_post_id = false;
224         }
225
226         if (active_post_id == false) {
227                 next_id = getFirstVisibleHeadlineId();
228                 prev_id = getLastVisibleHeadlineId();
229         } else {        
230                 for (var i = 0; i < rows.length; i++) {
231                         if (rows[i] == active_post_id) {
232                                 prev_id = rows[i-1];
233                                 next_id = rows[i+1];                    
234                         }
235                 }
236         }
237
238         if (mode == "next") {
239                 if (next_id != undefined) {
240                         view(next_id, getActiveFeedId());
241                 }
242         }
243
244         if (mode == "prev") {
245                 if ( prev_id != undefined) {
246                         view(prev_id, getActiveFeedId());
247                 }
248         } 
249 }
250
251 function toggleUnread(id, cmode) {
252         try {
253                 if (!xmlhttp_ready(xmlhttp_rpc)) {
254                         printLockingError();
255                         return;
256                 }
257         
258                 var row = document.getElementById("RROW-" + id);
259                 if (row) {
260                         var nc = row.className;
261                         nc = nc.replace("Unread", "");
262                         nc = nc.replace("Selected", "");
263
264                         if (row.className.match("Unread")) {
265                                 row.className = nc;
266                         } else {
267                                 row.className = nc + "Unread";
268                         }
269
270                         if (!cmode) cmode = 2;
271
272                         var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
273                                 param_escape(id) + "&cmode=" + param_escape(cmode);
274
275                         notify_progress("Loading, please wait...");
276
277                         xmlhttp_rpc.open("GET", query, true);
278                         xmlhttp_rpc.onreadystatechange=all_counters_callback;
279                         xmlhttp_rpc.send(null);
280
281                 }
282
283
284         } catch (e) {
285                 exception_error("toggleUnread", e);
286         }
287 }
288
289 function selectionToggleUnread(cdm_mode, set_state, callback_func) {
290         try {
291                 if (!xmlhttp_ready(xmlhttp_rpc)) {
292                         printLockingError();
293                         return;
294                 }
295         
296                 var rows;
297
298                 if (cdm_mode) {
299                         rows = cdmGetSelectedArticles();
300                 } else {        
301                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
302                 }
303
304                 if (rows.length == 0) {
305                         alert(__("No articles are selected."));
306                         return;
307                 }
308
309                 for (i = 0; i < rows.length; i++) {
310                         var row = document.getElementById("RROW-" + rows[i]);
311                         if (row) {
312                                 var nc = row.className;
313                                 nc = nc.replace("Unread", "");
314                                 nc = nc.replace("Selected", "");
315
316                                 if (row.className.match("Unread")) {
317                                         row.className = nc + "Selected";
318                                 } else {
319                                         row.className = nc + "UnreadSelected";
320                                 }
321                         }
322                 }
323
324                 if (rows.length > 0) {
325
326                         var cmode = "";
327
328                         if (set_state == undefined) {
329                                 cmode = "2";
330                         } else if (set_state == true) {
331                                 cmode = "1";
332                         } else if (set_state == false) {
333                                 cmode = "0";
334                         }
335
336                         var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
337                                 param_escape(rows.toString()) + "&cmode=" + cmode;
338
339                         _catchup_callback_func = callback_func;
340
341                         notify_progress("Loading, please wait...");
342
343                         xmlhttp_rpc.open("GET", query, true);
344                         xmlhttp_rpc.onreadystatechange=catchup_callback;
345                         xmlhttp_rpc.send(null);
346
347                 }
348
349         } catch (e) {
350                 exception_error("selectionToggleUnread", e);
351         }
352 }
353
354 function selectionToggleMarked(cdm_mode) {
355         try {
356                 if (!xmlhttp_ready(xmlhttp_rpc)) {
357                         printLockingError();
358                         return;
359                 }
360         
361                 var rows;
362                 
363                 if (cdm_mode) {
364                         rows = cdmGetSelectedArticles();
365                 } else {        
366                         rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
367                 }       
368
369                 if (rows.length == 0) {
370                         alert(__("No articles are selected."));
371                         return;
372                 }
373
374                 for (i = 0; i < rows.length; i++) {
375                         var row = document.getElementById("RROW-" + rows[i]);
376                         var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
377
378                         if (row && mark_img) {
379
380                                 if (mark_img.alt == "Set mark") {
381                                         mark_img.src = "images/mark_set.png";
382                                         mark_img.alt = "Reset mark";
383                                         mark_img.setAttribute('onclick', 
384                                                 'javascript:toggleMark('+rows[i]+', false)');
385
386                                 } else {
387                                         mark_img.src = "images/mark_unset.png";
388                                         mark_img.alt = "Set mark";
389                                         mark_img.setAttribute('onclick', 
390                                                 'javascript:toggleMark('+rows[i]+', true)');
391                                 }
392                         }
393                 }
394
395                 if (rows.length > 0) {
396
397                         var query = "backend.php?op=rpc&subop=markSelected&ids=" +
398                                 param_escape(rows.toString()) + "&cmode=2";
399
400                         xmlhttp_rpc.open("GET", query, true);
401                         xmlhttp_rpc.onreadystatechange=all_counters_callback;
402                         xmlhttp_rpc.send(null);
403
404                 }
405
406         } catch (e) {
407                 exception_error("selectionToggleMarked", e);
408         }
409 }
410
411 function cdmGetSelectedArticles() {
412         var sel_articles = new Array();
413         var container = document.getElementById("headlinesInnerContainer");
414
415         for (i = 0; i < container.childNodes.length; i++) {
416                 var child = container.childNodes[i];
417
418                 if (child.id.match("RROW-") && child.className.match("Selected")) {
419                         var c_id = child.id.replace("RROW-", "");
420                         sel_articles.push(c_id);
421                 }
422         }
423
424         return sel_articles;
425 }
426
427 // mode = all,none,unread
428 function cdmSelectArticles(mode) {
429         var container = document.getElementById("headlinesInnerContainer");
430
431         for (i = 0; i < container.childNodes.length; i++) {
432                 var child = container.childNodes[i];
433
434                 if (child.id.match("RROW-")) {
435                         var aid = child.id.replace("RROW-", "");
436
437                         var cb = document.getElementById("RCHK-" + aid);
438
439                         if (mode == "all") {
440                                 if (!child.className.match("Selected")) {
441                                         child.className = child.className + "Selected";
442                                         cb.checked = true;
443                                 }
444                         } else if (mode == "unread") {
445                                 if (child.className.match("Unread") && !child.className.match("Selected")) {
446                                         child.className = child.className + "Selected";
447                                         cb.checked = true;
448                                 }
449                         } else {
450                                 child.className = child.className.replace("Selected", "");
451                                 cb.checked = false;
452                         }
453                 }               
454         }
455 }
456
457 function catchupPage() {
458
459         if (document.getElementById("headlinesList")) {
460                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
461                 selectionToggleUnread(false, false, 'viewCurrentFeed()');
462                 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
463         } else {
464                 cdmSelectArticles('all');
465                 selectionToggleUnread(true, false, 'viewCurrentFeed()')
466                 cdmSelectArticles('none');
467         }
468 }
469
470 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
471
472         if (!xmlhttp_ready(xmlhttp_rpc)) {
473                 printLockingError();
474         }
475
476         var title = prompt("Please enter label title:", "");
477
478         if (title) {
479
480                 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
481                         "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
482                         "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) + 
483                         "&title=" + param_escape(title);
484
485                 debug("LFS: " + query);
486         
487                 xmlhttp_rpc.open("GET", query, true);
488                 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
489                 xmlhttp_rpc.send(null);
490         }
491
492 }
493
494 function editArticleTags(id, feed_id, cdm_enabled) {
495         _tag_active_post_id = id;
496         _tag_active_feed_id = feed_id;
497         _tag_active_cdm = cdm_enabled;
498         try {
499                 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
500         } catch (e) { }
501         displayDlg('editArticleTags', id);
502 }
503
504
505 function tag_saved_callback() {
506         if (xmlhttp_rpc.readyState == 4) {
507                 try {
508                         debug("in tag_saved_callback");
509
510                         closeInfoBox();
511                         notify("");
512
513                         if (tagsAreDisplayed()) {
514                                 _reload_feedlist_after_view = true;
515                         }
516
517                         if (!_tag_active_cdm) {
518                                 if (active_post_id == _tag_active_post_id) {
519                                         debug("reloading current article");
520                                         view(_tag_active_post_id, _tag_active_feed_id);                 
521                                 }
522                         } else {
523                                 debug("reloading current feed");
524                                 viewCurrentFeed();
525                         }
526
527                 } catch (e) {
528                         exception_error("catchup_callback", e);
529                 }
530         }
531 }
532
533 function editTagsSave() {
534
535         if (!xmlhttp_ready(xmlhttp_rpc)) {
536                 printLockingError();
537         }
538
539         notify_progress("Saving article tags...");
540
541         var form = document.forms["tag_edit_form"];
542
543         var query = Form.serialize("tag_edit_form");
544
545         xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query, true);                      
546         xmlhttp_rpc.onreadystatechange=tag_saved_callback;
547         xmlhttp_rpc.send(null);
548
549 }
550
551 function editTagsInsert() {
552         try {
553
554                 var form = document.forms["tag_edit_form"];
555
556                 var found_tags = form.found_tags;
557                 var tags_str = form.tags_str;
558
559                 var tag = found_tags[found_tags.selectedIndex].value;
560
561                 if (tags_str.value.length > 0 && 
562                                 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
563
564                         tags_str.value = tags_str.value + ", ";
565                 }
566
567                 tags_str.value = tags_str.value + tag + ", ";
568
569                 found_tags.selectedIndex = 0;
570                 
571         } catch (e) {
572                 exception_error(e, "editTagsInsert");
573         }
574 }
575
576 function cdmWatchdog() {
577
578         try {
579
580                 var ctr = document.getElementById("headlinesInnerContainer");
581
582                 if (!ctr) return;
583
584                 var ids = new Array();
585
586                 var e = ctr.firstChild;
587
588                 while (e) {
589                         if (e.className && e.className == "cdmArticleUnread" && e.id &&
590                                         e.id.match("RROW-")) {
591
592                                 // article fits in viewport OR article is longer than viewport and
593                                 // its bottom is visible
594
595                                 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
596                                                 ctr.scrollTop + ctr.offsetHeight) {
597
598 //                                      debug(e.id + " is visible " + e.offsetTop + "." + 
599 //                                              (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
600 //                                              (ctr.scrollTop + ctr.offsetHeight));
601
602                                         ids.push(e.id.replace("RROW-", ""));
603
604                                 } else if (e.offsetHeight > ctr.offsetHeight &&
605                                                 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
606                                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
607
608                                         ids.push(e.id.replace("RROW-", "")); 
609
610                                 }
611
612                                 // method 2: article bottom is visible and is in upper 1/2 of the viewport
613
614 /*                              if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
615                                                 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
616
617                                         ids.push(e.id.replace("RROW-", "")); 
618
619                                 } */
620
621                         }
622
623                         e = e.nextSibling;
624                 }
625
626                 debug("cdmWatchdog, ids= " + ids.toString());
627
628                 if (ids.length > 0 && xmlhttp_ready(xmlhttp_rpc)) {
629
630                         for (var i = 0; i < ids.length; i++) {
631                                 var e = document.getElementById("RROW-" + ids[i]);
632                                 if (e) {
633                                         e.className = e.className.replace("Unread", "");
634                                 }
635                         }
636
637                         var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
638                                 param_escape(ids.toString()) + "&cmode=0";
639
640                         xmlhttp_rpc.open("GET", query, true);
641                         xmlhttp_rpc.onreadystatechange=all_counters_callback;
642                         xmlhttp_rpc.send(null); 
643
644                 }
645
646                 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
647
648         } catch (e) {
649                 exception_error(e, "cdmWatchdog");
650         }
651
652 }