]> git.wh0rd.org - tt-rss.git/blobdiff - functions.js
outputHeadlinesList: don't try to force update categories
[tt-rss.git] / functions.js
index 2266bfc25e528aa774e8317f2c5b912a816403f1..a0092f932fbd5e5befa818d9753172910d9ff6b7 100644 (file)
@@ -15,9 +15,9 @@ Array.prototype.remove = function(s) {
 /* create console.log if it doesn't exist */
 
 if (!window.console) console = {};
-console.log = console.log || function(msg) { debug(msg); };
-console.warn = console.warn || function(msg) { debug(msg); };
-console.error = console.error || function(msg) { debug(msg); };
+console.log = console.log || function(msg) { };
+console.warn = console.warn || function(msg) { };
+console.error = console.error || function(msg) { };
 
 function exception_error(location, e, ext_info) {
        var msg = format_exception_error(location, e);
@@ -383,18 +383,18 @@ function parse_counters(reply, scheduled_call) {
 
                var feeds_found = 0;
 
-               var elems = reply.getElementsByTagName("counter");
+               var elems = JSON.parse(reply.firstChild.nodeValue);
 
                for (var l = 0; l < elems.length; l++) {
 
-                       var id = elems[l].getAttribute("id");
-                       var t = elems[l].getAttribute("type");
-                       var ctr = elems[l].getAttribute("counter");
-                       var error = elems[l].getAttribute("error");
-                       var has_img = elems[l].getAttribute("hi");
-                       var updated = elems[l].getAttribute("updated");
-                       var title = elems[l].getAttribute("title");
-                       var xmsg = elems[l].getAttribute("xmsg");
+                       var id = elems[l].id
+                       var kind = elems[l].kind;
+                       var ctr = parseInt(elems[l].counter)
+                       var error = elems[l].error;
+                       var has_img = elems[l].has_img;
+                       var updated = elems[l].updated;
+                       var title = elems[l].title;
+                       var xmsg = elems[l].xmsg;
        
                        if (id == "global-unread") {
 
@@ -412,7 +412,7 @@ function parse_counters(reply, scheduled_call) {
                                continue;
                        }
        
-                       if (t == "category") {
+                       if (kind && kind == "cat") {
                                var catctr = $("FCATCTR-" + id);
                                if (catctr) {
                                        catctr.innerHTML = "(" + ctr + ")";
@@ -460,7 +460,7 @@ function parse_counters(reply, scheduled_call) {
 
                        if (has_img && feed_img) {
                                if (!feed_img.src.match(id + ".ico")) {
-                                       feed_img.src = getInitParam("icons_location") + "/" + id + ".ico";
+                                       feed_img.src = getInitParam("icons_url") + "/" + id + ".ico";
                                }
                        }
 
@@ -562,11 +562,13 @@ function parse_counters_reply(transport, scheduled_call) {
 
        var counters = reply.getElementsByTagName("counters")[0];
        
-       parse_counters(counters, scheduled_call);
+       if (counters)
+               parse_counters(counters, scheduled_call);
 
        var runtime_info = reply.getElementsByTagName("runtime-info")[0];
 
-       parse_runtime_info(runtime_info);
+       if (runtime_info)
+               parse_runtime_info(runtime_info);
 
        if (feedsSortByUnread()) {
                resort_feedlist();
@@ -579,12 +581,9 @@ function parse_counters_reply(transport, scheduled_call) {
 function all_counters_callback2(transport, async_call) {
        try {
                if (async_call) async_counters_work = true;
-               
                if (offline_mode) return;
 
-               console.log("<b>all_counters_callback2 IN: " + transport + "</b>");
                parse_counters_reply(transport);
-               console.log("<b>all_counters_callback2 OUT: " + transport + "</b>");
 
        } catch (e) {
                exception_error("all_counters_callback2", e, transport);
@@ -651,8 +650,10 @@ function resort_category(node, cat_mode) {
                                var tmp_name = get_feed_entry_name(list[i]);
                                var cur_name = get_feed_entry_name(list[j]);
 
-                               var valid_pair = cat_mode || (list[i].id.match(/FEEDR-[0-9]/) &&
-                                               list[j].id.match(/FEEDR-[0-9]/));
+                               /* we don't want to match FEEDR-0 - e.g. Archived articles */
+
+                               var valid_pair = cat_mode || (list[i].id.match(/FEEDR-[1-9]/) &&
+                                               list[j].id.match(/FEEDR-[1-9]/));
 
                                if (valid_pair && ((by_unread && (cur_val > tmp_val)) || (!by_unread && (cur_name < tmp_name)))) {
                                        tempnode_i = list[i].cloneNode(true);
@@ -1093,70 +1094,6 @@ function getRelativeFeedId2(id, is_cat, direction, unread_only) {
        }
 }
 
-function getRelativeFeedId(list, id, direction, unread_only) { 
-       var rows = list.getElementsByTagName("LI");
-       var feeds = new Array();
-
-       for (var i = 0; i < rows.length; i++) {
-               if (rows[i].id.match("FEEDR-")) {
-
-                       if (rows[i].id == "FEEDR-" + id || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
-
-                               if (!unread_only || 
-                                               (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
-                                       feeds.push(rows[i].id.replace("FEEDR-", ""));
-                               }
-                       }
-               }
-       }
-
-       if (!id) {
-               if (direction == "next") {
-                       return feeds.shift();
-               } else {
-                       return feeds.pop();
-               }
-       } else {
-               if (direction == "next") {
-                       var idx = feeds.indexOf(id);
-                       if (idx != -1 && idx < feeds.length) {
-                               return feeds[idx+1];                                    
-                       } else {
-                               return getRelativeFeedId(list, false, direction, unread_only);
-                       }
-               } else {
-                       var idx = feeds.indexOf(id);
-                       if (idx > 0) {
-                               return feeds[idx-1];
-                       } else {
-                               return getRelativeFeedId(list, false, direction, unread_only);
-                       }
-               }
-
-       }
-}
-
-function showBlockElement(id, h_id) {
-       var elem = $(id);
-
-       if (elem) {
-               elem.style.display = "block";
-
-               if (h_id) {
-                       elem = $(h_id);
-                       if (elem) {
-                               elem.style.display = "none";
-                       }
-               }
-       } else {
-               alert("[showBlockElement] can't find element with id " + id);
-       } 
-}
-
-function appearBlockElement_afh(effect) {
-
-}
-
 function checkboxToggleElement(elem, id) {
        if (elem.checked) {
                Effect.Appear(id, {duration : 0.5});
@@ -1165,23 +1102,6 @@ function checkboxToggleElement(elem, id) {
        }
 }
 
-function appearBlockElement(id, h_id) {
-
-       try {
-               if (h_id) {
-                       Effect.Fade(h_id);
-               }
-               Effect.SlideDown(id, {duration : 1.0, afterFinish: appearBlockElement_afh});
-       } catch (e) {
-               exception_error("appearBlockElement", e);
-       }
-
-}
-
-function hideParentElement(e) {
-       e.parentNode.style.display = "none";
-}
-
 function dropboxSelect(e, v) {
        for (i = 0; i < e.length; i++) {
                if (e[i].value == v) {
@@ -1382,30 +1302,68 @@ function subscribeToFeed() {
                onComplete: function(transport) { 
                        //dlg_frefresh_callback(transport); 
 
-                       notify('');
+                       try {
 
-                       var result = transport.responseXML.getElementsByTagName('result')[0];
-                       var rc = parseInt(result.getAttribute('code'));
-
-                       Form.enable("feed_add_form");
-
-                       switch (rc) {
-                       case 1:
-                               closeInfoBox();
-                               notify_info(__("Subscribed to %s").replace("%s", feed_url));
+                               if (!transport.responseXML)
+                                       console.log(transport.responseText);
 
-                               if (inPreferences()) {
-                                       updateFeedList();
-                               } else {
-                                       setTimeout('updateFeedList(false, false)', 50);
+                               var result = transport.responseXML.getElementsByTagName('result')[0];
+                               var rc = parseInt(result.getAttribute('code'));
+       
+                               Form.enable("feed_add_form");
+       
+                               notify('');
+       
+                               switch (rc) {
+                               case 1:
+                                       closeInfoBox();
+                                       notify_info(__("Subscribed to %s").replace("%s", feed_url));
+       
+                                       if (inPreferences()) {
+                                               updateFeedList();
+                                       } else {
+                                               setTimeout('updateFeedList(false, false)', 50);
+                                       }
+                                       break;
+                               case 2:
+                                       alert(__("Specified URL seems to be invalid."));
+                                       break;
+                               case 3:
+                                       alert(__("Specified URL doesn't seem to contain any feeds."));
+                                       break;
+                               case 4:
+                                       new Ajax.Request("backend.php", {
+                                               parameters: 'op=rpc&subop=extractfeedurls&url=' + encodeURIComponent(feed_url),
+                                               onComplete: function(transport) {
+                                                       var result = transport.responseXML.getElementsByTagName('urls')[0];
+                                                       var feeds = JSON.parse(result.firstChild.nodeValue);
+                                                       var select = document.getElementById("faad_feeds_container_select");
+       
+                                                       while (select.hasChildNodes()) {
+                                                               select.removeChild(elem.firstChild);
+                                                       }
+                                                       var count = 0;
+                                                       for (var feedUrl in feeds) {
+                                                               select.insert(new Option(feeds[feedUrl], feedUrl, false));
+                                                               count++;
+                                                       }
+                                                       if (count > 5) count = 5;
+                                                       select.size = count;
+       
+                                                       Effect.Appear('fadd_feeds_container', {duration : 0.5});
+                                               }
+                                       });
+                                       break;
+                               case 5:
+                                       alert(__("Couldn't download the specified URL."));
+                                       break;
+                               case 0:
+                                       alert(__("You are already subscribed to this feed."));
+                                       break;
                                }
-                               break;
-                       case 2:
-                               alert(__("Can't subscribe to the specified URL."));
-                               break;
-                       case 0:
-                               alert(__("You are already subscribed to this feed."));
-                               break;
+
+                       } catch (e) {
+                               exception_error("subscribeToFeed", e);
                        }
 
                } });
@@ -1438,28 +1396,6 @@ function filterCR(e, f)
        }
 }
 
-var debug_last_class = "even";
-
-function debug(msg) {
-
-       if (debug_last_class == "even") {
-               debug_last_class = "odd";
-       } else {
-               debug_last_class = "even";
-       }
-
-       var c = $('debug_output');
-       if (c && Element.visible(c)) {
-               while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
-                       c.removeChild(c.lastChild);
-               }
-       
-               var ts = make_timestamp();
-               c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " + 
-                       msg + "</li>" + c.innerHTML;
-       }
-}
-
 function getInitParam(key) {
        return init_params[key];
 }
@@ -1476,7 +1412,7 @@ function fatalError(code, msg, ext_info) {
                if (code == 6) {
                        window.location.href = "tt-rss.php";                    
                } else if (code == 5) {
-                       window.location.href = "update.php";
+                       window.location.href = "db-updater.php";
                } else {
        
                        if (msg == "") msg = "Unknown error";
@@ -1737,36 +1673,6 @@ function openArticleInNewWindow(id) {
        }
 }
 
-/* http://textsnippets.com/posts/show/835 */
-
-Position.GetWindowSize = function(w) {
-        w = w ? w : window;
-        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
-        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
-        return [width, height]
-}
-
-/* http://textsnippets.com/posts/show/836 */
-
-Position.Center = function(element, parent) {
-        var w, h, pw, ph;
-        var d = Element.getDimensions(element);
-        w = d.width;
-        h = d.height;
-        Position.prepare();
-        if (!parent) {
-                var ws = Position.GetWindowSize();
-                pw = ws[0];
-                ph = ws[1];
-        } else {
-                pw = parent.offsetWidth;
-                ph = parent.offsetHeight;
-        }
-        element.style.top = (ph/2) - (h/2) -  Position.deltaY + "px";
-        element.style.left = (pw/2) - (w/2) -  Position.deltaX + "px";
-}
-
-
 function isCdmMode() {
        return !$("headlinesList");
 }
@@ -1802,21 +1708,11 @@ function displayHelpInfobox(topic_id) {
 
 }
 
-function focus_element(id) {
-       try {
-               var e = $(id);
-               if (e) e.focus();
-       } catch (e) {
-               exception_error("focus_element", e);
-       }
-       return false;
-}
-
 function loading_set_progress(p) {
        try {
                if (p < last_progress_point || !Element.visible("overlay")) return;
 
-               console.log("<b>loading_set_progress : " + p + " (" + last_progress_point + ")</b>");
+               console.log("loading_set_progress : " + p + " (" + last_progress_point + ")");
 
                var o = $("l_progress_i");
 
@@ -1971,7 +1867,7 @@ function displayNewContentPrompt(id) {
 
                $('auxDlg').innerHTML = msg;
 
-               Element.show('auxDlg');
+               new Effect.Appear('auxDlg', {duration : 0.5});
 
        } catch (e) {
                exception_error("displayNewContentPrompt", e);
@@ -2126,7 +2022,7 @@ function uploadFeedIcon() {
        }
 }
 
-function addLabel() {
+function addLabel(select, callback) {
 
        try {
 
@@ -2142,14 +2038,19 @@ function addLabel() {
                        var query = "?op=pref-labels&subop=add&caption=" + 
                                param_escape(caption);
 
+                       if (select)
+                               query += "&output=select";
+
                        notify_progress("Loading, please wait...", true);
 
-                       if (inPreferences()) active_tab = "labelConfig";
+                       if (inPreferences() && !select) active_tab = "labelConfig";
 
                        new Ajax.Request("backend.php", {
                                parameters: query,
                                onComplete: function(transport) { 
-                                       if (inPreferences()) {
+                                       if (callback) {
+                                               callback(transport);
+                                       } else if (inPreferences()) {
                                                infobox_submit_callback2(transport);
                                        } else {
                                                updateFeedList();
@@ -2227,7 +2128,7 @@ function backend_sanity_check_callback(transport) {
                        return init_offline();
                }
 
-               var reply = transport.responseXML.firstChild.firstChild;
+               var reply = transport.responseXML.getElementsByTagName("error")[0];
 
                if (!reply) {
                        fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
@@ -2242,25 +2143,28 @@ function backend_sanity_check_callback(transport) {
 
                console.log("sanity check ok");
 
-               var params = reply.nextSibling;
+               var params = transport.responseXML.getElementsByTagName("init-params")[0];
 
                if (params) {
                        console.log('reading init-params...');
-                       var param = params.firstChild;
-
-                       while (param) {
-                               var k = param.getAttribute("key");
-                               var v = param.getAttribute("value");
-                               console.log(k + " => " + v);
-                               init_params[k] = v;                                     
-
-                               if (db) {
-                                       db.execute("DELETE FROM init_params WHERE key = ?", [k]);
-                                       db.execute("INSERT INTO init_params (key,value) VALUES (?, ?)",
-                                               [k, v]);
-                               }
 
-                               param = param.nextSibling;
+                       params = JSON.parse(params.firstChild.nodeValue);
+
+                       if (params) {
+                               for (var i = 0; i < params.length; i++) {
+       
+                                       var k = params[i].param;
+                                       var v = params[i].value;
+       
+                                       if (getURLParam('debug')) console.log(k + " => " + v);
+                                       init_params[k] = v;                                     
+       
+                                       if (db) {
+                                               db.execute("DELETE FROM init_params WHERE key = ?", [k]);
+                                               db.execute("INSERT INTO init_params (key,value) VALUES (?, ?)",
+                                                       [k, v]);
+                                       }
+                               }
                        }
                }
 
@@ -2273,4 +2177,172 @@ function backend_sanity_check_callback(transport) {
        } 
 }
 
+function has_local_storage() {
+       try {
+               return 'localStorage' in window && window['localStorage'] != null;
+       } catch (e) {
+               return false;
+       }
+}
+
+function catSelectOnChange(elem) {
+       try {
+               var value = elem[elem.selectedIndex].value;
+               var def = elem.getAttribute('default');
+
+               if (value == "ADD_CAT") {
+
+                       if (def)
+                               dropboxSelect(elem, def);
+                       else
+                               elem.selectedIndex = 0;
+
+                       quickAddCat(elem);
+               }
+
+       } catch (e) {
+               exception_error("catSelectOnChange", e);
+       }
+}
+
+function quickAddCat(elem) {
+       try {
+               var cat = prompt(__("Please enter category title:"));
+
+               if (cat) {
+
+                       var query = "?op=rpc&subop=quickAddCat&cat=" + param_escape(cat);
+
+                       notify_progress("Loading, please wait...", true);
+
+                       new Ajax.Request("backend.php", {
+                               parameters: query,
+                               onComplete: function (transport) {
+                                       var response = transport.responseXML;
+                                       var select = response.getElementsByTagName("select")[0];
+                                       var options = select.getElementsByTagName("option");
+
+                                       dropbox_replace_options(elem, options);
+
+                                       notify('');
+
+                       } });
+
+               }
+
+       } catch (e) {
+               exception_error("quickAddCat", e);
+       }
+}
+
+function genUrlChangeKey(feed, is_cat) {
+
+       try {
+               var ok = confirm(__("Generate new syndication address for this feed?"));
+       
+               if (ok) {
+       
+                       notify_progress("Trying to change address...", true);
+       
+                       var query = "?op=rpc&subop=regenFeedKey&id=" + param_escape(feed) + 
+                               "&is_cat=" + param_escape(is_cat);
 
+                       new Ajax.Request("backend.php", {
+                               parameters: query,
+                               onComplete: function(transport) {
+                                               var new_link = transport.responseXML.getElementsByTagName("link")[0];
+       
+                                               var e = $('gen_feed_url');
+       
+                                               if (new_link) {
+                                                       
+                                                       new_link = new_link.firstChild.nodeValue;
+
+                                                       e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/, 
+                                                               "&amp;key=" + new_link);
+
+                                                       e.href = e.href.replace(/\&amp;key=.*$/,
+                                                               "&amp;key=" + new_link);
+
+                                                       new Effect.Highlight(e);
+
+                                                       notify('');
+       
+                                               } else {
+                                                       notify_error("Could not change feed URL.");
+                                               }
+                               } });
+               }
+       } catch (e) {
+               exception_error("genUrlChangeKey", e);
+       }
+       return false;
+}
+
+function labelSelectOnChange(elem) {
+       try {
+               var value = elem[elem.selectedIndex].value;
+               var def = elem.getAttribute('default');
+
+               if (value == "ADD_LABEL") {
+
+                       if (def)
+                               dropboxSelect(elem, def);
+                       else
+                               elem.selectedIndex = 0;
+
+                       addLabel(elem, function(transport) {
+
+                                       try {
+
+                                               var response = transport.responseXML;
+                                               var select = response.getElementsByTagName("select")[0];
+                                               var options = select.getElementsByTagName("option");
+
+                                               dropbox_replace_options(elem, options);
+
+                                               notify('');
+                                       } catch (e) {
+                                               exception_error("addLabel", e);
+                                       }
+                       });
+               }
+
+       } catch (e) {
+               exception_error("labelSelectOnChange", e);
+       }
+}
+
+function dropbox_replace_options(elem, options) {
+
+       try {
+               while (elem.hasChildNodes())
+                       elem.removeChild(elem.firstChild);
+
+               var sel_idx = -1;
+
+               for (var i = 0; i < options.length; i++) {
+                       var text = options[i].firstChild.nodeValue;
+                       var value = options[i].getAttribute("value");
+
+                       if (value == undefined) value = text;
+
+                       var issel = options[i].getAttribute("selected") == "1";
+
+                       var option = new Option(text, value, issel);
+
+                       if (options[i].getAttribute("disabled"))
+                               option.setAttribute("disabled", true);
+
+                       elem.insert(option);
+
+                       if (issel) sel_idx = i;
+               }
+
+               // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
+               if (sel_idx >= 0) elem.selectedIndex = sel_idx;
+
+       } catch (e) {
+               exception_error("dropbox_replace_options", e);
+       }
+}