Abstract chrome browser action and windows stuff

This commit is contained in:
lilia 2015-01-21 22:21:37 -10:00
parent 71da6a1df1
commit 607d5d3307
5 changed files with 45 additions and 12 deletions

View file

@ -59,10 +59,10 @@
var opened = false; var opened = false;
var panel = 0; var panel = 0;
chrome.browserAction.onClicked.addListener(function () { extension.browserAction(function () {
if (opened === false) { if (opened === false) {
opened = true; opened = true;
chrome.windows.create({ extension.windows.open({
url: 'index.html', url: 'index.html',
type: 'panel', type: 'panel',
focused: true, focused: true,
@ -73,9 +73,9 @@
panel = window.id; panel = window.id;
}); });
} else if (opened === true) { } else if (opened === true) {
chrome.windows.update(panel, { focused: true }); extension.windows.focus(panel);
} }
chrome.windows.onRemoved.addListener(function (windowId) { extension.windows.onClosed(function (windowId) {
if (windowId === panel) { if (windowId === panel) {
panel = 0; panel = 0;
opened = false; opened = false;
@ -226,7 +226,7 @@
var windowMap = Whisper.windowMap = new Whisper.Bimap('windowId', 'modelId'); var windowMap = Whisper.windowMap = new Whisper.Bimap('windowId', 'modelId');
// make sure panels are cleaned up on close // make sure panels are cleaned up on close
chrome.windows.onRemoved.addListener(function (windowId) { extension.windows.onClosed(function (windowId) {
if (windowMap.windowId[windowId]) { if (windowMap.windowId[windowId]) {
closeConversation(windowId); closeConversation(windowId);
} }

View file

@ -51,6 +51,36 @@
}); });
}; };
extension.windows = {
open: function(options, callback) {
chrome.windows.create(options, callback);
},
focus: function(id, callback) {
chrome.windows.update(id, { focused: true }, callback);
},
onClosed: function(callback) {
chrome.windows.onRemoved.addListener(callback);
},
getCurrent: function(callback) {
chrome.windows.getCurrent(callback);
},
remove: function(windowId) {
chrome.windows.remove(windowId);
},
getBackground: function() {
return chrome.extension.getBackgroundPage();
}
};
extension.browserAction = function(callback) {
chrome.browserAction.onClicked.addListener(callback);
};
window.textsecure = window.textsecure || {}; window.textsecure = window.textsecure || {};
window.textsecure.registration = { window.textsecure.registration = {
done: function () { done: function () {

View file

@ -22,18 +22,21 @@
function loadConversation (id) { function loadConversation (id) {
var conversation = new Whisper.Conversation({ id: id }); var conversation = new Whisper.Conversation({ id: id });
conversation.fetch().then(function () { conversation.fetch().then(function () {
new Whisper.ConversationView({ model: conversation}).render().$el.appendTo($('#conversation-container')); new Whisper.ConversationView({
model: conversation
}).render().$el.appendTo($('#conversation-container'));
}); });
}; };
var bg = chrome.extension.getBackgroundPage(); var bg = extension.windows.getBackground();
chrome.windows.getCurrent(function (windowInfo) { extension.windows.getCurrent(function (windowInfo) {
var windowId = window.document.title = windowInfo.id; var windowId = window.document.title = windowInfo.id;
// close the panel if background.html is refreshed // close the panel if background.html is refreshed
bg.addEventListener('beforeunload', function () { bg.addEventListener('beforeunload', function () {
chrome.windows.remove(windowId); // TODO: reattach after reload instead of closing.
extension.windows.remove(windowId);
}); });
loadConversation(bg.Whisper.windowMap.modelIdFrom(windowId)); loadConversation(bg.Whisper.windowMap.modelIdFrom(windowId));

View file

@ -31,7 +31,7 @@
// prevent multiple copies of the same conversation from being opened // prevent multiple copies of the same conversation from being opened
if (!windowId) { if (!windowId) {
// open the panel // open the panel
chrome.windows.create({ extension.windows.open({
url: 'conversation.html', url: 'conversation.html',
type: 'panel', type: 'panel',
focused: true, focused: true,
@ -48,7 +48,7 @@
}); });
} else { } else {
// focus the panel // focus the panel
chrome.windows.update(windowId, { focused: true }, function () { extension.windows.focus(windowId, function () {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
// panel isn't actually open... // panel isn't actually open...
window.closeConversation(windowId); window.closeConversation(windowId);

View file

@ -19,7 +19,7 @@ var Whisper = Whisper || {};
(function () { (function () {
'use strict'; 'use strict';
var bg = chrome.extension.getBackgroundPage(); var bg = extension.windows.getBackground();
// list of conversations, showing user/group and last message sent // list of conversations, showing user/group and last message sent
Whisper.ConversationListItemView = Backbone.View.extend({ Whisper.ConversationListItemView = Backbone.View.extend({