OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // <include src="../../assert.js"> | 5 // <include src="../../assert.js"> |
6 | 6 |
7 cr.exportPath('cr.ui'); | 7 cr.exportPath('cr.ui'); |
8 | 8 |
9 /** | 9 /** |
10 * Enum for type of hide. Delayed is used when called by clicking on a | 10 * Enum for type of hide. Delayed is used when called by clicking on a |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 !this.menu.contains(e.target)) { | 103 !this.menu.contains(e.target)) { |
104 this.hideMenu(); | 104 this.hideMenu(); |
105 } else { | 105 } else { |
106 e.preventDefault(); | 106 e.preventDefault(); |
107 } | 107 } |
108 } else { | 108 } else { |
109 if (this.isMenuShown()) { | 109 if (this.isMenuShown()) { |
110 this.hideMenu(); | 110 this.hideMenu(); |
111 } else if (e.button == 0) { // Only show the menu when using left | 111 } else if (e.button == 0) { // Only show the menu when using left |
112 // mouse button. | 112 // mouse button. |
113 this.showMenu(false); | 113 this.showMenu(false, {x: e.screenX, y: e.screenY}); |
114 | 114 |
115 // Prevent the button from stealing focus on mousedown. | 115 // Prevent the button from stealing focus on mousedown. |
116 e.preventDefault(); | 116 e.preventDefault(); |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 // Hide the focus ring on mouse click. | 120 // Hide the focus ring on mouse click. |
121 this.classList.add('using-mouse'); | 121 this.classList.add('using-mouse'); |
122 break; | 122 break; |
123 case 'keydown': | 123 case 'keydown': |
(...skipping 27 matching lines...) Expand all Loading... |
151 if (!(e.target == this.menu || this.menu.contains(e.target))) | 151 if (!(e.target == this.menu || this.menu.contains(e.target))) |
152 this.hideMenu(); | 152 this.hideMenu(); |
153 break; | 153 break; |
154 case 'popstate': | 154 case 'popstate': |
155 case 'resize': | 155 case 'resize': |
156 this.hideMenu(); | 156 this.hideMenu(); |
157 break; | 157 break; |
158 case 'contextmenu': | 158 case 'contextmenu': |
159 if ((!this.menu || !this.menu.contains(e.target)) && | 159 if ((!this.menu || !this.menu.contains(e.target)) && |
160 (!this.hideTimestamp_ || Date.now() - this.hideTimestamp_ > 50)) | 160 (!this.hideTimestamp_ || Date.now() - this.hideTimestamp_ > 50)) |
161 this.showMenu(true); | 161 this.showMenu(true, {x: e.screenX, y: e.screenY}); |
162 e.preventDefault(); | 162 e.preventDefault(); |
163 // Don't allow elements further up in the DOM to show their menus. | 163 // Don't allow elements further up in the DOM to show their menus. |
164 e.stopPropagation(); | 164 e.stopPropagation(); |
165 break; | 165 break; |
166 case 'dblclick': | 166 case 'dblclick': |
167 // Don't allow double click events to propagate. | 167 // Don't allow double click events to propagate. |
168 e.preventDefault(); | 168 e.preventDefault(); |
169 e.stopPropagation(); | 169 e.stopPropagation(); |
170 break; | 170 break; |
171 } | 171 } |
172 }, | 172 }, |
173 | 173 |
174 /** | 174 /** |
175 * Shows the menu. | 175 * Shows the menu. |
176 * @param {boolean} shouldSetFocus Whether to set focus on the | 176 * @param {boolean} shouldSetFocus Whether to set focus on the |
177 * selected menu item. | 177 * selected menu item. |
| 178 * @param {{x: number, y: number}=} opt_mousePos The position of the mouse |
| 179 * when shown (in screen coordinates). |
178 */ | 180 */ |
179 showMenu: function(shouldSetFocus) { | 181 showMenu: function(shouldSetFocus, opt_mousePos) { |
180 this.hideMenu(); | 182 this.hideMenu(); |
181 | 183 |
182 this.menu.updateCommands(this); | 184 this.menu.updateCommands(this); |
183 | 185 |
184 var event = new UIEvent('menushow',{ | 186 var event = new UIEvent('menushow',{ |
185 bubbles: true, | 187 bubbles: true, |
186 cancelable: true, | 188 cancelable: true, |
187 view: window | 189 view: window |
188 }); | 190 }); |
189 if (!this.dispatchEvent(event)) | 191 if (!this.dispatchEvent(event)) |
190 return; | 192 return; |
191 | 193 |
192 this.menu.hidden = false; | 194 this.menu.show(opt_mousePos); |
193 | 195 |
194 this.setAttribute('menu-shown', ''); | 196 this.setAttribute('menu-shown', ''); |
195 | 197 |
196 // When the menu is shown we steal all keyboard events. | 198 // When the menu is shown we steal all keyboard events. |
197 var doc = this.ownerDocument; | 199 var doc = this.ownerDocument; |
198 var win = doc.defaultView; | 200 var win = doc.defaultView; |
199 this.showingEvents_.add(doc, 'keydown', this, true); | 201 this.showingEvents_.add(doc, 'keydown', this, true); |
200 this.showingEvents_.add(doc, 'mousedown', this, true); | 202 this.showingEvents_.add(doc, 'mousedown', this, true); |
201 this.showingEvents_.add(doc, 'focus', this, true); | 203 this.showingEvents_.add(doc, 'focus', this, true); |
202 this.showingEvents_.add(doc, 'scroll', this, true); | 204 this.showingEvents_.add(doc, 'scroll', this, true); |
(...skipping 15 matching lines...) Expand all Loading... |
218 */ | 220 */ |
219 hideMenu: function(opt_hideType) { | 221 hideMenu: function(opt_hideType) { |
220 if (!this.isMenuShown()) | 222 if (!this.isMenuShown()) |
221 return; | 223 return; |
222 | 224 |
223 this.removeAttribute('menu-shown'); | 225 this.removeAttribute('menu-shown'); |
224 if (opt_hideType == HideType.DELAYED) | 226 if (opt_hideType == HideType.DELAYED) |
225 this.menu.classList.add('hide-delayed'); | 227 this.menu.classList.add('hide-delayed'); |
226 else | 228 else |
227 this.menu.classList.remove('hide-delayed'); | 229 this.menu.classList.remove('hide-delayed'); |
228 this.menu.hidden = true; | 230 this.menu.hide(); |
229 | 231 |
230 this.showingEvents_.removeAll(); | 232 this.showingEvents_.removeAll(); |
231 this.focus(); | 233 this.focus(); |
232 | 234 |
233 var event = new UIEvent('menuhide', { | 235 var event = new UIEvent('menuhide', { |
234 bubbles: true, | 236 bubbles: true, |
235 cancelable: false, | 237 cancelable: false, |
236 view: window | 238 view: window |
237 }); | 239 }); |
238 this.dispatchEvent(event); | 240 this.dispatchEvent(event); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 break; | 284 break; |
283 } | 285 } |
284 } | 286 } |
285 }; | 287 }; |
286 | 288 |
287 // Export | 289 // Export |
288 return { | 290 return { |
289 MenuButton: MenuButton, | 291 MenuButton: MenuButton, |
290 }; | 292 }; |
291 }); | 293 }); |
OLD | NEW |