OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 |
| 6 /** |
| 7 * DefaultActionDialog contains a message, a list box, an ok button, and a |
| 8 * cancel button. |
| 9 * This dialog should be used as action picker for file operations. |
| 10 */ |
| 11 cr.define('cr.filebrowser', function() { |
| 12 |
| 13 /** |
| 14 * Creates dialog in DOM tree. |
| 15 * |
| 16 * @param {HTMLElement} parentNode Node to be parent for this dialog. |
| 17 */ |
| 18 function DefaultActionDialog(parentNode) { |
| 19 cr.ui.dialogs.BaseDialog.call(this, parentNode); |
| 20 |
| 21 this.list_ = new cr.ui.List(); |
| 22 this.list_.id = 'default-actions-list'; |
| 23 this.frame_.insertBefore(this.list_, this.text_.nextSibling); |
| 24 |
| 25 this.selectionModel_ = this.list_.selectionModel = |
| 26 new cr.ui.ListSingleSelectionModel(); |
| 27 this.dataModel_ = this.list_.dataModel = new cr.ui.ArrayDataModel([]); |
| 28 |
| 29 // List has max-height defined at css, so that list grows automatically, |
| 30 // but doesn't exceed predefined size. |
| 31 this.list_.autoExpands = true; |
| 32 this.list_.activateItemAtIndex = this.activateItemAtIndex_.bind(this); |
| 33 |
| 34 this.initialFocusElement_ = this.list_; |
| 35 |
| 36 var self = this; |
| 37 |
| 38 // Binding stuff doesn't work with constructors, so we have to create |
| 39 // closure here. |
| 40 this.list_.itemConstructor = function(item) { |
| 41 return self.renderItem(item); |
| 42 } |
| 43 } |
| 44 |
| 45 DefaultActionDialog.prototype = { |
| 46 __proto__: cr.ui.dialogs.BaseDialog.prototype |
| 47 }; |
| 48 |
| 49 /** |
| 50 * Overrides BaseDialog::onInputFocus |
| 51 */ |
| 52 DefaultActionDialog.prototype.onInputFocus = function() { |
| 53 this.list_.select(); |
| 54 }; |
| 55 |
| 56 /** |
| 57 * Renders item for list. |
| 58 * @param {Object} item Item to render. |
| 59 */ |
| 60 DefaultActionDialog.prototype.renderItem = function(item) { |
| 61 var result = this.document_.createElement('li'); |
| 62 |
| 63 var iconNode = this.document_.createElement('img'); |
| 64 iconNode.src = item.iconUrl; |
| 65 result.appendChild(iconNode); |
| 66 |
| 67 var labelNode = this.document_.createElement('span'); |
| 68 labelNode.textContent = item.label; |
| 69 result.appendChild(labelNode); |
| 70 |
| 71 cr.defineProperty(result, 'lead', cr.PropertyKind.BOOL_ATTR); |
| 72 cr.defineProperty(result, 'selected', cr.PropertyKind.BOOL_ATTR); |
| 73 |
| 74 return result; |
| 75 } |
| 76 |
| 77 /** |
| 78 * Shows dialog. |
| 79 * |
| 80 * @param {String} message Message in dialog caption. |
| 81 * @param {Array} items Items to render in list |
| 82 * @param {int} defaultIndex Item to select by default. |
| 83 * @param {Function} onOk Callback function. |
| 84 * @param {Function} onCancel Callback function. |
| 85 * @param {Function} onShow Callback function. |
| 86 */ |
| 87 DefaultActionDialog.prototype.show = function(message, items, defaultIndex, |
| 88 onOk, onCancel, onShow) { |
| 89 |
| 90 cr.ui.dialogs.BaseDialog.prototype.show.apply(this, |
| 91 [message, onOk, onCancel, onShow]); |
| 92 |
| 93 this.list_.startBatchUpdates(); |
| 94 |
| 95 this.dataModel_.splice(0, this.dataModel_.length); |
| 96 |
| 97 for (var i = 0; i < items.length; i++) { |
| 98 this.dataModel_.push(items[i]); |
| 99 } |
| 100 |
| 101 this.selectionModel_.selectedIndex = defaultIndex; |
| 102 |
| 103 this.list_.endBatchUpdates(); |
| 104 }; |
| 105 |
| 106 /** |
| 107 * List activation handler. Closes dialog and calls 'ok' callback. |
| 108 * |
| 109 * @param {int} index Activated index. |
| 110 */ |
| 111 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { |
| 112 this.hide(); |
| 113 if (this.onOk_) |
| 114 this.onOk_(this.dataModel_.item(index).task); |
| 115 } |
| 116 |
| 117 /** |
| 118 * Closes dialog and invokes callback with currently-selected item. |
| 119 */ |
| 120 DefaultActionDialog.prototype.onOkClick_ = function() { |
| 121 this.activateItemAtIndex_(this.selectionModel_.selectedIndex); |
| 122 }; |
| 123 |
| 124 // Overrides BaseDialog::onContainerKeyDown_; |
| 125 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { |
| 126 // Handle Escape. |
| 127 if (event.keyCode == 27) { |
| 128 this.onCancelClick_(event); |
| 129 event.preventDefault(); |
| 130 } else if (event.keyCode == 32 || event.keyCode == 13) { |
| 131 this.onOkClick_(); |
| 132 event.preventDefault(); |
| 133 } |
| 134 }; |
| 135 |
| 136 return {DefaultActionDialog: DefaultActionDialog}; |
| 137 }); |
OLD | NEW |