]> git.wh0rd.org - tt-rss.git/blobdiff - offline.js
reenable piggie (refs #42)
[tt-rss.git] / offline.js
index c192c59661779979cbda99d3dbc2f4aa47a57ae0..9abad919ae2ba7de85f3128e5980f5a027831a49 100644 (file)
@@ -8,7 +8,7 @@ var articles_synced = 0;
 var sync_in_progress = false;
 var sync_timer = false;
 
-function view_offline(id, feed_id) {
+function view_offline(id) {
        try {
 
                enableHotkeys();
@@ -110,13 +110,12 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                        catchup_local_feed(feed_id, is_cat);
                }
 
-               disableContainerChildren("headlinesToolbar", false);
                Form.enable("main_toolbar_form");
 
                var f = $("headlines-frame");
                try {
                        if (reply.offset == 0) { 
-                               debug("resetting headlines scrollTop");
+                               console.log("resetting headlines scrollTop");
                                f.scrollTop = 0; 
                        }
                } catch (e) { };
@@ -146,16 +145,29 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                                var sel_none_link;
                                var sel_inv_link;
 
+                               var catchup_feed_link = "javascript:catchupCurrentFeed()";
+                               var catchup_sel_link = "javascript:catchupSelection()";
+
+                               var tog_unread_link;
+                               var tog_marked_link;
+
                                if ($("content-frame")) {
                                        sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
                                        sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
                                        sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
                                        sel_inv_link = "javascript:invertHeadlineSelection()";
+
+                                       tog_unread_link = "javascript:selectionToggleUnread()";
+                                       tog_marked_link = "javascript:selectionToggleMarked()";
+
                                } else {
                                        sel_all_link = "javascript:cdmSelectArticles('all')";
                                        sel_unread_link = "javascript:cdmSelectArticles('unread')";
                                        sel_none_link = "javascript:cdmSelectArticles('none')";
                                        sel_inv_link = "javascript:invertHeadlineSelection()";
+
+                                       tog_unread_link = "javascript:selectionToggleUnread(true)";
+                                       tog_marked_link = "javascript:selectionToggleMarked(true)";
                                }
 
                                tmp += __('Select:')+
@@ -166,6 +178,25 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
        
                                tmp += "  ";
        
+                               tmp += "<span onmouseover=\"enable_selection(false)\" "+
+                                       "onmouseout=\"enable_selection(true) \"" +
+                                       "onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">" +
+                                       __("Actions...") + "&nbsp;&nbsp;<img src=\"images/down_arrow.png\">" +
+                               "</span>";
+
+                               tmp += "<ul id=\"headlineActionsBody\" style=\"display : none\">";
+
+                               tmp += "<li class=\"insensitive\">"+__('Selection toggle:')+"</li>"+
+                               "<li onclick=\""+tog_unread_link+"\">&nbsp;&nbsp;"+__('Unread')+"</li>"+
+                               "<li onclick=\""+tog_marked_link+"\">&nbsp;&nbsp;"+__('Starred')+"</li>"+
+                               "<li class=\"insensitive\">"+__('Mark as read:')+"</li>"+
+                               "<li onclick=\""+catchup_sel_link+"\">&nbsp;&nbsp;"+__('Selection')+"</li>";
+
+                               tmp += "<li onclick=\""+catchup_feed_link+"\">&nbsp;&nbsp;"+__('Entire feed')+
+                                       "</li>";
+
+                               tmp += "</ul>";
+
                                tmp += "</div>";
        
                                tmp += "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
@@ -179,7 +210,7 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                
                        var toolbar_form = document.forms["main_toolbar_form"];
                        
-                       var limit = toolbar_form.limit[toolbar_form.limit.selectedIndex].value;
+                       //var limit = toolbar_form.limit[toolbar_form.limit.selectedIndex].value;
                        var view_mode = toolbar_form.view_mode[toolbar_form.view_mode.selectedIndex].value;
 
                        var limit_qpart = "";
@@ -305,12 +336,12 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                                        
                                        tmp += "<td class='hlMarkedPic'>"+marked_pic+"</td>";
                
-                                       tmp += "<td onclick='view("+id+","+feed_id+")' "+
+                                       tmp += "<td onclick='view("+id+")' "+
                                                "class='hlContent' valign='middle'>";
                
                                        tmp += "<a target=\"_blank\" id=\"RTITLE-"+id+"\" href=\"" + 
                                                rs.fieldByName("link") + "\"" +
-                                               "onclick=\"return view("+id+","+feed_id+");\">"+
+                                               "onclick=\"return view("+id+");\">"+
                                                rs.fieldByName("title");
        
                                        tmp += "<span class=\"contentPreview\"> - "+content_preview+"</span>";
@@ -327,7 +358,7 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
 
                                        tmp += "</td>";
 
-                                       tmp += "<td class=\"hlUpdated\" onclick='view("+id+","+feed_id+")'>"+
+                                       tmp += "<td class=\"hlUpdated\" onclick='view("+id+")'>"+
                                                "<nobr>"+rs.fieldByName("updated").substring(0,16)+
                                                "</nobr></td>";
        
@@ -567,9 +598,9 @@ function render_offline_feedlist() {
 function init_offline() {
        try {
                offline_mode = true;
+               store.enabled = true;
 
                Element.hide("dispSwitchPrompt");
-               Element.hide("feedBrowserPrompt");
 
                Element.hide("topLinksOnline");
                Element.show("topLinksOffline");
@@ -625,8 +656,17 @@ function offline_download_parse(stage, transport) {
                        if (sync_ok.length > 0) {
                                for (var i = 0; i < sync_ok.length; i++) {
                                        var id = sync_ok[i].getAttribute("id");
+                                       var unread = sync_ok[i].getAttribute("unread");
+                                       var marked = sync_ok[i].getAttribute("marked");
+
                                        if (id) {
-                                               debug("synced offline info for id " + id);
+                                               console.log("synced offline info for id " + id);
+
+                                               if (unread != undefined && marked != undefined) {
+                                                       db.execute("UPDATE articles SET "+
+                                                               "unread = ?, marked = ? WHERE id = ?", [unread, marked, id]);
+
+                                               }
                                                db.execute("UPDATE articles SET modified = '' WHERE id = ?", [id]);
                                        }
                                }
@@ -731,7 +771,7 @@ function offline_download_parse(stage, transport) {
                                        }
                                }
 
-                               debug("downloaded articles: " + articles_found + " limit: " + limit);
+                               console.log("downloaded articles: " + articles_found + " limit: " + limit);
 
                                articles_synced += articles_found;
 
@@ -744,11 +784,11 @@ function offline_download_parse(stage, transport) {
                                if (articles_found >= limit || has_sync_data) {
                                        sync_timer = window.setTimeout("update_offline_data("+(stage+1)+")", 
                                                3*1000);
-                                       debug("<b>update_offline_data: done " + stage + " HSD: " + 
+                                       console.log("<b>update_offline_data: done " + stage + " HSD: " + 
                                                has_sync_data + "</b>");
                                } else {
                                        window.setTimeout("offlineDownloadStart()", 180*1000);
-                                       debug("update_offline_data: finished");
+                                       console.log("update_offline_data: finished");
 
                                        var pic = $("offlineModePic");
 
@@ -812,9 +852,7 @@ function update_offline_data(stage) {
 
                if (!db || offline_mode || getInitParam("offline_enabled") != "1") return;
 
-//             notify_progress("Updating offline data... (" + stage +")", true);
-
-               var query = "backend.php?op=rpc&subop=download";
+               var query = "?op=rpc&subop=download";
                
                var rs = db.execute("SELECT MAX(id), MIN(id) FROM articles");
 
@@ -825,22 +863,22 @@ function update_offline_data(stage) {
                        query = query + "&cidt=" + offline_dl_max_id;
                        query = query + "&cidb=" + offline_dl_min_id;
 
-                       stage = 1;
+                       if (stage > 0) stage = 1;
                }
 
                rs.close();
 
-               debug("update_offline_data: stage " + stage);
+               console.log("update_offline_data: stage " + stage);
 
                query = query + "&stage=" + stage;
 
                var to_sync = prepare_local_sync_data();
 
                if (to_sync != "") {
-                       to_sync = "?sync=" + param_escape(to_sync);
+                       to_sync = "&sync=" + param_escape(to_sync);
                }
 
-               debug(query + "/" + to_sync);
+               query = query + to_sync;
 
                var pic = $("offlineModePic");
 
@@ -852,11 +890,14 @@ function update_offline_data(stage) {
                }
 
                offlineSyncShowHideElems(true);
+               offlineUpdateStore();
 
                sync_in_progress = true;
 
-               new Ajax.Request(query, {
-                       parameters: to_sync,
+               console.log(query);
+
+               new Ajax.Request("backend.php", {
+                       parameters: query,
                        onComplete: function(transport) { 
                                offline_download_parse(stage, transport);                               
                        } });
@@ -1039,6 +1080,7 @@ function init_gears() {
 
                        store = localServer.createManagedStore("tt-rss");
                        store.manifestUrl = "manifest.json.php";
+                       store.enabled = false;
 
                        db = google.gears.factory.create('beta.database');
                        db.open('tt-rss');
@@ -1127,9 +1169,7 @@ function init_gears() {
        }
 }
 
-function gotoOffline() {
-
-//     debug("[Local store] currentVersion = " + store.currentVersion);
+function offlineArticlesStored() {
 
        var rs = db.execute("SELECT COUNT(*) FROM articles");
        var count = 0;
@@ -1139,13 +1179,22 @@ function gotoOffline() {
 
        rs.close();
 
-       if (count == 0) {
+       return count;
+}
+
+function gotoOffline() {
+
+//     console.log("[Local store] currentVersion = " + store.currentVersion);
+
+
+       if (offlineArticlesStored() == 0) {
                notify_error("You have to synchronize some articles before going into offline mode.");
                return;
        }
 
        if (confirm(__("Switch Tiny Tiny RSS into offline mode?"))) {
 
+               store.enabled = true;
                store.checkForUpdate();
        
                notify_progress("Preparing offline mode...", true);
@@ -1153,27 +1202,27 @@ function gotoOffline() {
                var timerId = window.setInterval(function() {
                        if (store.currentVersion) {
                                window.clearInterval(timerId);
-                               debug("[Local store] sync complete: " + store.currentVersion);
+                               console.log("[Local store] sync complete: " + store.currentVersion);
 
                                //window.location.href = "tt-rss.php";
 
                                offlineDownloadStop();
-                               offline_mode = true;
                                init_offline();
 
                                notify_info("Tiny Tiny RSS is in offline mode.");
 
                        } else if (store.updateStatus == 3) {
-                               debug("[Local store] sync error: " + store.lastErrorMessage);
+                               console.log("[Local store] sync error: " + store.lastErrorMessage);
                                notify_error(store.lastErrorMessage, true);
                        } }, 500);
        }
 }
 
 function gotoOnline() {
-       if (confirm(__("You won't be able to access offline version of Tiny Tiny RSS until you switch it into offline mode again. Go online?"))) {
-               localServer.removeManagedStore("tt-rss");
-               window.location.href = "tt-rss.php";
+//     if (confirm(__("You won't be able to access offline version of Tiny Tiny RSS until you switch it into offline mode again. Go online?"))) {
+       if (confirm(__("Tiny Tiny RSS will reload. Go online?"))) {
+               //localServer.removeManagedStore("tt-rss");
+               window.location.href = "tt-rss.php?online=1";
        }
 }
 
@@ -1338,7 +1387,7 @@ function get_local_article_labels(id) {
 
 function label_local_add_article(id, label_id) {
        try {
-               //debug("label_local_add_article " + id + " => " + label_id);
+               //console.log("label_local_add_article " + id + " => " + label_id);
 
                var rs = db.execute("SELECT COUNT(id) FROM article_labels WHERE "+
                        "id = ? AND label_id = ?", [id, label_id]);
@@ -1576,10 +1625,12 @@ function toggleOfflineModeInfo() {
        }
 }
 
-function offlineDownloadStart() {
+function offlineDownloadStart(stage) {
        try {
+               if (!stage) stage = 0;
+
                if (db && !sync_in_progress && getInitParam("offline_enabled") == "1") {
-                       window.setTimeout("update_offline_data(0)", 100);
+                       window.setTimeout("update_offline_data("+stage+")", 100);
                }
        } catch (e) {
                exception_error("offlineDownloadStart", e);
@@ -1640,8 +1691,17 @@ function offlineClearData() {
 
 function offlineUpdateStore() {
        try {
+               if (offline_mode || !store) return;
 
-               if (offline_mode) return;
+               store.checkForUpdate();
+       
+               var timerId = window.setInterval(function() {
+                       if (store.currentVersion) {
+                               window.clearInterval(timerId);
+                               console.log("[Local store] sync complete: " + store.currentVersion);
+                       } else if (store.updateStatus == 3) {
+                               console.log("[Local store] sync error: " + store.lastErrorMessage);
+                       } }, 500);
 
        } catch (e) {
                exception_error("offlineUpdateStore", e);
@@ -1675,3 +1735,55 @@ function offlineSyncShowHideElems(syncing) {
                exception_error("offlineSyncShowHideElems", e);
        }
 }
+
+function offlineConfirmModeChange() {
+       try {
+               
+               if (db && getInitParam("offline_enabled") == "1" && offlineArticlesStored() > 0) {
+                       var ok = confirm(__("Tiny Tiny RSS has trouble accessing its server. Would you like to go offline?")); 
+
+                       if (ok) {
+                               init_offline();
+                       }
+               }
+
+       } catch (e) {
+               exception_error("offlineConfirmModeChange", e);
+       }
+}
+
+function printFeedEntry(id, title, row_class, unread, icon) {
+
+       var tmp = "";
+       var fctr_class = "";
+       var feed_icon = "";
+
+       if (unread > 0) {
+               row_class += "Unread";
+               fctr_class = "feedCtrHasUnread";
+       } else {
+               fctr_class = "feedCtrNoUnread";
+       }
+
+       if (icon) {
+               feed_icon = "<img id='FIMG-"+id+"' src='" + icon + "'>";
+       } else {
+               feed_icon = "<img id='FIMG-"+id+"' src='images/blank_icon.gif'>";
+       }
+
+       var link = "<a title=\"FIXME\" id=\"FEEDL-"+id+"\""+
+               "href=\"javascript:viewfeed('"+id+"', '', false, '', false, 0);\">"+
+               title + "</a>";
+
+       tmp += "<li id='FEEDR-"+id+"' class="+row_class+">" + feed_icon + 
+               "<span id=\"FEEDN-"+id+"\">" + link + "</span>";
+
+       tmp += " <span class='"+fctr_class+"' id=\"FEEDCTR-"+id+"\">" +
+           "(<span id=\"FEEDU-"+id+"\">"+unread+"</span>)</span>";
+                       
+       tmp += "</li>";
+
+       return tmp;
+}
+
+