1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('options.language', function() {
const ArrayDataModel = cr.ui.ArrayDataModel;
const LanguageOptions = options.LanguageOptions;
const List = cr.ui.List;
const ListItem = cr.ui.ListItem;
const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel;
/**
* Creates a new language list.
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {cr.ui.List}
*/
var LanguageList = cr.ui.define('list');
/**
* Gets display name from the given language code.
* @param {string} languageCode Language code (ex. "fr").
*/
LanguageList.getDisplayNameFromLanguageCode = function(languageCode) {
// Build the language code to display name dictionary at first time.
if (!this.languageCodeToDisplayName_) {
this.languageCodeToDisplayName_ = {};
var languageList = templateData.languageList;
for (var i = 0; i < languageList.length; i++) {
var language = languageList[i];
this.languageCodeToDisplayName_[language.code] = language.displayName;
}
}
return this.languageCodeToDisplayName_[languageCode];
}
/**
* Gets native display name from the given language code.
* @param {string} languageCode Language code (ex. "fr").
*/
LanguageList.getNativeDisplayNameFromLanguageCode = function(languageCode) {
// Build the language code to display name dictionary at first time.
if (!this.languageCodeToNativeDisplayName_) {
this.languageCodeToNativeDisplayName_ = {};
var languageList = templateData.languageList;
for (var i = 0; i < languageList.length; i++) {
var language = languageList[i];
this.languageCodeToNativeDisplayName_[language.code] =
language.nativeDisplayName;
}
}
return this.languageCodeToNativeDisplayName_[languageCode];
}
/**
* Returns true if the given language code is valid.
* @param {string} languageCode Language code (ex. "fr").
*/
LanguageList.isValidLanguageCode = function(languageCode) {
// Having the display name for the language code means that the
// language code is valid.
if (LanguageList.getDisplayNameFromLanguageCode(languageCode)) {
return true;
}
return false;
}
LanguageList.prototype = {
__proto__: List.prototype,
// The list item being dragged.
draggedItem: null,
// The drop position information: "below" or "above".
dropPos: null,
// The preference is a CSV string that describes preferred languages
// in Chrome OS. The language list is used for showing the language
// list in "Language and Input" options page.
preferredLanguagesPref: 'settings.language.preferred_languages',
// The preference is a CSV string that describes accept languages used
// for content negotiation. To be more precise, the list will be used
// in "Accept-Language" header in HTTP requests.
acceptLanguagesPref: 'intl.accept_languages',
/** @inheritDoc */
decorate: function() {
List.prototype.decorate.call(this);
this.selectionModel = new ListSingleSelectionModel;
// HACK(arv): http://crbug.com/40902
window.addEventListener('resize', this.redraw.bind(this));
// Listen to pref change.
Preferences.getInstance().addEventListener(this.preferredLanguagesPref,
this.handlePreferredLanguagesPrefChange_.bind(this));
// Listen to drag and drop events.
this.addEventListener('dragstart', this.handleDragStart_.bind(this));
this.addEventListener('dragenter', this.handleDragEnter_.bind(this));
this.addEventListener('dragover', this.handleDragOver_.bind(this));
this.addEventListener('drop', this.handleDrop_.bind(this));
},
createItem: function(languageCode) {
var languageDisplayName =
LanguageList.getDisplayNameFromLanguageCode(languageCode);
var languageNativeDisplayName =
LanguageList.getNativeDisplayNameFromLanguageCode(languageCode);
return new ListItem({
label: languageDisplayName,
draggable: true,
languageCode: languageCode,
title: languageNativeDisplayName // Show native name as tooltip.
});
},
/*
* Adds a language to the language list.
* @param {string} languageCode language code (ex. "fr").
*/
addLanguage: function(languageCode) {
// It shouldn't happen but ignore the language code if it's
// null/undefined, or already present.
if (!languageCode || this.dataModel.indexOf(languageCode) >= 0) {
return;
}
this.dataModel.push(languageCode);
// Select the last item, which is the language added.
this.selectionModel.selectedIndex = this.dataModel.length - 1;
this.savePreference_();
},
/*
* Gets the language codes of the currently listed languages.
*/
getLanguageCodes: function() {
return this.dataModel.slice();
},
/*
* Gets the language code of the selected language.
*/
getSelectedLanguageCode: function() {
return this.selectedItem;
},
/*
* Selects the language by the given language code.
* @returns {boolean} True if the operation is successful.
*/
selectLanguageByCode: function(languageCode) {
var index = this.dataModel.indexOf(languageCode);
if (index >= 0) {
this.selectionModel.selectedIndex = index;
return true;
}
return false;
},
/*
* Removes the currently selected language.
*/
removeSelectedLanguage: function() {
if (this.selectionModel.selectedIndex >= 0) {
this.dataModel.splice(this.selectionModel.selectedIndex, 1);
// Once the selected item is removed, there will be no selected item.
// Select the item pointed by the lead index.
this.selectionModel.selectedIndex = this.selectionModel.leadIndex;
this.savePreference_();
}
},
/*
* Handles the dragstart event.
* @param {Event} e The dragstart event.
* @private
*/
handleDragStart_: function(e) {
var target = e.target;
// ListItem should be the only draggable element type in the page,
// but just in case.
if (target instanceof ListItem) {
this.draggedItem = target;
e.dataTransfer.effectAllowed = 'move';
}
},
/*
* Handles the dragenter event.
* @param {Event} e The dragenter event.
* @private
*/
handleDragEnter_: function(e) {
e.preventDefault();
},
/*
* Handles the dragover event.
* @param {Event} e The dragover event.
* @private
*/
handleDragOver_: function(e) {
var dropTarget = e.target;
// Determins whether the drop target is to accept the drop.
// The drop is only successful on another ListItem.
if (!(dropTarget instanceof ListItem) ||
dropTarget == this.draggedItem) {
return;
}
// Compute the drop postion. Should we move the dragged item to
// below or above the drop target?
var rect = dropTarget.getBoundingClientRect();
var dy = e.clientY - rect.top;
var yRatio = dy / rect.height;
var dropPos = yRatio <= .5 ? 'above' : 'below';
this.dropPos = dropPos;
e.preventDefault();
// TODO(satorux): Show the drop marker just like the bookmark manager.
},
/*
* Handles the drop event.
* @param {Event} e The drop event.
* @private
*/
handleDrop_: function(e) {
var dropTarget = e.target;
// Delete the language from the original position.
var languageCode = this.draggedItem.languageCode;
var originalIndex = this.dataModel.indexOf(languageCode);
this.dataModel.splice(originalIndex, 1);
// Insert the language to the new position.
var newIndex = this.dataModel.indexOf(dropTarget.languageCode);
if (this.dropPos == 'below')
newIndex += 1;
this.dataModel.splice(newIndex, 0, languageCode);
// The cursor should move to the moved item.
this.selectionModel.selectedIndex = newIndex;
// Save the preference.
this.savePreference_();
},
/**
* Handles preferred languages pref change.
* @param {Event} e The change event object.
* @private
*/
handlePreferredLanguagesPrefChange_: function(e) {
var languageCodesInCsv = e.value.value;
var languageCodes = this.filterBadLanguageCodes_(
languageCodesInCsv.split(','));
this.load_(languageCodes);
},
/**
* Loads given language list.
* @param {Array} languageCodes List of language codes.
* @private
*/
load_: function(languageCodes) {
// Preserve the original selected index. See comments below.
var originalSelectedIndex = (this.selectionModel ?
this.selectionModel.selectedIndex : -1);
this.dataModel = new ArrayDataModel(languageCodes);
if (originalSelectedIndex >= 0 &&
originalSelectedIndex < this.dataModel.length) {
// Restore the original selected index if the selected index is
// valid after the data model is loaded. This is neeeded to keep
// the selected language after the languge is added or removed.
this.selectionModel.selectedIndex = originalSelectedIndex;
// The lead index should be updated too.
this.selectionModel.leadIndex = originalSelectedIndex;
} else if (this.dataModel.length > 0){
// Otherwise, select the first item if it's not empty.
// Note that ListSingleSelectionModel won't select an item
// automatically, hence we manually select the first item here.
this.selectionModel.selectedIndex = 0;
}
},
/**
* Saves the preference.
*/
savePreference_: function() {
// Encode the language codes into a CSV string.
Preferences.setStringPref(this.preferredLanguagesPref,
this.dataModel.slice().join(','));
// Save the same language list as accept languages preference as
// well, but we need to expand the language list, to make it more
// acceptable. For instance, some web sites don't understand 'en-US'
// but 'en'. See crosbug.com/9884.
var acceptLanguages = this.expandLanguageCodes(this.dataModel.slice());
Preferences.setStringPref(this.acceptLanguagesPref,
acceptLanguages.join(','));
cr.dispatchSimpleEvent(this, 'save');
},
/**
* Expands language codes to make these more suitable for Accept-Language.
* Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA'].
* 'en' won't appear twice as this function eliminates duplicates.
* @param {Array} languageCodes List of language codes.
* @private
*/
expandLanguageCodes: function(languageCodes) {
var expandedLanguageCodes = [];
var seen = {}; // Used to eliminiate duplicates.
for (var i = 0; i < languageCodes.length; i++) {
var languageCode = languageCodes[i];
if (!(languageCode in seen)) {
expandedLanguageCodes.push(languageCode);
seen[languageCode] = true;
}
var parts = languageCode.split('-');
if (!(parts[0] in seen)) {
expandedLanguageCodes.push(parts[0]);
seen[parts[0]] = true;
}
}
return expandedLanguageCodes;
},
/**
* Filters bad language codes in case bad language codes are
* stored in the preference. Removes duplicates as well.
* @param {Array} languageCodes List of language codes.
* @private
*/
filterBadLanguageCodes_: function(languageCodes) {
var filteredLanguageCodes = [];
var seen = {};
for (var i = 0; i < languageCodes.length; i++) {
// Check if the the language code is valid, and not
// duplicate. Otherwise, skip it.
if (LanguageList.isValidLanguageCode(languageCodes[i]) &&
!(languageCodes[i] in seen)) {
filteredLanguageCodes.push(languageCodes[i]);
seen[languageCodes[i]] = true;
}
}
return filteredLanguageCodes;
},
};
return {
LanguageList: LanguageList
};
});
|