annotate background.js @ 18:cb06734b3a7b version-5

Release version 5
author Guido Berhoerster <guido+tab-mover@berhoerster.name>
date Thu, 31 May 2018 13:07:29 +0200
parents 6b4680867e49
children 4704e5216412
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
1 /*
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2017 Guido Berhoerster <guido+tab-mover@berhoerster.name>
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
3 *
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
4 * This Source Code Form is subject to the terms of the Mozilla Public
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
7 */
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
8
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
9 'use strict';
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
10
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
11 function createContextMenuItem(createProperties) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
12 return new Promise((resolve, reject) => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
13 browser.contextMenus.create(createProperties, () => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
14 if (browser.runtime.lastError) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
15 reject(browser.runtime.lastError);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
16 } else {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
17 resolve();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
18 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
19 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
20 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
21 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
22
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
23 const Observable = (superclass) => class extends superclass {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
24 constructor(...args) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
25 super(...args);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
26
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
27 this._observers = new Map();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
28 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
29
10
2a87d7a3863f Clean up code
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 0
diff changeset
30 addObserver(eventName, observer) {
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
31 if (!this._observers.has(eventName)) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
32 this._observers.set(eventName, new Set());
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
33 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
34
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
35 this._observers.get(eventName).add(observer);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
36 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
37
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
38 deleteObserver(eventName, observer) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
39 if (this._observers.has(eventName)) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
40 this._observers.get(eventName).delete(observer);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
41 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
42 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
43
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
44 notifyObservers(eventName, ...args) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
45 if (!this._observers.has(eventName)) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
46 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
47 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
48
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
49 for (let observer of this._observers.get(eventName)) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
50 observer(eventName, ...args);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
51 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
52 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
53 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
54
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
55 class WindowsModel extends Observable(Object) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
56 constructor() {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
57 super();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
58
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
59 this.windows = new Map();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
60 this.focusedWindowId = browser.windows.WINDOW_ID_NONE;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
61 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
62
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
63 getWindow(id) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
64 return this.windows.get(id);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
65 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
66
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
67 getAllWindows() {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
68 return this.windows.values();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
69 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
70
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
71 getfocusedWindowId() {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
72 return this.focusedWindowId;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
73 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
74
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
75 openWindow(id, incognito = false) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
76 this.windows.set(id, {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
77 id,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
78 title: browser.i18n.getMessage(incognito ?
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
79 'defaultIncognitoWindowTitle' : 'defaultWindowTitle', id),
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
80 incognito
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
81 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
82
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
83 this.notifyObservers('window-opened', id);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
84 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
85
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
86 updateWindowTitle(id, title) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
87 if (!this.windows.has(id)) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
88 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
89 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
90
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
91 let windowInfo = this.windows.get(id)
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
92 windowInfo.title = browser.i18n.getMessage(windowInfo.incognito ?
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
93 'incognitoWindowTitle' : 'windowTitle', title);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
94
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
95 this.notifyObservers('window-title-updated', id, title);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
96 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
97
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
98 focusWindow(id) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
99 this.focusedWindowId = this.windows.has(id) ? id :
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
100 browser.windows.WINDOW_ID_NONE;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
101
10
2a87d7a3863f Clean up code
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 0
diff changeset
102 this.notifyObservers('window-focus-changed', id);
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
103 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
104
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
105 closeWindow(id) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
106 if (!this.windows.has(id)) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
107 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
108 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
109
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
110 this.windows.delete(id);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
111
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
112 if (id === this.focusedWindowId) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
113 this.focusedWindowId = browser.windows.WINDOW_ID_NONE;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
114 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
115
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
116 this.notifyObservers('window-closed', id);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
117 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
118 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
119
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
120 class MenuView {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
121 constructor(model) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
122 this.model = model;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
123 this.moveMenuIds = new Set();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
124 this.reopenMenuIds = new Set();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
125 this.menuContexts = ['tab'];
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
126
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
127 browser.runtime.getBrowserInfo().then(browserInfo => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
128 // Firefox before version 53 does not support tab context menus
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
129 let majorVersion = browserInfo.version.match(/^\d+/);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
130 if (majorVersion !== null && majorVersion < 53) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
131 this.menuContexts = ['all'];
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
132 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
133
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
134 return Promise.all([
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
135 // create submenus
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
136 createContextMenuItem({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
137 id: 'move-menu',
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
138 title: browser.i18n.getMessage('moveToWindowMenu'),
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
139 enabled: false,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
140 contexts: this.menuContexts
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
141 }),
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
142 createContextMenuItem({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
143 id: 'reopen-menu',
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
144 title: browser.i18n.getMessage('reopenInWindowMenu'),
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
145 enabled: false,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
146 contexts: this.menuContexts
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
147 })
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
148 ]);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
149 }).then(values => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
150 this.model.addObserver('window-opened',
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
151 this.onWindowOpened.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
152 this.model.addObserver('window-title-updated',
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
153 this.onWindowTitleUpdated.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
154 this.model.addObserver('window-focus-changed',
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
155 this.onWindowFocusChanged.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
156 this.model.addObserver('window-closed',
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
157 this.onWindowClosed.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
158 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
159 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
160 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
161 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
162
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
163 enableMenus() {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
164 return Promise.all([
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
165 browser.contextMenus.update('move-menu', {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
166 enabled: this.moveMenuIds.size > 0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
167 }),
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
168 browser.contextMenus.update('reopen-menu', {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
169 enabled: this.reopenMenuIds.size > 0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
170 })
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
171 ]);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
172 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
173
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
174 onWindowOpened(eventName, windowId) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
175 let focusedWindowId = this.model.getfocusedWindowId();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
176 if (focusedWindowId === browser.windows.WINDOW_ID_NONE) {
10
2a87d7a3863f Clean up code
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 0
diff changeset
177 // no window is focused so there is no need to update the menu
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
178 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
179 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
180
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
181 let menuId = String(windowId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
182 let windowInfo = this.model.getWindow(windowId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
183 let incognito = this.model.getWindow(focusedWindowId).incognito;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
184
11
5d7914307782 Allow reopening tabs from normal windows in incognito windows
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 10
diff changeset
185 if (incognito !== windowInfo.incognito) {
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
186 this.reopenMenuIds.add(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
187 } else {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
188 this.moveMenuIds.add(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
189 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
190
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
191 createContextMenuItem({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
192 id: menuId,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
193 title: windowInfo.title,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
194 contexts: this.menuContexts,
11
5d7914307782 Allow reopening tabs from normal windows in incognito windows
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 10
diff changeset
195 parentId: (incognito !== windowInfo.incognito) ?
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
196 'reopen-menu' : 'move-menu'
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
197 }).then(() => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
198 return this.enableMenus();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
199 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
200 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
201 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
202 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
203
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
204 onWindowTitleUpdated(eventName, windowId, title) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
205 if (this.model.getfocusedWindowId() ===
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
206 browser.windows.WINDOW_ID_NONE) {
10
2a87d7a3863f Clean up code
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 0
diff changeset
207 // no window is focused so there is no need to update the menu
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
208 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
209 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
210
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
211 browser.contextMenus.update(String(windowId), {title}).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
212 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
213 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
214 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
215
10
2a87d7a3863f Clean up code
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 0
diff changeset
216 onWindowFocusChanged(eventName, newWindowId) {
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
217 let promises = [
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
218 // disable submenus
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
219 browser.contextMenus.update('move-menu', {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
220 enabled: false
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
221 }),
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
222 browser.contextMenus.update('reopen-menu', {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
223 enabled: false
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
224 })
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
225 ];
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
226
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
227 if (newWindowId === browser.windows.WINDOW_ID_NONE) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
228 // just disable the submenus if focus moved to a window not tracked
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
229 Promise.all(promises).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
230 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
231 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
232 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
233 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
234
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
235 Promise.all(promises).then(values => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
236 // remove all submenu items
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
237 let promises = new Array(...this.moveMenuIds,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
238 ...this.reopenMenuIds).map(menuId => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
239 this.moveMenuIds.delete(menuId) ||
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
240 this.reopenMenuIds.delete(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
241
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
242 return browser.contextMenus.remove(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
243 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
244
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
245 return Promise.all(promises);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
246 }).then(values => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
247 let incognito = this.model.getWindow(newWindowId).incognito;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
248
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
249 // rebuild submenus
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
250 let promises = [];
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
251 for (let windowInfo of this.model.getAllWindows()) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
252 if (windowInfo.id === newWindowId) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
253 // skip the currently focused window
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
254 continue;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
255 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
256
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
257 let menuId = String(windowInfo.id);
11
5d7914307782 Allow reopening tabs from normal windows in incognito windows
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 10
diff changeset
258 if (incognito !== windowInfo.incognito) {
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
259 this.reopenMenuIds.add(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
260 } else {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
261 this.moveMenuIds.add(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
262 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
263
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
264 // create menu item
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
265 promises.push(createContextMenuItem({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
266 id: menuId,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
267 title: windowInfo.title,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
268 contexts: this.menuContexts,
11
5d7914307782 Allow reopening tabs from normal windows in incognito windows
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 10
diff changeset
269 parentId: (incognito !== windowInfo.incognito) ?
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
270 'reopen-menu' : 'move-menu'
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
271 }));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
272 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
273
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
274 return Promise.all(promises);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
275 }).then(values => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
276 return this.enableMenus();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
277 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
278 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
279 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
280 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
281
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
282 onWindowClosed(eventName, windowId) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
283 if (this.model.getfocusedWindowId() ===
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
284 browser.windows.WINDOW_ID_NONE) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
285 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
286 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
287
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
288 let menuId = String(windowId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
289
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
290 this.moveMenuIds.delete(menuId) || this.reopenMenuIds.delete(menuId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
291
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
292 browser.contextMenus.remove(menuId).then(() => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
293 return this.enableMenus();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
294 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
295 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
296 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
297 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
298 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
299
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
300 class Presenter {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
301 constructor(model, view) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
302 this.model = model;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
303 this.view = view;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
304
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
305 browser.windows.getAll({windowTypes: ['normal']}).then(windows => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
306 // populate model with existing windows
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
307 for (let windowInfo of windows) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
308 this.onWindowCreated(windowInfo);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
309
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
310 if (windowInfo.focused) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
311 this.onWindowFocusChanged(windowInfo.id);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
312 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
313 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
314
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
315 browser.windows.onCreated
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
316 .addListener(this.onWindowCreated.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
317 browser.windows.onRemoved
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
318 .addListener(this.onWindowRemoved.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
319 browser.windows.onFocusChanged
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
320 .addListener(this.onWindowFocusChanged.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
321 browser.contextMenus.onClicked
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
322 .addListener(this.onMenuItemClicked.bind(this));
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
323 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
324 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
325 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
326 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
327
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
328 onWindowCreated(windowInfo) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
329 // only track normal windows
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
330 if (windowInfo.type !== 'normal') {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
331 return;
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
332 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
333
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
334 this.model.openWindow(windowInfo.id, windowInfo.incognito);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
335
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
336 // get the window title and update the model
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
337 browser.tabs.query({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
338 active: true,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
339 windowId: windowInfo.id
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
340 }).then(tabs => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
341 this.model.updateWindowTitle(tabs[0].windowId, tabs[0].title)
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
342 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
343 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
344 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
345 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
346
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
347 onWindowRemoved(windowId) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
348 this.model.closeWindow(windowId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
349 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
350
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
351 onWindowFocusChanged(windowId) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
352 let prevFocusedWindowId = this.model.getfocusedWindowId();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
353 if (prevFocusedWindowId !== browser.windows.WINDOW_ID_NONE) {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
354 // get title of the previously focused window and update the model
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
355 browser.tabs.query({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
356 active: true,
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
357 windowId: prevFocusedWindowId
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
358 }).then(tabs => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
359 this.model.updateWindowTitle(tabs[0].windowId, tabs[0].title)
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
360 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
361 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
362 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
363 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
364
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
365 this.model.focusWindow(windowId);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
366 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
367
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
368 onMenuItemClicked(info, tab) {
12
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
369 var windowId = parseInt(info.menuItemId);
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
370
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
371 if (info.parentMenuItemId === 'move-menu') {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
372 // move tab from the current window to the selected window
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
373 browser.tabs.move(tab.id, {
12
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
374 windowId,
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
375 index: -1
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
376 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
377 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
378 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
379 } else {
12
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
380 // open the URL of the current tab in the destination window
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
381 browser.tabs.create({
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
382 url: tab.url,
12
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
383 windowId,
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
384 }).then(newTab => {
12
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
385 // close the current tab
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
386 return browser.tabs.remove(tab.id);
12
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
387 }).then(() => {
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
388 // get the new title of the destination window
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
389 return browser.tabs.query({
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
390 active: true,
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
391 windowId
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
392 });
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
393 }).then(tabs => {
e32b90567f39 Update menu when reopening a tab
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents: 11
diff changeset
394 this.model.updateWindowTitle(windowId, tabs[0].title)
0
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
395 }).catch(error => {
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
396 console.log('Error:', error);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
397 });
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
398 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
399 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
400 }
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
401
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
402 let windowsModel = new WindowsModel();
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
403 let menuView = new MenuView(windowsModel);
480f8e4f4500 Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff changeset
404 let presenter = new Presenter(windowsModel, menuView);