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) { 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"); } if(url.indexOf("#list-") == 0) { var feedId = parseInt(url.replace("#list-", ""), 10); _this.reload(feedId); _this.changeToPage("#list"); } else if(url == "#reload") { _this.reload(); } else if(url == "#settings") { _this.changeToPage("#settings"); } else if(url == "#categories" || url == "#categories-back") { _this.changeToPage("#categories"); var isComingBack = (url == "#categories-back") ? true : false; _this.loadCategories(isComingBack); } else if (url.indexOf("#categories-") == 0) { var categoryId = parseInt(url.replace("#categories-", ""), 10); _this.loadCategoryFeeds(categoryId); } 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(); }); 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("invisible"); } this.changeToPage("#categories"); this.loadCategories(); 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.populateList(); this.login.log_out(); }; App.prototype.changeToPage = function(page) { // FIXME var active = $(".active"); if(active.id == "list") { this.saveScrollTop = document.body.scrollTop; } if(page == "#list") { document.body.scrollTop = this.saveScrollTop; } else { window.scroll(0, 0); } active.removeClass("active"); $(page).addClass("active"); }; App.prototype.setColor = function(color) { localStorage.color = color; document.body.className = ""; document.body.addClass(color); this.updatePieChart(); }; App.prototype.reload = function(feedId) { this.unread_articles = []; $("#all-read").addClass('inactive'); var number=parseInt(localStorage.numArticles); this.backend.reload(this.gotUnreadFeeds.bind(this),number, feedId); }; App.prototype.loadCategories = function (isComingBack) { if (this.categories && isComingBack != true) { populateCategoryList(); } else { this.backend.getCategories(this.gotCategories.bind(this)); } } App.prototype.loadCategoryFeeds = function (categoryId) { this.backend.getFeedsByCategory(categoryId, this.gotCategoryFeeds.bind(this)); } 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(); this.isShowingFeeds = true; } } else { this.unread_articles = this.unread_articles.concat(new_articles); if(new_articles.length > 0) { this.populateList(); this.isShowingFeeds = true; } } }; App.prototype.gotCategories = function (categories) { if (!categories) { //FIXME this is repeated code, so create a function for it // 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 { this.categories = categories; if(categories.length > 0) { try { //To check if when it fails it is the same localStorage.categories = JSON.stringify(this.categories); } catch (e) { alert("Reached maximum memory by app " + e.name + " " + e.message + ". We will keep working in anycase with: " + localStorage.categories.length); } this.populateCategoryList(); } } }; App.prototype.gotCategoryFeeds = function (feeds) { this.feeds = feeds; this.populateFeedList(); }; 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; }; App.prototype.populateList = function() { var html_str = ""; for (var i = 0; i < this.unread_articles.length; i++) { var article = this.unread_articles[i]; html_str += "
" + article.feed_title + "
"; html_str += "" + article.excerpt + "
"; html_str += "" + category.title + "
"; html_str += ""; html_str += "" + feed.title + "
"; html_str += ""; html_str += "