Skip to content

Commit

Permalink
Polishing tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Nov 22, 2009
1 parent e1d81da commit f30e8a5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 57 deletions.
87 changes: 53 additions & 34 deletions background.html
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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();
Expand All @@ -208,66 +223,70 @@
}

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];
},

isTweetRead: function(id) {
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();
Expand Down
58 changes: 35 additions & 23 deletions popup.html
Expand Up @@ -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());
Expand All @@ -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";
Expand Down Expand Up @@ -303,7 +309,7 @@
loadingNewTweets = false;
}

function loadTimeline(force) {
function loadTimeline(force, forcedTimeline) {
loadingNewTweets = true;
$("#loading").show();
if(force) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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 = $("<div class='tab_modifier'></div>")
.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";
Expand All @@ -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();
Expand All @@ -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);
}
}
});
Expand All @@ -417,7 +430,6 @@
} else {
$("#workspace").show();
ThemeManager.updateThemeSelector();
prepareAndLoadTimeline();
}
if(tweetManager.isComposing) {
Composer.showComposeArea(true, true);
Expand All @@ -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) {
Expand Down

0 comments on commit f30e8a5

Please sign in to comment.