Chromium Code Reviews| Index: chrome/browser/resources/file_manager/js/default_action_dialog.js |
| diff --git a/chrome/browser/resources/file_manager/js/default_action_dialog.js b/chrome/browser/resources/file_manager/js/default_action_dialog.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c91c110115677794813829ab40d01d8a3fcccff2 |
| --- /dev/null |
| +++ b/chrome/browser/resources/file_manager/js/default_action_dialog.js |
| @@ -0,0 +1,117 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| + |
| +/** |
| + * DefaultActionDialog contains a message, a list box, an ok button, and a |
|
dgozman
2012/05/22 14:55:27
Better describe what is this dialog for, not the e
Dmitry Zvorygin
2012/05/22 15:11:10
Done.
|
| + * cancel button. |
| + */ |
| +cr.define('cr.filebrowser', function() { |
| + |
| + /** |
| + * Creates dialog in DOM tree. |
| + * |
| + * @param {HTMLElement} parentNode Node to be parent for this dialog. |
| + */ |
| + function DefaultActionDialog(parentNode) { |
| + cr.ui.dialogs.BaseDialog.call(this, parentNode); |
| + |
| + this.list_ = new cr.ui.List(); |
| + this.list_.id = 'default-actions-list'; |
| + this.frame_.insertBefore(this.list_, this.text_.nextSibling); |
| + |
| + this.selectionModel_ = this.list_.selectionModel = |
| + new cr.ui.ListSingleSelectionModel(); |
| + this.dataModel_ = this.list_.dataModel = new cr.ui.ArrayDataModel([]); |
| + |
| + // List has max-height defined at css, so that list grows automatically, |
| + // but doesn't exceed predefined size. |
| + this.list_.autoExpands = true; |
| + this.list_.activateItemAtIndex = this.activateItemAtIndex_.bind(this); |
| + |
| + this.initialFocusElement_ = this.list_; |
| + |
| + var self = this; |
| + |
| + |
| + // Binding stuff doesn't work with constructors, so we have to create |
| + // closure here. |
| + this.list_.itemConstructor = function(item) { |
| + return self.renderItem(item); |
| + } |
| + } |
| + |
| + DefaultActionDialog.prototype = { |
| + __proto__: cr.ui.dialogs.BaseDialog.prototype |
| + }; |
| + |
| + DefaultActionDialog.prototype.onInputFocus = function() { |
| + this.list_.select(); |
| + }; |
| + |
| + DefaultActionDialog.prototype.onKeyDown_ = function(event) { |
| + if (event.keyCode == 13) // Enter |
| + this.onOkClick_(event); |
| + }; |
| + |
| + DefaultActionDialog.prototype.renderItem = function(item) { |
| + var result = this.document_.createElement('li'); |
| + |
| + var iconNode = this.document_.createElement('img'); |
| + iconNode.src = item.iconUrl; |
| + result.appendChild(iconNode); |
| + |
| + var labelNode = this.document_.createElement('span'); |
| + labelNode.textContent = item.label; |
| + result.appendChild(labelNode); |
| + |
| + cr.defineProperty(result, 'lead', cr.PropertyKind.BOOL_ATTR); |
| + cr.defineProperty(result, 'selected', cr.PropertyKind.BOOL_ATTR); |
| + |
| + return result; |
| + } |
| + |
| + DefaultActionDialog.prototype.show = function(message, items, defaultIdx, |
|
dgozman
2012/05/22 14:55:27
idx -> index
Dmitry Zvorygin
2012/05/22 15:11:10
Done.
|
| + onOk, onCancel, onShow) { |
| + |
| + cr.ui.dialogs.BaseDialog.prototype.show.apply(this, |
| + [message, onOk, onCancel, onShow]); |
| + |
| + this.list_.startBatchUpdates(); |
| + |
| + this.dataModel_.splice(0, this.dataModel_.length); |
| + |
| + for (var i = 0; i < items.length; i++) { |
| + this.dataModel_.push(items[i]); |
| + } |
| + |
| + this.selectionModel_.selectedIndex = defaultIdx; |
| + |
| + this.list_.endBatchUpdates(); |
| + }; |
| + |
| + DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { |
| + this.hide(); |
| + if (this.onOk_) |
| + this.onOk_(this.dataModel_.item(index).task); |
| + } |
| + |
| + DefaultActionDialog.prototype.onOkClick_ = function() { |
| + this.activateItemAtIndex_(this.selectionModel_.selectedIndex); |
| + }; |
| + |
| + // Overrides BaseDialog::onContainerKeyDown_; |
|
dgozman
2012/05/22 14:55:27
Call the basic implementation somewhere?
Dmitry Zvorygin
2012/05/22 15:11:10
Not needed.
|
| + DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { |
| + // Handle Escape. |
| + if (event.keyCode == 27) { |
| + this.onCancelClick_(event); |
| + event.preventDefault(); |
| + } else if (event.keyCode == 32 || event.keyCode == 13) { |
| + this.onOkClick_(); |
| + event.preventDefault(); |
| + } |
| + }; |
| + |
| + return {DefaultActionDialog: DefaultActionDialog}; |
| +}); |