From 607d5d3307d704adfc825ad6b48b333bddcdae6e Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 21 Jan 2015 22:21:37 -1000 Subject: [PATCH] Abstract chrome browser action and windows stuff --- js/background.js | 10 ++++----- js/chromium.js | 30 +++++++++++++++++++++++++ js/conversation_panel.js | 11 +++++---- js/panel_controller.js | 4 ++-- js/views/conversation_list_item_view.js | 2 +- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/js/background.js b/js/background.js index 8a9067d3..9c573d88 100644 --- a/js/background.js +++ b/js/background.js @@ -59,10 +59,10 @@ var opened = false; var panel = 0; - chrome.browserAction.onClicked.addListener(function () { + extension.browserAction(function () { if (opened === false) { opened = true; - chrome.windows.create({ + extension.windows.open({ url: 'index.html', type: 'panel', focused: true, @@ -73,9 +73,9 @@ panel = window.id; }); } 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) { panel = 0; opened = false; @@ -226,7 +226,7 @@ var windowMap = Whisper.windowMap = new Whisper.Bimap('windowId', 'modelId'); // make sure panels are cleaned up on close - chrome.windows.onRemoved.addListener(function (windowId) { + extension.windows.onClosed(function (windowId) { if (windowMap.windowId[windowId]) { closeConversation(windowId); } diff --git a/js/chromium.js b/js/chromium.js index 2ac9011e..38b26d2e 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -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.registration = { done: function () { diff --git a/js/conversation_panel.js b/js/conversation_panel.js index f1f81171..8bf0b8ef 100644 --- a/js/conversation_panel.js +++ b/js/conversation_panel.js @@ -22,18 +22,21 @@ function loadConversation (id) { var conversation = new Whisper.Conversation({ id: id }); 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; // close the panel if background.html is refreshed 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)); diff --git a/js/panel_controller.js b/js/panel_controller.js index 1c6a7b64..e55ffacd 100644 --- a/js/panel_controller.js +++ b/js/panel_controller.js @@ -31,7 +31,7 @@ // prevent multiple copies of the same conversation from being opened if (!windowId) { // open the panel - chrome.windows.create({ + extension.windows.open({ url: 'conversation.html', type: 'panel', focused: true, @@ -48,7 +48,7 @@ }); } else { // focus the panel - chrome.windows.update(windowId, { focused: true }, function () { + extension.windows.focus(windowId, function () { if (chrome.runtime.lastError) { // panel isn't actually open... window.closeConversation(windowId); diff --git a/js/views/conversation_list_item_view.js b/js/views/conversation_list_item_view.js index 5cd8d0ca..cc0d281a 100644 --- a/js/views/conversation_list_item_view.js +++ b/js/views/conversation_list_item_view.js @@ -19,7 +19,7 @@ var Whisper = Whisper || {}; (function () { 'use strict'; - var bg = chrome.extension.getBackgroundPage(); + var bg = extension.windows.getBackground(); // list of conversations, showing user/group and last message sent Whisper.ConversationListItemView = Backbone.View.extend({