-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
161 lines (149 loc) · 4.53 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
const tryToFocusOnInput = () => {
const inputEls =
Array.prototype.slice.call(document.querySelectorAll('input'));
_.remove(inputEls, inputEl => inputEl.offsetParent === null);
if (inputEls.length) {
inputEls[0].focus();
};
};
const getStashEl = el => el.closest('.stash');
/** Moves focus to next/previous stash. `direction` is 'up' or 'down'. */
const moveFocus = (direction, event) => {
if (document.activeElement instanceof Element) {
const stash = getStashEl(document.activeElement);
if (stash) {
const next =
stash[{up: 'previousElementSibling',
down: 'nextElementSibling'}[direction]];
if (next) {
next.focus();
event.preventDefault();
}
return;
}
}
const first = document.getElementById(
'stash-list')[{up: 'lastElementChild',
down: 'firstElementChild'}[direction]];
if (first && first !== document.activeElement) {
first.focus();
event.preventDefault();
}
};
const init = async function() {
const [highlightedTabs, windowTabs, stashes, messages, originalStashName] =
await Promise.all([
chrome.promise.tabs.query({currentWindow: true, highlighted: true}),
chrome.promise.tabs.query({currentWindow: true}), getStashes(),
getMessages(), getOriginalStashName()
]);
const mode = (() => {
if (windowTabs.length == 1) {
return 'singleTab';
} else {
if (highlightedTabs.length > 1) {
return 'selection';
} else {
return 'default';
}
}
})();
const stashNameTab = (() => {
if (mode == 'singleTab' && originalStashName) {
return originalStashName;
} else {
return highlightedTabs[0].title || '';
}
})();
Vue.filter('tabs', numTabs => {
return numTabs.toString() + (numTabs == 1 ? ' tab' : ' tabs');
});
Vue.filter('from-now', timestamp => {
return moment(timestamp).fromNow();
});
const vm = new Vue({
el: 'html',
data: {
mode: mode,
highlightedTabsLenth: highlightedTabs.length,
stashNameWindow: originalStashName,
stashNameTab: stashNameTab,
stathNameTabs: originalStashName,
stashes: stashes,
messages: messages,
modifierKey: (navigator.platform.toLowerCase().indexOf('mac') >= 0) ?
'Command' :
'Control',
stashNamePlaceholder: stashNamePlaceholder,
topUpTabsLabel: mode == 'selection' ?
highlightedTabs.length.toString() + ' tabs' :
'current tab'
},
methods: {
stashWindow: function() {
saveStash(
this.stashNameWindow, windowTabs,
_.findIndex(windowTabs, {'active': true}));
},
stashTab: function() {
saveStash(this.stashNameTab, highlightedTabs, 0);
},
stashTabs: function() {
saveStash(
this.stathNameTabs, highlightedTabs,
_.findIndex(highlightedTabs, {'active': true}));
},
topUp: function(stashId) {
topUp(stashId, highlightedTabs);
},
unstash: function(stashId, stash) {
// Have to call the function in the background page because the popup
// closes too early.
chrome.extension.getBackgroundPage().unstash(
stashId, stash, !this.messages.openStash);
// For better visual transition.
window.close();
},
deleteStash: deleteStash,
gotIt: function() {
setMessageRead('welcome');
setTimeout(tryToFocusOnInput, 100);
},
isEmpty: _.isEmpty,
handleFocus: function(event) {
getStashEl(event.target).classList.add('focused');
},
handleBlur: function(event) {
const stashOut = getStashEl(event.target);
if (event.relatedTarget instanceof Element) {
const stashIn = getStashEl(event.relatedTarget);
if (stashIn === stashOut) {
return;
};
}
stashOut.classList.remove('focused');
},
up: function(event) {
moveFocus('up', event);
},
down: function(event) {
moveFocus('down', event);
}
},
ready: function() {
tryToFocusOnInput();
}
});
chrome.storage.onChanged.addListener(async(changes, areaName) => {
const [stashes, messages] =
await Promise.all([getStashes(), getMessages()]);
vm.stashes = stashes;
vm.messages = messages;
});
// Workaround for https://bugs.chromium.org/p/chromium/issues/detail?id=307912
setTimeout(() => {
document.body.style.width = '421px';
}, 200)
};
init();