]> git.wh0rd.org - tt-rss.git/blobdiff - tt-rss.js
tweak README
[tt-rss.git] / tt-rss.js
index a986931f7bb5b009bd3cda826fbfa1418c1428c3..0af11bfca9d9a4e40e3f8b109c30f64a9ff32eac 100644 (file)
--- a/tt-rss.js
+++ b/tt-rss.js
@@ -1,14 +1,16 @@
-/*
-       This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>              
-       Licensed under GPL v.2 or (at your preference) any later version.
-*/
-
 var xmlhttp = false;
-var xmlhttp_rpc = false;
 
 var total_unread = 0;
 var first_run = true;
 
+var display_tags = false;
+
+var global_unread = 0;
+
+var active_title_text = "";
+
+var current_subtitle = "";
+
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5)
 // JScript gives us Conditional compilation, we can cope with old IE versions.
@@ -26,269 +28,535 @@ try {
 
 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        xmlhttp = new XMLHttpRequest();
-       xmlhttp_rpc = new XMLHttpRequest();
+}
 
+function toggleTags() {
+       display_tags = !display_tags;
+
+       var p = document.getElementById("dispSwitchPrompt");
+
+       if (display_tags) {
+               p.innerHTML = "display feeds";
+       } else {
+               p.innerHTML = "display tags";
+       }
+       
+       updateFeedList();
 }
 
