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

Side by Side Diff: chrome/browser/resources/shared/js/cr/ui/menu.js

Issue 10377169: Added menuItem icon support, and little API for menu modification. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added separator API. Simplified code. Created 8 years, 7 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) 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 cr.define('cr.ui', function() { 5 cr.define('cr.ui', function() {
6 6
7 /** @const */ var MenuItem = cr.ui.MenuItem; 7 /** @const */ var MenuItem = cr.ui.MenuItem;
8 8
9 /** 9 /**
10 * Creates a new menu element. 10 * Creates a new menu element.
(...skipping 16 matching lines...) Expand all
27 this.addEventListener('mouseout', this.handleMouseOut_); 27 this.addEventListener('mouseout', this.handleMouseOut_);
28 28
29 // Decorate the children as menu items. 29 // Decorate the children as menu items.
30 var children = this.children; 30 var children = this.children;
31 for (var i = 0, child; child = children[i]; i++) { 31 for (var i = 0, child; child = children[i]; i++) {
32 cr.ui.decorate(child, MenuItem); 32 cr.ui.decorate(child, MenuItem);
33 } 33 }
34 }, 34 },
35 35
36 /** 36 /**
37 * Adds menu item at the end of the list.
38 * @param {Object} item Menu item properties.
39 * @return {cr.ui.MenuItem} The created menu item.
40 */
41 addMenuItem: function(item) {
42 var menuItem = this.ownerDocument.createElement('menuitem');
43 this.appendChild(menuItem);
44
45 cr.ui.decorate(menuItem, MenuItem);
46
47 if (item.label)
48 menuItem.label = item.label;
49
50 if (item.iconUrl)
51 menuItem.iconUrl = item.iconUrl;
52
53 return menuItem;
54 },
55
56 /**
57 * Adds separator at the end of the list.
58 */
59 addSeparator: function() {
60 var separator = this.ownerDocument.createElement('hr');
61 this.appendChild(separator);
62 },
63
64 /**
65 * Clears menu.
66 */
67 clear: function() {
68 this.textContent = '';
69 },
70
71 /**
37 * Walks up the ancestors of |el| until a menu item belonging to this menu 72 * Walks up the ancestors of |el| until a menu item belonging to this menu
38 * is found. 73 * is found.
39 * @param {Element} el The element to start searching from. 74 * @param {Element} el The element to start searching from.
40 * @return {cr.ui.MenuItem} The found menu item or null. 75 * @return {cr.ui.MenuItem} The found menu item or null.
41 * @private 76 * @private
42 */ 77 */
43 findMenuItem_: function(el) { 78 findMenuItem_: function(el) {
44 while (el && el.parentNode != this) { 79 while (el && el.parentNode != this) {
45 el = el.parentNode; 80 el = el.parentNode;
46 } 81 }
(...skipping 25 matching lines...) Expand all
72 */ 107 */
73 get selectedItem() { 108 get selectedItem() {
74 return this.children[this.selectedIndex]; 109 return this.children[this.selectedIndex];
75 }, 110 },
76 set selectedItem(item) { 111 set selectedItem(item) {
77 var index = Array.prototype.indexOf.call(this.children, item); 112 var index = Array.prototype.indexOf.call(this.children, item);
78 this.selectedIndex = index; 113 this.selectedIndex = index;
79 }, 114 },
80 115
81 /** 116 /**
117 * Menu length
118 */
119 get length() {
120 return this.children.length;
121 },
122
123 /**
82 * This is the function that handles keyboard navigation. This is usually 124 * This is the function that handles keyboard navigation. This is usually
83 * called by the element responsible for managing the menu. 125 * called by the element responsible for managing the menu.
84 * @param {Event} e The keydown event object. 126 * @param {Event} e The keydown event object.
85 * @return {boolean} Whether the event was handled be the menu. 127 * @return {boolean} Whether the event was handled be the menu.
86 */ 128 */
87 handleKeyDown: function(e) { 129 handleKeyDown: function(e) {
88 var item = this.selectedItem; 130 var item = this.selectedItem;
89 131
90 var self = this; 132 var self = this;
91 function selectNextVisible(m) { 133 function selectNextVisible(m) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 * @type {number} 183 * @type {number}
142 */ 184 */
143 cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS, 185 cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS,
144 selectedIndexChanged); 186 selectedIndexChanged);
145 187
146 // Export 188 // Export
147 return { 189 return {
148 Menu: Menu 190 Menu: Menu
149 }; 191 };
150 }); 192 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698