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 * Encapsulates all settings and logic related to the media size selection UI.
10 * @param {!print_preview.ticket_items.MediaSize} ticketItem Used to read and
11 * write the media size ticket item.
13 * @extends {print_preview.Component}
15 function MediaSizeSettings(ticketItem) {
16 print_preview.Component.call(this);
18 /** @private {!print_preview.ticket_items.MediaSize} */
19 this.ticketItem_ = ticketItem;
22 MediaSizeSettings.prototype = {
23 __proto__: print_preview.Component.prototype,
25 /** @param {boolean} isEnabled Whether this component is enabled. */
26 set isEnabled(isEnabled) {
27 this.select_.disabled = !isEnabled;
31 enterDocument: function() {
32 print_preview.Component.prototype.enterDocument.call(this);
33 fadeOutOption(this.getElement(), true);
34 this.tracker.add(this.select_, 'change', this.onSelectChange_.bind(this));
37 print_preview.ticket_items.TicketItem.EventType.CHANGE,
38 this.onTicketItemChange_.bind(this));
42 * @return {HTMLSelectElement} Select element containing media size options.
46 return this.getElement().getElementsByClassName(
47 'media-size-settings-select')[0];
51 * Makes sure the content of the select element matches the capabilities of
55 updateSelect_: function() {
56 var select = this.select_;
57 if (!this.ticketItem_.isCapabilityAvailable()) {
58 select.innerHtml = '';
61 // Should the select content be updated?
63 this.ticketItem_.capability.option.length == select.length &&
64 this.ticketItem_.capability.option.every(function(option, index) {
65 return select.options[index].value == JSON.stringify(option);
67 var indexToSelect = select.selectedIndex;
69 select.innerHtml = '';
70 // TODO: Better heuristics for the display name and options grouping.
71 this.ticketItem_.capability.option.forEach(function(option, index) {
72 var selectOption = document.createElement('option');
73 selectOption.text = option.custom_display_name || option.name;
74 selectOption.value = JSON.stringify(option);
75 select.add(selectOption);
76 if (option.is_default) {
77 indexToSelect = index;
81 var valueToSelect = JSON.stringify(this.ticketItem_.getValue());
82 for (var i = 0, option; option = select.options[i]; i++) {
83 if (option.value == valueToSelect) {
89 select.selectedIndex = indexToSelect;
93 * Called when the select element is changed. Updates the print ticket.
96 onSelectChange_: function() {
97 var select = this.select_;
98 var mediaSize = JSON.parse(select.options[select.selectedIndex].value);
99 this.ticketItem_.updateValue(mediaSize);
103 * Called when the print ticket store changes. Selects the corresponding
107 onTicketItemChange_: function() {
108 if (this.ticketItem_.isCapabilityAvailable()) {
109 this.updateSelect_();
110 fadeInOption(this.getElement());
112 fadeOutOption(this.getElement());
119 MediaSizeSettings: MediaSizeSettings