OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /////////////////////////////////////////////////////////////////////////////// | 5 /////////////////////////////////////////////////////////////////////////////// |
6 // LanguageOptions class: | 6 // LanguageOptions class: |
7 | 7 |
8 /** | 8 /** |
9 * Encapsulated handling of ChromeOS language options page. | 9 * Encapsulated handling of ChromeOS language options page. |
10 * @constructor | 10 * @constructor |
(...skipping 22 matching lines...) Expand all Loading... |
33 languageOptionsList.addEventListener('change', | 33 languageOptionsList.addEventListener('change', |
34 cr.bind(this.handleLanguageOptionsListChange_, this)); | 34 cr.bind(this.handleLanguageOptionsListChange_, this)); |
35 | 35 |
36 this.addEventListener('visibleChange', | 36 this.addEventListener('visibleChange', |
37 cr.bind(this.handleVisibleChange_, this)); | 37 cr.bind(this.handleVisibleChange_, this)); |
38 | 38 |
39 this.initializeInputMethodList_(); | 39 this.initializeInputMethodList_(); |
40 }, | 40 }, |
41 | 41 |
42 languageListInitalized_: false, | 42 languageListInitalized_: false, |
| 43 // The preference is a CSV string that describes preload engines |
| 44 // (i.e. active input methods). |
| 45 preloadEnginesPref: 'settings.language.preload_engines', |
| 46 preloadEngines_: [], |
43 | 47 |
44 /** | 48 /** |
45 * Initializes the input method list. | 49 * Initializes the input method list. |
46 */ | 50 */ |
47 initializeInputMethodList_: function() { | 51 initializeInputMethodList_: function() { |
48 var inputMethodList = $('language-options-input-method-list'); | 52 var inputMethodList = $('language-options-input-method-list'); |
49 var inputMethodListData = templateData.inputMethodList; | 53 var inputMethodListData = templateData.inputMethodList; |
50 | 54 |
51 // Add all input methods, but make all of them invisible here. We'll | 55 // Add all input methods, but make all of them invisible here. We'll |
52 // change the visibility in handleLanguageOptionsListChange_() based | 56 // change the visibility in handleLanguageOptionsListChange_() based |
53 // on the selected language. Note that we only have less than 100 | 57 // on the selected language. Note that we only have less than 100 |
54 // input methods, so creating DOM nodes at once here should be ok. | 58 // input methods, so creating DOM nodes at once here should be ok. |
55 for (var i = 0; i < inputMethodListData.length; i++) { | 59 for (var i = 0; i < inputMethodListData.length; i++) { |
56 var inputMethod = inputMethodListData[i]; | 60 var inputMethod = inputMethodListData[i]; |
57 var input = document.createElement('input'); | 61 var input = document.createElement('input'); |
58 input.type = 'checkbox'; | 62 input.type = 'checkbox'; |
59 input.inputMethodId = inputMethod.id; | 63 input.inputMethodId = inputMethod.id; |
60 | 64 // Listen to user clicks. |
| 65 input.addEventListener('click', |
| 66 cr.bind(this.handleCheckboxClick_, this)); |
61 var label = document.createElement('label'); | 67 var label = document.createElement('label'); |
62 label.appendChild(input); | 68 label.appendChild(input); |
63 label.appendChild(document.createTextNode(inputMethod.displayName)); | 69 label.appendChild(document.createTextNode(inputMethod.displayName)); |
64 label.style.display = 'none'; | 70 label.style.display = 'none'; |
65 label.languageCode = inputMethod.languageCode; | 71 label.languageCode = inputMethod.languageCode; |
66 | 72 |
67 inputMethodList.appendChild(label); | 73 inputMethodList.appendChild(label); |
68 } | 74 } |
| 75 // Listen to pref change once the input method list is initialized. |
| 76 Preferences.getInstance().addEventListener(this.preloadEnginesPref, |
| 77 cr.bind(this.handlePreloadEnginesPrefChange_, this)); |
69 }, | 78 }, |
70 | 79 |
71 /** | 80 /** |
72 * Handler for OptionsPage's visible property change event. | 81 * Handler for OptionsPage's visible property change event. |
73 * @param {Event} e Property change event. | 82 * @param {Event} e Property change event. |
74 * @private | 83 * @private |
75 */ | 84 */ |
76 handleVisibleChange_ : function(e) { | 85 handleVisibleChange_ : function(e) { |
77 if (!this.languageListInitalized_ && this.visible) { | 86 if (!this.languageListInitalized_ && this.visible) { |
78 this.languageListInitalized_ = true; | 87 this.languageListInitalized_ = true; |
(...skipping 26 matching lines...) Expand all Loading... |
105 // matches |languageCode| will become visible. | 114 // matches |languageCode| will become visible. |
106 var inputMethodList = $('language-options-input-method-list'); | 115 var inputMethodList = $('language-options-input-method-list'); |
107 var labels = inputMethodList.querySelectorAll('label'); | 116 var labels = inputMethodList.querySelectorAll('label'); |
108 for (var i = 0; i < labels.length; i++) { | 117 for (var i = 0; i < labels.length; i++) { |
109 if (labels[i].languageCode == languageCode) { | 118 if (labels[i].languageCode == languageCode) { |
110 labels[i].style.display = 'block'; | 119 labels[i].style.display = 'block'; |
111 } else { | 120 } else { |
112 labels[i].style.display = 'none'; | 121 labels[i].style.display = 'none'; |
113 } | 122 } |
114 } | 123 } |
| 124 }, |
| 125 |
| 126 /** |
| 127 * Handles preloadEnginesPref change. |
| 128 * @param {Event} e Change event. |
| 129 * @private |
| 130 */ |
| 131 handlePreloadEnginesPrefChange_: function(e) { |
| 132 this.preloadEngines_ = this.filterBadPreloadEngines_(e.value.split(',')); |
| 133 this.updateCheckboxesFromPreloadEngines_(); |
| 134 }, |
| 135 |
| 136 /** |
| 137 * Handles input method checkbox's click event. |
| 138 * @param {Event} e Click event. |
| 139 * @private |
| 140 */ |
| 141 handleCheckboxClick_ : function(e) { |
| 142 this.updatePreloadEnginesFromCheckboxes_(); |
| 143 Preferences.setStringPref(this.preloadEnginesPref, |
| 144 this.preloadEngines_.join(',')); |
| 145 }, |
| 146 |
| 147 /** |
| 148 * Updates the checkboxes in the input method list from the preload |
| 149 * engines preference. |
| 150 * @private |
| 151 */ |
| 152 updateCheckboxesFromPreloadEngines_: function() { |
| 153 // Convert the list into a dictonary for simpler lookup. |
| 154 var dictionary = {}; |
| 155 for (var i = 0; i < this.preloadEngines_.length; i++) { |
| 156 dictionary[this.preloadEngines_[i]] = true; |
| 157 } |
| 158 |
| 159 var inputMethodList = $('language-options-input-method-list'); |
| 160 var checkboxes = inputMethodList.querySelectorAll('input'); |
| 161 for (var i = 0; i < checkboxes.length; i++) { |
| 162 checkboxes[i].checked = (checkboxes[i].inputMethodId in dictionary); |
| 163 } |
| 164 }, |
| 165 |
| 166 /** |
| 167 * Updates the preload engines preference from the checkboxes in the |
| 168 * input method list. |
| 169 * @private |
| 170 */ |
| 171 updatePreloadEnginesFromCheckboxes_: function() { |
| 172 this.preloadEngines_ = []; |
| 173 var inputMethodList = $('language-options-input-method-list'); |
| 174 var checkboxes = inputMethodList.querySelectorAll('input'); |
| 175 for (var i = 0; i < checkboxes.length; i++) { |
| 176 if (checkboxes[i].checked) { |
| 177 this.preloadEngines_.push(checkboxes[i].inputMethodId); |
| 178 } |
| 179 } |
| 180 }, |
| 181 |
| 182 /** |
| 183 * Filters bad preload engines in case bad preload engines are |
| 184 * stored in the preference. |
| 185 * @param {Array} preloadEngines List of preload engines. |
| 186 * @private |
| 187 */ |
| 188 filterBadPreloadEngines_: function(preloadEngines) { |
| 189 // Convert the list into a dictonary for simpler lookup. |
| 190 var dictionary = {}; |
| 191 for (var i = 0; i < templateData.inputMethodList.length; i++) { |
| 192 dictionary[templateData.inputMethodList[i].id] = true; |
| 193 } |
| 194 |
| 195 var filteredPreloadEngines = []; |
| 196 for (var i = 0; i < preloadEngines.length; i++) { |
| 197 // Check if the preload engine is present in the |
| 198 // dictionary. Otherwise, skip it. |
| 199 if (preloadEngines[i] in dictionary) { |
| 200 filteredPreloadEngines.push(preloadEngines[i]); |
| 201 } |
| 202 } |
| 203 return filteredPreloadEngines; |
115 } | 204 } |
116 }; | 205 }; |
OLD | NEW |