-
Notifications
You must be signed in to change notification settings - Fork 20
/
popup.js
205 lines (164 loc) · 6.22 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// FEATURE: support drag & drop re-ordering
// FEATURE: support drag & drop merging
var backgroundPage = chrome.extension.getBackgroundPage();
var savedWindowListEl, formEl, nameInput, template;
var undo = new Object();
function init() {
// initialize variables we'll need
savedWindowListEl = document.getElementById("savedWindowList");
formEl = document.getElementById("form");
nameInput = document.getElementById("nameInput");
template = document.getElementById("template");
// initialize links
formEl.addEventListener("submit", saveWindow, false);
// CSS buster - only used when developing locally
// style = document.getElementById("style");
// style.href = style.href + "?salt=" + Math.random();
// populate list of windows
chrome.windows.getCurrent(function(currentWindow) {
var currentWindowName = backgroundPage.windowIdToName[currentWindow.id];
var savedWindows = backgroundPage.savedWindows;
var savedWindowNames = backgroundPage.savedWindowNames;
for (var i in savedWindowNames) {
var name = savedWindowNames[i];
var savedWindow = savedWindows[name];
appendWindowToList(savedWindow, currentWindowName);
}
if (!currentWindowName) {
if (currentWindow.incognito) {
document.getElementById("incognitoMsg").style.display = "block";
} else {
nameInput.value = backgroundPage.DEFAULT_NAME;
formEl.style.display = "block";
nameInput.focus();
nameInput.select();
}
}
});
// decrement update message counter
var count = backgroundPage.updateMsgCount;
if (count > 0) {
backgroundPage.updateMsgCount = count - 1;
localStorage.updateMsgCount = count - 1;
document.getElementById("update").style.display = "block";
} else {
backgroundPage.updateBadgeForAllWindows();
}
// this does nothing in the packaged extension
backgroundPage.debug.addDebugUI(document);
}
document.addEventListener('DOMContentLoaded', init);
// add window to HTML list of windows
function appendWindowToList(savedWindow, currentWindowName) {
var li = template.cloneNode(true);
li.removeAttribute("id");
li.setAttribute("data-name", savedWindow.name);
li.querySelector(".delete").addEventListener("click", deleteSavedWindow, false);
li.querySelector(".undo").addEventListener("click", undoDeleteSavedWindow, false);
var count = savedWindow.tabs.length;
var text = savedWindow.displayName + " (" + count + ")";
if (savedWindow.name == currentWindowName) {
li.className = "current";
text = "This is <b>" + text + "<\/b>.";
} else if (savedWindow.id) {
li.className = "open";
li.addEventListener("click", focusOpenWindow, false);
} else {
li.addEventListener("click", openSavedWindow, false);
}
setText(li, text);
// FEATURE: add "add to this set" to add current tabs to existing one
savedWindowListEl.insertBefore(li, savedWindowListEl.firstChild);
}
// save window in background page and update display
function saveWindow(event) {
event.preventDefault();
chrome.windows.getCurrent(function(currentWindow) {
chrome.tabs.getAllInWindow(null, function(tabs) {
currentWindow.tabs = tabs;
savedWindow = backgroundPage.saveWindow(currentWindow, nameInput.value);
formEl.style.display = "none";
appendWindowToList(savedWindow, nameInput.value);
backgroundPage._gaq.push(['_trackEvent', 'popup', 'saveWindow', 'Value is tab count.', savedWindow.tabs.length]);
});
});
}
// open a saved window
// called when the user clicks the name of a saved window that is closed.
function openSavedWindow(event) {
event.preventDefault();
var name = event.currentTarget.getAttribute("data-name");
backgroundPage.openWindow(name);
var savedWindow = backgroundPage.savedWindows[name];
backgroundPage._gaq.push(['_trackEvent', 'popup', 'openWindow', 'Value is tab count.', savedWindow.tabs.length]);
}
// focus an open window
// called when the user clicks the name of a saved window that is open.
function focusOpenWindow(event) {
event.preventDefault();
var name = event.currentTarget.getAttribute("data-name");
var savedWindow = backgroundPage.savedWindows[name];
chrome.windows.update(savedWindow.id, {focused: true});
backgroundPage._gaq.push(['_trackEvent', 'popup', 'focusWindow']);
}
// delete a saved window
// called when the user presses the delete button
function deleteSavedWindow(event) {
event.preventDefault();
event.stopPropagation();
// get data
var li = event.target.parentNode;
var name = li.getAttribute("data-name");
var savedWindow = backgroundPage.savedWindows[name]
var text = li.childNodes[1].innerHTML;
// save information for undo
undo[name] = {
className: li.className,
text: text,
savedWindow: savedWindow,
id: savedWindow.id
};
// we save this separately since deleteSavedWindow nixes it.
if (savedWindow.id) {
undo[name].id = savedWindow.id
}
// actually perform the deletion
backgroundPage.deleteSavedWindow(name);
// update display
li.className = "deleted";
setText(li, "<b>" + getDisplayName(savedWindow) + "<\/b> was deleted.");
// TODO: show the form if current window
backgroundPage._gaq.push(['_trackEvent', 'popup', 'deleteWindow', 'Value is tab count.', savedWindow.tabs.length]);
}
// undo a deletion
// called when the user presse the undo button
function undoDeleteSavedWindow(event) {
event.preventDefault();
event.stopPropagation();
// get data
var li = event.target.parentNode;
var name = li.getAttribute("data-name");
var undoInfo = undo[name];
var savedWindow = undoInfo.savedWindow;
// restore the window id
if (undoInfo.id) {
savedWindow.id = undoInfo.id;
}
// resave the window
backgroundPage.saveWindow(savedWindow, name);
// update display
li.className = undoInfo.className;
setText(li, undoInfo.text);
// clean up
delete undo[name];
// TODO: hide the form if current window
backgroundPage._gaq.push(['_trackEvent', 'popup', 'undoDeleteWindow', 'Value is tab count.', savedWindow.tabs.length]);
}
// given a list element, sets the text
function setText(element, text) {
element.childNodes[1].innerHTML = text;
}
// formats the name for display
function getDisplayName(savedWindow) {
return savedWindow.displayName + " (" + savedWindow.tabs.length + ")";
}