diff --git a/background.html b/background.html index 7f099c99..adf912ac 100755 --- a/background.html +++ b/background.html @@ -56,13 +56,20 @@ } this.setError(null); + var unreadLength = 0; for(var i = tweets.length - 1; i >= 0; --i) { this.newTweetsCache.unshift(tweets[i]); - this.manager.unreadTweets[tweets[i].id] = true; + var tid = tweets[i].id; + if(context.onFinish) { + this.manager.readTweet(tid); + } else if(!this.manager.readTweets[tid]) { + ++unreadLength; + this.manager.unreadTweets[tid] = true; + } } if(tweets.length > 0) { - this.manager.notifyNewTweets(this.timelineId, this.newTweetsCache.length); + this.manager.notifyNewTweets(this.timelineId, this.newTweetsCache.length, unreadLength); } if(context.onFinish) context.onFinish(tweets.length); @@ -135,7 +142,7 @@ _this.updateNewTweets(); _this.manager.clearAlert(); _this.giveMeTweets(tweetsCallback, false, true); - _this.newTweetsCallback = oldNewTweetsCallback; + _this.manager.newTweetsCallback = oldNewTweetsCallback; } this.fetchNewTweets(onFinishCallback); return; @@ -171,7 +178,7 @@ return; var i = len - 1; for(; i >= TweetManagerConstants.MAX_CACHED_ELEMENTS; --i) { - if(!this.isTweetRead(this.tweetsCache[i].id)) { + if(!this.manager.isTweetRead(this.tweetsCache[i].id)) { break; } } @@ -184,12 +191,20 @@ }, newTweetsCount: function() { - return this.newTweetsCache.length; + var unreadCount = 0; + for(var i = this.newTweetsCache.length - 1; i >= 0; --i) { + if(!this.manager.isTweetRead(this.newTweetsCache[i].id)) { + ++unreadCount; + } + } + return [this.newTweetsCache.length, unreadCount]; } } function TweetManager() { this.unreadTweets = {}; + this.readTweets = {}; + this.newTweetsCallback = null; this.iconRed = new Image(); this.iconBlue = new Image(); @@ -208,25 +223,24 @@ } TweetManager.prototype = { + eachTimeline: function(callback) { + for(var tId in this.timelines) { + callback.call(tId, this.timelines[tId]); + } + }, + clearAlert: function() { chrome.browserAction.setTitle({title: "Check your tweets"}); this.canvasContext.drawImage(this.iconBlue, 0, 0); chrome.browserAction.setIcon({imageData: this.canvasContext.getImageData(0, 0, 19, 19)}); }, - giveMeTweets: function(timelineId, callback, syncNew, cacheOnly) { - var timeline = this.timelines[timelineId]; - if(!timeline) { - return false; - } - return timeline.giveMeTweets(callback, syncNew, cacheOnly); - }, - registerNewTweetsCallback: function(callback) { this.newTweetsCallback = callback; }, readTweet: function(id) { + this.readTweets[id] = true; delete this.unreadTweets[id]; }, @@ -234,40 +248,45 @@ return !this.unreadTweets[id]; }, - notifyNewTweets: function(timelineId, size) { + notifyNewTweets: function(timelineId, size, unreadSize) { try { // The callback might be invalid (popup not active), so let's ignore errors for now. - this.newTweetsCallback(size, timelineId); + this.newTweetsCallback(size, unreadSize, timelineId); } catch(e) { /* ignoring */ } - var title = size + " new tweet"; - if(size > 1) - title += "s"; - chrome.browserAction.setTitle({title: title}); + if(unreadSize > 0) { + var title = size + " new tweet"; + if(size > 1) + title += "s"; + chrome.browserAction.setTitle({title: title}); - this.canvasContext.drawImage(this.iconRed, 0, 0); - chrome.browserAction.setIcon({imageData: this.canvasContext.getImageData(0, 0, 19, 19)}); + this.canvasContext.drawImage(this.iconRed, 0, 0); + chrome.browserAction.setIcon({imageData: this.canvasContext.getImageData(0, 0, 19, 19)}); + } + }, + + postTweet: function(callback, msg) { + return twitterBackend.tweet(callback, msg); + }, + + /* Delegates */ + + giveMeTweets: function(timelineId, callback, syncNew, cacheOnly) { + return this.timelines[timelineId].giveMeTweets(callback, syncNew, cacheOnly); }, newTweetsCount: function(timelineId) { - var timeline = this.timelines[timelineId]; - if(!timeline) { - return false; - } - return timeline.newTweetsCount(); + return this.timelines[timelineId].newTweetsCount(); }, - updateNewTweets: function(timelineId) { - var timeline = this.timelines[timelineId]; - if(!timeline) { - return false; - } - return timeline.updateNewTweets(); + updateNewTweets: function() { + return this.timelines[this.currentTimeline].updateNewTweets(); }, - getTimeline: function(timelineId) { - return this.timelines[timelineId]; + getCurrentTimeline: function() { + return this.timelines[this.currentTimeline]; } + } TweetManager.instance = new TweetManager(); diff --git a/popup.html b/popup.html index 93c511af..d6da2ab6 100755 --- a/popup.html +++ b/popup.html @@ -148,16 +148,22 @@ $("#loading").show(); $("#compose_tweet_area input[type='button']").attr("disabled", "disabled"); $("#compose_tweet_area textarea").attr("disabled", "disabled"); - var twitterBackend = chrome.extension.getBackgroundPage().getTwitterBackend(); - twitterBackend.tweet(function(success, data, status) { + tweetManager.postTweet(function(success, data, status) { $("#loading").hide(); $("#compose_tweet_area input[type='button']").removeAttr("disabled"); $("#compose_tweet_area textarea").removeAttr("disabled"); if(success) { - $("#compose_tweet_area textarea").val(""); + var textArea = $("#compose_tweet_area textarea"); + var msg = textArea.val(); + textArea.val(""); Composer.textareaChanged(); Composer.showComposeArea(); - loadTimeline(true); + + if(msg.match(/^d\s\w*?\s/i)) { + loadTimeline(true, "dms"); + } else { + loadTimeline(true, "home"); + } } else { } }, $("#compose_tweet_area textarea").val()); @@ -172,9 +178,9 @@ var diff = (nowTimestamp - inputTimestamp) / 1000; if(diff < 15) { - return "Just now"; + return "just now"; } else if(diff < 60) { - return "Less than 1 minute ago"; + return "less than 1 minute ago"; } else if(diff < 60 * 60) { var minutes = parseInt(diff / 60); return minutes + " minute" + ((minutes > 1) ? "s" : "") + " ago"; @@ -303,7 +309,7 @@ loadingNewTweets = false; } -function loadTimeline(force) { +function loadTimeline(force, forcedTimeline) { loadingNewTweets = true; $("#loading").show(); if(force) { @@ -313,7 +319,10 @@ if(Paginator.needsMore) { cacheOnly = false; } - tweetManager.giveMeTweets(tweetManager.currentTimeline, onTimelineRetrieved, force, cacheOnly); + if(!forcedTimeline) { + forcedTimeline = tweetManager.currentTimeline; + } + tweetManager.giveMeTweets(forcedTimeline, onTimelineRetrieved, force, cacheOnly); } function clearUserData() { @@ -347,15 +356,18 @@ window.close(); } -function newTweetsAvailable(count, timelineId) { +function newTweetsAvailable(count, unreadCount, timelineId) { var currentTimeline = tweetManager.currentTimeline; if(timelineId != currentTimeline) { + if(unreadCount == 0) + return; $("#tab_modifier_" + timelineId).remove(); var timelineTabLink = $("a[href='#timeline_" + timelineId + "']"); var divEl = $("
") .attr('id', "tab_modifier_" + timelineId); timelineTabLink.before(divEl); - divEl.css({width: (timelineTabLink.parent().width() - 12) + 'px'}); + var modWidth = parseInt(timelineTabLink.parent().width()) - 12; + divEl.css({width: modWidth + 'px'}); return; } var text = count + " more tweet"; @@ -369,7 +381,7 @@ function loadNewTweets() { Paginator.firstPage(); tweetManager.clearAlert(); - tweetManager.updateNewTweets(tweetManager.currentTimeline); + tweetManager.updateNewTweets(); $("#tab_modifier_" + tweetManager.currentTimeline).remove(); $("#update_tweets").fadeOut(); loadTimeline(); @@ -382,17 +394,18 @@ function prepareAndLoadTimeline() { var currentTimeline = tweetManager.currentTimeline; - $(["home", "mentions", "dms"]).each(function() { + tweetManager.eachTimeline(function(timeline) { var timelineId = this; - var newTweetsCount = tweetManager.newTweetsCount(timelineId); + var newTweetsInfo = tweetManager.newTweetsCount(timelineId); + var newTweetsCount = newTweetsInfo[0]; if(newTweetsCount > 0) { if(currentTimeline == timelineId && - tweetManager.getTimeline(timelineId).currentScroll == 0) { + timeline.currentScroll == 0) { tweetManager.clearAlert(); - tweetManager.updateNewTweets(timelineId); + tweetManager.updateNewTweets(); $("#tab_modifier_" + timelineId).remove(); } else { - newTweetsAvailable(newTweetsCount, timelineId); + newTweetsAvailable(newTweetsCount, newTweetsInfo[1], timelineId); } } }); @@ -417,7 +430,6 @@ } else { $("#workspace").show(); ThemeManager.updateThemeSelector(); - prepareAndLoadTimeline(); } if(tweetManager.isComposing) { Composer.showComposeArea(true, true); @@ -432,21 +444,21 @@ show: function(event, ui) { $("#update_tweets").hide(); prepareAndLoadTimeline(); - $(ui.panel).scrollTop(tweetManager.getTimeline(tweetManager.currentTimeline).currentScroll); + $(ui.panel).scrollTop(tweetManager.getCurrentTimeline().currentScroll); } }); $("#tabs").tabs('select', "#timeline_" + tweetManager.currentTimeline); ThemeManager.initTheme(); replaceUsername(); - $(["home", "mentions", "dms"]).each(function() { - var iterTab = this; - var tabEl = $("#timeline_" + iterTab); - tabEl.scrollTop(tweetManager.getTimeline(iterTab).currentScroll); + tweetManager.eachTimeline(function(timeline) { + var timelineId = this; + var tabEl = $("#timeline_" + timelineId); + tabEl.scrollTop(timeline.currentScroll); tabEl.scroll(function(e) { var $this = $(this); var threshold = 50; - tweetManager.getTimeline(iterTab).currentScroll = $this.scrollTop(); + timeline.currentScroll = $this.scrollTop(); var maxScroll = $this.attr("scrollHeight") - $this.height(); if(maxScroll - $this.scrollTop() < threshold) { if(!loadingNewTweets) {