michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 1 | // Copyright 2015 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 | /** |
tommycli | 5bcd3c1b | 2015-10-07 23:25:54 | [diff] [blame] | 6 | * @fileoverview 'settings-languages-page' is the settings page |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 7 | * for language and input method settings. |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 8 | */ |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 9 | cr.exportPath('settings'); |
| 10 | |
| 11 | /** |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 12 | * @type {number} Millisecond delay that can be used when closing an action |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 13 | * menu to keep it briefly on-screen. |
| 14 | */ |
| 15 | settings.kMenuCloseDelay = 100; |
| 16 | |
Renjie Liu | b350ff32 | 2017-09-05 05:02:36 | [diff] [blame] | 17 | /** |
| 18 | * Name of the language setting is shown uma histogram. |
| 19 | * @type {string} |
| 20 | */ |
| 21 | const LANGUAGE_SETTING_IS_SHOWN_UMA_NAME = 'Translate.LanguageSettingsIsShown'; |
| 22 | |
michaelpg | 0f479fec | 2015-09-25 04:03:49 | [diff] [blame] | 23 | (function() { |
| 24 | 'use strict'; |
| 25 | |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 26 | Polymer({ |
tommycli | 5bcd3c1b | 2015-10-07 23:25:54 | [diff] [blame] | 27 | is: 'settings-languages-page', |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 28 | |
| 29 | properties: { |
| 30 | /** |
| 31 | * Preferences state. |
| 32 | */ |
| 33 | prefs: { |
| 34 | type: Object, |
| 35 | notify: true, |
| 36 | }, |
| 37 | |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 38 | /** |
michaelpg | 0f479fec | 2015-09-25 04:03:49 | [diff] [blame] | 39 | * Read-only reference to the languages model provided by the |
tommycli | 5bcd3c1b | 2015-10-07 23:25:54 | [diff] [blame] | 40 | * 'settings-languages' instance. |
michaelpg | abbd180d | 2016-04-23 02:42:35 | [diff] [blame] | 41 | * @type {!LanguagesModel|undefined} |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 42 | */ |
michaelpg | 0f479fec | 2015-09-25 04:03:49 | [diff] [blame] | 43 | languages: { |
| 44 | type: Object, |
| 45 | notify: true, |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 46 | }, |
dschuyler | 825c18d4 | 2016-02-11 02:51:05 | [diff] [blame] | 47 | |
michaelpg | 75252ef7 | 2016-08-19 01:47:39 | [diff] [blame] | 48 | /** @type {!LanguageHelper} */ |
| 49 | languageHelper: Object, |
| 50 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 51 | // <if expr="not is_macosx"> |
dschuyler | 825c18d4 | 2016-02-11 02:51:05 | [diff] [blame] | 52 | /** @private */ |
michaelpg | 44d9dc8 | 2016-12-08 02:04:00 | [diff] [blame] | 53 | spellCheckSecondaryText_: { |
dschuyler | 825c18d4 | 2016-02-11 02:51:05 | [diff] [blame] | 54 | type: String, |
michaelpg | 44d9dc8 | 2016-12-08 02:04:00 | [diff] [blame] | 55 | value: '', |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 56 | computed: 'getSpellCheckSecondaryText_(languages.enabled.*, ' + |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 57 | 'languages.forcedSpellCheckLanguages.*, ' + |
| 58 | 'prefs.browser.enable_spellchecking.*)', |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 59 | }, |
| 60 | |
| 61 | /** @private */ |
| 62 | spellCheckLanguages_: { |
| 63 | type: Array, |
| 64 | value: function() { |
| 65 | return []; |
| 66 | }, |
dschuyler | 825c18d4 | 2016-02-11 02:51:05 | [diff] [blame] | 67 | }, |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 68 | |
| 69 | /** @private */ |
| 70 | spellCheckDisabled_: { |
| 71 | type: Boolean, |
| 72 | value: false, |
| 73 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 74 | // </if> |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * The language to display the details for. |
michaelpg | abbd180d | 2016-04-23 02:42:35 | [diff] [blame] | 78 | * @type {!LanguageState|undefined} |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 79 | * @private |
| 80 | */ |
| 81 | detailLanguage_: Object, |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 82 | |
Renjie Liu | b350ff32 | 2017-09-05 05:02:36 | [diff] [blame] | 83 | /** |
| 84 | * Whether the language settings list is opened. |
| 85 | * @private |
| 86 | */ |
| 87 | languagesOpened_: { |
| 88 | type: Boolean, |
| 89 | observer: 'onLanguagesOpenedChanged_', |
| 90 | }, |
| 91 | |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 92 | /** @private */ |
| 93 | showAddLanguagesDialog_: Boolean, |
dpapad | a4375e0 | 2017-04-11 04:07:49 | [diff] [blame] | 94 | |
| 95 | /** @private {!Map<string, string>} */ |
| 96 | focusConfig_: { |
| 97 | type: Object, |
| 98 | value: function() { |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 99 | const map = new Map(); |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 100 | // <if expr="not is_macosx"> |
scottchen | c31781b | 2017-07-10 19:52:42 | [diff] [blame] | 101 | if (settings.routes.EDIT_DICTIONARY) { |
| 102 | map.set( |
| 103 | settings.routes.EDIT_DICTIONARY.path, |
dpapad | fc810ef | 2018-07-09 23:44:15 | [diff] [blame] | 104 | '#spellCheckCollapse .subpage-arrow button'); |
scottchen | c31781b | 2017-07-10 19:52:42 | [diff] [blame] | 105 | } |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 106 | // </if> |
| 107 | // <if expr="chromeos"> |
scottchen | c31781b | 2017-07-10 19:52:42 | [diff] [blame] | 108 | if (settings.routes.INPUT_METHODS) { |
| 109 | map.set( |
| 110 | settings.routes.INPUT_METHODS.path, |
dpapad | fc810ef | 2018-07-09 23:44:15 | [diff] [blame] | 111 | '#inputMethodsCollapse .subpage-arrow button'); |
scottchen | c31781b | 2017-07-10 19:52:42 | [diff] [blame] | 112 | } |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 113 | // </if> |
dpapad | a4375e0 | 2017-04-11 04:07:49 | [diff] [blame] | 114 | return map; |
| 115 | }, |
| 116 | }, |
michaelpg | 45acdc7a | 2016-04-17 01:27:11 | [diff] [blame] | 117 | }, |
michaelpg | a49c98b | 2015-12-04 20:27:25 | [diff] [blame] | 118 | |
Marc-Antoine Courteau | 75c1ff3 | 2018-02-02 02:22:43 | [diff] [blame] | 119 | // <if expr="not is_macosx"> |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 120 | observers: [ |
| 121 | 'updateSpellcheckLanguages_(languages.enabled.*, ' + |
| 122 | 'languages.forcedSpellCheckLanguages.*)', |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 123 | 'updateSpellcheckEnabled_(prefs.browser.enable_spellchecking.*)', |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 124 | ], |
Esmael El-Moslimany | 3f225c8 | 2018-03-29 23:27:32 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Checks if there are any errors downloading the spell check dictionary. This |
| 128 | * is used for showing/hiding error messages, spell check toggle and retry. |
| 129 | * button. |
| 130 | * @param {number} downloadDictionaryFailureCount |
| 131 | * @param {number} threshold |
| 132 | * @return {boolean} |
| 133 | * @private |
| 134 | */ |
| 135 | errorsGreaterThan_: function(downloadDictionaryFailureCount, threshold) { |
| 136 | return downloadDictionaryFailureCount > threshold; |
| 137 | }, |
Marc-Antoine Courteau | 75c1ff3 | 2018-02-02 02:22:43 | [diff] [blame] | 138 | // </if> |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 139 | |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 140 | /** |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 141 | * Stamps and opens the Add Languages dialog, registering a listener to |
| 142 | * disable the dialog's dom-if again on close. |
hcarmona | 6083648 | 2016-12-08 22:42:07 | [diff] [blame] | 143 | * @param {!Event} e |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 144 | * @private |
| 145 | */ |
hcarmona | 6083648 | 2016-12-08 22:42:07 | [diff] [blame] | 146 | onAddLanguagesTap_: function(e) { |
| 147 | e.preventDefault(); |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 148 | this.showAddLanguagesDialog_ = true; |
dpapad | 2bd0fd44 | 2018-06-15 17:09:02 | [diff] [blame] | 149 | }, |
| 150 | |
| 151 | /** @private */ |
| 152 | onAddLanguagesDialogClose_: function() { |
| 153 | this.showAddLanguagesDialog_ = false; |
| 154 | cr.ui.focusWithoutInk(assert(this.$.addLanguages)); |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 155 | }, |
| 156 | |
| 157 | /** |
michaelpg | 4fed8870 | 2017-02-02 20:57:19 | [diff] [blame] | 158 | * Used to determine which "Move" buttons to show for ordering enabled |
| 159 | * languages. |
| 160 | * @param {number} n |
michaelpg | 4fed8870 | 2017-02-02 20:57:19 | [diff] [blame] | 161 | * @return {boolean} True if |language| is at the |n|th index in the list of |
| 162 | * enabled languages. |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 163 | * @private |
| 164 | */ |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 165 | isNthLanguage_: function(n) { |
| 166 | if (this.languages == undefined || this.detailLanguage_ == undefined) |
| 167 | return false; |
| 168 | |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 169 | const compareLanguage = assert(this.languages.enabled[n]); |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 170 | return this.detailLanguage_.language == compareLanguage.language; |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 171 | }, |
| 172 | |
| 173 | /** |
michaelpg | 4fed8870 | 2017-02-02 20:57:19 | [diff] [blame] | 174 | * @return {boolean} True if the "Move to top" option for |language| should be |
| 175 | * visible. |
michaelpg | e11da42 | 2016-09-29 00:36:40 | [diff] [blame] | 176 | * @private |
| 177 | */ |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 178 | showMoveUp_: function() { |
michaelpg | 4fed8870 | 2017-02-02 20:57:19 | [diff] [blame] | 179 | // "Move up" is a no-op for the top language, and redundant with |
| 180 | // "Move to top" for the 2nd language. |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 181 | return !this.isNthLanguage_(0) && !this.isNthLanguage_(1); |
michaelpg | e11da42 | 2016-09-29 00:36:40 | [diff] [blame] | 182 | }, |
| 183 | |
| 184 | /** |
michaelpg | 4fed8870 | 2017-02-02 20:57:19 | [diff] [blame] | 185 | * @return {boolean} True if the "Move down" option for |language| should be |
| 186 | * visible. |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 187 | * @private |
| 188 | */ |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 189 | showMoveDown_: function() { |
| 190 | return this.languages != undefined && |
| 191 | !this.isNthLanguage_(this.languages.enabled.length - 1); |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 192 | }, |
| 193 | |
| 194 | /** |
| 195 | * @param {!Object} change Polymer change object for languages.enabled.*. |
| 196 | * @return {boolean} True if there are less than 2 languages. |
| 197 | */ |
| 198 | isHelpTextHidden_: function(change) { |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 199 | return this.languages != undefined && this.languages.enabled.length <= 1; |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 200 | }, |
| 201 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 202 | // <if expr="chromeos"> |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 203 | /** |
| 204 | * Applies Chrome OS session tweaks to the menu. |
| 205 | * @param {!CrActionMenuElement} menu |
| 206 | * @private |
| 207 | */ |
| 208 | tweakMenuForCrOS_: function(menu) { |
| 209 | // In a CrOS multi-user session, the primary user controls the UI language. |
| 210 | // TODO(michaelpg): The language selection should not be hidden, but should |
| 211 | // show a policy indicator. crbug.com/648498 |
| 212 | if (this.isSecondaryUser_()) |
| 213 | menu.querySelector('#uiLanguageItem').hidden = true; |
| 214 | |
| 215 | // The UI language choice doesn't persist for guests. |
Wenzhao Zang | c7402e8 | 2018-10-23 17:49:17 | [diff] [blame^] | 216 | if (loadTimeData.getBoolean('isGuest') && |
| 217 | !loadTimeData.getBoolean('isDemoSession')) { |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 218 | menu.querySelector('#uiLanguageItem').hidden = true; |
Wenzhao Zang | c7402e8 | 2018-10-23 17:49:17 | [diff] [blame^] | 219 | } |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 220 | }, |
| 221 | |
| 222 | /** |
| 223 | * Opens the Manage Input Methods page. |
| 224 | * @private |
| 225 | */ |
| 226 | onManageInputMethodsTap_: function() { |
scottchen | c31781b | 2017-07-10 19:52:42 | [diff] [blame] | 227 | settings.navigateTo(settings.routes.INPUT_METHODS); |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 228 | }, |
| 229 | |
| 230 | /** |
| 231 | * Handler for tap and <Enter> events on an input method on the main page, |
| 232 | * which sets it as the current input method. |
| 233 | * @param {!{model: !{item: !chrome.languageSettingsPrivate.InputMethod}, |
| 234 | * target: !{tagName: string}, |
| 235 | * type: string, |
| 236 | * key: (string|undefined)}} e |
| 237 | */ |
| 238 | onInputMethodTap_: function(e) { |
Dave Schuyler | ede4e78 | 2017-07-11 01:04:03 | [diff] [blame] | 239 | // Taps on the button are handled in onInputMethodOptionsTap_. |
| 240 | // TODO(dschuyler): The row has two operations that are not clearly |
| 241 | // delineated. crbug.com/740691 |
| 242 | if (e.target.tagName == 'BUTTON') |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 243 | return; |
| 244 | |
| 245 | // Ignore key presses other than <Enter>. |
| 246 | if (e.type == 'keypress' && e.key != 'Enter') |
| 247 | return; |
| 248 | |
| 249 | // Set the input method. |
| 250 | this.languageHelper.setCurrentInputMethod(e.model.item.id); |
| 251 | }, |
| 252 | |
| 253 | /** |
| 254 | * Opens the input method extension's options page in a new tab (or focuses |
| 255 | * an existing instance of the IME's options). |
| 256 | * @param {!{model: !{item: chrome.languageSettingsPrivate.InputMethod}}} e |
| 257 | * @private |
| 258 | */ |
| 259 | onInputMethodOptionsTap_: function(e) { |
| 260 | this.languageHelper.openInputMethodOptions(e.model.item.id); |
| 261 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 262 | // </if> |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 263 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 264 | // <if expr="chromeos or is_win"> |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 265 | /** |
| 266 | * @return {boolean} True for a secondary user in a multi-profile session. |
| 267 | * @private |
| 268 | */ |
| 269 | isSecondaryUser_: function() { |
| 270 | return cr.isChromeOS && loadTimeData.getBoolean('isSecondaryUser'); |
| 271 | }, |
| 272 | |
| 273 | /** |
| 274 | * @param {string} languageCode The language code identifying a language. |
| 275 | * @param {string} prospectiveUILanguage The prospective UI language. |
| 276 | * @return {boolean} True if the prospective UI language is set to |
| 277 | * |languageCode| but requires a restart to take effect. |
| 278 | * @private |
| 279 | */ |
| 280 | isRestartRequired_: function(languageCode, prospectiveUILanguage) { |
| 281 | return prospectiveUILanguage == languageCode && |
| 282 | this.languageHelper.requiresRestart(); |
| 283 | }, |
| 284 | |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 285 | /** |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 286 | * @param {!LanguageState} languageState |
| 287 | * @param {string} prospectiveUILanguage The chosen UI language. |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 288 | * @return {boolean} True if the given language cannot be set as the |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 289 | * prospective UI language by the user. |
| 290 | * @private |
| 291 | */ |
| 292 | disableUILanguageCheckbox_: function(languageState, prospectiveUILanguage) { |
dpapad | 4e55d03 | 2018-09-12 18:08:12 | [diff] [blame] | 293 | if (this.detailLanguage_ === undefined) |
| 294 | return true; |
| 295 | |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 296 | // UI language setting belongs to the primary user. |
| 297 | if (this.isSecondaryUser_()) |
| 298 | return true; |
| 299 | |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 300 | // If the language cannot be a UI language, we can't set it as the |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 301 | // prospective UI language. |
| 302 | if (!languageState.language.supportsUI) |
| 303 | return true; |
| 304 | |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 305 | // Unchecking the currently chosen language doesn't make much sense. |
| 306 | if (languageState.language.code == prospectiveUILanguage) |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 307 | return true; |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 308 | |
Alexander Hendrich | ca7c8b1 | 2018-10-13 09:52:08 | [diff] [blame] | 309 | // Check if the language is prohibited by the current "AllowedUILocales" |
Alexander Hendrich | 55d0b0d9 | 2018-05-08 11:29:10 | [diff] [blame] | 310 | // policy. |
Alexander Hendrich | ca7c8b1 | 2018-10-13 09:52:08 | [diff] [blame] | 311 | if (languageState.language.isProhibitedUILocale) |
Alexander Hendrich | 55d0b0d9 | 2018-05-08 11:29:10 | [diff] [blame] | 312 | return true; |
| 313 | |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 314 | // Otherwise, the prospective language can be changed to this language. |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 315 | return false; |
| 316 | }, |
| 317 | |
| 318 | /** |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 319 | * Handler for changes to the UI language checkbox. |
Scott Chen | 5ad171e | 2018-05-08 20:29:17 | [diff] [blame] | 320 | * @param {!{target: !Element}} e |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 321 | * @private |
| 322 | */ |
| 323 | onUILanguageChange_: function(e) { |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 324 | // We don't support unchecking this checkbox. TODO(michaelpg): Ask for a |
| 325 | // simpler widget. |
| 326 | assert(e.target.checked); |
| 327 | this.languageHelper.setProspectiveUILanguage( |
| 328 | this.detailLanguage_.language.code); |
| 329 | |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 330 | this.closeMenuSoon_(); |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 331 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 332 | // </if> |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 333 | |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 334 | /** |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 335 | * @param {!chrome.languageSettingsPrivate.Language} language |
| 336 | * @param {string} targetLanguageCode The default translate target language. |
| 337 | * @return {boolean} True if the translate checkbox should be disabled. |
| 338 | * @private |
| 339 | */ |
| 340 | disableTranslateCheckbox_: function(language, targetLanguageCode) { |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 341 | if (language == undefined || !language.supportsTranslate) |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 342 | return true; |
| 343 | |
| 344 | return this.languageHelper.convertLanguageCodeForTranslate(language.code) == |
| 345 | targetLanguageCode; |
| 346 | }, |
| 347 | |
| 348 | /** |
| 349 | * Handler for changes to the translate checkbox. |
Scott Chen | 5ad171e | 2018-05-08 20:29:17 | [diff] [blame] | 350 | * @param {!{target: !Element}} e |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 351 | * @private |
| 352 | */ |
| 353 | onTranslateCheckboxChange_: function(e) { |
| 354 | if (e.target.checked) { |
| 355 | this.languageHelper.enableTranslateLanguage( |
| 356 | this.detailLanguage_.language.code); |
| 357 | } else { |
| 358 | this.languageHelper.disableTranslateLanguage( |
| 359 | this.detailLanguage_.language.code); |
| 360 | } |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 361 | this.closeMenuSoon_(); |
michaelpg | e11da42 | 2016-09-29 00:36:40 | [diff] [blame] | 362 | }, |
| 363 | |
| 364 | /** |
| 365 | * Returns "complex" if the menu includes checkboxes, which should change the |
| 366 | * spacing of items and show a separator in the menu. |
| 367 | * @param {boolean} translateEnabled |
| 368 | * @return {string} |
| 369 | */ |
| 370 | getMenuClass_: function(translateEnabled) { |
| 371 | if (translateEnabled || cr.isChromeOS || cr.isWindows) |
| 372 | return 'complex'; |
| 373 | return ''; |
| 374 | }, |
| 375 | |
| 376 | /** |
| 377 | * Moves the language to the top of the list. |
| 378 | * @private |
| 379 | */ |
| 380 | onMoveToTopTap_: function() { |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 381 | /** @type {!CrActionMenuElement} */ (this.$.menu.get()).close(); |
michaelpg | e11da42 | 2016-09-29 00:36:40 | [diff] [blame] | 382 | this.languageHelper.moveLanguageToFront(this.detailLanguage_.language.code); |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 383 | }, |
| 384 | |
| 385 | /** |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 386 | * Moves the language up in the list. |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 387 | * @private |
| 388 | */ |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 389 | onMoveUpTap_: function() { |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 390 | /** @type {!CrActionMenuElement} */ (this.$.menu.get()).close(); |
Claudio Magni | 72414d98 | 2017-11-07 06:29:48 | [diff] [blame] | 391 | this.languageHelper.moveLanguage( |
| 392 | this.detailLanguage_.language.code, true /* upDirection */); |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 393 | }, |
| 394 | |
| 395 | /** |
| 396 | * Moves the language down in the list. |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 397 | * @private |
| 398 | */ |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 399 | onMoveDownTap_: function() { |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 400 | /** @type {!CrActionMenuElement} */ (this.$.menu.get()).close(); |
Claudio Magni | 72414d98 | 2017-11-07 06:29:48 | [diff] [blame] | 401 | this.languageHelper.moveLanguage( |
| 402 | this.detailLanguage_.language.code, false /* upDirection */); |
mahmadi | 04d3fdc6 | 2016-06-28 14:43:24 | [diff] [blame] | 403 | }, |
| 404 | |
| 405 | /** |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 406 | * Disables the language. |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 407 | * @private |
| 408 | */ |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 409 | onRemoveLanguageTap_: function() { |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 410 | /** @type {!CrActionMenuElement} */ (this.$.menu.get()).close(); |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 411 | this.languageHelper.disableLanguage(this.detailLanguage_.language.code); |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 412 | }, |
| 413 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 414 | // <if expr="chromeos or is_win"> |
michaelpg | 9a670d4 | 2016-08-25 02:06:37 | [diff] [blame] | 415 | /** |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 416 | * Checks whether the prospective UI language (the pref that indicates what |
| 417 | * language to use in Chrome) matches the current language. This pref is used |
| 418 | * only on Chrome OS and Windows; we don't control the UI language elsewhere. |
| 419 | * @param {string} languageCode The language code identifying a language. |
| 420 | * @param {string} prospectiveUILanguage The prospective UI language. |
| 421 | * @return {boolean} True if the given language matches the prospective UI |
| 422 | * pref (which may be different from the actual UI language). |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 423 | * @private |
| 424 | */ |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 425 | isProspectiveUILanguage_: function(languageCode, prospectiveUILanguage) { |
| 426 | return languageCode == prospectiveUILanguage; |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 427 | }, |
| 428 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 429 | /** |
| 430 | * @param {string} prospectiveUILanguage |
| 431 | * @return {string} |
| 432 | * @private |
| 433 | */ |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 434 | getProspectiveUILanguageName_: function(prospectiveUILanguage) { |
| 435 | return this.languageHelper.getLanguage(prospectiveUILanguage).displayName; |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 436 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 437 | // </if> |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 438 | |
| 439 | /** |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 440 | * @return {string} |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 441 | * @private |
| 442 | */ |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 443 | getLanguageListTwoLine_: function() { |
| 444 | return cr.isChromeOS || cr.isWindows ? 'two-line' : ''; |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 445 | }, |
| 446 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 447 | // <if expr="not is_macosx"> |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 448 | /** |
michaelpg | 9fefdaf | 2016-06-13 22:33:11 | [diff] [blame] | 449 | * Returns the secondary text for the spell check subsection based on the |
| 450 | * enabled spell check languages, listing at most 2 languages. |
| 451 | * @return {string} |
| 452 | * @private |
| 453 | */ |
| 454 | getSpellCheckSecondaryText_: function() { |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 455 | if (this.languages == undefined || this.prefs == undefined) |
| 456 | return ''; |
| 457 | |
Marc-Antoine Courteau | 401867a | 2018-06-08 16:25:04 | [diff] [blame] | 458 | if (this.getSpellCheckDisabledByPolicy_()) |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 459 | return loadTimeData.getString('spellCheckDisabled'); |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 460 | const enabledSpellCheckLanguages = |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 461 | this.getSpellCheckLanguages_().filter(function(languageState) { |
| 462 | return (languageState.spellCheckEnabled || languageState.isManaged) && |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 463 | languageState.language.supportsSpellcheck; |
michaelpg | 9fefdaf | 2016-06-13 22:33:11 | [diff] [blame] | 464 | }); |
| 465 | switch (enabledSpellCheckLanguages.length) { |
| 466 | case 0: |
| 467 | return ''; |
| 468 | case 1: |
| 469 | return enabledSpellCheckLanguages[0].language.displayName; |
| 470 | case 2: |
| 471 | return loadTimeData.getStringF( |
| 472 | 'spellCheckSummaryTwoLanguages', |
| 473 | enabledSpellCheckLanguages[0].language.displayName, |
| 474 | enabledSpellCheckLanguages[1].language.displayName); |
| 475 | case 3: |
| 476 | // "foo, bar, and 1 other" |
| 477 | return loadTimeData.getStringF( |
| 478 | 'spellCheckSummaryThreeLanguages', |
| 479 | enabledSpellCheckLanguages[0].language.displayName, |
| 480 | enabledSpellCheckLanguages[1].language.displayName); |
| 481 | default: |
| 482 | // "foo, bar, and [N-2] others" |
| 483 | return loadTimeData.getStringF( |
| 484 | 'spellCheckSummaryMultipleLanguages', |
| 485 | enabledSpellCheckLanguages[0].language.displayName, |
| 486 | enabledSpellCheckLanguages[1].language.displayName, |
| 487 | (enabledSpellCheckLanguages.length - 2).toLocaleString()); |
| 488 | } |
| 489 | }, |
| 490 | |
| 491 | /** |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 492 | * Returns whether spellcheck is disabled by policy or not. |
| 493 | * @return {boolean} |
| 494 | * @private |
| 495 | */ |
Marc-Antoine Courteau | 401867a | 2018-06-08 16:25:04 | [diff] [blame] | 496 | getSpellCheckDisabledByPolicy_: function() { |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 497 | const pref = /** @type {!chrome.settingsPrivate.PrefObject} */ ( |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 498 | this.get('browser.enable_spellchecking', this.prefs)); |
Marc-Antoine Courteau | 401867a | 2018-06-08 16:25:04 | [diff] [blame] | 499 | return pref.enforcement == chrome.settingsPrivate.Enforcement.ENFORCED && |
| 500 | pref.value === false; |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 501 | }, |
| 502 | |
| 503 | /** |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 504 | * Returns an array of enabled languages, plus spellcheck languages that are |
| 505 | * forced by policy. |
| 506 | * @return {!Array<!LanguageState|!ForcedLanguageState>} |
| 507 | * @private |
| 508 | */ |
| 509 | getSpellCheckLanguages_: function() { |
| 510 | return this.languages.enabled.concat( |
Esmael El-Moslimany | 3f225c8 | 2018-03-29 23:27:32 | [diff] [blame] | 511 | this.languages.forcedSpellCheckLanguages); |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 512 | }, |
| 513 | |
| 514 | /** @private */ |
| 515 | updateSpellcheckLanguages_: function() { |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 516 | if (this.languages == undefined) |
| 517 | return; |
| 518 | |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 519 | this.set('spellCheckLanguages_', this.getSpellCheckLanguages_()); |
| 520 | |
| 521 | // Notify Polymer of subproperties that might have changed on the items in |
| 522 | // the spellCheckLanguages_ array, to make sure the UI updates. Polymer |
| 523 | // would otherwise not notice the changes in the subproperties, as some of |
| 524 | // them are references to those from |this.languages.enabled|. It would be |
| 525 | // possible to |this.linkPaths()| objects from |this.languages.enabled| to |
| 526 | // |this.spellCheckLanguages_|, but that would require complex housekeeping |
| 527 | // to |this.unlinkPaths()| as |this.languages.enabled| changes. |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 528 | for (let i = 0; i < this.spellCheckLanguages_.length; i++) { |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 529 | this.notifyPath(`spellCheckLanguages_.${i}.isManaged`); |
| 530 | this.notifyPath(`spellCheckLanguages_.${i}.spellCheckEnabled`); |
Esmael El-Moslimany | 3f225c8 | 2018-03-29 23:27:32 | [diff] [blame] | 531 | this.notifyPath( |
| 532 | `spellCheckLanguages_.${i}.downloadDictionaryFailureCount`); |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 533 | } |
| 534 | }, |
| 535 | |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 536 | /** @private */ |
| 537 | updateSpellcheckEnabled_: function() { |
dpapad | f7345e7e | 2018-06-26 22:46:33 | [diff] [blame] | 538 | if (this.prefs == undefined) |
| 539 | return; |
| 540 | |
Marc-Antoine Courteau | 401867a | 2018-06-08 16:25:04 | [diff] [blame] | 541 | this.set('spellCheckDisabled_', this.getSpellCheckDisabledByPolicy_()); |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 542 | |
| 543 | // If the spellcheck section was expanded, close it. |
| 544 | if (this.spellCheckDisabled_) |
| 545 | this.set('spellCheckOpened_', false); |
| 546 | }, |
| 547 | |
Marc-Antoine Courteau | 311a7cec | 2017-12-14 21:18:49 | [diff] [blame] | 548 | /** |
michaelpg | 53fc07e | 2015-10-27 20:18:16 | [diff] [blame] | 549 | * Opens the Custom Dictionary page. |
| 550 | * @private |
| 551 | */ |
| 552 | onEditDictionaryTap_: function() { |
scottchen | c31781b | 2017-07-10 19:52:42 | [diff] [blame] | 553 | settings.navigateTo(settings.routes.EDIT_DICTIONARY); |
michaelpg | 53fc07e | 2015-10-27 20:18:16 | [diff] [blame] | 554 | }, |
michaelpg | 53fc07e | 2015-10-27 20:18:16 | [diff] [blame] | 555 | |
michaelpg | af3a928 | 2015-10-01 00:07:33 | [diff] [blame] | 556 | /** |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 557 | * Handler for enabling or disabling spell check. |
| 558 | * @param {!{target: Element, model: !{item: !LanguageState}}} e |
michaelpg | af3a928 | 2015-10-01 00:07:33 | [diff] [blame] | 559 | */ |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 560 | onSpellCheckChange_: function(e) { |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 561 | const item = e.model.item; |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 562 | if (!item.language.supportsSpellcheck) |
| 563 | return; |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 564 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 565 | this.languageHelper.toggleSpellCheck( |
| 566 | item.language.code, !item.spellCheckEnabled); |
michaelpg | 04f42db2 | 2016-11-29 02:18:14 | [diff] [blame] | 567 | }, |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 568 | |
| 569 | /** |
Esmael El-Moslimany | 3f225c8 | 2018-03-29 23:27:32 | [diff] [blame] | 570 | * Handler to initiate another attempt at downloading the spell check |
| 571 | * dictionary for a specified language. |
| 572 | * @param {!{target: Element, model: !{item: !LanguageState}}} e |
| 573 | */ |
| 574 | onRetryDictionaryDownloadClick_: function(e) { |
| 575 | assert(this.errorsGreaterThan_( |
| 576 | e.model.item.downloadDictionaryFailureCount, 0)); |
| 577 | this.languageHelper.retryDownloadDictionary(e.model.item.language.code); |
| 578 | }, |
| 579 | |
| 580 | /** |
| 581 | * Handler for clicking on the name of the language. The action taken must |
| 582 | * match the control that is available. |
| 583 | * @param {!{target: Element, model: !{item: !LanguageState}}} e |
| 584 | */ |
| 585 | onSpellCheckNameClick_: function(e) { |
| 586 | assert(!this.isSpellCheckNameClickDisabled_(e.model.item)); |
| 587 | this.onSpellCheckChange_(e); |
| 588 | }, |
| 589 | |
| 590 | /** |
| 591 | * Name only supports clicking when language is not managed, supports |
| 592 | * spellcheck, and the dictionary has been downloaded with no errors. |
| 593 | * @param {!LanguageState|!ForcedLanguageState} item |
| 594 | * @return {boolean} |
| 595 | * @private |
| 596 | */ |
| 597 | isSpellCheckNameClickDisabled_: function(item) { |
| 598 | return item.isManaged || !item.language.supportsSpellcheck || |
| 599 | item.downloadDictionaryFailureCount > 0; |
| 600 | }, |
| 601 | |
| 602 | /** |
michaelpg | 44d9dc8 | 2016-12-08 02:04:00 | [diff] [blame] | 603 | * @return {string} |
| 604 | * @private |
| 605 | */ |
| 606 | getSpellCheckListTwoLine_: function() { |
| 607 | return this.spellCheckSecondaryText_.length ? 'two-line' : ''; |
| 608 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 609 | // </if> |
michaelpg | 44d9dc8 | 2016-12-08 02:04:00 | [diff] [blame] | 610 | |
| 611 | /** |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 612 | * Returns either the "selected" class, if the language matches the |
| 613 | * prospective UI language, or an empty string. Languages can only be |
| 614 | * selected on Chrome OS and Windows. |
| 615 | * @param {string} languageCode The language code identifying a language. |
| 616 | * @param {string} prospectiveUILanguage The prospective UI language. |
| 617 | * @return {string} The class name for the language item. |
| 618 | * @private |
| 619 | */ |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 620 | getLanguageItemClass_: function(languageCode, prospectiveUILanguage) { |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 621 | if ((cr.isChromeOS || cr.isWindows) && |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 622 | languageCode == prospectiveUILanguage) { |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 623 | return 'selected'; |
michaelpg | abbd180d | 2016-04-23 02:42:35 | [diff] [blame] | 624 | } |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 625 | return ''; |
| 626 | }, |
dbeam | ed688838 | 2016-06-25 01:33:15 | [diff] [blame] | 627 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 628 | // <if expr="chromeos"> |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 629 | /** |
| 630 | * @param {string} id The input method ID. |
| 631 | * @param {string} currentId The ID of the currently enabled input method. |
| 632 | * @return {boolean} True if the IDs match. |
| 633 | * @private |
| 634 | */ |
| 635 | isCurrentInputMethod_: function(id, currentId) { |
| 636 | assert(cr.isChromeOS); |
| 637 | return id == currentId; |
| 638 | }, |
michaelpg | 53fc07e | 2015-10-27 20:18:16 | [diff] [blame] | 639 | |
| 640 | /** |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 641 | * @param {string} id The input method ID. |
| 642 | * @param {string} currentId The ID of the currently enabled input method. |
| 643 | * @return {string} The class for the input method item. |
| 644 | * @private |
| 645 | */ |
| 646 | getInputMethodItemClass_: function(id, currentId) { |
michaelpg | abbd180d | 2016-04-23 02:42:35 | [diff] [blame] | 647 | assert(cr.isChromeOS); |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 648 | return this.isCurrentInputMethod_(id, currentId) ? 'selected' : ''; |
| 649 | }, |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 650 | |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 651 | getInputMethodName_: function(id) { |
| 652 | assert(cr.isChromeOS); |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 653 | const inputMethod = |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 654 | this.languages.inputMethods.enabled.find(function(inputMethod) { |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 655 | return inputMethod.id == id; |
| 656 | }); |
| 657 | return inputMethod ? inputMethod.displayName : ''; |
| 658 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 659 | // </if> |
michaelpg | d700e56 | 2016-04-24 00:41:59 | [diff] [blame] | 660 | |
michaelpg | 4d04ee9 | 2016-04-08 19:56:28 | [diff] [blame] | 661 | /** |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 662 | * @param {!Event} e |
| 663 | * @private |
| 664 | */ |
dpapad | 46b5ca1 | 2016-10-15 02:11:42 | [diff] [blame] | 665 | onDotsTap_: function(e) { |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 666 | // Set a copy of the LanguageState object since it is not data-bound to the |
| 667 | // languages model directly. |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 668 | this.detailLanguage_ = /** @type {!LanguageState} */ (Object.assign( |
michaelpg | 4cb2126 | 2016-12-14 02:46:48 | [diff] [blame] | 669 | {}, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 670 | /** @type {!{model: !{item: !LanguageState}}} */ (e).model.item)); |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 671 | |
| 672 | // Ensure the template has been stamped. |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 673 | let menu = /** @type {?CrActionMenuElement} */ (this.$.menu.getIfExists()); |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 674 | if (!menu) { |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 675 | menu = /** @type {!CrActionMenuElement} */ (this.$.menu.get()); |
| 676 | // <if expr="chromeos"> |
dpapad | eb8e8df | 2017-04-19 17:45:26 | [diff] [blame] | 677 | this.tweakMenuForCrOS_(menu); |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 678 | // </if> |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 679 | } |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 680 | |
dpapad | 46b5ca1 | 2016-10-15 02:11:42 | [diff] [blame] | 681 | menu.showAt(/** @type {!Element} */ (e.target)); |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 682 | }, |
| 683 | |
| 684 | /** |
Renjie Liu | b350ff32 | 2017-09-05 05:02:36 | [diff] [blame] | 685 | * @param {boolean} newVal The new value of languagesOpened_. |
| 686 | * @param {boolean} oldVal The old value of languagesOpened_. |
| 687 | * @private |
| 688 | */ |
| 689 | onLanguagesOpenedChanged_: function(newVal, oldVal) { |
| 690 | if (!oldVal && newVal) { |
| 691 | chrome.send( |
| 692 | 'metricsHandler:recordBooleanHistogram', |
| 693 | [LANGUAGE_SETTING_IS_SHOWN_UMA_NAME, true]); |
| 694 | } |
| 695 | }, |
| 696 | |
| 697 | /** |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 698 | * Closes the shared action menu after a short delay, so when a checkbox is |
dpapad | 04f68ff8 | 2018-02-17 03:35:28 | [diff] [blame] | 699 | * clicked it can be seen to change state before disappearing. |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 700 | * @private |
| 701 | */ |
| 702 | closeMenuSoon_: function() { |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 703 | const menu = /** @type {!CrActionMenuElement} */ (this.$.menu.get()); |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 704 | setTimeout(function() { |
| 705 | if (menu.open) |
| 706 | menu.close(); |
| 707 | }, settings.kMenuCloseDelay); |
| 708 | }, |
| 709 | |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 710 | // <if expr="chromeos or is_win"> |
michaelpg | c5bbdca | 2017-01-18 02:59:37 | [diff] [blame] | 711 | /** |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 712 | * Handler for the restart button. |
| 713 | * @private |
| 714 | */ |
| 715 | onRestartTap_: function() { |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 716 | // <if expr="chromeos"> |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 717 | settings.LifetimeBrowserProxyImpl.getInstance().signOutAndRestart(); |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 718 | // </if> |
| 719 | // <if expr="is_win"> |
michaelpg | c3b245a4 | 2016-09-22 03:20:56 | [diff] [blame] | 720 | settings.LifetimeBrowserProxyImpl.getInstance().restart(); |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 721 | // </if> |
michaelpg | b44ce49 | 2016-08-27 10:39:01 | [diff] [blame] | 722 | }, |
dbeam | 141ae1f | 2017-06-19 18:28:35 | [diff] [blame] | 723 | // </if> |
michaelpg | 10719a7 | 2016-12-07 09:05:21 | [diff] [blame] | 724 | |
| 725 | /** |
| 726 | * Toggles the expand button within the element being listened to. |
| 727 | * @param {!Event} e |
| 728 | * @private |
| 729 | */ |
| 730 | toggleExpandButton_: function(e) { |
| 731 | // The expand button handles toggling itself. |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 732 | const expandButtonTag = 'CR-EXPAND-BUTTON'; |
michaelpg | 10719a7 | 2016-12-07 09:05:21 | [diff] [blame] | 733 | if (e.target.tagName == expandButtonTag) |
| 734 | return; |
| 735 | |
Marc-Antoine Courteau | 41f4c37 | 2017-12-18 19:33:07 | [diff] [blame] | 736 | if (!e.currentTarget.hasAttribute('actionable')) |
| 737 | return; |
| 738 | |
michaelpg | 10719a7 | 2016-12-07 09:05:21 | [diff] [blame] | 739 | /** @type {!CrExpandButtonElement} */ |
dpapad | 00039d9 | 2018-01-08 22:01:48 | [diff] [blame] | 740 | const expandButton = e.currentTarget.querySelector(expandButtonTag); |
michaelpg | 10719a7 | 2016-12-07 09:05:21 | [diff] [blame] | 741 | assert(expandButton); |
| 742 | expandButton.expanded = !expandButton.expanded; |
| 743 | }, |
michaelpg | 790ff8a | 2015-09-03 03:27:22 | [diff] [blame] | 744 | }); |
michaelpg | 0f479fec | 2015-09-25 04:03:49 | [diff] [blame] | 745 | })(); |