]> git.wh0rd.org - tt-rss.git/blobdiff - js/viewfeed.js
add net-alert icon
[tt-rss.git] / js / viewfeed.js
index 4bb56ddfa503e0fa3cb517f10ffb261d17e3078f..d6801a186a1e8a201e63a91397d26d9a785b7311 100644 (file)
@@ -13,6 +13,7 @@ var feed_precache_timeout_id = false;
 var precache_idle_timeout_id = false;
 
 var cids_requested = [];
+var loaded_article_ids = [];
 
 var has_storage = 'sessionStorage' in window && window['sessionStorage'] !== null;
 
@@ -53,6 +54,10 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
 
                        setActiveFeedId(feed_id, is_cat);
 
+                       dijit.getEnclosingWidget(
+                               document.forms["main_toolbar_form"].update).attr('disabled',
+                                       is_cat || feed_id <= 0);
+
                        try {
                                if (offset == 0 && infscroll_req == false) {
                                        $("headlines-frame").scrollTop = 0;
@@ -74,6 +79,8 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                        //var runtime_info = reply['runtime-info'];
 
                        if (offset == 0 && infscroll_req == false) {
+                               loaded_article_ids = [];
+
                                dijit.byId("headlines-frame").attr('content',
                                        reply['headlines']['content']);
 
@@ -81,8 +88,10 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                        reply['headlines']['toolbar']);
 
                                $$("#headlines-frame > div[id*=RROW]").each(function(row) {
-                                       if ($$("#headlines-frame DIV[id="+row.id+"]").length > 1) {
+                                       if (loaded_article_ids.indexOf(row.id) != -1) {
                                                row.parentNode.removeChild(row);
+                                       } else {
+                                               loaded_article_ids.push(row.id);
                                        }
                                });
 
@@ -94,6 +103,12 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
 
                                initHeadlinesMenu();
 
+                               if (_search_query) {
+                                       $("feed_title").innerHTML += "<span id='cancel_search'>" +
+                                               " (<a href='#' onclick='cancelSearch()'>" + __("Cancel search") + "</a>)" +
+                                               "</span>";
+                               }
+
                        } else {
 
                                if (headlines_count > 0 && feed_id == getActiveFeedId() && is_cat == activeFeedIsCat()) {
@@ -101,7 +116,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
 
                                        var c = dijit.byId("headlines-frame");
                                        var ids = getSelectedArticleIds2();
-                                       var num_added = 0;
+                                       var new_elems = [];
 
                                        $("headlines-tmp").innerHTML = reply['headlines']['content'];
 
@@ -111,10 +126,15 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                                c.domNode.removeChild(hsp);
 
                                        $$("#headlines-tmp > div").each(function(row) {
-                                               if ($$("#headlines-frame DIV[id="+row.id+"]").length == 0) {
+                                               if (row.className == 'cdmFeedTitle') {
+                                                       row.style.display = 'none';
+                                                       c.domNode.appendChild(row);
+                                                       new_elems.push(row);
+                                               } else if (loaded_article_ids.indexOf(row.id) == -1) {
                                                        row.style.display = 'none';
                                                        c.domNode.appendChild(row);
-                                                       ++num_added;
+                                                       new_elems.push(row);
+                                                       loaded_article_ids.push(row.id);
                                                } else {
                                                        row.parentNode.removeChild(row);
                                                }
@@ -128,7 +148,10 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                                c.domNode.appendChild(hsp);
                                        }
 
-                                       console.log("added " + num_added + " headlines");
+                                       console.log("added " + new_elems.size() + " headlines");
+
+                                       if (new_elems.size() == 0)
+                                               _infscroll_disable = true;
 
                                        console.log("restore selected ids: " + ids);
 
@@ -138,10 +161,17 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
 
                                        initHeadlinesMenu();
 
-                                       $$("#headlines-frame > div[id*=RROW]").each(
-                                       function(child) {
-                                               if (!Element.visible(child))
-                                                       new Effect.Appear(child, { duration : 0.5 });
+                                       new_elems.each(function(child) {
+                                               var cb = dijit.byId(child.id.replace("RROW-", "RCHK-"));
+
+                                               if (!cb) {
+                                                       dojo.parser.parse(child);
+
+                                                       if (!Element.visible(child))
+                                                               new Effect.Appear(child, { duration : 0.5 });
+                                               } else {
+                                                       c.domNode.removeChild(child);
+                                               }
                                        });
 
                                } else {
@@ -174,11 +204,14 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                        else
                                request_counters();
 
-               } else {
+               } else if (transport.responseText) {
                        console.error("Invalid object received: " + transport.responseText);
                        dijit.byId("headlines-frame").attr('content', "<div class='whiteBox'>" +
                                        __('Could not update headlines (invalid object received - see error console for details)') +
                                        "</div>");
+               } else {
+                       //notify_error("Error communicating with server.");
+                       Element.show("net-alert");
                }
 
                _infscroll_request_sent = 0;
@@ -230,8 +263,6 @@ function showArticleInHeadlines(id) {
 
                selectArticles('none');
 
-               var upd_img_pic = $("FUPDPIC-" + id);
-
                var view_mode = false;
 
                try {
@@ -241,14 +272,7 @@ function showArticleInHeadlines(id) {
                        //
                }
 
-               if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
-                                       upd_img_pic.src.match("fresh_sign.png"))) {
-
-                       upd_img_pic.src = "images/blank_icon.gif";
-
-                       cache_headlines(getActiveFeedId(), activeFeedIsCat(), null, $("headlines-frame").innerHTML);
-
-               } else if (article_is_unread && view_mode == "all_articles") {
+               if (article_is_unread && view_mode == "all_articles") {
                        cache_headlines(getActiveFeedId(), activeFeedIsCat(), null, $("headlines-frame").innerHTML);
                }
 
@@ -278,10 +302,6 @@ function article_callback2(transport, id) {
 
                if (reply) {
 
-                       var upic = $('FUPDPIC-' + id);
-
-                       if (upic) upic.src = 'images/blank_icon.gif';
-
                        reply.each(function(article) {
                                if (active_post_id == article['id']) {
                                        render_article(article['content']);
@@ -296,16 +316,20 @@ function article_callback2(transport, id) {
 //                             return;
 //                     }
 
-               } else {
+               } else if (transport.responseText) {
                        console.error("Invalid object received: " + transport.responseText);
 
                        render_article("<div class='whiteBox'>" +
                                        __('Could not display article (invalid object received - see error console for details)') + "</div>");
+               } else {
+                       Element.show("net-alert");
                }
 
                request_counters();
 
-               try {
+               headlines_scroll_handler($("headlines-frame"));
+
+/*             try {
                        if (!_infscroll_disable &&
                                        $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
 
@@ -313,7 +337,7 @@ function article_callback2(transport, id) {
                        }
                } catch (e) {
                        console.warn(e);
-               }
+               } */
 
                notify("");
        } catch (e) {
@@ -359,15 +383,7 @@ function view(id) {
 
                precache_headlines();
 
-               if (!cached_article) {
-
-                       var upic = $('FUPDPIC-' + id);
-
-                       if (upic) {
-                               upic.src = getInitParam("sign_progress");
-                       }
-
-               } else if (cached_article && article_is_unread) {
+               if (cached_article && article_is_unread) {
 
                        query = query + "&mode=prefetch";
 
@@ -382,7 +398,7 @@ function view(id) {
                        // the server roundtrip altogether
                        if (cids_to_request.length == 0) {
 
-                               try {
+/*                             try {
                                        if (!_infscroll_disable &&
                                                $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
 
@@ -390,7 +406,9 @@ function view(id) {
                                        }
                                } catch (e) {
                                        console.warn(e);
-                               }
+                               } */
+
+                               headlines_scroll_handler($("headlines-frame"));
 
                                return;
                        }
@@ -560,16 +578,16 @@ function moveToPost(mode) {
 function toggleSelected(id, force_on) {
        try {
 
-               var cb = $("RCHK-" + id);
+               var cb = dijit.byId("RCHK-" + id);
                var row = $("RROW-" + id);
 
                if (row) {
                        if (row.hasClassName('Selected') && !force_on) {
                                row.removeClassName('Selected');
-                               if (cb) cb.checked = false;
+                               if (cb) cb.attr("checked", false);
                        } else {
                                row.addClassName('Selected');
-                               if (cb) cb.checked = true;
+                               if (cb) cb.attr("checked", true);
                        }
                }
        } catch (e) {
@@ -847,7 +865,7 @@ function getLoadedArticleIds() {
 
 }
 
-// mode = all,none,unread,invert
+// mode = all,none,unread,invert,marked,published
 function selectArticles(mode) {
        try {
 
@@ -855,31 +873,52 @@ function selectArticles(mode) {
 
                children.each(function(child) {
                        var id = child.id.replace("RROW-", "");
-                       var cb = $("RCHK-" + id);
+                       var cb = dijit.byId("RCHK-" + id);
 
                        if (mode == "all") {
                                child.addClassName("Selected");
-                               cb.checked = true;
+                               if (cb) cb.attr("checked", true);
                        } else if (mode == "unread") {
                                if (child.hasClassName("Unread")) {
                                        child.addClassName("Selected");
-                                       cb.checked = true;
+                                       if (cb) cb.attr("checked", true);
+                               } else {
+                                       child.removeClassName("Selected");
+                                       if (cb) cb.attr("checked", false);
+                               }
+                       } else if (mode == "marked") {
+                               var img = $("FMPIC-" + child.id.replace("RROW-", ""));
+
+                               if (img && img.src.match("mark_set")) {
+                                       child.addClassName("Selected");
+                                       if (cb) cb.attr("checked", true);
                                } else {
                                        child.removeClassName("Selected");
-                                       cb.checked = false;
+                                       if (cb) cb.attr("checked", false);
                                }
+                       } else if (mode == "published") {
+                               var img = $("FPPIC-" + child.id.replace("RROW-", ""));
+
+                               if (img && img.src.match("pub_set")) {
+                                       child.addClassName("Selected");
+                                       if (cb) cb.attr("checked", true);
+                               } else {
+                                       child.removeClassName("Selected");
+                                       if (cb) cb.attr("checked", false);
+                               }
+
                        } else if (mode == "invert") {
                                if (child.hasClassName("Selected")) {
                                        child.removeClassName("Selected");
-                                       cb.checked = false;
+                                       if (cb) cb.attr("checked", false);
                                } else {
                                        child.addClassName("Selected");
-                                       cb.checked = true;
+                                       if (cb) cb.attr("checked", true);
                                }
 
                        } else {
                                child.removeClassName("Selected");
-                               cb.checked = false;
+                               if (cb) cb.attr("checked", false);
                        }
                });
 
@@ -1113,8 +1152,9 @@ function headlines_scroll_handler(e) {
                var hsp = $("headlines-spacer");
 
                if (!_infscroll_disable) {
-                       if (hsp && (e.scrollTop + e.offsetHeight > hsp.offsetTop) ||
-                                       e.scrollTop + e.offsetHeight > e.scrollHeight - 100) {
+                       if ((hsp && e.scrollTop + e.offsetHeight >= hsp.offsetTop - hsp.offsetHeight) ||
+                                       (e.scrollHeight != 0 &&
+                                               ((e.scrollTop + e.offsetHeight) / e.scrollHeight >= 0.7))) {
 
                                if (hsp)
                                        hsp.innerHTML = "<img src='images/indicator_tiny.gif'> " +
@@ -1266,14 +1306,6 @@ function cdmExpandArticle(id) {
 
                var elem = $("CICD-" + active_post_id);
 
-               var upd_img_pic = $("FUPDPIC-" + id);
-
-               if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
-                               upd_img_pic.src.match("fresh_sign.png"))) {
-
-                       upd_img_pic.src = "images/blank_icon.gif";
-               }
-
                if (id == active_post_id && Element.visible(elem))
                        return true;
 
@@ -1293,52 +1325,6 @@ function cdmExpandArticle(id) {
                if (!Element.visible(elem)) {
                        Element.show(elem);
                        Element.hide("CEXC-" + id);
-
-                       if ($("CWRAP-" + id).innerHTML == "") {
-
-                               $("FUPDPIC-" + id).src = "images/indicator_tiny.gif";
-
-                               $("CWRAP-" + id).innerHTML = "<div class=\"insensitive\">" +
-                                       __("Loading, please wait...") + "</div>";
-
-                               var query = "?op=rpc&method=cdmGetArticle&id=" + param_escape(id);
-
-                               var neighbor_ids = getRelativePostIds(id);
-
-                               /* only request uncached articles */
-                               var cids_to_request = [];
-
-                               for (var i = 0; i < neighbor_ids.length; i++) {
-                                       if (cids_requested.indexOf(neighbor_ids[i]) == -1)
-                                               if ($("CWRAP-" + neighbor_ids[i]).innerHTML == "") {
-                                                       cids_to_request.push(neighbor_ids[i]);
-                                                       cids_requested.push(neighbor_ids[i]);
-                                               }
-                               }
-
-                               console.log("additional ids: " + cids_to_request.toString());
-
-                               query = query + "&cids=" + cids_to_request.toString();
-
-                               console.log(query);
-
-                               new Ajax.Request("backend.php", {
-                                       parameters: query,
-                                       onComplete: function(transport) {
-
-                                               $("FUPDPIC-" + id).src = 'images/blank_icon.gif';
-
-                                               handle_rpc_json(transport);
-
-                                               var reply = JSON.parse(transport.responseText);
-
-                                               reply.each(function(article) {
-                                                       $("CWRAP-" + article['id']).innerHTML = article['content'];
-                                                       cids_requested.remove(article['id']);
-                                               });
-                               }});
-
-                       }
                }
 
                var new_offset = $("RROW-" + id).offsetTop;
@@ -1632,14 +1618,6 @@ function cdmClicked(event, id) {
                                if (elem)
                                        elem.removeClassName("Unread");
 
-                               var upd_img_pic = $("FUPDPIC-" + id);
-
-                               if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
-                                               upd_img_pic.src.match("fresh_sign.png"))) {
-
-                                       upd_img_pic.src = "images/blank_icon.gif";
-                               }
-
                                active_post_id = id;
 
                                if (article_is_unread) {
@@ -1749,10 +1727,10 @@ function isCdmMode() {
 function markHeadline(id) {
        var row = $("RROW-" + id);
        if (row) {
-               var check = $("RCHK-" + id);
+               var check = dijit.byId("RCHK-" + id);
 
                if (check) {
-                       check.checked = true;
+                       check.attr("checked", true);
                }
 
                row.addClassName("Selected");
@@ -1793,6 +1771,8 @@ function correctHeadlinesOffset(id) {
                var container = $("headlines-frame");
                var row = $("RROW-" + id);
 
+               if (!container || !row) return;
+
                var viewport = container.offsetHeight;
 
                var rel_offset_top = row.offsetTop - container.scrollTop;
@@ -1930,16 +1910,26 @@ function initHeadlinesMenu() {
                                        label: name,
                                        labelId: bare_id,
                                        onClick: function(event) {
-                                               selectionAssignLabel(this.labelId,
-                                                       [this.getParent().ownerMenu.callerRowId]);
+                                               var ids = getSelectedArticleIds2();
+                                               // cast to string
+                                               var id = this.getParent().ownerMenu.callerRowId + "";
+
+                                               ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
+
+                                               selectionAssignLabel(this.labelId, ids);
                                }}));
 
                                labelDelMenu.addChild(new dijit.MenuItem({
                                        label: name,
                                        labelId: bare_id,
                                        onClick: function(event) {
-                                               selectionRemoveLabel(this.labelId,
-                                                       [this.getParent().ownerMenu.callerRowId]);
+                                               var ids = getSelectedArticleIds2();
+                                               // cast to string
+                                               var id = this.getParent().ownerMenu.callerRowId + "";
+
+                                               ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
+
+                                               selectionRemoveLabel(this.labelId, ids);
                                }}));
 
                        });
@@ -2055,6 +2045,10 @@ function render_local_headlines(feed, is_cat, obj) {
                setActiveFeedId(feed, is_cat);
                initHeadlinesMenu();
 
+               dijit.getEnclosingWidget(
+                       document.forms["main_toolbar_form"].update).attr('disabled',
+                               is_cat || feed <= 0);
+
                precache_headlines();
 
        } catch (e) {
@@ -2113,4 +2107,83 @@ function precache_headlines() {
        }
 }
 
+function cancelSearch() {
+       try {
+               _search_query = "";
+               viewCurrentFeed();
+       } catch (e) {
+               exception_error("cancelSearch", e);
+       }
+}
 
+function setSelectionScore() {
+       try {
+               var ids = getSelectedArticleIds2();
+
+               if (ids.length > 0) {
+                       console.log(ids);
+
+                       var score = prompt(__("Please enter new score for selected articles:"), score);
+
+                       if (score != undefined) {
+                               var query = "op=rpc&method=setScore&id=" + param_escape(ids.toString()) +
+                                       "&score=" + param_escape(score);
+
+                               new Ajax.Request("backend.php", {
+                                       parameters: query,
+                                       onComplete: function(transport) {
+                                               var reply = JSON.parse(transport.responseText);
+                                               if (reply) {
+                                                       console.log(ids);
+
+                                                       ids.each(function(id) {
+                                                               var row = $("RROW-" + id);
+
+                                                               if (row) {
+                                                                       var pic = row.getElementsByClassName("hlScorePic")[0];
+
+                                                                       if (pic) {
+                                                                               pic.src = pic.src.replace(/score_.*?\.png/,
+                                                                                       reply["score_pic"]);
+                                                                               pic.setAttribute("score", score);
+                                                                       }
+                                                               }
+                                                       });
+                                               }
+                                       } });
+                       }
+
+               } else {
+                       alert(__("No articles are selected."));
+               }
+       } catch (e) {
+               exception_error("setSelectionScore", e);
+       }
+}
+
+function changeScore(id, pic) {
+       try {
+               var score = pic.getAttribute("score");
+
+               var new_score = prompt(__("Please enter new score for this article:"), score);
+
+               if (new_score != undefined) {
+
+                       var query = "op=rpc&method=setScore&id=" + param_escape(id) +
+                               "&score=" + param_escape(new_score);
+
+                       new Ajax.Request("backend.php", {
+                               parameters: query,
+                               onComplete: function(transport) {
+                                       var reply = JSON.parse(transport.responseText);
+
+                                       if (reply) {
+                                               pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
+                                               pic.setAttribute("score", new_score);
+                                       }
+                               } });
+               }
+       } catch (e) {
+               exception_error("changeScore", e);
+       }
+}