Prevent duplicate conversations and refocus on click (still buggy)
This commit is contained in:
parent
7ec27f814f
commit
783a3f7c15
7 changed files with 116 additions and 21 deletions
|
@ -24,6 +24,8 @@
|
||||||
<script type="text/javascript" src="js/models/conversations.js"></script>
|
<script type="text/javascript" src="js/models/conversations.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="js/chromium.js"></script>
|
<script type="text/javascript" src="js/chromium.js"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript" src="js/panel_controller.js"></script>
|
||||||
<script type="text/javascript" src="js/background.js"></script>
|
<script type="text/javascript" src="js/background.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
|
@ -57,6 +57,9 @@
|
||||||
<script type="text/javascript" src="js/models/conversations.js"></script>
|
<script type="text/javascript" src="js/models/conversations.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="js/chromium.js"></script>
|
<script type="text/javascript" src="js/chromium.js"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript" src="js/panel_controller.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="js/views/notifications.js"></script>
|
<script type="text/javascript" src="js/views/notifications.js"></script>
|
||||||
<script type="text/javascript" src="js/views/file_input_view.js"></script>
|
<script type="text/javascript" src="js/views/file_input_view.js"></script>
|
||||||
<script type="text/javascript" src="js/views/list_view.js"></script>
|
<script type="text/javascript" src="js/views/list_view.js"></script>
|
||||||
|
|
|
@ -225,4 +225,12 @@
|
||||||
|
|
||||||
extension.on('log', console.log.bind(console));
|
extension.on('log', console.log.bind(console));
|
||||||
|
|
||||||
|
chrome.runtime.onConnect.addListener(function (port) {
|
||||||
|
if (port.name === 'panel_presence') {
|
||||||
|
port.onDisconnect.addListener(function (message) {
|
||||||
|
closeConversation(message.sender.tab.windowId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -26,12 +26,9 @@
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var windowId, windowMap = {};
|
var windowId, windowMap = JSON.parse(localStorage.getItem('idPairs'));
|
||||||
|
|
||||||
extension.trigger('log', 'loaded page');
|
|
||||||
|
|
||||||
window.addEventListener('storage', function (e) {
|
window.addEventListener('storage', function (e) {
|
||||||
extension.trigger('log', 'got storage event');
|
|
||||||
if (e.key = 'idPairs') {
|
if (e.key = 'idPairs') {
|
||||||
windowMap = JSON.parse(e.newValue);
|
windowMap = JSON.parse(e.newValue);
|
||||||
|
|
||||||
|
@ -48,12 +45,13 @@
|
||||||
chrome.windows.getCurrent(function (windowInfo) {
|
chrome.windows.getCurrent(function (windowInfo) {
|
||||||
window.document.title = windowId = windowInfo.id;
|
window.document.title = windowId = windowInfo.id;
|
||||||
|
|
||||||
extension.trigger('log', 'got page id');
|
|
||||||
|
|
||||||
var conversationId = windowMap[windowId];
|
var conversationId = windowMap[windowId];
|
||||||
|
|
||||||
if (typeof conversationId !== 'undefined') {
|
if (typeof conversationId !== 'undefined') {
|
||||||
loadConversation(conversationId);
|
loadConversation(conversationId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// lets background.js know when a panel disconnects
|
||||||
|
var port = chrome.runtime.connect({name: "panel_presence"});
|
||||||
}());
|
}());
|
||||||
|
|
97
js/panel_controller.js
Normal file
97
js/panel_controller.js
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
/*global $, Whisper, Backbone, textsecure, extension*/
|
||||||
|
/* vim: ts=4:sw=4:expandtab:
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
|
// TODO: RJS
|
||||||
|
// resetting for now, but super fragile:
|
||||||
|
// 1. if this file is included in conversation.html we're doomed.
|
||||||
|
// 2. if index.html is refreshed, duplicates can be opened
|
||||||
|
// 2.5. ...and refreshing conversation windows will fuck up.
|
||||||
|
if (!localStorage.getItem('activeConversations')) {
|
||||||
|
localStorage.setItem('activeConversations', '{}');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!localStorage.getItem('idPairs')) {
|
||||||
|
localStorage.setItem('idPairs', '{}');
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: RJS
|
||||||
|
// How do we normally export from modules like this?
|
||||||
|
// Is it necessary to have n copies of each of these scripts..?
|
||||||
|
// (n Whisper objects, etc in existence) Using localStorage for
|
||||||
|
// sync feels like a hack...
|
||||||
|
//
|
||||||
|
window.openConversation = function openConversation (modelId) {
|
||||||
|
var activeConversations = JSON.parse(localStorage.getItem('activeConversations'));
|
||||||
|
var windowId = activeConversations[modelId];
|
||||||
|
|
||||||
|
// prevent multiple copies of the same conversation from being opened
|
||||||
|
if (!windowId) {
|
||||||
|
localStorage.setItem('activeConversations', JSON.stringify(activeConversations));
|
||||||
|
|
||||||
|
// open the window
|
||||||
|
chrome.windows.create({
|
||||||
|
url: 'conversation.html',
|
||||||
|
type: 'panel',
|
||||||
|
focused: true,
|
||||||
|
width: 280,
|
||||||
|
height: 420
|
||||||
|
}, function (windowInfo) {
|
||||||
|
var idPairs = JSON.parse(localStorage.getItem('idPairs'));
|
||||||
|
var newWindowId = windowInfo.id;
|
||||||
|
|
||||||
|
// TODO: RJS
|
||||||
|
// should we make a class for bijection?
|
||||||
|
// bit sketchy that we have to keep these two hashes synced...
|
||||||
|
activeConversations[modelId] = newWindowId;
|
||||||
|
idPairs[newWindowId] = modelId;
|
||||||
|
|
||||||
|
localStorage.setItem('activeConversations', JSON.stringify(activeConversations));
|
||||||
|
localStorage.setItem('idPairs', JSON.stringify(idPairs));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
chrome.windows.update(windowId, { focused: true }, function () {
|
||||||
|
if (chrome.runtime.lastError) {
|
||||||
|
window.closeConversation(windowId);
|
||||||
|
} else {
|
||||||
|
// Tab exists
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
// TODO: RJS
|
||||||
|
// - should check the err type
|
||||||
|
// - should open a new panel here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.closeConversation = function closeConversation (windowId) {
|
||||||
|
var activeConversations = JSON.parse(localStorage.getItem('activeConversations'));
|
||||||
|
var idPairs = JSON.parse(localStorage.getItem('idPairs'));
|
||||||
|
|
||||||
|
delete activeConversations[idPairs[windowId]];
|
||||||
|
delete idPairs[windowId];
|
||||||
|
|
||||||
|
localStorage.setItem('activeConversations', JSON.stringify(activeConversations));
|
||||||
|
localStorage.setItem('idPairs', JSON.stringify(idPairs));
|
||||||
|
};
|
||||||
|
}());
|
|
@ -45,21 +45,7 @@ var Whisper = Whisper || {};
|
||||||
this.view = new Whisper.ConversationView({ model: this.model });
|
this.view = new Whisper.ConversationView({ model: this.model });
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.windows.create({
|
openConversation(modelId);
|
||||||
url: 'conversation.html#' + modelId,
|
|
||||||
type: 'panel',
|
|
||||||
focused: true,
|
|
||||||
width: 280,
|
|
||||||
height: 420
|
|
||||||
}, function (windowInfo) {
|
|
||||||
extension.trigger('log', 'maybe up here?');
|
|
||||||
extension.trigger('log', localStorage.getItem('idPairs'));
|
|
||||||
|
|
||||||
var idPairs = JSON.parse(localStorage.getItem('idPairs') || '{}');
|
|
||||||
idPairs[windowInfo.id] = modelId;
|
|
||||||
localStorage.setItem('idPairs', JSON.stringify(idPairs));
|
|
||||||
extension.trigger('log', 'set idPairs item');
|
|
||||||
});
|
|
||||||
|
|
||||||
this.model.collection.trigger('selected', this.view);
|
this.model.collection.trigger('selected', this.view);
|
||||||
},
|
},
|
||||||
|
|
|
@ -17,6 +17,7 @@ var Whisper = Whisper || {};
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
Whisper.FileInputView = Backbone.View.extend({
|
Whisper.FileInputView = Backbone.View.extend({
|
||||||
tagName: 'span',
|
tagName: 'span',
|
||||||
className: 'file-input',
|
className: 'file-input',
|
||||||
|
|
Loading…
Reference in a new issue