Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: chrome/browser/resources/settings/languages_page/manage_input_methods_page.js

Issue 2252323002: MD Settings: reduce complexity and overhead of Languages singleton (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@RemoveLanguageInputMethods
Patch Set: Wrong whitespace to improve diff Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 * @fileoverview 'settings-manage-input-methods-page' is a sub-page for enabling 6 * @fileoverview 'settings-manage-input-methods-page' is a sub-page for enabling
7 * and disabling input methods. Input methods are grouped by base languages to 7 * and disabling input methods. Input methods are grouped by base languages to
8 * avoid showing duplicate or ambiguous input methods. 8 * avoid showing duplicate or ambiguous input methods.
9 * 9 *
10 * @group Chrome Settings Elements 10 * @group Chrome Settings Elements
(...skipping 21 matching lines...) Expand all
32 value: function() { return []; }, 32 value: function() { return []; },
33 }, 33 },
34 }, 34 },
35 35
36 observers: [ 36 observers: [
37 'availableInputMethodsChanged_(languages.enabled.*,' + 37 'availableInputMethodsChanged_(languages.enabled.*,' +
38 'languages.inputMethods.supported.*)', 38 'languages.inputMethods.supported.*)',
39 'enabledInputMethodsChanged_(languages.inputMethods.enabled.*)', 39 'enabledInputMethodsChanged_(languages.inputMethods.enabled.*)',
40 ], 40 ],
41 41
42 /** @override */
43 created: function() {
44 this.languageHelper_ = LanguageHelperImpl.getInstance();
45 },
46
47 /** @private */ 42 /** @private */
48 availableInputMethodsChanged_: function() { 43 availableInputMethodsChanged_: function() {
49 this.populateLanguageList_(); 44 this.populateLanguageList_();
50 }, 45 },
51 46
52 /** @private */ 47 /** @private */
53 enabledInputMethodsChanged_: function() { 48 enabledInputMethodsChanged_: function() {
54 this.populateLanguageList_(); 49 this.populateLanguageList_();
55 }, 50 },
56 51
57 /** 52 /**
58 * Handler for an input method checkbox. 53 * Handler for an input method checkbox.
59 * @param {!{model: !{item: chrome.languageSettingsPrivate.InputMethod}, 54 * @param {!{model: !{item: chrome.languageSettingsPrivate.InputMethod},
60 * target: !PaperCheckboxElement}} e 55 * target: !PaperCheckboxElement}} e
61 * @private 56 * @private
62 */ 57 */
63 onCheckboxChange_: function(e) { 58 onCheckboxChange_: function(e) {
64 // TODO(michaelpg): Show confirmation dialog for 3rd-party IMEs. 59 // TODO(michaelpg): Show confirmation dialog for 3rd-party IMEs.
65 var id = e.model.item.id; 60 var id = e.model.item.id;
66 if (e.target.checked) 61 if (e.target.checked)
67 this.languageHelper_.addInputMethod(id); 62 settings.LanguageHelperImpl.getInstance().addInputMethod(id);
68 else 63 else
69 this.languageHelper_.removeInputMethod(id); 64 settings.LanguageHelperImpl.getInstance().removeInputMethod(id);
70 }, 65 },
71 66
72 /** 67 /**
73 * Returns true if the input method can be removed. 68 * Returns true if the input method can be removed.
74 * @param {!chrome.languageSettingsPrivate.InputMethod} targetInputMethod 69 * @param {!chrome.languageSettingsPrivate.InputMethod} targetInputMethod
75 * @param {!Object} change Polymer change object (provided in the HTML so this 70 * @param {!Object} change Polymer change object (provided in the HTML so this
76 * gets called whenever languages.inputMethods.enabled.* changes). 71 * gets called whenever languages.inputMethods.enabled.* changes).
77 * @return {boolean} 72 * @return {boolean}
78 * @private 73 * @private
79 */ 74 */
80 enableInputMethodCheckbox_: function(targetInputMethod, change) { 75 enableInputMethodCheckbox_: function(targetInputMethod, change) {
81 if (!targetInputMethod.enabled) 76 if (!targetInputMethod.enabled)
82 return true; 77 return true;
83 78
79 var languageHelper = settings.LanguageHelperImpl.getInstance();
80
84 // Third-party IMEs can always be removed. 81 // Third-party IMEs can always be removed.
85 if (!this.languageHelper_.isComponentIme(targetInputMethod)) 82 if (!languageHelper.isComponentIme(targetInputMethod))
86 return true; 83 return true;
87 84
88 // Can be removed as long as there is another component IME. 85 // Can be removed as long as there is another component IME.
89 return this.languages.inputMethods.enabled.some(function(inputMethod) { 86 return this.languages.inputMethods.enabled.some(function(inputMethod) {
90 return inputMethod != targetInputMethod && 87 return inputMethod != targetInputMethod &&
91 this.languageHelper_.isComponentIme(inputMethod); 88 languageHelper.isComponentIme(inputMethod);
92 }, this); 89 }, this);
93 }, 90 },
94 91
95 /** 92 /**
96 * Creates the list of languages and their input methods as the data source 93 * Creates the list of languages and their input methods as the data source
97 * for the view. 94 * for the view.
98 * @private 95 * @private
99 */ 96 */
100 populateLanguageList_: function() { 97 populateLanguageList_: function() {
101 var languageList = []; 98 var languageList = [];
99 var languageHelper = settings.LanguageHelperImpl.getInstance();
102 100
103 // Languages that have already been listed further up. 101 // Languages that have already been listed further up.
104 var /** !Set<string> */ usedLanguages = new Set(); 102 var /** !Set<string> */ usedLanguages = new Set();
105 103
106 // Add languages in preference order. However, if there are multiple 104 // Add languages in preference order. However, if there are multiple
107 // enabled variants of the same base language, group them all as the base 105 // enabled variants of the same base language, group them all as the base
108 // language instead of showing each variant individually. This prevents us 106 // language instead of showing each variant individually. This prevents us
109 // from displaying duplicate input methods under different variants. 107 // from displaying duplicate input methods under different variants.
110 for (var i = 0; i < this.languages.enabled.length; i++) { 108 for (var i = 0; i < this.languages.enabled.length; i++) {
111 var languageState = this.languages.enabled[i]; 109 var languageState = this.languages.enabled[i];
112 // Skip the language if we have already included it or its base language. 110 // Skip the language if we have already included it or its base language.
113 if (usedLanguages.has(languageState.language.code)) 111 if (usedLanguages.has(languageState.language.code))
114 continue; 112 continue;
115 var baseLanguageCode = this.languageHelper_.getLanguageCodeWithoutRegion( 113 var baseLanguageCode = languageHelper.getLanguageCodeWithoutRegion(
116 languageState.language.code); 114 languageState.language.code);
117 if (usedLanguages.has(baseLanguageCode)) 115 if (usedLanguages.has(baseLanguageCode))
118 continue; 116 continue;
119 117
120 // Find the other languages further down in the preferred languages list 118 // Find the other languages further down in the preferred languages list
121 // which also use this language's base language code. 119 // which also use this language's base language code.
122 var languageFamilyCodes = [languageState.language.code]; 120 var languageFamilyCodes = [languageState.language.code];
123 for (var j = i + 1; j < this.languages.enabled.length; j++) { 121 for (var j = i + 1; j < this.languages.enabled.length; j++) {
124 var otherCode = this.languages.enabled[j].language.code; 122 var otherCode = this.languages.enabled[j].language.code;
125 if (this.languageHelper_.getLanguageCodeWithoutRegion(otherCode) == 123 if (languageHelper.getLanguageCodeWithoutRegion(otherCode) ==
126 baseLanguageCode) { 124 baseLanguageCode) {
127 languageFamilyCodes.push(this.languages.enabled[j].language.code); 125 languageFamilyCodes.push(this.languages.enabled[j].language.code);
128 } 126 }
129 } 127 }
130 128
131 var combinedInputMethods = 129 var combinedInputMethods =
132 this.getInputMethodsForLanguages(languageFamilyCodes); 130 this.getInputMethodsForLanguages(languageFamilyCodes);
133 131
134 // Skip the language if it has no new input methods. 132 // Skip the language if it has no new input methods.
135 if (!combinedInputMethods.length) 133 if (!combinedInputMethods.length)
136 continue; 134 continue;
137 135
138 // Add the language or base language. 136 // Add the language or base language.
139 var displayLanguage = languageState.language; 137 var displayLanguage = languageState.language;
140 if (languageFamilyCodes.length > 1) { 138 if (languageFamilyCodes.length > 1) {
141 var baseLanguage = this.languageHelper_.getLanguage(baseLanguageCode); 139 var baseLanguage = languageHelper.getLanguage(baseLanguageCode);
142 if (baseLanguage) 140 if (baseLanguage)
143 displayLanguage = baseLanguage; 141 displayLanguage = baseLanguage;
144 } 142 }
145 languageList.push({ 143 languageList.push({
146 language: displayLanguage, 144 language: displayLanguage,
147 inputMethods: combinedInputMethods, 145 inputMethods: combinedInputMethods,
148 }); 146 });
149 for (var languageCode of languageFamilyCodes) 147 for (var languageCode of languageFamilyCodes)
150 usedLanguages.add(languageCode); 148 usedLanguages.add(languageCode);
151 } 149 }
152 150
153 this.languageList_ = languageList; 151 this.languageList_ = languageList;
154 this.notifyInputMethodsChanged_(); 152 this.notifyInputMethodsChanged_();
155 }, 153 },
156 154
157 /** 155 /**
158 * Returns the input methods that support any of the given languages. 156 * Returns the input methods that support any of the given languages.
159 * @param {!Array<string>} languageCodes 157 * @param {!Array<string>} languageCodes
160 * @return {!Array<!chrome.languageSettingsPrivate.InputMethod>} 158 * @return {!Array<!chrome.languageSettingsPrivate.InputMethod>}
161 * @private 159 * @private
162 */ 160 */
163 getInputMethodsForLanguages: function(languageCodes) { 161 getInputMethodsForLanguages: function(languageCodes) {
164 // Input methods that have already been listed for this language. 162 // Input methods that have already been listed for this language.
165 var /** !Set<string> */ usedInputMethods = new Set(); 163 var /** !Set<string> */ usedInputMethods = new Set();
166 /** @type {!Array<chrome.languageSettingsPrivate.InputMethod>} */ 164 /** @type {!Array<chrome.languageSettingsPrivate.InputMethod>} */
167 var combinedInputMethods = []; 165 var combinedInputMethods = [];
168 for (var languageCode of languageCodes) { 166 for (var languageCode of languageCodes) {
169 var inputMethods = this.languageHelper_.getInputMethodsForLanguage( 167 var inputMethods =
170 languageCode); 168 settings.LanguageHelperImpl.getInstance().getInputMethodsForLanguage(
169 languageCode);
171 // Get the language's unused input methods and mark them as used. 170 // Get the language's unused input methods and mark them as used.
172 var newInputMethods = inputMethods.filter(function(inputMethod) { 171 var newInputMethods = inputMethods.filter(function(inputMethod) {
173 if (usedInputMethods.has(inputMethod.id)) 172 if (usedInputMethods.has(inputMethod.id))
174 return false; 173 return false;
175 usedInputMethods.add(inputMethod.id); 174 usedInputMethods.add(inputMethod.id);
176 return true; 175 return true;
177 }); 176 });
178 [].push.apply(combinedInputMethods, newInputMethods); 177 [].push.apply(combinedInputMethods, newInputMethods);
179 } 178 }
180 return combinedInputMethods; 179 return combinedInputMethods;
181 }, 180 },
182 181
183 // TODO(Polymer/polymer#3603): We have to notify Polymer of properties that 182 // TODO(Polymer/polymer#3603): We have to notify Polymer of properties that
184 // may have changed on nested objects, even when the outer property itself 183 // may have changed on nested objects, even when the outer property itself
185 // is set to a new array. 184 // is set to a new array.
186 // TODO(michaelpg): Test this behavior. 185 // TODO(michaelpg): Test this behavior.
187 /** @private */ 186 /** @private */
188 notifyInputMethodsChanged_: function() { 187 notifyInputMethodsChanged_: function() {
189 for (var i = 0; i < this.languageList_.length; i++) { 188 for (var i = 0; i < this.languageList_.length; i++) {
190 for (var j = 0; j < this.languageList_[i].inputMethods.length; j++) { 189 for (var j = 0; j < this.languageList_[i].inputMethods.length; j++) {
191 this.notifyPath( 190 this.notifyPath(
192 'languageList_.' + i + '.inputMethods.' + j + '.enabled', 191 'languageList_.' + i + '.inputMethods.' + j + '.enabled',
193 this.languageList_[i].inputMethods[j].enabled); 192 this.languageList_[i].inputMethods[j].enabled);
194 } 193 }
195 } 194 }
196 }, 195 },
197 }); 196 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698