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 cr.define('options', function() { | |
6 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; | |
7 /** @const */ var List = cr.ui.List; | |
8 /** @const */ var ListItem = cr.ui.ListItem; | |
9 /** @const */ var VirtualKeyboardOptions = options.VirtualKeyboardOptions; | |
10 | |
11 /** | |
12 * Creates a virtual keyboard list item. | |
13 * | |
14 * Accepts values in the form | |
15 * { layout: 'us(dvorak)', | |
16 * layoutName: 'US Dvorak layout', | |
17 * preferredKeyboard: 'http://...', [optional] | |
18 * supportedKeyboards: [ | |
19 * { name: 'Simple Virtual Keyboard', | |
20 * isSystem: true, | |
21 * url: 'http://...' }, | |
22 * { name: '3rd party Virtual Keyboard', | |
23 * isSystem: false, | |
24 * url: 'http://...' }, | |
25 * ..., | |
26 * ] | |
27 * } | |
28 * @param {Object} entry A dictionary describing the virtual keyboards for a | |
29 * given layout. | |
30 * @constructor | |
31 * @extends {cr.ui.ListItem} | |
32 */ | |
33 function VirtualKeyboardListItem(entry) { | |
34 var el = cr.doc.createElement('div'); | |
35 el.dataItem = entry; | |
36 el.__proto__ = VirtualKeyboardListItem.prototype; | |
37 el.decorate(); | |
38 return el; | |
39 } | |
40 | |
41 VirtualKeyboardListItem.prototype = { | |
42 __proto__: ListItem.prototype, | |
43 | |
44 buildWidget_: function(data, delegate) { | |
45 // Layout name. | |
46 var layoutNameElement = document.createElement('div'); | |
47 layoutNameElement.textContent = data.layoutName; | |
48 layoutNameElement.className = 'virtual-keyboard-layout-column'; | |
49 this.appendChild(layoutNameElement); | |
50 | |
51 // Virtual keyboard selection. | |
52 var keyboardElement = document.createElement('div'); | |
53 var selectElement = document.createElement('select'); | |
54 var defaultOptionElement = document.createElement('option'); | |
55 defaultOptionElement.selected = (data.preferredKeyboard == null); | |
56 defaultOptionElement.textContent = | |
57 loadTimeData.getString('defaultVirtualKeyboard'); | |
58 defaultOptionElement.value = -1; | |
59 selectElement.appendChild(defaultOptionElement); | |
60 | |
61 for (var i = 0; i < data.supportedKeyboards.length; ++i) { | |
62 var optionElement = document.createElement('option'); | |
63 optionElement.selected = | |
64 (data.preferredKeyboard != null && | |
65 data.preferredKeyboard == data.supportedKeyboards[i].url); | |
66 optionElement.textContent = data.supportedKeyboards[i].name; | |
67 optionElement.value = i; | |
68 selectElement.appendChild(optionElement); | |
69 } | |
70 | |
71 selectElement.addEventListener('change', function(e) { | |
72 var index = e.target.value; | |
73 if (index == -1) { | |
74 // The 'Default' menu item is selected. Delete the preference. | |
75 delegate.clearPreference(data.layout); | |
76 } else { | |
77 delegate.setPreference( | |
78 data.layout, data.supportedKeyboards[index].url); | |
79 } | |
80 }); | |
81 | |
82 keyboardElement.appendChild(selectElement); | |
83 keyboardElement.className = 'virtual-keyboard-keyboard-column'; | |
84 this.appendChild(keyboardElement); | |
85 }, | |
86 | |
87 /** @inheritDoc */ | |
88 decorate: function() { | |
89 ListItem.prototype.decorate.call(this); | |
90 | |
91 var delegate = { | |
92 clearPreference: function(layout) { | |
93 // Call a C++ function in chrome/browser/ui/webui/options/chromeos/. | |
94 chrome.send('clearVirtualKeyboardPreference', [layout]); | |
95 }, | |
96 setPreference: function(layout, url) { | |
97 chrome.send('setVirtualKeyboardPreference', [layout, url]); | |
98 }, | |
99 }; | |
100 | |
101 this.buildWidget_(this.dataItem, delegate); | |
102 }, | |
103 }; | |
104 | |
105 /** | |
106 * Create a new virtual keyboard list. | |
107 * @constructor | |
108 * @extends {cr.ui.List} | |
109 */ | |
110 var VirtualKeyboardsList = cr.ui.define('list'); | |
111 | |
112 VirtualKeyboardsList.prototype = { | |
113 __proto__: List.prototype, | |
114 | |
115 /** @inheritDoc */ | |
116 createItem: function(entry) { | |
117 return new VirtualKeyboardListItem(entry); | |
118 }, | |
119 | |
120 /** | |
121 * The length of the list. | |
122 */ | |
123 get length() { | |
124 return this.dataModel.length; | |
125 }, | |
126 | |
127 /** | |
128 * Set the virtual keyboards displayed by this list. | |
129 * See VirtualKeyboardListItem for an example of the format the list should | |
130 * take. | |
131 * | |
132 * @param {Object} list A list of layouts with their registered virtual | |
133 * keyboards. | |
134 */ | |
135 setVirtualKeyboardList: function(list) { | |
136 this.dataModel = new ArrayDataModel(list); | |
137 }, | |
138 }; | |
139 | |
140 return { | |
141 VirtualKeyboardListItem: VirtualKeyboardListItem, | |
142 VirtualKeyboardsList: VirtualKeyboardsList, | |
143 }; | |
144 }); | |
OLD | NEW |