1 // Copyright 2014 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.
5 cr.define('print_preview', function() {
9 * Base class for the printer option element visualizing the generic selection
11 * @param {!print_preview.ticket_items.TicketItem} ticketItem Ticket item
12 * visualized by this component.
14 * @extends {print_preview.SettingsSection}
16 function SettingsSectionSelect(ticketItem) {
17 print_preview.SettingsSection.call(this);
19 /** @private {!print_preview.ticket_items.TicketItem} */
20 this.ticketItem_ = ticketItem;
23 SettingsSectionSelect.prototype = {
24 __proto__: print_preview.SettingsSection.prototype,
27 isAvailable: function() {
28 return this.ticketItem_.isCapabilityAvailable();
32 hasCollapsibleContent: function() {
33 return this.isAvailable();
37 set isEnabled(isEnabled) {
38 this.select_.disabled = !isEnabled;
42 enterDocument: function() {
43 print_preview.SettingsSection.prototype.enterDocument.call(this);
44 this.tracker.add(assert(this.select_),
46 this.onSelectChange_.bind(this));
47 this.tracker.add(this.ticketItem_,
48 print_preview.ticket_items.TicketItem.EventType.CHANGE,
49 this.onTicketItemChange_.bind(this));
53 * @return {HTMLSelectElement} Select element containing option items.
57 return this.getElement().querySelector('.settings-select');
61 * Makes sure the content of the select element matches the capabilities of
65 updateSelect_: function() {
66 var select = this.select_;
67 if (!this.isAvailable()) {
68 select.innerHTML = '';
71 // Should the select content be updated?
73 this.ticketItem_.capability.option.length == select.length &&
74 this.ticketItem_.capability.option.every(function(option, index) {
75 return select.options[index].value == JSON.stringify(option);
77 var indexToSelect = select.selectedIndex;
79 select.innerHTML = '';
80 this.ticketItem_.capability.option.forEach(function(option, index) {
81 var selectOption = document.createElement('option');
82 var displayName = option.custom_display_name;
83 if (!displayName && option.custom_display_name_localized) {
84 var getLocaleToCompare =
85 /** @type {function(string, boolean=): string} */
86 (function(locale, opt_languageOnly) {
87 var code = opt_languageOnly ? locale.split('-')[0] : locale;
88 return code.toLowerCase();
90 var getItemForLocale = function(items, locale, languageOnly) {
91 locale = getLocaleToCompare(locale, languageOnly);
92 for (var i = 0; i < items.length; i++) {
93 if (getLocaleToCompare(items[i].locale) == locale)
94 return items[i].value;
98 var items = option.custom_display_name_localized;
100 getItemForLocale(items, navigator.language, false) ||
101 getItemForLocale(items, navigator.language, true);
103 selectOption.text = displayName ||
104 this.getDefaultDisplayName_(option);
105 selectOption.value = JSON.stringify(option);
106 select.appendChild(selectOption);
107 if (option.is_default)
108 indexToSelect = index;
111 // Try to select current ticket item.
112 var valueToSelect = JSON.stringify(this.ticketItem_.getValue());
113 for (var i = 0, option; option = select.options[i]; i++) {
114 if (option.value == valueToSelect) {
119 select.selectedIndex = indexToSelect;
120 this.onSelectChange_();
124 * @param {!Object} option Option to get the default display name for.
125 * @return {string} Default option display name.
128 getDefaultDisplayName_: function(option) {
129 throw Error('Abstract method not overridden');
133 * Called when the select element is changed. Updates the print ticket.
136 onSelectChange_: function() {
137 var select = this.select_;
138 this.ticketItem_.updateValue(
139 JSON.parse(select.options[select.selectedIndex].value));
143 * Called when the print ticket store changes. Selects the corresponding
147 onTicketItemChange_: function() {
148 this.updateSelect_();
149 this.updateUiStateInternal();
155 SettingsSectionSelect: SettingsSectionSelect