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.
9 * DefaultActionDialog contains a message, a list box, an ok button, and a
11 * This dialog should be used as action picker for file operations.
13 cr.define('cr.filebrowser', function() {
16 * Creates dialog in DOM tree.
18 * @param {HTMLElement} parentNode Node to be parent for this dialog.
20 * @extends {FileManagerDialogBase}
22 function DefaultActionDialog(parentNode) {
23 FileManagerDialogBase.call(this, parentNode);
25 this.frame_.id = 'default-action-dialog';
27 this.list_ = new cr.ui.List();
28 this.list_.id = 'default-actions-list';
29 this.frame_.insertBefore(this.list_, this.text_.nextSibling);
31 this.selectionModel_ = this.list_.selectionModel =
32 new cr.ui.ListSingleSelectionModel();
33 this.dataModel_ = this.list_.dataModel = new cr.ui.ArrayDataModel([]);
35 // List has max-height defined at css, so that list grows automatically,
36 // but doesn't exceed predefined size.
37 this.list_.autoExpands = true;
38 this.list_.activateItemAtIndex = this.activateItemAtIndex_.bind(this);
40 this.initialFocusElement_ = this.list_;
44 // Binding stuff doesn't work with constructors, so we have to create
46 this.list_.itemConstructor = function(item) {
47 return self.renderItem(item);
51 DefaultActionDialog.prototype = {
52 __proto__: FileManagerDialogBase.prototype
58 DefaultActionDialog.prototype.onInputFocus = function() {
63 * Renders item for list.
64 * @param {Object} item Item to render.
66 DefaultActionDialog.prototype.renderItem = function(item) {
67 var result = this.document_.createElement('li');
69 var div = this.document_.createElement('div');
70 div.textContent = item.label;
73 div.setAttribute('file-type-icon', item.iconType);
74 } else if (item.iconUrl) {
75 div.style.backgroundImage = 'url(' + item.iconUrl + ')';
79 div.classList.add(item.class);
81 result.appendChild(div);
83 cr.defineProperty(result, 'lead', cr.PropertyKind.BOOL_ATTR);
84 cr.defineProperty(result, 'selected', cr.PropertyKind.BOOL_ATTR);
92 * @param {string} title Title in dialog caption.
93 * @param {string} message Message in dialog caption.
94 * @param {Array.<Object>} items Items to render in the list.
95 * @param {number} defaultIndex Item to select by default.
96 * @param {function(Object=)} opt_onOk OK callback with the selected item.
97 * @param {function()=} opt_onCancel Cancel callback.
99 DefaultActionDialog.prototype.show = function(title, message, items,
100 defaultIndex, opt_onOk, opt_onCancel) {
102 var show = FileManagerDialogBase.prototype.showOkCancelDialog.call(
103 this, title, message, opt_onOk, opt_onCancel);
106 console.error('DefaultActionDialog can\'t be shown.');
111 this.text_.setAttribute('hidden', 'hidden');
113 this.text_.removeAttribute('hidden');
116 this.list_.startBatchUpdates();
117 this.dataModel_.splice(0, this.dataModel_.length);
118 for (var i = 0; i < items.length; i++) {
119 this.dataModel_.push(items[i]);
121 this.selectionModel_.selectedIndex = defaultIndex;
122 this.list_.endBatchUpdates();
126 * List activation handler. Closes dialog and calls 'ok' callback.
127 * @param {number} index Activated index.
129 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) {
131 this.onOk_(this.dataModel_.item(index));
135 * Closes dialog and invokes callback with currently-selected item.
138 DefaultActionDialog.prototype.onOkClick_ = function() {
139 this.activateItemAtIndex_(this.selectionModel_.selectedIndex);
145 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) {
147 if (event.keyCode == 27) {
148 this.onCancelClick_(event);
149 event.preventDefault();
150 } else if (event.keyCode == 32 || event.keyCode == 13) {
152 event.preventDefault();
156 return {DefaultActionDialog: DefaultActionDialog};