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.
pull/749/head
lilia 10 years ago
parent 90140556e4
commit 3279dddcc3

@ -27,8 +27,8 @@
<script type="text/javascript" src="js/inbox_controller.js"></script> <script type="text/javascript" src="js/inbox_controller.js"></script>
<script type="text/javascript" src="js/bimap.js"></script> <script type="text/javascript" src="js/bimap.js"></script>
<script type="text/javascript" src="js/background.js"></script>
<script type="text/javascript" src="js/panel_controller.js"></script> <script type="text/javascript" src="js/panel_controller.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</head> </head>
<body> <body>
</body> </body>

@ -59,29 +59,7 @@
var opened = false; var opened = false;
var panel = 0; var panel = 0;
extension.browserAction(function () { extension.browserAction(window.openInbox);
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;
}
});
});
}; };
function onMessageReceived(pushMessage) { function onMessageReceived(pushMessage) {
@ -228,14 +206,4 @@
console.log('got delivery receipt for unknown message', pushMessage.source, timestamp); 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);
}
});
})(); })();

@ -19,28 +19,12 @@
window.Whisper = window.Whisper || {}; 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) { extension.windows.getCurrent(function (windowInfo) {
var windowId = windowInfo.id; var bg = extension.windows.getBackground();
var conversation = bg.getConversationForWindow(windowInfo.id);
// close the panel if background.html is refreshed window.document.title = conversation.getTitle();
bg.addEventListener('beforeunload', function () { new Whisper.ConversationView({
// TODO: reattach after reload instead of closing. model: conversation
extension.windows.remove(windowId); }).render().$el.prependTo($('body'));
});
loadConversation(bg.Whisper.windowMap.modelIdFrom(windowId));
}); });
}()); }());

@ -18,46 +18,85 @@
// This script should only be included in background.html // This script should only be included in background.html
// Whisper.windowMap is defined in background.js // Whisper.windowMap is defined in background.js
(function () { (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) {
var windowId = windowMap.windowIdFrom(modelId); return conversations.get(windowMap.modelIdFrom(windowId));
};
// prevent multiple copies of the same conversation from being opened
if (!windowId) { function closeConversation (windowId) {
// open the panel windowMap.remove('windowId', windowId);
extension.windows.open({ };
url: 'conversation.html',
type: 'panel', window.openConversation = function openConversation (modelId) {
focused: true, var conversation = conversations.add({id: modelId});
width: 280, conversation.fetch();
height: 420
}, function (windowInfo) { var windowId = windowMap.windowIdFrom(modelId);
windowMap.add({
windowId: windowInfo.id, // prevent multiple copies of the same conversation from being opened
modelId: modelId if (!windowId) {
}); // open the panel
}); extension.windows.open({
} else { url: 'conversation.html',
// focus the panel type: 'panel',
extension.windows.focus(windowId, function () { focused: true,
if (chrome.runtime.lastError) { width: 280,
// panel isn't actually open... height: 420
window.closeConversation(windowId); }, function (windowInfo) {
windowMap.add({ windowId: windowInfo.id, modelId: modelId });
// ...and so we try again.
openConversation(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.
}
});
}
};
/* 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);
} }
}); };
}
};
window.closeConversation = function closeConversation (windowId) { // make sure windows are cleaned up on close
windowMap.remove('windowId', windowId); extension.windows.onClosed(function (windowId) {
}; if (windowMap.windowId[windowId]) {
closeConversation(windowId);
}
if (windowId === inboxWindowId) {
inboxWindowId = 0;
inboxOpened = false;
}
});
})(); })();

Loading…
Cancel
Save