From 3279dddcc3541d7c78843f5901f5326f9aa77d65 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 11 Feb 2015 02:47:50 -0800 Subject: [PATCH] Consolidate window logic in panel controller Previously the conversation window would query the background page for a model id and then fetch the conversation. Instead, we can fetch the conversation before opening the window, which simplifies the front end scripts and avoids creating multiple copies of the same model. --- background.html | 2 +- js/background.js | 34 +------------ js/conversation_panel.js | 28 +++------- js/panel_controller.js | 107 ++++++++++++++++++++++++++------------- 4 files changed, 81 insertions(+), 90 deletions(-) diff --git a/background.html b/background.html index b7759385..1ec48cea 100644 --- a/background.html +++ b/background.html @@ -27,8 +27,8 @@ - + diff --git a/js/background.js b/js/background.js index 4b32093f..7057da01 100644 --- a/js/background.js +++ b/js/background.js @@ -59,29 +59,7 @@ var opened = false; var panel = 0; - extension.browserAction(function () { - if (opened === false) { - opened = true; - extension.windows.open({ - url: 'index.html', - type: 'panel', - focused: true, - width: 260, // 280 for chat - height: 440 // 420 for chat - }, function (window) { - var isPanelEnabled = window.alwaysOnTop; - panel = window.id; - }); - } else if (opened === true) { - extension.windows.focus(panel); - } - extension.windows.onClosed(function (windowId) { - if (windowId === panel) { - panel = 0; - opened = false; - } - }); - }); + extension.browserAction(window.openInbox); }; function onMessageReceived(pushMessage) { @@ -228,14 +206,4 @@ console.log('got delivery receipt for unknown message', pushMessage.source, timestamp); }); }; - - var windowMap = Whisper.windowMap = new Whisper.Bimap('windowId', 'modelId'); - - // make sure panels are cleaned up on close - extension.windows.onClosed(function (windowId) { - if (windowMap.windowId[windowId]) { - closeConversation(windowId); - } - }); - })(); diff --git a/js/conversation_panel.js b/js/conversation_panel.js index e3d34128..f414c38a 100644 --- a/js/conversation_panel.js +++ b/js/conversation_panel.js @@ -19,28 +19,12 @@ window.Whisper = window.Whisper || {}; - function loadConversation (id) { - var conversation = new Whisper.Conversation({ id: id }); - conversation.fetch().then(function () { - window.document.title = conversation.getTitle(); - new Whisper.ConversationView({ - model: conversation - }).render().$el.prependTo($('body')); - window.conversation = conversation; - }); - }; - - var bg = extension.windows.getBackground(); - extension.windows.getCurrent(function (windowInfo) { - var windowId = windowInfo.id; - - // close the panel if background.html is refreshed - bg.addEventListener('beforeunload', function () { - // TODO: reattach after reload instead of closing. - extension.windows.remove(windowId); - }); - - loadConversation(bg.Whisper.windowMap.modelIdFrom(windowId)); + var bg = extension.windows.getBackground(); + var conversation = bg.getConversationForWindow(windowInfo.id); + window.document.title = conversation.getTitle(); + new Whisper.ConversationView({ + model: conversation + }).render().$el.prependTo($('body')); }); }()); diff --git a/js/panel_controller.js b/js/panel_controller.js index 4ec64d07..9d6bc2b2 100644 --- a/js/panel_controller.js +++ b/js/panel_controller.js @@ -18,46 +18,85 @@ // This script should only be included in background.html // Whisper.windowMap is defined in background.js (function () { - 'use strict'; + 'use strict'; - window.Whisper = window.Whisper || {}; + window.Whisper = window.Whisper || {}; - var windowMap = Whisper.windowMap; + var windowMap = new Whisper.Bimap('windowId', 'modelId'); + var conversations = new Whisper.ConversationCollection(); - window.openConversation = function openConversation (modelId) { + window.getConversationForWindow = function(windowId) { + return conversations.get(windowMap.modelIdFrom(windowId)); + }; - var windowId = windowMap.windowIdFrom(modelId); + function closeConversation (windowId) { + windowMap.remove('windowId', windowId); + }; - // prevent multiple copies of the same conversation from being opened - if (!windowId) { - // open the panel - extension.windows.open({ - url: 'conversation.html', - type: 'panel', - focused: true, - width: 280, - height: 420 - }, function (windowInfo) { - windowMap.add({ - windowId: windowInfo.id, - modelId: modelId - }); - }); - } else { - // focus the panel - extension.windows.focus(windowId, function () { - if (chrome.runtime.lastError) { - // panel isn't actually open... - window.closeConversation(windowId); + window.openConversation = function openConversation (modelId) { + var conversation = conversations.add({id: modelId}); + conversation.fetch(); - // ...and so we try again. - openConversation(modelId); + var windowId = windowMap.windowIdFrom(modelId); + + // prevent multiple copies of the same conversation from being opened + if (!windowId) { + // open the panel + extension.windows.open({ + url: 'conversation.html', + type: 'panel', + focused: true, + width: 280, + height: 420 + }, function (windowInfo) { + windowMap.add({ windowId: windowInfo.id, modelId: modelId }); + + // close the panel if background.html is refreshed + window.addEventListener('beforeunload', function () { + // TODO: reattach after reload instead of closing. + extension.windows.remove(windowInfo.id); + }); + }); + } else { + // focus the panel + extension.windows.focus(windowId, function () { + if (chrome.runtime.lastError) { + closeConversation(windowId); // panel isn't actually open... + openConversation(modelId); // ...and so we try again. + } + }); } - }); - } - }; + }; - window.closeConversation = function closeConversation (windowId) { - windowMap.remove('windowId', windowId); - }; + /* Inbox window controller */ + var inboxOpened = false; + var inboxWindowId = 0; + window.openInbox = function() { + if (inboxOpened === false) { + inboxOpened = true; + extension.windows.open({ + url: 'index.html', + type: 'panel', + focused: true, + width: 260, // 280 for chat + height: 440 // 420 for chat + }, function (windowInfo) { + inboxWindowId = windowInfo.id; + }); + } else if (inboxOpened === true) { + extension.windows.focus(inboxWindowId); + } + }; + + // make sure windows are cleaned up on close + extension.windows.onClosed(function (windowId) { + if (windowMap.windowId[windowId]) { + closeConversation(windowId); + } + + if (windowId === inboxWindowId) { + inboxWindowId = 0; + inboxOpened = false; + } + }); })();