OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 getAppsCallback(data) { | 5 function getAppsCallback(data) { |
6 logEvent('recieved apps'); | 6 logEvent('recieved apps'); |
7 var appsSection = $('apps'); | 7 var appsSection = $('apps'); |
8 var appsSectionContent = $('apps-maxiview'); | 8 var appsSectionContent = $('apps-maxiview'); |
9 var appsMiniview = appsSection.getElementsByClassName('miniview')[0]; | 9 var appsMiniview = appsSection.getElementsByClassName('miniview')[0]; |
10 appsSectionContent.textContent = ''; | 10 appsSectionContent.textContent = ''; |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 data.apps.slice(0, MAX_MINIVIEW_ITEMS).forEach(function(app) { | 24 data.apps.slice(0, MAX_MINIVIEW_ITEMS).forEach(function(app) { |
25 appsMiniview.appendChild(apps.createMiniviewElement(app)); | 25 appsMiniview.appendChild(apps.createMiniviewElement(app)); |
26 }); | 26 }); |
27 | 27 |
28 appsSection.classList.remove('disabled'); | 28 appsSection.classList.remove('disabled'); |
29 updateMiniviewClipping(appsMiniview); | 29 updateMiniviewClipping(appsMiniview); |
30 layoutSections(); | 30 layoutSections(); |
31 } | 31 } |
32 | 32 |
33 var apps = { | 33 var apps = (function() { |
34 /** | |
35 * @this {!HTMLAnchorElement} | |
36 */ | |
37 handleClick_: function() { | |
38 // TODO(arv): Handle zoom? | |
39 var rect = this.getBoundingClientRect(); | |
40 var cs = getComputedStyle(this); | |
41 var size = cs.backgroundSize.split(/\s+/); // background-size has the | |
42 // format '123px 456px'. | |
43 var width = parseInt(size[0], 10); | |
44 var height = parseInt(size[1], 10); | |
45 // We are using background-position-x 50%. | |
46 var left = rect.left + ((rect.width - width) >> 1); // Integer divide by 2. | |
47 var top = rect.top + parseInt(cs.backgroundPositionY, 10); | |
48 | 34 |
49 chrome.send('launchApp', [this.getAttribute("app_id"), | 35 function createElement(app) { |
50 String(left), String(top), | |
51 String(width), String(height)]); | |
52 return false; | |
53 }, | |
54 | |
55 createElement_: function(app) { | |
56 var div = document.createElement('div'); | 36 var div = document.createElement('div'); |
57 div.className = 'app'; | 37 div.className = 'app'; |
58 | 38 |
59 var front = div.appendChild(document.createElement('div')); | 39 var a = div.appendChild(document.createElement('a')); |
60 front.className = 'front'; | 40 a.setAttribute('app-id', app['id']); |
61 | |
62 var a = front.appendChild(document.createElement('a')); | |
63 a.setAttribute('app_id', app['id']); | |
64 a.xtitle = a.textContent = app['name']; | 41 a.xtitle = a.textContent = app['name']; |
65 a.href = app['launch_url']; | 42 a.href = app['launch_url']; |
66 | 43 |
67 return div; | 44 return div; |
68 }, | 45 } |
69 | 46 |
70 createElement: function(app) { | 47 function createContextMenu(app) { |
71 var div = this.createElement_(app); | 48 var menu = new cr.ui.Menu; |
72 var front = div.firstChild; | 49 var button = document.createElement(button); |
73 var a = front.firstChild; | 50 } |
74 | 51 |
75 a.onclick = apps.handleClick_; | 52 function launchApp(appId) { |
76 a.style.backgroundImage = url(app['icon_big']); | 53 var appsSection = $('apps'); |
77 if (hashParams['app-id'] == app['id']) { | 54 var expanded = !appsSection.classList.contains('hidden'); |
78 div.setAttribute('new', 'new'); | 55 var element = document.querySelector( |
79 // Delay changing the attribute a bit to let the page settle down a bit. | 56 (expanded ? '.maxiview' : '.miniview') + ' a[app-id=' + appId + ']'); |
80 setTimeout(function() { | 57 |
81 div.setAttribute('new', 'installed'); | 58 // TODO(arv): Handle zoom? |
82 }, 500); | 59 var rect = element.getBoundingClientRect(); |
| 60 var cs = getComputedStyle(element); |
| 61 var size = cs.backgroundSize.split(/\s+/); // background-size has the |
| 62 // format '123px 456px'. |
| 63 |
| 64 var width = parseInt(size[0], 10); |
| 65 var height = parseInt(size[1], 10); |
| 66 |
| 67 var top, left; |
| 68 if (expanded) { |
| 69 // We are using background-position-x 50%. |
| 70 top = rect.top + parseInt(cs.backgroundPositionY, 10); |
| 71 left = rect.left + ((rect.width - width) >> 1); // Integer divide by 2. |
| 72 |
| 73 } else { |
| 74 // We are using background-position-y 50%. |
| 75 top = rect.top + ((rect.height - width) >> 1); // Integer divide by 2. |
| 76 if (getComputedStyle(element).direction == 'rtl') |
| 77 left = rect.left + rect.width - width; |
| 78 else |
| 79 left = rect.left; |
83 } | 80 } |
84 | 81 |
85 var settingsButton = front.appendChild(document.createElement('button')); | 82 chrome.send('launchApp', [appId, |
86 settingsButton.className = 'flip'; | 83 String(left), String(top), |
87 settingsButton.title = localStrings.getString('appsettings'); | 84 String(width), String(height)]); |
| 85 } |
88 | 86 |
89 var back = div.appendChild(document.createElement('div')); | 87 /** |
90 back.className = 'back'; | 88 * @this {!HTMLAnchorElement} |
| 89 */ |
| 90 function handleClick(e) { |
| 91 var appId = e.currentTarget.getAttribute('app-id'); |
| 92 launchApp(appId); |
| 93 return false; |
| 94 } |
91 | 95 |
92 var header = back.appendChild(document.createElement('h2')); | 96 var currentApp; |
93 header.textContent = app['name']; | |
94 | 97 |
95 var optionsButton = back.appendChild(document.createElement('button')); | 98 function addContextMenu(el, app) { |
96 optionsButton.textContent = localStrings.getString('appoptions'); | 99 el.addEventListener('contextmenu', cr.ui.contextMenuHandler); |
97 optionsButton.disabled = !app['options_url']; | 100 el.addEventListener('keydown', cr.ui.contextMenuHandler); |
98 optionsButton.onclick = function() { | 101 el.addEventListener('keyup', cr.ui.contextMenuHandler); |
99 window.location = app['options_url']; | |
100 }; | |
101 | 102 |
102 var uninstallButton = back.appendChild(document.createElement('button')); | 103 Object.defineProperty(el, 'contextMenu', { |
103 uninstallButton.textContent = uninstallButton.xtitle = | 104 get: function() { |
104 localStrings.getString('appuninstall'); | 105 currentApp = app; |
105 uninstallButton.onclick = function() { | |
106 chrome.send('uninstallApp', [app['id']]); | |
107 }; | |
108 | 106 |
109 var closeButton = back.appendChild(document.createElement('button')); | 107 $('apps-launch-command').label = app['name']; |
110 closeButton.title = localStrings.getString('close'); | 108 $('apps-options-command').canExecuteChange(); |
111 closeButton.className = 'flip'; | |
112 closeButton.onclick = settingsButton.onclick = function() { | |
113 div.classList.toggle('config'); | |
114 }; | |
115 | 109 |
116 return div; | 110 return $('app-context-menu'); |
117 }, | 111 } |
118 | |
119 createMiniviewElement: function(app) { | |
120 var span = document.createElement('span'); | |
121 var a = span.appendChild(document.createElement('a')); | |
122 | |
123 a.setAttribute('app_id', app['id']); | |
124 a.textContent = app['name']; | |
125 a.href = app['launch_url']; | |
126 a.onclick = apps.handleClick_; | |
127 a.style.backgroundImage = url(app['icon_small']); | |
128 a.className = 'item'; | |
129 span.appendChild(a); | |
130 return span; | |
131 }, | |
132 | |
133 createWebStoreElement: function() { | |
134 return this.createElement_({ | |
135 'id': 'web-store-entry', | |
136 'name': localStrings.getString('web_store_title'), | |
137 'launch_url': localStrings.getString('web_store_url') | |
138 }); | 112 }); |
139 } | 113 } |
140 }; | 114 |
| 115 document.addEventListener('command', function(e) { |
| 116 if (!currentApp) |
| 117 return; |
| 118 |
| 119 switch (e.command.id) { |
| 120 case 'apps-options-command': |
| 121 window.location = currentApp['options_url']; |
| 122 break; |
| 123 case 'apps-launch-command': |
| 124 launchApp(currentApp['id']); |
| 125 break; |
| 126 case 'apps-uninstall-command': |
| 127 chrome.send('uninstallApp', [currentApp['id']]); |
| 128 break; |
| 129 } |
| 130 }); |
| 131 |
| 132 document.addEventListener('canExecute', function(e) { |
| 133 switch (e.command.id) { |
| 134 case 'apps-options-command': |
| 135 e.canExecute = currentApp && currentApp['options_url']; |
| 136 break; |
| 137 case 'apps-launch-command': |
| 138 case 'apps-uninstall-command': |
| 139 e.canExecute = true; |
| 140 break; |
| 141 } |
| 142 }); |
| 143 |
| 144 return { |
| 145 createElement: function(app) { |
| 146 var div = createElement(app); |
| 147 var a = div.firstChild; |
| 148 |
| 149 a.onclick = handleClick; |
| 150 a.style.backgroundImage = url(app['icon_big']); |
| 151 if (hashParams['app-id'] == app['id']) { |
| 152 div.setAttribute('new', 'new'); |
| 153 // Delay changing the attribute a bit to let the page settle down a bit. |
| 154 setTimeout(function() { |
| 155 div.setAttribute('new', 'installed'); |
| 156 }, 500); |
| 157 } |
| 158 |
| 159 var settingsButton = div.appendChild(new cr.ui.ContextMenuButton); |
| 160 settingsButton.className = 'app-settings'; |
| 161 settingsButton.title = localStrings.getString('appsettings'); |
| 162 |
| 163 addContextMenu(div, app); |
| 164 |
| 165 return div; |
| 166 }, |
| 167 |
| 168 createMiniviewElement: function(app) { |
| 169 var span = document.createElement('span'); |
| 170 var a = span.appendChild(document.createElement('a')); |
| 171 |
| 172 a.setAttribute('app-id', app['id']); |
| 173 a.textContent = app['name']; |
| 174 a.href = app['launch_url']; |
| 175 a.onclick = handleClick; |
| 176 a.style.backgroundImage = url(app['icon_small']); |
| 177 a.className = 'item'; |
| 178 span.appendChild(a); |
| 179 |
| 180 addContextMenu(span, app); |
| 181 |
| 182 return span; |
| 183 }, |
| 184 |
| 185 createWebStoreElement: function() { |
| 186 return createElement({ |
| 187 'id': 'web-store-entry', |
| 188 'name': localStrings.getString('web_store_title'), |
| 189 'launch_url': localStrings.getString('web_store_url') |
| 190 }); |
| 191 } |
| 192 }; |
| 193 })(); |
OLD | NEW |