-function printLockingError() {
-       notify("Please wait until operation finishes");
+function dlg_frefresh_callback() {
+       if (xmlhttp.readyState == 4) {
+               updateFeedList(false, false);
+               closeDlg();
+       } 
 }
 
-function notify_callback() {
-       var container = document.getElementById('notify');
+function dialog_refresh_callback() {
        if (xmlhttp.readyState == 4) {
-               container.innerHTML=xmlhttp.responseText;
-       }
+               var dlg = document.getElementById("userDlg");
+
+               dlg.innerHTML = xmlhttp.responseText;
+               dlg.style.display = "block";
+       } 
 }
 
-function feedlist_callback() {
-       var container = document.getElementById('feeds');
+function refetch_callback() {
        if (xmlhttp.readyState == 4) {
-               container.innerHTML=xmlhttp.responseText;
 
-               var feedtu = document.getElementById("FEEDTU");
+//             document.title = "Tiny Tiny RSS";
 
-               if (feedtu) {
-                       total_unread = feedtu.innerHTML;
-                       update_title();
+               if (!xmlhttp.responseXML) {
+                       notify("refetch_callback: backend did not return valid XML");
+                       return;
                }
+       
+               var reply = xmlhttp.responseXML.firstChild;
 
-               if (first_run) {
-                       scheduleFeedUpdate();
-                       first_run = false;
-               } else {
-                       notify("");
+               if (!reply) {
+                       notify("refetch_callback: backend did not return expected XML object");
+                       return;
+               } 
+
+               var error_code = reply.getAttribute("error-code");
+       
+               if (error_code && error_code != 0) {
+                       return fatalError(error_code);
                }
-       }
-}
 
-function viewfeed_callback() {
-       var container = document.getElementById('headlines');
-       if (xmlhttp.readyState == 4) {
-               container.innerHTML = xmlhttp.responseText;
+               var f_document = window.frames["feeds-frame"].document;
+
+               for (var l = 0; l < reply.childNodes.length; l++) {
+                       var id = reply.childNodes[l].getAttribute("id");
+                       var ctr = reply.childNodes[l].getAttribute("counter");
 
-               var factive = document.getElementById("FACTIVE");
-               var funread = document.getElementById("FUNREAD");
-               var ftotal = document.getElementById("FTOTAL");
+                       var feedctr = f_document.getElementById("FEEDCTR-" + id);
+                       var feedu = f_document.getElementById("FEEDU-" + id);
+                       var feedr = f_document.getElementById("FEEDR-" + id);
 
-               if (ftotal && factive && funread) {
-                       var feed_id = factive.innerHTML;
+/*                     TODO figure out how to update this from viewfeed.js->view()
+                       disabled for now...
 
-                       var feedr = document.getElementById("FEEDR-" + feed_id);
-                       var feedt = document.getElementById("FEEDT-" + feed_id);
-                       var feedu = document.getElementById("FEEDU-" + feed_id);
+                       if (id == "global-unread") {
+                               global_unread = ctr;
+                       } */
 
-                       feedt.innerHTML = ftotal.innerHTML;
-                       feedu.innerHTML = funread.innerHTML;
+                       if (feedctr && feedu && feedr) {
 
-                       if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
-                                       feedr.className = feedr.className + "Unread";
-                       } else if (feedu.innerHTML <= 0) {      
+                               feedu.innerHTML = ctr;
+       
+                               if (ctr > 0) {
+                                       feedctr.className = "odd";
+                                       if (!feedr.className.match("Unread")) {
+                                               feedr.className = feedr.className + "Unread";
+                                       }
+                               } else {
+                                       feedctr.className = "invisible";
                                        feedr.className = feedr.className.replace("Unread", "");
+                               }
                        }
+               }  
 
-               }
-
-               notify("");
+               updateTitle("");
+               notify("All feeds updated.");
 
-       }       
+       }
 }
 
-function view_callback() {
-       var container = document.getElementById('content');
+function backend_sanity_check_callback() {
+
        if (xmlhttp.readyState == 4) {
-               container.innerHTML=xmlhttp.responseText;               
-       }
+               
+               if (!xmlhttp.responseXML) {
+                       fatalError(3);
+                       return;
+               }
+
+               var reply = xmlhttp.responseXML.firstChild;
+
+               if (!reply) {
+                       fatalError(3);
+                       return;
+               }
+
+               var error_code = reply.getAttribute("error-code");
+       
+               if (error_code && error_code != 0) {
+                       return fatalError(error_code);
+               }
+
+               init_second_stage();
+       } 
 }
 
-function refetch_callback() {
-       if (xmlhttp_rpc.readyState == 4) {
-               // feeds are updated in background
-               updateFeedList(false, false);
-//             notify("All feeds updated");
-       }
+/* wtf this is obsolete
+function updateFeed(feed_id) {
+
+       var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
+
+       if (xmlhttp_ready(xmlhttp)) {
+               xmlhttp.open("GET", query_str, true);
+               xmlhttp.onreadystatechange=feed_update_callback;
+               xmlhttp.send(null);
+       } else {
+               printLockingError();
+       }   
+
 }
+*/
 
-function scheduleFeedUpdate() {
+function scheduleFeedUpdate(force) {
 
        notify("Updating feeds in background...");
 
-       var query_str = "backend.php?op=rpc&subop=forceUpdateAllFeeds";
+//     document.title = "Tiny Tiny RSS - Updating...";
 
-       if (xmlhttp_rpc.readyState == 4 || xmlhttp_rpc.readyState == 0) {
-               xmlhttp_rpc.open("GET", query_str, true);
-               xmlhttp_rpc.onreadystatechange=refetch_callback;
-               xmlhttp_rpc.send(null);
+       updateTitle("Updating");
+
+       var query_str = "backend.php?op=rpc&subop=";
+
+       if (force) {
+               query_str = query_str + "forceUpdateAllFeeds";
        } else {
-               printLockingError();
+               query_str = query_str + "updateAllFeeds";
+       }
+
+       var omode;
+
+       if (display_tags) {
+               omode = "t";
+       } else {
+               omode = "fl";
        }
+
+       query_str = query_str + "&omode=" + omode;
+
+       if (xmlhttp_ready(xmlhttp)) {
+               xmlhttp.open("GET", query_str, true);
+               xmlhttp.onreadystatechange=refetch_callback;
+               xmlhttp.send(null);
+       } else {
+               printLockingError();
+       }   
 }
 
 function updateFeedList(silent, fetch) {
-       
-       if (silent != true) {
-               notify("Updating feed list...");
-       }
+
+//     if (silent != true) {
+//             notify("Loading feed list...");
+//     }
 
        var query_str = "backend.php?op=feeds";
 
+       if (display_tags) {
+               query_str = query_str + "&tags=1";
+       }
+
+       if (getActiveFeedId()) {
+               query_str = query_str + "&actid=" + getActiveFeedId();
+       }
+
        if (fetch) query_str = query_str + "&fetch=yes";
 
-       if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
-               xmlhttp.open("GET", query_str, true);
-               xmlhttp.onreadystatechange=feedlist_callback;
-               xmlhttp.send(null);
+       var feeds_frame = document.getElementById("feeds-frame");
+
+       feeds_frame.src = query_str;
+}
+
+function catchupAllFeeds() {
+
+       var query_str = "backend.php?op=feeds&subop=catchupAll";
+
+       notify("Marking all feeds as read...");
+
+       var feeds_frame = document.getElementById("feeds-frame");
+
+       feeds_frame.src = query_str;
+
+       global_unread = 0;
+       updateTitle("");
+
+}
+
+function viewCurrentFeed(skip, subop) {
+
+       if (getActiveFeedId()) {
+               viewfeed(getActiveFeedId(), skip, subop);
        } else {
-               printLockingError();
+               disableContainerChildren("headlinesToolbar", false, document);
+               viewfeed(-1, skip, subop); // FIXME
        }
 }
 
-function catchupPage(feed) {
+function viewfeed(feed, skip, subop) {
+       var f = window.frames["feeds-frame"];
+       f.viewfeed(feed, skip, subop);
+}
 
-       if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
-               printLockingError();
-               return
+function timeout() {
+       scheduleFeedUpdate(false);
+       setTimeout("timeout()", 1800*1000);
+}
+
+function resetSearch() {
+       var searchbox = document.getElementById("searchbox")
+
+       if (searchbox.value != "" && getActiveFeedId()) {       
+               searchbox.value = "";
+               viewfeed(getActiveFeedId(), 0, "");
        }
+}
 
-       var content = document.getElementById("headlinesList");
+function search() {
+       viewCurrentFeed(0, "");
+}
+
+function localPiggieFunction(enable) {
+       if (enable) {
+               var query_str = "backend.php?op=feeds&subop=piggie";
 
-       var rows = new Array();
+               if (xmlhttp_ready(xmlhttp)) {
 
-       for (i = 0; i < content.rows.length; i++) {
-               var row_id = content.rows[i].id.replace("RROW-", "");
-               if (row_id.length > 0) {
-                       rows.push(row_id);      
-                       content.rows[i].className = content.rows[i].className.replace("Unread", "");
+                       xmlhttp.open("GET", query_str, true);
+                       xmlhttp.onreadystatechange=feedlist_callback;
+                       xmlhttp.send(null);
                }
        }
+}
 
-       var feedr = document.getElementById("FEEDR-" + feed);
-       var feedu = document.getElementById("FEEDU-" + feed);
+function localHotkeyHandler(keycode) {
 
-       feedu.innerHTML = feedu.innerHTML - rows.length;
+/*     if (keycode == 78) {
+               return moveToPost('next');
+       }
 
-       if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
-                       feedr.className = feedr.className + "Unread";
-       } else if (feedu.innerHTML <= 0) {      
-                       feedr.className = feedr.className.replace("Unread", "");
-       } 
+       if (keycode == 80) {
+               return moveToPost('prev');
+       } */
 
-       var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" + 
-               param_escape(rows.toString());
+       if (keycode == 82) { // r
+               return scheduleFeedUpdate(true);
+       }
 
-       notify("Marking this page as read...");
+       if (keycode == 85) { // u
+               if (getActiveFeedId()) {
+                       return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
+               }
+       }
 
-       xmlhttp.open("GET", query_str, true);
-       xmlhttp.onreadystatechange=notify_callback;
-       xmlhttp.send(null);
+       if (keycode == 65) { // a
+               return toggleDispRead();
+       }
+
+//     notify("KC: " + keycode);
 
 }
 
-function catchupAllFeeds() {
+function updateTitle(s) {
+       var tmp = "Tiny Tiny RSS";
 
-       if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
-               printLockingError();
-               return
+       if (s && s.length > 0) {
+               current_subtitle = s;
        }
-       var query_str = "backend.php?op=feeds&subop=catchupAll";
 
-       notify("Marking all feeds as read...");
+       if (global_unread > 0) {
+               tmp = tmp + " (" + global_unread + ")";
+       }
 
-       xmlhttp.open("GET", query_str, true);
-       xmlhttp.onreadystatechange=feedlist_callback;
-       xmlhttp.send(null);
+       if (s) {
+               tmp = tmp + " - " + current_subtitle;
+       }
 
+       if (active_title_text.length > 0) {
+               tmp = tmp + " > " + active_title_text;
+       }
+
+       document.title = tmp;
 }
 
-function viewfeed(feed, skip, subop) {
+function genericSanityCheck() {
 
-//     document.getElementById('headlines').innerHTML='Loading headlines, please wait...';             
-//     document.getElementById('content').innerHTML='&nbsp;';          
+       if (!xmlhttp) fatalError(1);
 
-       if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
-               printLockingError();
-               return
+       setCookie("ttrss_vf_test", "TEST");
+       
+       if (getCookie("ttrss_vf_test") != "TEST") {
+               fatalError(2);
+       }
+
+/*     if (!xmlhttp) {
+               document.getElementById("headlines").innerHTML = 
+                       "<b>Fatal error:</b> This program requires XmlHttpRequest " + 
+                       "to function properly. Your browser doesn't seem to support it.";
+               return false;
        }
 
-       xmlhttp.open("GET", "backend.php?op=viewfeed&feed=" + param_escape(feed) +
-               "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) , true);
-       xmlhttp.onreadystatechange=viewfeed_callback;
+       setCookie("ttrss_vf_test", "TEST");
+       if (getCookie("ttrss_vf_test") != "TEST") {
+
+               document.getElementById("headlines").innerHTML = 
+                       "<b>Fatal error:</b> This program requires cookies " + 
+                       "to function properly. Your browser doesn't seem to support them.";
+
+               return false;
+       } */
+
+       return true;
+}
+
+function init() {
+
+       disableContainerChildren("headlinesToolbar", true);
+
+       if (!genericSanityCheck()) 
+               return;
+
+       xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
+       xmlhttp.onreadystatechange=backend_sanity_check_callback;
        xmlhttp.send(null);
 
-       notify("Loading headlines...");
+}
+
+function init_second_stage() {
+
+       setCookie("ttrss_vf_actfeed", "");
+
+       updateFeedList(false, false);
+       document.onkeydown = hotkey_handler;
+
+       var content = document.getElementById("content");
+
+       if (getCookie("ttrss_vf_vmode")) {
+               var viewbox = document.getElementById("viewbox");
+               viewbox.value = getCookie("ttrss_vf_vmode");
+       }
+
+       if (getCookie("ttrss_vf_limit")) {
+               var limitbox = document.getElementById("limitbox");
+               limitbox.value = getCookie("ttrss_vf_limit");
+       }
+
+//     if (getCookie("ttrss_vf_actfeed")) {
+//             viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
+//     }
+
+//     setTimeout("timeout()", 2*1000);
+//     scheduleFeedUpdate(true);
+
+       var splash = document.getElementById("splash");
+
+       if (splash) {
+               splash.style.display = "none";
+       }
 
 }
 
-function view(id,feed_id) {
+function quickMenuGo() {
 
-       if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
-               printLockingError();
-               return
+       var chooser = document.getElementById("quickMenuChooser");
+       var opid = chooser[chooser.selectedIndex].id;
+
+       if (opid == "qmcPrefs") {
+               gotoPreferences();
        }
 
-       var crow = document.getElementById("RROW-" + id);
+       if (opid == "qmcAdvSearch") {
+               displayDlg("search");
+               return;
+       }
+
+       if (opid == "qmcAddFeed") {
+               displayDlg("quickAddFeed");
+               return;
+       }
 
-       if (crow.className.match("Unread")) {
-               var umark = document.getElementById("FEEDU-" + feed_id);
-               umark.innerHTML = umark.innerHTML - 1;
-               crow.className = crow.className.replace("Unread", "");
+       if (opid == "qmcRemoveFeed") {
+               var actid = getActiveFeedId();
 
-               if (umark.innerHTML == "0") {
-                       var feedr = document.getElementById("FEEDR-" + feed_id);
-                       feedr.className = feedr.className.replace("Unread", "");
+               if (!actid) {
+                       notify("Please select some feed first.");
+                       return;
                }
        
-               total_unread--;
+               displayDlg("quickDelFeed", actid);
+               return;
+       }
 
-               update_title(); 
-       }       
+       if (opid == "qmcUpdateFeeds") {
+               scheduleFeedUpdate(true);
+               return;
+       }
 
-       var upd_img_pic = document.getElementById("FUPDPIC-" + id);
+       if (opid == "qmcCatchupAll") {
+               catchupAllFeeds();
+               return;
+       }
 
-       if (upd_img_pic) {
-               upd_img_pic.innerHTML = "";
-       } 
+       if (opid == "qmcShowOnlyUnread") {
+               toggleDispRead();
+               return;
+       }
 
-       document.getElementById('content').innerHTML='Loading, please wait...';         
+}
 
-       xmlhttp.open("GET", "backend.php?op=view&id=" + param_escape(id), true);
-       xmlhttp.onreadystatechange=view_callback;
-       xmlhttp.send(null);
+function qafAdd() {
+
+       if (!xmlhttp_ready(xmlhttp)) {
+               printLockingError();
+               return
+       }
+
+       var link = document.getElementById("qafInput");
+
+       if (link.value.length == 0) {
+               notify("Missing feed URL.");
+       } else {
+               notify("Adding feed...");
+               
+               var feeds_doc = window.frames["feeds-frame"].document;
+
+               feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
+
+               xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&link=" +
+                       param_escape(link.value), true);
+               xmlhttp.onreadystatechange=dlg_frefresh_callback;
+               xmlhttp.send(null);
 
+               link.value = "";
+
+       }
 }
 
-function timeout() {
+function displayDlg(id, param) {
 
-       scheduleFeedUpdate();
+       notify("");
 
-       setTimeout("timeout()", 1800*1000);
+       xmlhttp.open("GET", "backend.php?op=dlg&id=" +
+               param_escape(id) + "&param=" + param_escape(param), true);
+       xmlhttp.onreadystatechange=dialog_refresh_callback;
+       xmlhttp.send(null);
 
 }
 
-function search(feed, sender) {
+function closeDlg() {
+       var dlg = document.getElementById("userDlg");
+       dlg.style.display = "none";
+}
 
-       if (xmlhttp.readyState != 4 && xmlhttp.readyState != 0) {
-               printLockingError();
-               return
-       }
+function qfdDelete(feed_id) {
 
-       notify("Search: " + feed + ", " + sender.value)
+       notify("Removing feed...");
 
-       document.getElementById('headlines').innerHTML='Loading headlines, please wait...';             
-       document.getElementById('content').innerHTML='&nbsp;';          
+       var feeds_doc = window.frames["feeds-frame"].document;
+       feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
 
-       xmlhttp.open("GET", "backend.php?op=viewfeed&feed=" + param_escape(feed) +
-               "&search=" + param_escape(sender.value) + "&ext=SEARCH", true);
-       xmlhttp.onreadystatechange=viewfeed_callback;
+       xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=remove&ids=" + feed_id);
+       xmlhttp.onreadystatechange=dlg_frefresh_callback;
        xmlhttp.send(null);
+}
+
+
+function allFeedsMenuGo() {
+       var chooser = document.getElementById("allFeedsChooser");
+
+       var opname = chooser[chooser.selectedIndex].text;
+
+       if (opname == "Update") {
+               scheduleFeedUpdate(true);
+               return;
+       }
+
+       if (opname == "Mark as read") {
+               catchupAllFeeds();
+               return;
+       }
+
+       if (opname == "Show only unread") {
+               toggleDispRead();
+               return;
+       }
 
 }
 
-function update_title() {
-       //document.title = "Tiny Tiny RSS (" + total_unread + " unread)";
+function updateFeedTitle(t) {
+       active_title_text = t;
+       updateTitle();
 }
 
-function init() {
-       updateFeedList(false, false);
-       setTimeout("timeout()", 1800*1000);
+function toggleDispRead() {
+       var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
+
+       hide_read_feeds = !hide_read_feeds;
+
+       var feeds_doc = window.frames["feeds-frame"].document;
+
+       hideOrShowFeeds(feeds_doc, hide_read_feeds);
+
+       if (hide_read_feeds) {
+               setCookie("ttrss_vf_hreadf", 1);
+       } else {
+               setCookie("ttrss_vf_hreadf", 0);
+       }
+
 }
+
+