| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Encapsulates all settings and logic related to the media size selection UI. | 9 * Base class for the printer option element visualizing the generic selection |
| 10 * @param {!print_preview.ticket_items.MediaSize} ticketItem Used to read and | 10 * based option. |
| 11 * write the media size ticket item. | 11 * @param {!print_preview.ticket_items.TicketItem} ticketItem Ticket item |
| 12 * visualized by this component. |
| 12 * @constructor | 13 * @constructor |
| 13 * @extends {print_preview.SettingsSection} | 14 * @extends {print_preview.SettingsSection} |
| 14 */ | 15 */ |
| 15 function MediaSizeSettings(ticketItem) { | 16 function SettingsSectionSelect(ticketItem) { |
| 16 print_preview.SettingsSection.call(this); | 17 print_preview.SettingsSection.call(this); |
| 17 | 18 |
| 18 /** @private {!print_preview.ticket_items.MediaSize} */ | 19 /** @private {!print_preview.ticket_items.TicketItem} */ |
| 19 this.ticketItem_ = ticketItem; | 20 this.ticketItem_ = ticketItem; |
| 20 }; | 21 }; |
| 21 | 22 |
| 22 MediaSizeSettings.prototype = { | 23 SettingsSectionSelect.prototype = { |
| 23 __proto__: print_preview.SettingsSection.prototype, | 24 __proto__: print_preview.SettingsSection.prototype, |
| 24 | 25 |
| 25 /** @override */ | 26 /** @override */ |
| 26 isAvailable: function() { | 27 isAvailable: function() { |
| 27 return this.ticketItem_.isCapabilityAvailable(); | 28 return this.ticketItem_.isCapabilityAvailable(); |
| 28 }, | 29 }, |
| 29 | 30 |
| 30 /** @override */ | 31 /** @override */ |
| 31 hasCollapsibleContent: function() { | 32 hasCollapsibleContent: function() { |
| 32 return this.isAvailable(); | 33 return this.isAvailable(); |
| 33 }, | 34 }, |
| 34 | 35 |
| 35 /** @override */ | 36 /** @override */ |
| 36 set isEnabled(isEnabled) { | 37 set isEnabled(isEnabled) { |
| 37 this.select_.disabled = !isEnabled; | 38 this.select_.disabled = !isEnabled; |
| 38 }, | 39 }, |
| 39 | 40 |
| 40 /** @override */ | 41 /** @override */ |
| 41 enterDocument: function() { | 42 enterDocument: function() { |
| 42 print_preview.SettingsSection.prototype.enterDocument.call(this); | 43 print_preview.SettingsSection.prototype.enterDocument.call(this); |
| 43 this.tracker.add(this.select_, 'change', this.onSelectChange_.bind(this)); | 44 this.tracker.add(assert(this.select_), |
| 44 this.tracker.add( | 45 'change', |
| 45 this.ticketItem_, | 46 this.onSelectChange_.bind(this)); |
| 46 print_preview.ticket_items.TicketItem.EventType.CHANGE, | 47 this.tracker.add(this.ticketItem_, |
| 47 this.onTicketItemChange_.bind(this)); | 48 print_preview.ticket_items.TicketItem.EventType.CHANGE, |
| 49 this.onTicketItemChange_.bind(this)); |
| 48 }, | 50 }, |
| 49 | 51 |
| 50 /** | 52 /** |
| 51 * @return {HTMLSelectElement} Select element containing media size options. | 53 * @return {HTMLSelectElement} Select element containing option items. |
| 52 * @private | 54 * @private |
| 53 */ | 55 */ |
| 54 get select_() { | 56 get select_() { |
| 55 return this.getElement().getElementsByClassName( | 57 return this.getElement().querySelector('.settings-select'); |
| 56 'media-size-settings-select')[0]; | |
| 57 }, | 58 }, |
| 58 | 59 |
| 59 /** | 60 /** |
| 60 * Makes sure the content of the select element matches the capabilities of | 61 * Makes sure the content of the select element matches the capabilities of |
| 61 * the destination. | 62 * the destination. |
| 62 * @private | 63 * @private |
| 63 */ | 64 */ |
| 64 updateSelect_: function() { | 65 updateSelect_: function() { |
| 65 var select = this.select_; | 66 var select = this.select_; |
| 66 if (!this.isAvailable()) { | 67 if (!this.isAvailable()) { |
| 67 select.innerHTML = ''; | 68 select.innerHTML = ''; |
| 68 return; | 69 return; |
| 69 } | 70 } |
| 70 // Should the select content be updated? | 71 // Should the select content be updated? |
| 71 var sameContent = | 72 var sameContent = |
| 72 this.ticketItem_.capability.option.length == select.length && | 73 this.ticketItem_.capability.option.length == select.length && |
| 73 this.ticketItem_.capability.option.every(function(option, index) { | 74 this.ticketItem_.capability.option.every(function(option, index) { |
| 74 return select.options[index].value == JSON.stringify(option); | 75 return select.options[index].value == JSON.stringify(option); |
| 75 }); | 76 }); |
| 76 var indexToSelect = select.selectedIndex; | 77 var indexToSelect = select.selectedIndex; |
| 77 if (!sameContent) { | 78 if (!sameContent) { |
| 78 select.innerHTML = ''; | 79 select.innerHTML = ''; |
| 79 // TODO: Better heuristics for the display name and options grouping. | |
| 80 this.ticketItem_.capability.option.forEach(function(option, index) { | 80 this.ticketItem_.capability.option.forEach(function(option, index) { |
| 81 var selectOption = document.createElement('option'); | 81 var selectOption = document.createElement('option'); |
| 82 var displayName = option.custom_display_name; | 82 var displayName = option.custom_display_name; |
| 83 if (!displayName && option.custom_display_name_localized) { | 83 if (!displayName && option.custom_display_name_localized) { |
| 84 var getLocaleToCompare = | 84 var getLocaleToCompare = |
| 85 /** @type {function(string, boolean=): string} */ | 85 /** @type {function(string, boolean=): string} */ |
| 86 (function(locale, opt_languageOnly) { | 86 (function(locale, opt_languageOnly) { |
| 87 var code = opt_languageOnly ? locale.split('-')[0] : locale; | 87 var code = opt_languageOnly ? locale.split('-')[0] : locale; |
| 88 return code.toLowerCase(); | 88 return code.toLowerCase(); |
| 89 }); | 89 }); |
| 90 var getItemForLocale = function(items, locale, languageOnly) { | 90 var getItemForLocale = function(items, locale, languageOnly) { |
| 91 locale = getLocaleToCompare(locale, languageOnly); | 91 locale = getLocaleToCompare(locale, languageOnly); |
| 92 for (var i = 0; i < items.length; i++) { | 92 for (var i = 0; i < items.length; i++) { |
| 93 if (getLocaleToCompare(items[i].locale) == locale) | 93 if (getLocaleToCompare(items[i].locale) == locale) |
| 94 return items[i].value; | 94 return items[i].value; |
| 95 } | 95 } |
| 96 return ''; | 96 return ''; |
| 97 }; | 97 }; |
| 98 var items = option.custom_display_name_localized; | 98 var items = option.custom_display_name_localized; |
| 99 displayName = | 99 displayName = |
| 100 getItemForLocale(items, navigator.language, false) || | 100 getItemForLocale(items, navigator.language, false) || |
| 101 getItemForLocale(items, navigator.language, true); | 101 getItemForLocale(items, navigator.language, true); |
| 102 } | 102 } |
| 103 selectOption.text = displayName || option.name; | 103 selectOption.text = displayName || |
| 104 this.getDefaultDisplayName_(option); |
| 104 selectOption.value = JSON.stringify(option); | 105 selectOption.value = JSON.stringify(option); |
| 105 select.appendChild(selectOption); | 106 select.appendChild(selectOption); |
| 106 if (option.is_default) { | 107 if (option.is_default) |
| 107 indexToSelect = index; | 108 indexToSelect = index; |
| 108 } | |
| 109 }); | 109 }); |
| 110 } | 110 } |
| 111 // Try to select current ticket item. | 111 // Try to select current ticket item. |
| 112 var valueToSelect = JSON.stringify(this.ticketItem_.getValue()); | 112 var valueToSelect = JSON.stringify(this.ticketItem_.getValue()); |
| 113 for (var i = 0, option; option = select.options[i]; i++) { | 113 for (var i = 0, option; option = select.options[i]; i++) { |
| 114 if (option.value == valueToSelect) { | 114 if (option.value == valueToSelect) { |
| 115 indexToSelect = i; | 115 indexToSelect = i; |
| 116 break; | 116 break; |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 select.selectedIndex = indexToSelect; | 119 select.selectedIndex = indexToSelect; |
| 120 this.onSelectChange_(); | 120 this.onSelectChange_(); |
| 121 }, | 121 }, |
| 122 | 122 |
| 123 /** | 123 /** |
| 124 * @param {!Object} option Option to get the default display name for. |
| 125 * @return {string} Default option display name. |
| 126 * @private |
| 127 */ |
| 128 getDefaultDisplayName_: function(option) { |
| 129 throw Error('Abstract method not overridden'); |
| 130 }, |
| 131 |
| 132 /** |
| 124 * Called when the select element is changed. Updates the print ticket. | 133 * Called when the select element is changed. Updates the print ticket. |
| 125 * @private | 134 * @private |
| 126 */ | 135 */ |
| 127 onSelectChange_: function() { | 136 onSelectChange_: function() { |
| 128 var select = this.select_; | 137 var select = this.select_; |
| 129 var mediaSize = JSON.parse(select.options[select.selectedIndex].value); | 138 this.ticketItem_.updateValue( |
| 130 this.ticketItem_.updateValue(mediaSize); | 139 JSON.parse(select.options[select.selectedIndex].value)); |
| 131 }, | 140 }, |
| 132 | 141 |
| 133 /** | 142 /** |
| 134 * Called when the print ticket store changes. Selects the corresponding | 143 * Called when the print ticket store changes. Selects the corresponding |
| 135 * select option. | 144 * select option. |
| 136 * @private | 145 * @private |
| 137 */ | 146 */ |
| 138 onTicketItemChange_: function() { | 147 onTicketItemChange_: function() { |
| 139 this.updateSelect_(); | 148 this.updateSelect_(); |
| 140 this.updateUiStateInternal(); | 149 this.updateUiStateInternal(); |
| 141 } | 150 } |
| 142 }; | 151 }; |
| 143 | 152 |
| 144 // Export | 153 // Export |
| 145 return { | 154 return { |
| 146 MediaSizeSettings: MediaSizeSettings | 155 SettingsSectionSelect: SettingsSectionSelect |
| 147 }; | 156 }; |
| 148 }); | 157 }); |
| OLD | NEW |