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