Chromium Code Reviews| 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 this.selectionModel_.addEventListener( | |
| 40 t'change', this.onSelected_.bind(this)); | |
|
hirono
2013/12/13 03:31:09
nit: remove t.
| |
| 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 with the selected item. |
| 97 * @param {function()=} opt_onCancel Cancel callback. | 99 * @param {function()=} opt_onCancel Cancel callback with no selected item. |
| 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, opt_onCancel) { |
| 101 | 103 |
| 102 var show = FileManagerDialogBase.prototype.showOkCancelDialog.call( | 104 this.onSelectedItemCallback_ = onSelectedItem; |
| 103 this, title, message, opt_onOk, opt_onCancel); | 105 this.onCancelledCallback_ = opt_onCancel || function() {}; |
| 106 | |
| 107 var show = FileManagerDialogBase.prototype.showTitleAndTextDialog.call( | |
| 108 this, title, message); | |
| 104 | 109 |
| 105 if (!show) { | 110 if (!show) { |
| 106 console.error('DefaultActionDialog can\'t be shown.'); | 111 console.error('DefaultActionDialog can\'t be shown.'); |
| 107 return; | 112 return; |
| 108 } | 113 } |
| 109 | 114 |
| 110 if (!message) { | 115 if (!message) { |
| 111 this.text_.setAttribute('hidden', 'hidden'); | 116 this.text_.setAttribute('hidden', 'hidden'); |
| 112 } else { | 117 } else { |
| 113 this.text_.removeAttribute('hidden'); | 118 this.text_.removeAttribute('hidden'); |
| 114 } | 119 } |
| 115 | 120 |
| 116 this.list_.startBatchUpdates(); | 121 this.list_.startBatchUpdates(); |
| 117 this.dataModel_.splice(0, this.dataModel_.length); | 122 this.dataModel_.splice(0, this.dataModel_.length); |
| 118 for (var i = 0; i < items.length; i++) { | 123 for (var i = 0; i < items.length; i++) { |
| 119 this.dataModel_.push(items[i]); | 124 this.dataModel_.push(items[i]); |
| 120 } | 125 } |
| 121 this.selectionModel_.selectedIndex = defaultIndex; | 126 this.selectionModel_.selectedIndex = defaultIndex; |
| 122 this.list_.endBatchUpdates(); | 127 this.list_.endBatchUpdates(); |
| 123 }; | 128 }; |
| 124 | 129 |
| 125 /** | 130 /** |
| 126 * List activation handler. Closes dialog and calls 'ok' callback. | 131 * List activation handler. Closes dialog and calls 'ok' callback. |
| 127 * @param {number} index Activated index. | 132 * @param {number} index Activated index. |
| 128 */ | 133 */ |
| 129 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { | 134 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { |
| 130 this.hide(); | 135 this.hide(); |
| 131 this.onOk_(this.dataModel_.item(index)); | 136 this.onSelectedItemCallback_(this.dataModel_.item(index)); |
| 132 }; | 137 }; |
| 133 | 138 |
| 134 /** | 139 /** |
| 135 * Closes dialog and invokes callback with currently-selected item. | 140 * Closes dialog and invokes callback with currently-selected item. |
| 136 * @override | |
| 137 */ | 141 */ |
| 138 DefaultActionDialog.prototype.onOkClick_ = function() { | 142 DefaultActionDialog.prototype.onSelected_ = function() { |
| 139 this.activateItemAtIndex_(this.selectionModel_.selectedIndex); | 143 if (this.selectionModel_.selectedIndex !== -1) |
| 144 this.activateItemAtIndex_(this.selectionModel_.selectedIndex); | |
| 140 }; | 145 }; |
| 141 | 146 |
| 142 /** | 147 /** |
| 143 * @override | 148 * @override |
| 144 */ | 149 */ |
| 145 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { | 150 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { |
| 146 // Handle Escape. | 151 // Handle Escape. |
| 147 if (event.keyCode == 27) { | 152 if (event.keyCode == 27) { |
| 148 this.onCancelClick_(event); | 153 this.hide(); |
| 149 event.preventDefault(); | 154 event.preventDefault(); |
| 150 } else if (event.keyCode == 32 || event.keyCode == 13) { | 155 } else if (event.keyCode == 32 || event.keyCode == 13) { |
| 151 this.onOkClick_(); | 156 this.onSelected_(); |
| 152 event.preventDefault(); | 157 event.preventDefault(); |
| 153 } | 158 } |
| 154 }; | 159 }; |
| 155 | 160 |
| 156 return {DefaultActionDialog: DefaultActionDialog}; | 161 return {DefaultActionDialog: DefaultActionDialog}; |
| 157 }); | 162 }); |
| OLD | NEW |