Mercurial > addons > firefox-addons > tab-mover
annotate background.js @ 0:480f8e4f4500
Initial revision
author | Guido Berhoerster <guido+tab-mover@berhoerster.name> |
---|---|
date | Sun, 19 Feb 2017 00:20:26 +0100 |
parents | |
children | 2a87d7a3863f |
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 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
30 addObserver(eventName, observer, thisArg) { |
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 hasWindow(id) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
64 return this.windows.has(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 getWindow(id) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
68 return this.windows.get(id); |
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 getAllWindows() { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
72 return this.windows.values(); |
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 getfocusedWindowId() { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
76 return this.focusedWindowId; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
77 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
78 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
79 openWindow(id, incognito = false) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
80 this.windows.set(id, { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
81 id, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
82 title: browser.i18n.getMessage(incognito ? |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
83 'defaultIncognitoWindowTitle' : 'defaultWindowTitle', id), |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
84 incognito |
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 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
87 this.notifyObservers('window-opened', id); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
88 } |
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 updateWindowTitle(id, title) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
91 if (!this.windows.has(id)) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
92 return; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
93 } |
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 let windowInfo = this.windows.get(id) |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
96 windowInfo.title = browser.i18n.getMessage(windowInfo.incognito ? |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
97 'incognitoWindowTitle' : 'windowTitle', title); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
98 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
99 this.notifyObservers('window-title-updated', id, title); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
100 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
101 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
102 focusWindow(id) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
103 let oldId = this.focusedWindowId; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
104 this.focusedWindowId = this.windows.has(id) ? id : |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
105 browser.windows.WINDOW_ID_NONE; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
106 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
107 this.notifyObservers('window-focus-changed', oldId, id); |
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 closeWindow(id) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
111 if (!this.windows.has(id)) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
112 return; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
113 } |
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 this.windows.delete(id); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
116 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
117 if (id === this.focusedWindowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
118 this.focusedWindowId = browser.windows.WINDOW_ID_NONE; |
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 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
121 this.notifyObservers('window-closed', id); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
122 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
123 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
124 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
125 class MenuView { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
126 constructor(model) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
127 this.model = model; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
128 this.moveMenuIds = new Set(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
129 this.reopenMenuIds = new Set(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
130 this.menuContexts = ['tab']; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
131 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
132 browser.runtime.getBrowserInfo().then(browserInfo => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
133 // Firefox before version 53 does not support tab context menus |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
134 let majorVersion = browserInfo.version.match(/^\d+/); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
135 if (majorVersion !== null && majorVersion < 53) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
136 this.menuContexts = ['all']; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
137 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
138 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
139 return Promise.all([ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
140 // create submenus |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
141 createContextMenuItem({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
142 id: 'move-menu', |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
143 title: browser.i18n.getMessage('moveToWindowMenu'), |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
144 enabled: false, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
145 contexts: this.menuContexts |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
146 }), |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
147 createContextMenuItem({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
148 id: 'reopen-menu', |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
149 title: browser.i18n.getMessage('reopenInWindowMenu'), |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
150 enabled: false, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
151 contexts: this.menuContexts |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
152 }) |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
153 ]); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
154 }).then(values => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
155 this.model.addObserver('window-opened', |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
156 this.onWindowOpened.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
157 this.model.addObserver('window-title-updated', |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
158 this.onWindowTitleUpdated.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
159 this.model.addObserver('window-focus-changed', |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
160 this.onWindowFocusChanged.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
161 this.model.addObserver('window-closed', |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
162 this.onWindowClosed.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
163 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
164 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
165 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
166 } |
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 enableMenus() { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
169 return Promise.all([ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
170 browser.contextMenus.update('move-menu', { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
171 enabled: this.moveMenuIds.size > 0 |
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 browser.contextMenus.update('reopen-menu', { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
174 enabled: this.reopenMenuIds.size > 0 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
175 }) |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
176 ]); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
177 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
178 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
179 onWindowOpened(eventName, windowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
180 let focusedWindowId = this.model.getfocusedWindowId(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
181 if (focusedWindowId === browser.windows.WINDOW_ID_NONE) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
182 return; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
183 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
184 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
185 let menuId = String(windowId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
186 let windowInfo = this.model.getWindow(windowId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
187 let incognito = this.model.getWindow(focusedWindowId).incognito; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
188 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
189 if (incognito && !windowInfo.incognito) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
190 this.reopenMenuIds.add(menuId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
191 } else { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
192 this.moveMenuIds.add(menuId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
193 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
194 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
195 createContextMenuItem({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
196 id: menuId, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
197 title: windowInfo.title, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
198 contexts: this.menuContexts, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
199 parentId: (incognito && !windowInfo.incognito) ? |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
200 'reopen-menu' : 'move-menu' |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
201 }).then(() => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
202 return this.enableMenus(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
203 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
204 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
205 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
206 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
207 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
208 onWindowTitleUpdated(eventName, windowId, title) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
209 if (this.model.getfocusedWindowId() === |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
210 browser.windows.WINDOW_ID_NONE) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
211 return; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
212 } |
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 browser.contextMenus.update(String(windowId), {title}).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
215 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
216 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
217 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
218 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
219 onWindowFocusChanged(eventName, oldWindowId, newWindowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
220 let promises = [ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
221 // disable submenus |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
222 browser.contextMenus.update('move-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 browser.contextMenus.update('reopen-menu', { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
226 enabled: false |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
227 }) |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
228 ]; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
229 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
230 if (newWindowId === browser.windows.WINDOW_ID_NONE) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
231 // 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
|
232 Promise.all(promises).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
233 console.log('Error:', error); |
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 return; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
236 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
237 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
238 Promise.all(promises).then(values => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
239 // remove all submenu items |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
240 let promises = new Array(...this.moveMenuIds, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
241 ...this.reopenMenuIds).map(menuId => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
242 this.moveMenuIds.delete(menuId) || |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
243 this.reopenMenuIds.delete(menuId); |
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 browser.contextMenus.remove(menuId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
246 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
247 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
248 return Promise.all(promises); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
249 }).then(values => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
250 let incognito = this.model.getWindow(newWindowId).incognito; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
251 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
252 // rebuild submenus |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
253 let promises = []; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
254 for (let windowInfo of this.model.getAllWindows()) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
255 if (windowInfo.id === newWindowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
256 // skip the currently focused window |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
257 continue; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
258 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
259 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
260 let menuId = String(windowInfo.id); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
261 if (incognito && !windowInfo.incognito) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
262 this.reopenMenuIds.add(menuId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
263 } else { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
264 this.moveMenuIds.add(menuId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
265 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
266 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
267 // create menu item |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
268 promises.push(createContextMenuItem({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
269 id: menuId, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
270 title: windowInfo.title, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
271 contexts: this.menuContexts, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
272 parentId: (incognito && !windowInfo.incognito) ? |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
273 'reopen-menu' : 'move-menu' |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
274 })); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
275 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
276 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
277 return Promise.all(promises); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
278 }).then(values => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
279 return this.enableMenus(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
280 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
281 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
282 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
283 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
284 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
285 onWindowClosed(eventName, windowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
286 if (this.model.getfocusedWindowId() === |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
287 browser.windows.WINDOW_ID_NONE) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
288 return; |
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 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
291 let menuId = String(windowId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
292 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
293 this.moveMenuIds.delete(menuId) || this.reopenMenuIds.delete(menuId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
294 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
295 browser.contextMenus.remove(menuId).then(() => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
296 return this.enableMenus(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
297 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
298 console.log('Error:', error); |
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 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
301 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
302 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
303 class Presenter { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
304 constructor(model, view) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
305 this.model = model; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
306 this.view = view; |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
307 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
308 browser.windows.getAll({windowTypes: ['normal']}).then(windows => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
309 // populate model with existing windows |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
310 for (let windowInfo of windows) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
311 this.onWindowCreated(windowInfo); |
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 if (windowInfo.focused) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
314 this.onWindowFocusChanged(windowInfo.id); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
315 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
316 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
317 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
318 browser.windows.onCreated |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
319 .addListener(this.onWindowCreated.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
320 browser.windows.onRemoved |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
321 .addListener(this.onWindowRemoved.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
322 browser.windows.onFocusChanged |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
323 .addListener(this.onWindowFocusChanged.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
324 browser.contextMenus.onClicked |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
325 .addListener(this.onMenuItemClicked.bind(this)); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
326 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
327 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
328 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
329 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
330 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
331 onWindowCreated(windowInfo) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
332 // only track normal windows |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
333 if (windowInfo.type !== 'normal') { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
334 return; |
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 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
337 this.model.openWindow(windowInfo.id, windowInfo.incognito); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
338 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
339 // get the window title and update the model |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
340 browser.tabs.query({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
341 active: true, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
342 windowId: windowInfo.id |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
343 }).then(tabs => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
344 this.model.updateWindowTitle(tabs[0].windowId, tabs[0].title) |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
345 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
346 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
347 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
348 } |
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 onWindowRemoved(windowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
351 this.model.closeWindow(windowId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
352 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
353 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
354 onWindowFocusChanged(windowId) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
355 let prevFocusedWindowId = this.model.getfocusedWindowId(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
356 if (prevFocusedWindowId !== browser.windows.WINDOW_ID_NONE) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
357 // get title of the previously focused window and update the model |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
358 browser.tabs.query({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
359 active: true, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
360 windowId: prevFocusedWindowId |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
361 }).then(tabs => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
362 this.model.updateWindowTitle(tabs[0].windowId, tabs[0].title) |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
363 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
364 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
365 }); |
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 this.model.focusWindow(windowId); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
369 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
370 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
371 onMenuItemClicked(info, tab) { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
372 if (info.parentMenuItemId === 'move-menu') { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
373 // move tab from the current window to the selected window |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
374 browser.tabs.move(tab.id, { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
375 windowId: parseInt(info.menuItemId), |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
376 index: -1 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
377 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
378 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
379 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
380 } else { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
381 // open the URL of the current tab in the selected window and close |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
382 // the current tab |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
383 browser.tabs.create({ |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
384 url: tab.url, |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
385 windowId: parseInt(info.menuItemId), |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
386 index: -1 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
387 }).then(newTab => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
388 return browser.tabs.remove(tab.id); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
389 }).catch(error => { |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
390 console.log('Error:', error); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
391 }); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
392 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
393 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
394 } |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
395 |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
396 let windowsModel = new WindowsModel(); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
397 let menuView = new MenuView(windowsModel); |
480f8e4f4500
Initial revision
Guido Berhoerster <guido+tab-mover@berhoerster.name>
parents:
diff
changeset
|
398 let presenter = new Presenter(windowsModel, menuView); |