forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
213 lines (184 loc) · 5.44 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
206
207
208
209
210
211
212
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function $(id) {
return document.getElementById(id);
}
// Returns the largest size icon, or a default icon, for the given |app|.
function getIconURL(app) {
if (!app.icons || app.icons.length == 0) {
return chrome.extension.getURL('icon.png');
}
var largest = {size:0};
for (var i = 0; i < app.icons.length; i++) {
var icon = app.icons[i];
if (icon.size > largest.size) {
largest = icon;
}
}
return largest.url;
}
function launchApp(id) {
chrome.management.launchApp(id);
window.close(); // Only needed on OSX because of crbug.com/63594
}
// Adds DOM nodes for |app| into |appsDiv|.
function addApp(appsDiv, app, selected) {
var div = document.createElement('div');
div.className = 'app' + (selected ? ' app_selected' : '');
div.onclick = function() {
launchApp(app.id);
};
var img = document.createElement('img');
img.src = getIconURL(app);
div.appendChild(img);
var title = document.createElement('span');
title.className = 'app_title';
title.innerText = app.name;
div.appendChild(title);
appsDiv.appendChild(div);
}
// The list of all apps & extensions.
var completeList = [];
// A filtered list of apps we actually want to show.
var appList = [];
// The index of an app in |appList| that should be highlighted.
var selectedIndex = 0;
function reloadAppDisplay() {
var appsDiv = $('apps');
// Empty the current content.
appsDiv.innerHTML = '';
for (var i = 0; i < appList.length; i++) {
var item = appList[i];
addApp(appsDiv, item, i == selectedIndex);
}
}
// Puts only enabled apps from completeList into appList.
function rebuildAppList(filter) {
selectedIndex = 0;
appList = [];
for (var i = 0; i < completeList.length; i++){
var item = completeList[i];
// Skip extensions and disabled apps.
if (!item.isApp || !item.enabled) {
continue;
}
if (filter && item.name.toLowerCase().search(filter) < 0) {
continue;
}
appList.push(item);
}
}
// In order to keep the popup bubble from shrinking as your search narrows the
// list of apps shown, we set an explicit width on the outermost div.
var didSetExplicitWidth = false;
function adjustWidthIfNeeded(filter) {
if (filter.length > 0 && !didSetExplicitWidth) {
// Set an explicit width, correcting for any scroll bar present.
var outer = $('outer');
var correction = window.innerWidth - document.documentElement.clientWidth;
var width = outer.offsetWidth;
$('spacer_dummy').style.width = width + correction + 'px';
didSetExplicitWidth = true;
}
}
// Shows the list of apps based on the search box contents.
function onSearchInput() {
var filter = $('search').value;
adjustWidthIfNeeded(filter);
rebuildAppList(filter);
reloadAppDisplay();
}
function compare(a, b) {
return (a > b) ? 1 : (a == b ? 0 : -1);
}
function compareByName(app1, app2) {
return compare(app1.name.toLowerCase(), app2.name.toLowerCase());
}
// Changes the selected app in the list.
function changeSelection(newIndex) {
if (newIndex >= 0 && newIndex <= appList.length - 1) {
selectedIndex = newIndex;
reloadAppDisplay();
var selected = document.getElementsByClassName('app_selected')[0];
var rect = selected.getBoundingClientRect();
if (newIndex == 0) {
window.scrollTo(0, 0);
} else if (newIndex == appList.length - 1) {
window.scrollTo(0, document.height);
} else if (rect.top < 0) {
window.scrollBy(0, rect.top);
} else if (rect.bottom > innerHeight) {
window.scrollBy(0, rect.bottom - innerHeight);
}
}
}
var keys = {
ENTER : 13,
ESCAPE : 27,
END : 35,
HOME : 36,
LEFT : 37,
UP : 38,
RIGHT : 39,
DOWN : 40
};
// Set up a key event handler that handles moving the selected app up/down,
// hitting enter to launch the selected app, as well as auto-focusing the
// search box as soon as you start typing.
window.onkeydown = function(event) {
switch (event.keyCode) {
case keys.DOWN:
changeSelection(selectedIndex + 1);
break;
case keys.UP:
changeSelection(selectedIndex - 1);
break;
case keys.HOME:
changeSelection(0);
break;
case keys.END:
changeSelection(appList.length - 1);
break;
case keys.ENTER:
var app = appList[selectedIndex];
if (app) {
launchApp(app.id);
}
break;
default:
// Focus the search box and return true so you can just start typing even
// when the search box isn't focused.
$('search').focus();
return true;
}
return false;
};
// Initalize the popup window.
document.addEventListener('DOMContentLoaded', function () {
chrome.management.getAll(function(info) {
var appCount = 0;
for (var i = 0; i < info.length; i++) {
if (info[i].isApp) {
appCount++;
}
}
if (appCount == 0) {
$('search').style.display = 'none';
$('appstore_link').style.display = '';
return;
}
completeList = info.sort(compareByName);
onSearchInput();
});
$('search').addEventListener('input', onSearchInput);
// Opens the webstore in a new tab.
document.querySelector('#appstore_link button').addEventListener('click',
function () {
chrome.tabs.create({
'url':'https://chrome.google.com/webstore',
'selected':true
});
window.close();
});
});