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