Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / settings / settings_section_select.js
blob0799c94e1c0c200160bd45daf236c0bd71275e45
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() {
6 'use strict';
8 /**
9 * Base class for the printer option element visualizing the generic selection
10 * based option.
11 * @param {!print_preview.ticket_items.TicketItem} ticketItem Ticket item
12 * visualized by this component.
13 * @constructor
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,
26 /** @override */
27 isAvailable: function() {
28 return this.ticketItem_.isCapabilityAvailable();
31 /** @override */
32 hasCollapsibleContent: function() {
33 return this.isAvailable();
36 /** @override */
37 set isEnabled(isEnabled) {
38 this.select_.disabled = !isEnabled;
41 /** @override */
42 enterDocument: function() {
43 print_preview.SettingsSection.prototype.enterDocument.call(this);
44 this.tracker.add(assert(this.select_),
45 'change',
46 this.onSelectChange_.bind(this));
47 this.tracker.add(this.ticketItem_,
48 print_preview.ticket_items.TicketItem.EventType.CHANGE,
49 this.onTicketItemChange_.bind(this));
52 /**
53 * @return {HTMLSelectElement} Select element containing option items.
54 * @private
56 get select_() {
57 return this.getElement().querySelector('.settings-select');
60 /**
61 * Makes sure the content of the select element matches the capabilities of
62 * the destination.
63 * @private
65 updateSelect_: function() {
66 var select = this.select_;
67 if (!this.isAvailable()) {
68 select.innerHTML = '';
69 return;
71 // Should the select content be updated?
72 var sameContent =
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);
76 });
77 var indexToSelect = select.selectedIndex;
78 if (!sameContent) {
79 select.innerHTML = '';
80 this.ticketItem_.capability.option.forEach(function(option, index) {
81 var selectOption = document.createElement('option');
82 selectOption.text = this.getCustomDisplayName_(option) ||
83 this.getDefaultDisplayName_(option);
84 selectOption.value = JSON.stringify(option);
85 select.appendChild(selectOption);
86 if (option.is_default)
87 indexToSelect = index;
88 }, this);
90 // Try to select current ticket item.
91 var valueToSelect = JSON.stringify(this.ticketItem_.getValue());
92 for (var i = 0, option; option = select.options[i]; i++) {
93 if (option.value == valueToSelect) {
94 indexToSelect = i;
95 break;
98 select.selectedIndex = indexToSelect;
99 this.onSelectChange_();
103 * @param {!Object} option Option to get the custom display name for.
104 * @return {string} Custom display name for the option.
105 * @private
107 getCustomDisplayName_: function(option) {
108 var displayName = option.custom_display_name;
109 if (!displayName && option.custom_display_name_localized) {
110 displayName =
111 getStringForCurrentLocale(option.custom_display_name_localized);
113 return displayName;
117 * @param {!Object} option Option to get the default display name for.
118 * @return {string} Default option display name.
119 * @private
121 getDefaultDisplayName_: function(option) {
122 throw Error('Abstract method not overridden');
126 * Called when the select element is changed. Updates the print ticket.
127 * @private
129 onSelectChange_: function() {
130 var select = this.select_;
131 this.ticketItem_.updateValue(
132 JSON.parse(select.options[select.selectedIndex].value));
136 * Called when the print ticket store changes. Selects the corresponding
137 * select option.
138 * @private
140 onTicketItemChange_: function() {
141 this.updateSelect_();
142 this.updateUiStateInternal();
146 // Export
147 return {
148 SettingsSectionSelect: SettingsSectionSelect