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 | |
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.
| |
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 | |
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 DefaultActionDialog.prototype.onInputFocus = function() { | |
50 this.list_.select(); | |
51 }; | |
52 | |
53 DefaultActionDialog.prototype.onKeyDown_ = function(event) { | |
54 if (event.keyCode == 13) // Enter | |
55 this.onOkClick_(event); | |
56 }; | |
57 | |
58 DefaultActionDialog.prototype.renderItem = function(item) { | |
59 var result = this.document_.createElement('li'); | |
60 | |
61 var iconNode = this.document_.createElement('img'); | |
62 iconNode.src = item.iconUrl; | |
63 result.appendChild(iconNode); | |
64 | |
65 var labelNode = this.document_.createElement('span'); | |
66 labelNode.textContent = item.label; | |
67 result.appendChild(labelNode); | |
68 | |
69 cr.defineProperty(result, 'lead', cr.PropertyKind.BOOL_ATTR); | |
70 cr.defineProperty(result, 'selected', cr.PropertyKind.BOOL_ATTR); | |
71 | |
72 return result; | |
73 } | |
74 | |
75 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.
| |
76 onOk, onCancel, onShow) { | |
77 | |
78 cr.ui.dialogs.BaseDialog.prototype.show.apply(this, | |
79 [message, onOk, onCancel, onShow]); | |
80 | |
81 this.list_.startBatchUpdates(); | |
82 | |
83 this.dataModel_.splice(0, this.dataModel_.length); | |
84 | |
85 for (var i = 0; i < items.length; i++) { | |
86 this.dataModel_.push(items[i]); | |
87 } | |
88 | |
89 this.selectionModel_.selectedIndex = defaultIdx; | |
90 | |
91 this.list_.endBatchUpdates(); | |
92 }; | |
93 | |
94 DefaultActionDialog.prototype.activateItemAtIndex_ = function(index) { | |
95 this.hide(); | |
96 if (this.onOk_) | |
97 this.onOk_(this.dataModel_.item(index).task); | |
98 } | |
99 | |
100 DefaultActionDialog.prototype.onOkClick_ = function() { | |
101 this.activateItemAtIndex_(this.selectionModel_.selectedIndex); | |
102 }; | |
103 | |
104 // 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.
| |
105 DefaultActionDialog.prototype.onContainerKeyDown_ = function(event) { | |
106 // Handle Escape. | |
107 if (event.keyCode == 27) { | |
108 this.onCancelClick_(event); | |
109 event.preventDefault(); | |
110 } else if (event.keyCode == 32 || event.keyCode == 13) { | |
111 this.onOkClick_(); | |
112 event.preventDefault(); | |
113 } | |
114 }; | |
115 | |
116 return {DefaultActionDialog: DefaultActionDialog}; | |
117 }); | |
OLD | NEW |