]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
fix feeds frame size for Opera
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var _qfd_deleted_feed = 0;
10 var firsttime_update = true;
11
12 /*@cc_on @*/
13 /*@if (@_jscript_version >= 5)
14 // JScript gives us Conditional compilation, we can cope with old IE versions.
15 // and security blocked creation of the objects.
16 try {
17         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
18 } catch (e) {
19         try {
20                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
21         } catch (E) {
22                 xmlhttp = false;
23         }
24 }
25 @end @*/
26
27 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
28         xmlhttp = new XMLHttpRequest();
29 }
30
31 function toggleTags() {
32         display_tags = !display_tags;
33
34         var p = document.getElementById("dispSwitchPrompt");
35
36         if (display_tags) {
37                 p.innerHTML = "display feeds";
38         } else {
39                 p.innerHTML = "display tags";
40         }
41         
42         updateFeedList();
43 }
44
45 function dlg_frefresh_callback() {
46         if (xmlhttp.readyState == 4) {
47                 notify(xmlhttp.responseText);
48                 updateFeedList(false, false);
49                 if (_qfd_deleted_feed) {
50                         var hframe = document.getElementById("headlines-frame");
51                         if (hframe) {
52                                 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
53                         }
54                 }
55                 closeDlg();
56         } 
57 }
58
59 function dlg_submit_callback() {
60         if (xmlhttp.readyState == 4) {
61                 notify(xmlhttp.responseText);
62                 closeDlg();
63         } 
64 }
65
66 function dlg_display_callback() {
67         if (xmlhttp.readyState == 4) {
68                 var dlg = document.getElementById("userDlg");
69                 var dlg_s = document.getElementById("userDlgShadow");
70
71                 dlg.innerHTML = xmlhttp.responseText;
72                 dlg_s.style.display = "block";
73         } 
74 }
75
76 function hide_unread_callback() {
77         if (xmlhttp.readyState == 4) {
78
79                 try {
80
81                         var reply = xmlhttp.responseXML.firstChild.firstChild;
82                         var value = reply.getAttribute("value");
83                         var hide_read_feeds = (value != "false")
84                         var feeds_doc = window.frames["feeds-frame"].document;
85         
86                         hideOrShowFeeds(feeds_doc, hide_read_feeds);
87         
88                         if (hide_read_feeds) {
89                                 setCookie("ttrss_vf_hreadf", 1);
90                         } else {
91                                 setCookie("ttrss_vf_hreadf", 0);
92                         } 
93
94                 } catch (e) {
95                         exception_error("hide_unread_callback", e);
96                 }
97
98         } 
99 }
100
101 function refetch_callback() {
102         if (xmlhttp.readyState == 4) {
103                 try {
104
105                         if (!xmlhttp.responseXML) {
106                                 notify("refetch_callback: backend did not return valid XML");
107                                 return;
108                         }
109                 
110                         var reply = xmlhttp.responseXML.firstChild;
111         
112                         if (!reply) {
113                                 notify("refetch_callback: backend did not return expected XML object");
114                                 updateTitle("");
115                                 return;
116                         } 
117         
118                         var error_code = reply.getAttribute("error-code");
119                 
120                         if (error_code && error_code != 0) {
121                                 return fatalError(error_code);
122                         }
123         
124                         var f_document = window.frames["feeds-frame"].document;
125
126                         parse_counters(reply, f_document, window);
127
128                         if (!daemon_enabled) {
129                                 notify("All feeds updated.");
130                                 updateTitle("");
131                         }
132                 } catch (e) {
133                         exception_error("refetch_callback", e);
134                         updateTitle("");
135                 }
136         }
137 }
138
139 function backend_sanity_check_callback() {
140
141         if (xmlhttp.readyState == 4) {
142
143                 try {
144                 
145                         if (!xmlhttp.responseXML) {
146                                 fatalError(3);
147                                 return;
148                         }
149         
150                         var reply = xmlhttp.responseXML.firstChild;
151         
152                         if (!reply) {
153                                 fatalError(3);
154                                 return;
155                         }
156         
157                         var error_code = reply.getAttribute("error-code");
158                 
159                         if (error_code && error_code != 0) {
160                                 return fatalError(error_code);
161                         }
162         
163                         init_second_stage();
164
165                 } catch (e) {
166                         exception_error("backend_sanity_check_callback", e);
167                 }
168         } 
169 }
170
171 function scheduleFeedUpdate(force) {
172
173         if (!daemon_enabled) {
174                 notify("Updating feeds, please wait.");
175                 updateTitle("Updating");
176         }
177
178         var query_str = "backend.php?op=rpc&subop=";
179
180         if (force) {
181                 query_str = query_str + "forceUpdateAllFeeds";
182         } else {
183                 query_str = query_str + "updateAllFeeds";
184         }
185
186         var omode;
187
188         if (firsttime_update) {
189                 firsttime_update = false;
190                 omode = "T";
191         } else {
192                 if (display_tags) {
193                         omode = "t";
194                 } else {
195                         omode = "flc";
196                 }
197         }
198         
199         query_str = query_str + "&omode=" + omode;
200         query_str = query_str + "&uctr=" + global_unread;
201
202
203         if (xmlhttp_ready(xmlhttp)) {
204                 xmlhttp.open("GET", query_str, true);
205                 xmlhttp.onreadystatechange=refetch_callback;
206                 xmlhttp.send(null);
207         } else {
208                 printLockingError();
209         }   
210 }
211
212 function updateFeedList(silent, fetch) {
213
214 //      if (silent != true) {
215 //              notify("Loading feed list...");
216 //      }
217
218         var query_str = "backend.php?op=feeds";
219
220         if (display_tags) {
221                 query_str = query_str + "&tags=1";
222         }
223
224         if (getActiveFeedId()) {
225                 query_str = query_str + "&actid=" + getActiveFeedId();
226         }
227
228         if (fetch) query_str = query_str + "&fetch=yes";
229
230         var feeds_frame = document.getElementById("feeds-frame");
231
232         feeds_frame.src = query_str;
233 }
234
235 function catchupAllFeeds() {
236
237         var query_str = "backend.php?op=feeds&subop=catchupAll";
238
239         notify("Marking all feeds as read...");
240
241         var feeds_frame = document.getElementById("feeds-frame");
242
243         feeds_frame.src = query_str;
244
245         global_unread = 0;
246         updateTitle("");
247
248 }
249
250 function viewCurrentFeed(skip, subop) {
251
252         if (getActiveFeedId()) {
253                 viewfeed(getActiveFeedId(), skip, subop);
254         } else {
255                 disableContainerChildren("headlinesToolbar", false, document);
256                 viewfeed(-1, skip, subop); // FIXME
257         }
258 }
259
260 function viewfeed(feed, skip, subop) {
261         var f = window.frames["feeds-frame"];
262         f.viewfeed(feed, skip, subop);
263 }
264
265 function timeout() {
266         scheduleFeedUpdate(false);
267
268         var refresh_time = getCookie('ttrss_vf_refresh');
269
270         if (!refresh_time) refresh_time = 600;
271
272         setTimeout("timeout()", refresh_time*1000);
273 }
274
275 function resetSearch() {
276         var searchbox = document.getElementById("searchbox")
277
278         if (searchbox.value != "" && getActiveFeedId()) {       
279                 searchbox.value = "";
280                 viewfeed(getActiveFeedId(), 0, "");
281         }
282 }
283
284 function search() {
285         closeDlg();     
286         viewCurrentFeed(0, "");
287 }
288
289 function localPiggieFunction(enable) {
290         if (enable) {
291                 var query_str = "backend.php?op=feeds&subop=piggie";
292
293                 if (xmlhttp_ready(xmlhttp)) {
294
295                         xmlhttp.open("GET", query_str, true);
296                         xmlhttp.onreadystatechange=feedlist_callback;
297                         xmlhttp.send(null);
298                 }
299         }
300 }
301
302 function localHotkeyHandler(keycode) {
303
304         if (keycode == 82) { // r
305                 return scheduleFeedUpdate(true);
306         }
307
308         if (keycode == 85) { // u
309                 if (getActiveFeedId()) {
310                         return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
311                 }
312         }
313
314         if (keycode == 65) { // a
315                 return toggleDispRead();
316         }
317
318         var f_doc = window.frames["feeds-frame"].document;
319         var feedlist = f_doc.getElementById('feedList');
320
321         if (keycode == 74) { // j
322                 var feed = getActiveFeedId();
323                 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
324                 if (new_feed) viewfeed(new_feed, 0, '');
325         }
326
327         if (keycode == 75) { // k
328                 var feed = getActiveFeedId();
329                 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
330                 if (new_feed) viewfeed(new_feed, 0, '');
331         }
332
333 //      notify("KC: " + keycode);
334
335 }
336
337 // if argument is undefined, current subtitle is not updated
338 // use blank string to clear subtitle
339 function updateTitle(s) {
340         var tmp = "Tiny Tiny RSS";
341
342         if (s != undefined) {
343                 current_subtitle = s;
344         }
345
346         if (global_unread > 0) {
347                 tmp = tmp + " (" + global_unread + ")";
348         }
349
350         if (current_subtitle) {
351                 tmp = tmp + " - " + current_subtitle;
352         }
353
354         if (active_title_text.length > 0) {
355                 tmp = tmp + " > " + active_title_text;
356         }
357
358         document.title = tmp;
359 }
360
361 function genericSanityCheck() {
362
363         if (!xmlhttp) fatalError(1);
364
365         setCookie("ttrss_vf_test", "TEST");
366         
367         if (getCookie("ttrss_vf_test") != "TEST") {
368                 fatalError(2);
369         }
370
371         return true;
372 }
373
374 function init() {
375
376         try {
377
378                 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
379
380                 if (arguments.callee.done) return;
381                 arguments.callee.done = true;           
382
383                 disableContainerChildren("headlinesToolbar", true);
384
385                 if (!genericSanityCheck()) 
386                         return;
387
388                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
389                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
390                 xmlhttp.send(null);
391
392         } catch (e) {
393                 exception_error("init", e);
394         }
395 }
396
397 function init_second_stage() {
398
399         try {
400
401                 setCookie("ttrss_vf_actfeed", "");
402         
403                 updateFeedList(false, false);
404                 document.onkeydown = hotkey_handler;
405         
406                 var viewbox = document.getElementById("viewbox");                       
407                 var limitbox = document.getElementById("limitbox");
408
409                 dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
410                 dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
411
412                 daemon_enabled = getCookie("ttrss_vf_daemon");
413
414                 if (navigator.userAgent.match("Opera")) {
415                         var f = document.getElementById("feeds-frame");
416                         f.style.height = document.body.scrollHeight - 200 + "px";
417                 }
418         
419         } catch (e) {
420                 exception_error("init_second_stage", e);
421         }
422 }
423
424 function quickMenuChange() {
425         var chooser = document.getElementById("quickMenuChooser");
426         var opid = chooser[chooser.selectedIndex].id;
427
428         chooser.selectedIndex = 0;
429         quickMenuGo(opid);
430 }
431
432 function quickMenuGo(opid) {
433
434
435         if (opid == "qmcPrefs") {
436                 gotoPreferences();
437         }
438
439         if (opid == "qmcSearch") {
440                 displayDlg("search", getActiveFeedId());
441                 return;
442         }
443
444         if (opid == "qmcAddFeed") {
445                 displayDlg("quickAddFeed");
446                 return;
447         }
448
449         if (opid == "qmcRemoveFeed") {
450                 var actid = getActiveFeedId();
451
452                 if (!actid) {
453                         notify("Please select some feed first.");
454                         return;
455                 }
456
457                 if (confirm("Remove current feed?")) {
458                         qfdDelete(actid);
459                 }
460         
461                 return;
462         }
463
464         if (opid == "qmcUpdateFeeds") {
465                 scheduleFeedUpdate(true);
466                 return;
467         }
468
469         if (opid == "qmcCatchupAll") {
470                 catchupAllFeeds();
471                 return;
472         }
473
474         if (opid == "qmcShowOnlyUnread") {
475                 toggleDispRead();
476                 return;
477         }
478
479         if (opid == "qmcAddFilter") {
480                 displayDlg("quickAddFilter", getActiveFeedId());
481         }
482
483 }
484
485 function qafAdd() {
486
487         if (!xmlhttp_ready(xmlhttp)) {
488                 printLockingError();
489                 return
490         }
491
492         var link = document.getElementById("qafInput");
493
494         if (link.value.length == 0) {
495                 notify("Missing feed URL.");
496         } else {
497                 notify("Adding feed...");
498         
499                 var cat = document.getElementById("qafCat");
500                 var cat_id = "";
501                 
502                 if (cat) {
503                         cat_id = cat[cat.selectedIndex].id;
504                 } else {
505                         cat_id = 0;
506                 }
507
508                 var feeds_doc = window.frames["feeds-frame"].document;
509
510                 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
511
512                 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=add&link=" +
513                         param_escape(link.value) + "&cid=" + param_escape(cat_id), true);
514                 xmlhttp.onreadystatechange=dlg_frefresh_callback;
515                 xmlhttp.send(null);
516
517                 link.value = "";
518
519         }
520 }
521
522 function qaddFilter() {
523
524         if (!xmlhttp_ready(xmlhttp)) {
525                 printLockingError();
526                 return
527         }
528
529         var regexp = document.getElementById("fadd_regexp");
530         var match = document.getElementById("fadd_match");
531         var feed = document.getElementById("fadd_feed");
532         var action = document.getElementById("fadd_action");
533
534         if (regexp.value.length == 0) {
535                 notify("Missing filter expression.");
536         } else {
537                 notify("Adding filter...");
538
539                 var v_match = match[match.selectedIndex].text;
540                 var feed_id = feed[feed.selectedIndex].id;
541                 var action_id = action[action.selectedIndex].id;
542
543                 xmlhttp.open("GET", "backend.php?op=pref-filters&quiet=1&subop=add&regexp=" +
544                         param_escape(regexp.value) + "&match=" + v_match +
545                         "&fid=" + param_escape(feed_id) + "&aid=" + param_escape(action_id), true);
546                         
547                 xmlhttp.onreadystatechange=dlg_submit_callback;
548                 xmlhttp.send(null);
549
550                 regexp.value = "";
551         }
552
553 }
554
555
556 function displayDlg(id, param) {
557
558         if (!xmlhttp_ready(xmlhttp)) {
559                 printLockingError();
560                 return
561         }
562
563         notify("");
564
565         xmlhttp.open("GET", "backend.php?op=dlg&id=" +
566                 param_escape(id) + "&param=" + param_escape(param), true);
567         xmlhttp.onreadystatechange=dlg_display_callback;
568         xmlhttp.send(null);
569
570         disableHotkeys();
571 }
572
573 function closeDlg() {
574         var dlg = document.getElementById("userDlgShadow");
575         dlg.style.display = "none";
576         enableHotkeys();
577 }
578
579 function qfdDelete(feed_id) {
580
581         notify("Removing feed...");
582
583         if (!xmlhttp_ready(xmlhttp)) {
584                 printLockingError();
585                 return
586         }
587
588 //      var feeds_doc = window.frames["feeds-frame"].document;
589 //      feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
590
591         _qfd_deleted_feed = feed_id;
592
593         xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
594         xmlhttp.onreadystatechange=dlg_frefresh_callback;
595         xmlhttp.send(null);
596 }
597
598
599 function allFeedsMenuChange() {
600         var chooser = document.getElementById("allFeedsChooser");
601
602         var opname = chooser[chooser.selectedIndex].text;
603
604         chooser.selectedIndex = 0;
605
606         if (opname == "Update") {
607                 scheduleFeedUpdate(true);
608                 return;
609         }
610
611         if (opname == "Mark as read") {
612                 catchupAllFeeds();
613                 return;
614         }
615
616         if (opname == "Show only unread") {
617                 toggleDispRead();
618                 return;
619         }
620
621 }
622
623 function updateFeedTitle(t) {
624         active_title_text = t;
625         updateTitle();
626 }
627
628 function toggleDispRead() {
629         try {
630
631                 if (!xmlhttp_ready(xmlhttp)) {
632                         printLockingError();
633                         return
634                 }
635
636                 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
637
638                 hide_read_feeds = !hide_read_feeds;
639         
640                 var query = "backend.php?op=rpc&subop=setpref" +
641                         "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
642
643                 xmlhttp.open("GET", query);
644                 xmlhttp.onreadystatechange=hide_unread_callback;
645                 xmlhttp.send(null);
646                 
647         } catch (e) {
648                 exception_error("toggleDispRead", e);
649         }
650 }
651