function App() { this.login = new Login(this); this.currentIndex = -1; var color = localStorage.color; if(!color) color = "red"; this.setColor(color); this.fontChange(); var numArticles = localStorage.numArticles; if(!numArticles) numArticles = 50; this.numArticles(numArticles); var maxDownload = localStorage.maxDownload; if(!maxDownload) maxDownload = 500000; this.maxDownload(maxDownload); }; App.prototype.authenticate = function() { }; App.prototype.after_login = function(backend) { if (window.navigator.mozApps) { var request = window.navigator.mozApps.getSelf(); request.onsuccess = function() { $("#version").innerHTML = request.result.manifest.version; } } var _this = this; window.onhashchange = function(e) { // do not reload page e.preventDefault(); e.stopPropagation(); var url = window.location.hash; if (url == "#list") { _this.setCurrentRead(); _this.changeToPage("#list"); } else if(url == "#reload") { _this.reload(); } else if(url == "#settings") { _this.changeToPage("#settings"); } else if(url.indexOf("#color-") == 0) { var color = url.replace("#color-", ""); _this.setColor(color); } else if(url.indexOf("#full-") == 0) { var i = parseInt(url.replace("#full-", ""), 10); _this.showFull(_this.unread_articles[i]); } else if(url == "#unread") { _this.toggleCurrentUnread(); } else if(url == "#starred") { _this.toggleStarred(); } else if(url == "#published") { _this.togglePublished(); } else if(url == "#logout") { _this.logout(); } else if(url == "#reset-info") { alert("Info bubbles will be shown again.") $$(".info").forEach(function(o) { o.removeClass("hidden"); }); } else if(url == "#next") { _this.showNext(); } else if(url == "#previous") { _this.showPrevious(); } else if(url == "#font-smaller") { _this.fontChange("smaller"); } else if(url == "#font-bigger") { _this.fontChange("bigger"); } else if(url == "#all-read") { _this.toggleAllRead(); } // this is here so you can tap on a button more then once // and it will still call onhashchange window.location.hash = "#"; } // FIXME move that code somewhere else $(".info.swipe").ontouchend = function(e) { localStorage.info_swipe = true; $(".info.swipe").addClass("hidden"); }; var supportsTouch = 'ontouchend' in document; if(!supportsTouch || localStorage.info_swipe) { $(".info.swipe").addClass("hidden"); } // set up swiping var options = { dragLockToAxis: true, dragBlockHorizontal: true }; var hammertime = new Hammer($("#full"), options); hammertime.on("dragleft", function(ev){ ev.gesture.preventDefault(); }); hammertime.on("dragright", function(ev){ ev.gesture.preventDefault(); }); hammertime.on("swipeleft", function(ev){ _this.showNext(); ev.gesture.preventDefault(); }); hammertime.on("swiperight", function(ev){ _this.showPrevious(); ev.gesture.preventDefault(); }); this.changeToPage("#list"); if(backend == "OwnCloud") { this.backend = new OwnCloud(this, localStorage.server_url, localStorage.session_id); } else if (backend == "Pond") { this.backend = new Pond(this, localStorage.server_url, localStorage.session_id) } else { this.backend = new TinyTinyRSS(this, localStorage.server_url, localStorage.session_id); $("#setpublished").removeClass("hidden"); } var numArticles = localStorage.numArticles; if(!numArticles) numArticles = 50; this.numArticles(numArticles); var maxDownload = localStorage.maxDownload; if(!maxDownload) maxDownload = 500000; this.maxDownload(maxDownload); this.reload(); }; App.prototype.logout = function() { this.backend.logOut(); this.unread_articles = []; this.next_articles = []; this.populateList(); this.login.log_out(); }; App.prototype.changeToPage = function(page) { // FIXME var active = $(".active"); // Save old position if (active.id == "list") { this.saveScrollTop = document.documentElement.scrollTop; } // Switch displays active.removeClass("active"); $(page).addClass("active"); if (page == "#list") { var elem = document.documentElement; var posit = this.saveScrollTop; // Restore old position, after display settles setTimeout(function() { elem.scrollTop = posit; }, 500); } else { // Else top of page window.scroll(0, 0); } }; App.prototype.setColor = function(color) { localStorage.color = color; document.body.className = ""; document.body.addClass(color); this.updatePieChart(); }; App.prototype.reload = function() { this.unread_articles = []; this.next_articles = []; $("#all-read").addClass('inactive'); var number=parseInt(localStorage.numArticles); this.backend.reload(this.gotUnreadFeeds.bind(this),number); }; App.prototype.gotUnreadFeeds = function(new_articles) { if(new_articles == null || !this.validate(new_articles)) { // Check if we did not get a NOT_LOGGED_IN error, and ask the // user to login again if it is the case. // This can happen with TT-RSS backend if (new_articles.error && new_articles.error === "NOT_LOGGED_IN") { alert("Your TinyTinyRSS session has expired. Please login again"); this.login.fillLoginFormFromLocalStorage(); this.login.log_in(); } else { // On other errors, load the saved unread articles. var old_articles = localStorage.unread_articles; if(old_articles) { this.unread_articles = JSON.parse(old_articles); } this.populateList(); } } else { this.unread_articles = this.unread_articles.concat(new_articles); if(new_articles.length > 0) { try { //To check if when it fails it is the same localStorage.unread_articles = JSON.stringify(this.unread_articles); var size = parseInt(localStorage.maxDownload); if(localStorage.unread_articles.length < size) { var num = parseInt(localStorage.numArticles); this.backend.getUnreadFeeds(this.gotUnreadFeeds.bind(this), this.unread_articles,num); } else { alert("Limit size reached: Downloaded: " + this.unread_articles.length + " articles. Reached: " + localStorage.unread_articles.length +" bytes"); } } catch (e) { alert("Reached maximum memory by app " + e.name + " " + e.message + ". We will keep working in anycase with: " + localStorage.unread_articles.length); } this.populateList(); } } }; App.prototype.validate = function(articles) { if(articles.length == 0) return true; for (var i = 0; i < articles.length; i++) { if(typeof articles[i].title != "undefined") return true; } return false; }; // Utility function to toggle feed content visibility function toggleFeed(feedid) { var e = document.getElementById("feed" + feedid.toString()); if (e.style.display == "none") { e.style.display = ""; } else { e.style.display = "none"; } } App.prototype.populateList = function() { // Tabulate all articles, grouped by feed. // Note that this.unread_articles[] can be growing even // as our user reads, so we have to leave it alone and // just point to the appropriate articles in situ. const ua = this.unread_articles, ual = ua.length; const byfeed = {}, feeds = []; for (let x = 0; x < ual; ++x) { const article = ua[x]; // Add this article to an existing feed list, or // or start one for this feed ID. const fid = article.feed_id; if (fid in byfeed) { byfeed[fid].push(x); } else { byfeed[fid] = [x]; feeds.push( {"fid": fid, "name": article.feed_title} ); } } // Now build the article list; it's a