Chromium Code Reviews| 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 cr.define('options.language', function() { | 5 cr.define('options.language', function() { |
| 6 const List = cr.ui.List; | 6 const List = cr.ui.List; |
| 7 const ListItem = cr.ui.ListItem; | 7 const ListItem = cr.ui.ListItem; |
| 8 const ArrayDataModel = cr.ui.ArrayDataModel; | 8 const ArrayDataModel = cr.ui.ArrayDataModel; |
| 9 const LanguageOptions = options.LanguageOptions; | 9 const LanguageOptions = options.LanguageOptions; |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 var languageDisplayName = | 55 var languageDisplayName = |
| 56 LanguageList.getDisplayNameFromLanguageCode(languageCode); | 56 LanguageList.getDisplayNameFromLanguageCode(languageCode); |
| 57 return new ListItem({label: languageDisplayName}); | 57 return new ListItem({label: languageDisplayName}); |
| 58 }, | 58 }, |
| 59 | 59 |
| 60 /* | 60 /* |
| 61 * Adds a language to the language list. | 61 * Adds a language to the language list. |
| 62 * @param {string} languageCode language code (ex. "fr"). | 62 * @param {string} languageCode language code (ex. "fr"). |
| 63 */ | 63 */ |
| 64 addLanguage: function(languageCode) { | 64 addLanguage: function(languageCode) { |
| 65 var dataModel = this.dataModel; | 65 this.dataModel.push(languageCode); |
| 66 dataModel.push(languageCode); | 66 // Select the last item, which is the language added. |
| 67 this.selectionModel.selectedIndex = this.dataModel.length - 1; | |
|
arv (Not doing code reviews)
2010/08/04 17:19:54
You might want to set leadIndex and anchorIndex as
satorux1
2010/08/05 04:12:50
Thank you for pointing this out. We'll be supposed
| |
| 67 | 68 |
| 68 this.updateBackend_(); | 69 this.updateBackend_(); |
| 69 }, | 70 }, |
| 70 | 71 |
| 71 /* | 72 /* |
| 72 * Gets the language codes of the currently listed languages. | 73 * Gets the language codes of the currently listed languages. |
| 73 */ | 74 */ |
| 74 getLanguageCodes: function() { | 75 getLanguageCodes: function() { |
| 75 return this.dataModel.slice(); | 76 return this.dataModel.slice(); |
| 76 }, | 77 }, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 94 // handle that case here. | 95 // handle that case here. |
| 95 var originalIndex = this.selectionModel.selectedIndex; | 96 var originalIndex = this.selectionModel.selectedIndex; |
| 96 this.dataModel.splice(this.selectionModel.selectedIndex, 1); | 97 this.dataModel.splice(this.selectionModel.selectedIndex, 1); |
| 97 // Select the item at the original index if possible. Otherwise, | 98 // Select the item at the original index if possible. Otherwise, |
| 98 // select the last item. | 99 // select the last item. |
| 99 // TODO(satorux): This should be handled by the selection model | 100 // TODO(satorux): This should be handled by the selection model |
| 100 // See crbug.com/49893. | 101 // See crbug.com/49893. |
| 101 this.selectionModel.selectedIndex = Math.min( | 102 this.selectionModel.selectedIndex = Math.min( |
| 102 originalIndex, | 103 originalIndex, |
| 103 this.dataModel.length - 1); | 104 this.dataModel.length - 1); |
| 104 var newLanguageCodes = this.dataModel.slice(); | |
| 105 this.load_(newLanguageCodes); | |
| 106 this.updateBackend_(); | 105 this.updateBackend_(); |
| 107 } | 106 } |
| 108 }, | 107 }, |
| 109 | 108 |
| 110 /** | 109 /** |
| 111 * Handles pref change. | 110 * Handles pref change. |
| 112 * @param {Event} e The change event object. | 111 * @param {Event} e The change event object. |
| 113 * @private | 112 * @private |
| 114 */ | 113 */ |
| 115 handlePrefChange_: function(e) { | 114 handlePrefChange_: function(e) { |
| 116 var languageCodesInCsv = e.value; | 115 var languageCodesInCsv = e.value; |
| 117 var languageCodes = this.filterBadLanguageCodes_( | 116 var languageCodes = this.filterBadLanguageCodes_( |
| 118 languageCodesInCsv.split(',')); | 117 languageCodesInCsv.split(',')); |
| 119 this.load_(languageCodes); | 118 this.load_(languageCodes); |
| 120 }, | 119 }, |
| 121 | 120 |
| 122 /** | 121 /** |
| 123 * Loads given language list. | 122 * Loads given language list. |
| 124 * @param {Array} languageCodes List of language codes. | 123 * @param {Array} languageCodes List of language codes. |
| 125 * @private | 124 * @private |
| 126 */ | 125 */ |
| 127 load_: function(languageCodes) { | 126 load_: function(languageCodes) { |
| 127 // Preserve the original selected index. See comments below. | |
| 128 var originalSelectedIndex = (this.selectionModel ? | |
| 129 this.selectionModel.selectedIndex : -1); | |
| 128 this.dataModel = new ArrayDataModel(languageCodes); | 130 this.dataModel = new ArrayDataModel(languageCodes); |
| 129 // Select the first item if it's not empty. | 131 if (originalSelectedIndex >= 0 && |
| 130 // TODO(satorux): Switch to a single item selection model that does | 132 originalSelectedIndex < this.dataModel.length) { |
| 131 // not allow no selection, one it's ready: crbug.com/49893 | 133 // Restore the original selected index if the selected index is |
| 132 if (this.dataModel.length > 0) | 134 // valid after the data model is loaded. This is neeeded to keep |
| 135 // the selected language after the languge is added or removed. | |
| 136 this.selectionModel.selectedIndex = originalSelectedIndex; | |
| 137 } else if (this.dataModel.length > 0){ | |
| 138 // Otherwise, select the first item if it's not empty. | |
| 139 // TODO(satorux): Switch to a single item selection model that does | |
| 140 // not allow no selection, one it's ready: crbug.com/49893 | |
| 133 this.selectionModel.selectedIndex = 0; | 141 this.selectionModel.selectedIndex = 0; |
| 142 } | |
| 134 }, | 143 }, |
| 135 | 144 |
| 136 /** | 145 /** |
| 137 * Updates backend. | 146 * Updates backend. |
| 138 */ | 147 */ |
| 139 updateBackend_: function() { | 148 updateBackend_: function() { |
| 140 // Encode the language codes into a CSV string. | 149 // Encode the language codes into a CSV string. |
| 141 Preferences.setStringPref(this.pref, this.dataModel.slice().join(',')); | 150 Preferences.setStringPref(this.pref, this.dataModel.slice().join(',')); |
| 142 }, | 151 }, |
| 143 | 152 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 157 } | 166 } |
| 158 } | 167 } |
| 159 return filteredLanguageCodes; | 168 return filteredLanguageCodes; |
| 160 }, | 169 }, |
| 161 }; | 170 }; |
| 162 | 171 |
| 163 return { | 172 return { |
| 164 LanguageList: LanguageList | 173 LanguageList: LanguageList |
| 165 }; | 174 }; |
| 166 }); | 175 }); |
| OLD | NEW |