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