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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 | 7 |
8 /** | 8 /** |
9 * DefaultActionDialog contains a message, a list box, an ok button, and a | 9 * DefaultActionDialog contains a message, a list box, an ok button, and a |
10 * cancel button. | 10 * cancel button. |
(...skipping 18 matching lines...) Expand all Loading... | |
29 this.frame_.insertBefore(this.list_, this.text_.nextSibling); | 29 this.frame_.insertBefore(this.list_, this.text_.nextSibling); |
30 | 30 |
31 this.selectionModel_ = this.list_.selectionModel = | 31 this.selectionModel_ = this.list_.selectionModel = |
32 new cr.ui.ListSingleSelectionModel(); | 32 new cr.ui.ListSingleSelectionModel(); |
33 this.dataModel_ = this.list_.dataModel = new cr.ui.ArrayDataModel([]); | 33 this.dataModel_ = this.list_.dataModel = new cr.ui.ArrayDataModel([]); |
34 | 34 |
35 // List has max-height defined at css, so that list grows automatically, | 35 // List has max-height defined at css, so that list grows automatically, |
36 // but doesn't exceed predefined size. | 36 // but doesn't exceed predefined size. |
37 this.list_.autoExpands = true; | 37 this.list_.autoExpands = true; |
38 this.list_.activateItemAtIndex = this.activateItemAtIndex_.bind(this); | 38 this.list_.activateItemAtIndex = this.activateItemAtIndex_.bind(this); |
39 // Use 'click' instead of 'change' for keyboard users. | |
40 this.list_.addEventListener('click', this.onSelected_.bind(this)); | |
39 | 41 |
40 this.initialFocusElement_ = this.list_; | 42 this.initialFocusElement_ = this.list_; |
41 | 43 |
42 var self = this; | 44 var self = this; |
43 | 45 |
44 // Binding stuff doesn't work with constructors, so we have to create | 46 // Binding stuff doesn't work with constructors, so we have to create |
45 // closure here. | 47 // closure here. |
46 this.list_.itemConstructor = function(item) { | 48 this.list_.itemConstructor = function(item) { |
47 return self.renderItem(item); | 49 return self.renderItem(item); |
48 }; | 50 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 return result; | 88 return result; |
87 }; | 89 }; |
88 | 90 |
89 /** | 91 /** |
90 * Shows dialog. | 92 * Shows dialog. |
91 * | 93 * |
92 * @param {string} title Title in dialog caption. | 94 * @param {string} title Title in dialog caption. |
93 * @param {string} message Message in dialog caption. | 95 * @param {string} message Message in dialog caption. |
94 * @param {Array.<Object>} items Items to render in the list. | 96 * @param {Array.<Object>} items Items to render in the list. |
95 * @param {number} defaultIndex Item to select by default. | 97 * @param {number} defaultIndex Item to select by default. |
96 * @param {function(Object=)} opt_onOk OK callback with the selected item. | 98 * @param {function(Object)} onSelected Callback which called when an item is |
arv (Not doing code reviews)
2014/01/06 15:13:34
Callback which _is_ called ...
yoshiki
2014/01/08 04:59:02
Done.
| |
97 * @param {function()=} opt_onCancel Cancel callback. | 99 * selected. |
98 */ | 100 */ |
99 DefaultActionDialog.prototype.show = function(title, message, items, | 101 DefaultActionDialog.prototype.show = function(title, message, items, |
100 defaultIndex, opt_onOk, opt_onCancel) { | 102 defaultIndex, onSelectedItem) { |
arv (Not doing code reviews)
2014/01/06 15:13:34
The @param does not match
yoshiki
2014/01/08 04:59:02
Done.
| |
101 | 103 |
102 var show = FileManagerDialogBase.prototype.showOkCancelDialog.call( | 104 this.onSelectedItemCallback_ = onSelectedItem; |
103 this, title, message, opt_onOk, opt_onCancel); | 105 |
106 var show = FileManagerDialogBase.prototype.showTitleAndTextDialog.call( | |
107 this, title, message); | |
104 | 108 |
105 if (!show) { | 109 if (!show) { |
106 console.error('DefaultActionDialog can\'t be shown.'); | 110 console.error('DefaultActionDialog can\'t be shown.'); |
107 return; | 111 return; |
108 } | 112 } |
109 | 113 |
110 if (!message) { | 114 if (!message) { |
111 this.text_.setAttribute('hidden', 'hidden'); | 115 this.text_.setAttribute('hidden', 'hidden'); |
112 } else { | 116 } else { |
113 this.text_.removeAttribute('hidden'); | 117 this.text_.removeAttribute('hidden'); |
114 } | 118 } |
115 | 119 |
116 this.list_.startBatchUpdates(); | 120 this.list_.startBatchUpdates(); |
117 this.dataModel_.splice(0, this.dataModel_.length); | 121 this.dataModel_.splice(0, this.dataModel_.length); |
118 for (var i = 0; i < items.length; i++) { | 122 for (var i = 0; i < items.length; i++) { |
119 this.dataModel_.push(items[i]); | 123 this.dataModel_.push(items[i]); |
120 } | 124 } |
121 this.selectionModel_.selectedIndex = defaultIndex; | 125 this.selectionModel_.selectedIndex = defaultIndex; |
122 this.list_.endBatchUpdates(); | 126 this.list_.endBatchUpdates(); |
123 }; | 127 }; |
124 | 128 |
125 /** | 129 /** |
126 * List activation handler. Closes dialog and calls 'ok' callback. | 130 * List activation handler. Closes dialog and calls 'ok' callback. |
127 * @param {number} index Activated index. | 131 * @param {number} index Activated index. |
128 */ | 132 */ |
129 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { | 133 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { |
130 this.hide(); | 134 this.hide(); |
131 this.onOk_(this.dataModel_.item(index)); | 135 this.onSelectedItemCallback_(this.dataModel_.item(index)); |
132 }; | 136 }; |
133 | 137 |
134 /** | 138 /** |
135 * Closes dialog and invokes callback with currently-selected item. | 139 * Closes dialog and invokes callback with currently-selected item. |
136 * @override | |
137 */ | 140 */ |
138 DefaultActionDialog.prototype.onOkClick_ = function() { | 141 DefaultActionDialog.prototype.onSelected_ = function() { |
139 this.activateItemAtIndex_(this.selectionModel_.selectedIndex); | 142 if (this.selectionModel_.selectedIndex !== -1) |
143 this.activateItemAtIndex_(this.selectionModel_.selectedIndex); | |
140 }; | 144 }; |
141 | 145 |
142 /** | 146 /** |
143 * @override | 147 * @override |
144 */ | 148 */ |
145 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { | 149 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { |
146 // Handle Escape. | 150 // Handle Escape. |
147 if (event.keyCode == 27) { | 151 if (event.keyCode == 27) { |
148 this.onCancelClick_(event); | 152 this.hide(); |
arv (Not doing code reviews)
2014/01/06 15:13:34
wrong indentation
yoshiki
2014/01/08 04:59:02
Done.
| |
149 event.preventDefault(); | 153 event.preventDefault(); |
150 } else if (event.keyCode == 32 || event.keyCode == 13) { | 154 } else if (event.keyCode == 32 || event.keyCode == 13) { |
151 this.onOkClick_(); | 155 this.onSelected_(); |
152 event.preventDefault(); | 156 event.preventDefault(); |
153 } | 157 } |
154 }; | 158 }; |
155 | 159 |
156 return {DefaultActionDialog: DefaultActionDialog}; | 160 return {DefaultActionDialog: DefaultActionDialog}; |
157 }); | 161 }); |
OLD | NEW |