| OLD | NEW |
| 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-add-languages-dialog' is a dialog for enabling | 6 * @fileoverview 'settings-add-languages-dialog' is a dialog for enabling |
| 7 * languages. | 7 * languages. |
| 8 */ | 8 */ |
| 9 Polymer({ | 9 Polymer({ |
| 10 is: 'settings-add-languages-dialog', | 10 is: 'settings-add-languages-dialog', |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 languagesToAdd_: { | 23 languagesToAdd_: { |
| 24 type: Object, | 24 type: Object, |
| 25 value: function() { return new Set(); }, | 25 value: function() { return new Set(); }, |
| 26 }, | 26 }, |
| 27 | 27 |
| 28 /** @private */ | 28 /** @private */ |
| 29 disableActionButton_: { | 29 disableActionButton_: { |
| 30 type: Boolean, | 30 type: Boolean, |
| 31 value: true, | 31 value: true, |
| 32 }, | 32 }, |
| 33 |
| 34 /** @private */ |
| 35 filterValue_: { |
| 36 type: String, |
| 37 value: '', |
| 38 }, |
| 33 }, | 39 }, |
| 34 | 40 |
| 41 /** @override */ |
| 35 attached: function() { | 42 attached: function() { |
| 36 this.$.dialog.showModal(); | 43 this.$.dialog.showModal(); |
| 37 | |
| 38 // Prevent flashing the Cancel button's focus state. | |
| 39 this.$$('.cancel-button').blur(); | |
| 40 setTimeout(this.afterShown_.bind(this)); | |
| 41 }, | 44 }, |
| 42 | 45 |
| 43 /** | 46 /** |
| 44 * Re-initializes the dialog after it is shown. | 47 * @param {!CustomEvent} e |
| 45 * @private | 48 * @private |
| 46 */ | 49 */ |
| 47 afterShown_: function() { | 50 onSearchChanged_: function(e) { |
| 48 // Only fire iron-resize after the list displayed to prevent flickering. | 51 this.filterValue_ = /** @type {string} */ (e.detail); |
| 49 this.$$('iron-list').fire('iron-resize'); | |
| 50 | |
| 51 // Focus the top checkbox, assuming there are languages left to enable. | |
| 52 var firstCheckbox = this.$$('iron-list paper-checkbox'); | |
| 53 if (firstCheckbox) | |
| 54 firstCheckbox.focus(); | |
| 55 }, | 52 }, |
| 56 | 53 |
| 57 /** | 54 /** |
| 58 * Returns the supported languages that are not yet enabled, based on | 55 * @return {!Array<!chrome.languageSettingsPrivate.Language>} A list of |
| 59 * the LanguageHelper's enabled languages list. | 56 * languages to be displayed. |
| 60 * @param {!Array<!chrome.languageSettingsPrivate.Language>} | |
| 61 * supportedLanguages | |
| 62 * @param {!Object} enabledLanguagesChange Polymer change record for | |
| 63 * |enabledLanguages|. | |
| 64 * @return {!Array<!chrome.languageSettingsPrivate.Language>} | |
| 65 * @private | 57 * @private |
| 66 */ | 58 */ |
| 67 getAvailableLanguages_: function(supportedLanguages, enabledLanguagesChange) { | 59 getLanguages_: function() { |
| 68 return supportedLanguages.filter(function(language) { | 60 return this.languages.supported.filter(function(language) { |
| 69 return !this.languageHelper.isLanguageEnabled(language.code); | 61 var isAvailableLanguage = |
| 62 !this.languageHelper.isLanguageEnabled(language.code); |
| 63 |
| 64 if (!isAvailableLanguage) |
| 65 return false; |
| 66 |
| 67 if (!this.filterValue_) |
| 68 return isAvailableLanguage; |
| 69 |
| 70 return language.displayName.toLowerCase().includes( |
| 71 this.filterValue_.toLowerCase()); |
| 70 }.bind(this)); | 72 }.bind(this)); |
| 71 }, | 73 }, |
| 72 | 74 |
| 73 /** | 75 /** |
| 74 * True if the user has chosen to add this language (checked its checkbox). | 76 * True if the user has chosen to add this language (checked its checkbox). |
| 77 * @param {string} languageCode |
| 75 * @return {boolean} | 78 * @return {boolean} |
| 76 * @private | 79 * @private |
| 77 */ | 80 */ |
| 78 willAdd_: function(languageCode) { | 81 willAdd_: function(languageCode) { |
| 79 return this.languagesToAdd_.has(languageCode); | 82 return this.languagesToAdd_.has(languageCode); |
| 80 }, | 83 }, |
| 81 | 84 |
| 82 /** | 85 /** |
| 83 * Handler for checking or unchecking a language item. | 86 * Handler for checking or unchecking a language item. |
| 84 * @param {!{model: !{item: !chrome.languageSettingsPrivate.Language}, | 87 * @param {!{model: !{item: !chrome.languageSettingsPrivate.Language}, |
| 85 * target: !PaperCheckboxElement}} e | 88 * target: !PaperCheckboxElement}} e |
| 86 * @private | 89 * @private |
| 87 */ | 90 */ |
| 88 onLanguageCheckboxChange_: function(e) { | 91 onLanguageCheckboxChange_: function(e) { |
| 89 // Add or remove the item to the Set. No need to worry about data binding: | 92 // Add or remove the item to the Set. No need to worry about data binding: |
| 90 // willAdd_ is called to initialize the checkbox state (in case the | 93 // willAdd_ is called to initialize the checkbox state (in case the |
| 91 // iron-list re-uses a previous checkbox), and the checkbox can only be | 94 // iron-list re-uses a previous checkbox), and the checkbox can only be |
| 92 // changed after that by user action. | 95 // changed after that by user action. |
| 93 var code = e.model.item.code; | 96 var language = e.model.item; |
| 94 if (e.target.checked) | 97 if (e.target.checked) |
| 95 this.languagesToAdd_.add(code); | 98 this.languagesToAdd_.add(language.code); |
| 96 else | 99 else |
| 97 this.languagesToAdd_.delete(code); | 100 this.languagesToAdd_.delete(language.code); |
| 98 | 101 |
| 99 this.disableActionButton_ = !this.languagesToAdd_.size; | 102 this.disableActionButton_ = !this.languagesToAdd_.size; |
| 100 }, | 103 }, |
| 101 | 104 |
| 102 /** @private */ | 105 /** @private */ |
| 103 onCancelButtonTap_: function() { | 106 onCancelButtonTap_: function() { |
| 104 this.$.dialog.close(); | 107 this.$.dialog.close(); |
| 105 }, | 108 }, |
| 106 | 109 |
| 107 /** | 110 /** |
| 108 * Enables the checked languages. | 111 * Enables the checked languages. |
| 109 * @private | 112 * @private |
| 110 */ | 113 */ |
| 111 onActionButtonTap_: function() { | 114 onActionButtonTap_: function() { |
| 112 this.$.dialog.close(); | 115 this.$.dialog.close(); |
| 113 this.languagesToAdd_.forEach(function(language) { | 116 this.languagesToAdd_.forEach(function(languageCode) { |
| 114 this.languageHelper.enableLanguage(language); | 117 this.languageHelper.enableLanguage(languageCode); |
| 115 }.bind(this)); | 118 }.bind(this)); |
| 116 }, | 119 }, |
| 117 }); | 120 }); |
| OLD | NEW |