Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: chrome/common/extensions/docs/examples/extensions/app_launcher/popup.js

Issue 8309001: Adding `content_security_policy` to a few sample extensions. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: License and whitespace. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 function $(id) { 5 function $(id) {
6 return document.getElementById(id); 6 return document.getElementById(id);
7 } 7 }
8 8
9 // Returns the largest size icon, or a default icon, for the given |app|. 9 // Returns the largest size icon, or a default icon, for the given |app|.
10 function getIconURL(app) { 10 function getIconURL(app) {
11 if (!app.icons || app.icons.length == 0) { 11 if (!app.icons || app.icons.length == 0) {
12 return chrome.extension.getURL("icon.png"); 12 return chrome.extension.getURL('icon.png');
13 } 13 }
14 var largest = {size:0}; 14 var largest = {size:0};
15 for (var i = 0; i < app.icons.length; i++) { 15 for (var i = 0; i < app.icons.length; i++) {
16 var icon = app.icons[i]; 16 var icon = app.icons[i];
17 if (icon.size > largest.size) { 17 if (icon.size > largest.size) {
18 largest = icon; 18 largest = icon;
19 } 19 }
20 } 20 }
21 return largest.url; 21 return largest.url;
22 } 22 }
23 23
24 function launchApp(id) { 24 function launchApp(id) {
25 chrome.management.launchApp(id); 25 chrome.management.launchApp(id);
26 window.close(); // Only needed on OSX because of crbug.com/63594 26 window.close(); // Only needed on OSX because of crbug.com/63594
27 } 27 }
28 28
29 // Adds DOM nodes for |app| into |appsDiv|. 29 // Adds DOM nodes for |app| into |appsDiv|.
30 function addApp(appsDiv, app, selected) { 30 function addApp(appsDiv, app, selected) {
31 var div = document.createElement("div"); 31 var div = document.createElement('div');
32 div.className = "app" + (selected ? " app_selected" : ""); 32 div.className = 'app' + (selected ? ' app_selected' : '');
33 33
34 div.onclick = function() { 34 div.onclick = function() {
35 launchApp(app.id); 35 launchApp(app.id);
36 }; 36 };
37 37
38 var img = document.createElement("img"); 38 var img = document.createElement('img');
39 img.src = getIconURL(app); 39 img.src = getIconURL(app);
40 div.appendChild(img); 40 div.appendChild(img);
41 41
42 var title = document.createElement("span"); 42 var title = document.createElement('span');
43 title.className = "app_title"; 43 title.className = 'app_title';
44 title.innerText = app.name; 44 title.innerText = app.name;
45 div.appendChild(title); 45 div.appendChild(title);
46 46
47 appsDiv.appendChild(div); 47 appsDiv.appendChild(div);
48 } 48 }
49 49
50 // The list of all apps & extensions. 50 // The list of all apps & extensions.
51 var completeList = []; 51 var completeList = [];
52 52
53 // A filtered list of apps we actually want to show. 53 // A filtered list of apps we actually want to show.
54 var appList = []; 54 var appList = [];
55 55
56 // The index of an app in |appList| that should be highlighted. 56 // The index of an app in |appList| that should be highlighted.
57 var selectedIndex = 0; 57 var selectedIndex = 0;
58 58
59 function reloadAppDisplay() { 59 function reloadAppDisplay() {
60 var appsDiv = $("apps"); 60 var appsDiv = $('apps');
61 61
62 // Empty the current content. 62 // Empty the current content.
63 appsDiv.innerHTML = ""; 63 appsDiv.innerHTML = '';
64 64
65 for (var i = 0; i < appList.length; i++) { 65 for (var i = 0; i < appList.length; i++) {
66 var item = appList[i]; 66 var item = appList[i];
67 addApp(appsDiv, item, i == selectedIndex); 67 addApp(appsDiv, item, i == selectedIndex);
68 } 68 }
69 } 69 }
70 70
71 // Puts only enabled apps from completeList into appList. 71 // Puts only enabled apps from completeList into appList.
72 function rebuildAppList(filter) { 72 function rebuildAppList(filter) {
73 selectedIndex = 0; 73 selectedIndex = 0;
(...skipping 11 matching lines...) Expand all
85 } 85 }
86 } 86 }
87 87
88 // In order to keep the popup bubble from shrinking as your search narrows the 88 // In order to keep the popup bubble from shrinking as your search narrows the
89 // list of apps shown, we set an explicit width on the outermost div. 89 // list of apps shown, we set an explicit width on the outermost div.
90 var didSetExplicitWidth = false; 90 var didSetExplicitWidth = false;
91 91
92 function adjustWidthIfNeeded(filter) { 92 function adjustWidthIfNeeded(filter) {
93 if (filter.length > 0 && !didSetExplicitWidth) { 93 if (filter.length > 0 && !didSetExplicitWidth) {
94 // Set an explicit width, correcting for any scroll bar present. 94 // Set an explicit width, correcting for any scroll bar present.
95 var outer = $("outer"); 95 var outer = $('outer');
96 var correction = window.innerWidth - document.documentElement.clientWidth; 96 var correction = window.innerWidth - document.documentElement.clientWidth;
97 var width = outer.offsetWidth; 97 var width = outer.offsetWidth;
98 $("spacer_dummy").style.width = width + correction + "px"; 98 $('spacer_dummy').style.width = width + correction + 'px';
99 didSetExplicitWidth = true; 99 didSetExplicitWidth = true;
100 } 100 }
101 } 101 }
102 102
103 // Shows the list of apps based on the search box contents. 103 // Shows the list of apps based on the search box contents.
104 function onSearchInput() { 104 function onSearchInput() {
105 var filter = $("search").value; 105 var filter = $('search').value;
106 adjustWidthIfNeeded(filter); 106 adjustWidthIfNeeded(filter);
107 rebuildAppList(filter); 107 rebuildAppList(filter);
108 reloadAppDisplay(); 108 reloadAppDisplay();
109 } 109 }
110 110
111 function compare(a, b) { 111 function compare(a, b) {
112 return (a > b) ? 1 : (a == b ? 0 : -1); 112 return (a > b) ? 1 : (a == b ? 0 : -1);
113 } 113 }
114 114
115 function compareByName(app1, app2) { 115 function compareByName(app1, app2) {
116 return compare(app1.name.toLowerCase(), app2.name.toLowerCase()); 116 return compare(app1.name.toLowerCase(), app2.name.toLowerCase());
117 } 117 }
118 118
119 function onLoad() {
120 chrome.management.getAll(function(info) {
121 var appCount = 0;
122 for (var i = 0; i < info.length; i++) {
123 if (info[i].isApp) {
124 appCount++;
125 }
126 }
127 if (appCount == 0) {
128 $("search").style.display = "none";
129 $("appstore_link").style.display = "";
130 return;
131 }
132 completeList = info.sort(compareByName);
133 onSearchInput();
134 });
135 }
136
137 // Changes the selected app in the list. 119 // Changes the selected app in the list.
138 function changeSelection(newIndex) { 120 function changeSelection(newIndex) {
139 if (newIndex >= 0 && newIndex <= appList.length - 1) { 121 if (newIndex >= 0 && newIndex <= appList.length - 1) {
140 selectedIndex = newIndex; 122 selectedIndex = newIndex;
141 reloadAppDisplay(); 123 reloadAppDisplay();
142 124
143 var selected = document.getElementsByClassName("app_selected")[0]; 125 var selected = document.getElementsByClassName('app_selected')[0];
144 var rect = selected.getBoundingClientRect(); 126 var rect = selected.getBoundingClientRect();
145 if (newIndex == 0) { 127 if (newIndex == 0) {
146 window.scrollTo(0, 0); 128 window.scrollTo(0, 0);
147 } else if (newIndex == appList.length - 1) { 129 } else if (newIndex == appList.length - 1) {
148 window.scrollTo(0, document.height); 130 window.scrollTo(0, document.height);
149 } else if (rect.top < 0) { 131 } else if (rect.top < 0) {
150 window.scrollBy(0, rect.top); 132 window.scrollBy(0, rect.top);
151 } else if (rect.bottom > innerHeight) { 133 } else if (rect.bottom > innerHeight) {
152 window.scrollBy(0, rect.bottom - innerHeight); 134 window.scrollBy(0, rect.bottom - innerHeight);
153 } 135 }
(...skipping 30 matching lines...) Expand all
184 break; 166 break;
185 case keys.ENTER: 167 case keys.ENTER:
186 var app = appList[selectedIndex]; 168 var app = appList[selectedIndex];
187 if (app) { 169 if (app) {
188 launchApp(app.id); 170 launchApp(app.id);
189 } 171 }
190 break; 172 break;
191 default: 173 default:
192 // Focus the search box and return true so you can just start typing even 174 // Focus the search box and return true so you can just start typing even
193 // when the search box isn't focused. 175 // when the search box isn't focused.
194 $("search").focus(); 176 $('search').focus();
195 return true; 177 return true;
196 } 178 }
197 return false; 179 return false;
198 }; 180 };
181
182
183 // Initalize the popup window.
184 document.addEventListener('DOMContentLoaded', function () {
185 chrome.management.getAll(function(info) {
186 var appCount = 0;
187 for (var i = 0; i < info.length; i++) {
188 if (info[i].isApp) {
189 appCount++;
190 }
191 }
192 if (appCount == 0) {
193 $('search').style.display = 'none';
194 $('appstore_link').style.display = '';
195 return;
196 }
197 completeList = info.sort(compareByName);
198 onSearchInput();
199 });
200
201 $('search').addEventListener('input', onSearchInput);
202
203 // Opens the webstore in a new tab.
204 document.querySelector('#appstore_link button').addEventListener('click',
205 function () {
206 chrome.tabs.create({
207 'url':'https://chrome.google.com/webstore',
208 'selected':true
209 });
210 window.close();
211 });
212 });
213
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698