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.
7 * DefaultActionDialog contains a message, a list box, an ok button, and a
9 * This dialog should be used as action picker for file operations.
11 cr
.define('cr.filebrowser', function() {
14 * Creates dialog in DOM tree.
16 * @param {HTMLElement} parentNode Node to be parent for this dialog.
18 function DefaultActionDialog(parentNode
) {
19 cr
.ui
.dialogs
.BaseDialog
.call(this, parentNode
);
21 this.frame_
.id
= 'default-action-dialog';
23 this.list_
= new cr
.ui
.List();
24 this.list_
.id
= 'default-actions-list';
25 this.frame_
.insertBefore(this.list_
, this.text_
.nextSibling
);
27 this.selectionModel_
= this.list_
.selectionModel
=
28 new cr
.ui
.ListSingleSelectionModel();
29 this.dataModel_
= this.list_
.dataModel
= new cr
.ui
.ArrayDataModel([]);
31 // List has max-height defined at css, so that list grows automatically,
32 // but doesn't exceed predefined size.
33 this.list_
.autoExpands
= true;
34 this.list_
.activateItemAtIndex
= this.activateItemAtIndex_
.bind(this);
36 this.initialFocusElement_
= this.list_
;
40 // Binding stuff doesn't work with constructors, so we have to create
42 this.list_
.itemConstructor = function(item
) {
43 return self
.renderItem(item
);
47 DefaultActionDialog
.prototype = {
48 __proto__
: cr
.ui
.dialogs
.BaseDialog
.prototype
52 * Overrides BaseDialog::onInputFocus
54 DefaultActionDialog
.prototype.onInputFocus = function() {
59 * Renders item for list.
60 * @param {Object} item Item to render.
62 DefaultActionDialog
.prototype.renderItem = function(item
) {
63 var result
= this.document_
.createElement('li');
65 var div
= this.document_
.createElement('div');
66 div
.textContent
= item
.label
;
69 div
.setAttribute('file-type-icon', item
.iconType
);
71 div
.style
.backgroundImage
= 'url(' + item
.iconUrl
+ ')';
73 result
.appendChild(div
);
75 cr
.defineProperty(result
, 'lead', cr
.PropertyKind
.BOOL_ATTR
);
76 cr
.defineProperty(result
, 'selected', cr
.PropertyKind
.BOOL_ATTR
);
84 * @param {string} title Title in dialog caption.
85 * @param {string} message Message in dialog caption.
86 * @param {Array} items Items to render in list
87 * @param {int} defaultIndex Item to select by default.
88 * @param {Function} onOk Callback function.
89 * @param {Function} onCancel Callback function.
90 * @param {Function} onShow Callback function.
92 DefaultActionDialog
.prototype.show = function(title
, message
, items
,
93 defaultIndex
, onOk
, onCancel
, onShow
) {
95 cr
.ui
.dialogs
.BaseDialog
.prototype.showWithTitle
.apply(this,
96 [title
, message
, onOk
, onCancel
, onShow
]);
99 this.text_
.setAttribute('hidden', 'hidden');
101 this.text_
.removeAttribute('hidden');
104 this.list_
.startBatchUpdates();
106 this.dataModel_
.splice(0, this.dataModel_
.length
);
108 for (var i
= 0; i
< items
.length
; i
++) {
109 this.dataModel_
.push(items
[i
]);
112 this.selectionModel_
.selectedIndex
= defaultIndex
;
114 this.list_
.endBatchUpdates();
118 * List activation handler. Closes dialog and calls 'ok' callback.
120 * @param {int} index Activated index.
122 DefaultActionDialog
.prototype.activateItemAtIndex_ = function(index
) {
125 this.onOk_(this.dataModel_
.item(index
).task
);
129 * Closes dialog and invokes callback with currently-selected item.
131 DefaultActionDialog
.prototype.onOkClick_ = function() {
132 this.activateItemAtIndex_(this.selectionModel_
.selectedIndex
);
135 // Overrides BaseDialog::onContainerKeyDown_;
136 DefaultActionDialog
.prototype.onContainerKeyDown_ = function(event
) {
138 if (event
.keyCode
== 27) {
139 this.onCancelClick_(event
);
140 event
.preventDefault();
141 } else if (event
.keyCode
== 32 || event
.keyCode
== 13) {
143 event
.preventDefault();
147 return {DefaultActionDialog
: DefaultActionDialog
};