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 * Modal dialog for print destination's advanced settings.
10 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the
11 * print ticket to print.
13 * @extends {print_preview.Overlay}
15 function AdvancedSettings(printTicketStore) {
16 print_preview.Overlay.call(this);
19 * Contains the print ticket to print.
20 * @private {!print_preview.PrintTicketStore}
22 this.printTicketStore_ = printTicketStore;
25 * Used to record usage statistics.
26 * @private {!print_preview.PrintSettingsUiMetricsContext}
28 this.metrics_ = new print_preview.PrintSettingsUiMetricsContext();
30 /** @private {!print_preview.SearchBox} */
31 this.searchBox_ = new print_preview.SearchBox(
32 loadTimeData.getString('advancedSettingsSearchBoxPlaceholder'));
33 this.addChild(this.searchBox_);
35 /** @private {print_preview.Destination} */
36 this.destination_ = null;
38 /** @private {!Array<!print_preview.AdvancedSettingsItem>} */
43 * CSS classes used by the component.
47 AdvancedSettings.Classes_ = {
48 EXTRA_PADDING: 'advanced-settings-item-extra-padding'
51 AdvancedSettings.prototype = {
52 __proto__: print_preview.Overlay.prototype,
55 * @param {!print_preview.Destination} destination Destination to show
56 * advanced settings for.
58 showForDestination: function(destination) {
59 assert(!this.destination_);
60 this.destination_ = destination;
61 this.getChildElement('.advanced-settings-title').textContent =
62 loadTimeData.getStringF('advancedSettingsDialogTitle',
63 this.destination_.displayName);
64 this.setIsVisible(true);
65 this.renderSettings_();
69 enterDocument: function() {
70 print_preview.Overlay.prototype.enterDocument.call(this);
73 this.getChildElement('.button-strip .cancel-button'),
75 this.cancel.bind(this));
78 this.getChildElement('.button-strip .done-button'),
80 this.onApplySettings_.bind(this));
84 print_preview.SearchBox.EventType.SEARCH,
85 this.onSearch_.bind(this));
89 decorateInternal: function() {
90 this.searchBox_.render(this.getChildElement('.search-box-area'));
94 onSetVisibleInternal: function(isVisible) {
96 this.searchBox_.focus();
97 this.metrics_.record(print_preview.Metrics.PrintSettingsUiBucket.
98 ADVANCED_SETTINGS_DIALOG_SHOWN);
101 this.destination_ = null;
106 onCancelInternal: function() {
107 this.metrics_.record(print_preview.Metrics.PrintSettingsUiBucket.
108 ADVANCED_SETTINGS_DIALOG_CANCELED);
112 onEnterPressedInternal: function() {
113 var doneButton = this.getChildElement('.button-strip .done-button');
114 if (!doneButton.disabled)
116 return !doneButton.disabled;
120 * @return {number} Height available for settings, in pixels.
123 getAvailableContentHeight_: function() {
124 var elStyle = window.getComputedStyle(this.getElement());
125 return this.getElement().offsetHeight -
126 parseInt(elStyle.getPropertyValue('padding-top'), 10) -
127 parseInt(elStyle.getPropertyValue('padding-bottom'), 10) -
128 this.getChildElement('.settings-area').offsetTop -
129 this.getChildElement('.action-area').offsetHeight;
133 * Filters displayed settings with the given query.
134 * @param {?string} query Query to filter settings by.
137 filterLists_: function(query) {
138 var atLeastOneMatch = false;
139 var lastVisibleItemWithBubble = null;
140 this.items_.forEach(function(item) {
141 item.updateSearchQuery(query);
142 if (getIsVisible(item.getElement()))
143 atLeastOneMatch = true;
144 if (item.searchBubbleShown)
145 lastVisibleItemWithBubble = item;
148 this.getChildElement('.no-settings-match-hint'), !atLeastOneMatch);
150 this.getChildElement('.' + AdvancedSettings.Classes_.EXTRA_PADDING),
151 !!lastVisibleItemWithBubble);
155 * Resets the filter query.
158 resetSearch_: function() {
159 this.searchBox_.setQuery(null);
160 this.filterLists_(null);
164 * Renders all of the available settings.
167 renderSettings_: function() {
168 // Remove all children settings elements.
169 this.items_.forEach(function(item) {
170 this.removeChild(item);
175 this.getChildElement('.' + AdvancedSettings.Classes_.EXTRA_PADDING);
177 extraPadding.parentNode.removeChild(extraPadding);
179 var vendorCapabilities = this.printTicketStore_.vendorItems.capability;
180 if (!vendorCapabilities)
183 var availableHeight = this.getAvailableContentHeight_();
184 var containerEl = this.getChildElement('.settings-area');
185 containerEl.style.maxHeight = availableHeight + 'px';
186 var settingsEl = this.getChildElement('.settings');
188 vendorCapabilities.forEach(function(capability) {
189 var item = new print_preview.AdvancedSettingsItem(
190 this.eventTarget_, this.printTicketStore_, capability);
192 item.render(settingsEl);
193 this.items_.push(item);
196 extraPadding = document.createElement('div');
197 extraPadding.classList.add(AdvancedSettings.Classes_.EXTRA_PADDING);
198 extraPadding.hidden = true;
199 settingsEl.appendChild(extraPadding);
203 * Called when settings search query changes. Filters displayed settings
204 * with the given query.
205 * @param {Event} evt Contains search query.
208 onSearch_: function(evt) {
209 this.filterLists_(evt.queryRegExp);
213 * Called when current settings selection need to be stored in the ticket.
216 onApplySettings_: function(evt) {
217 this.setIsVisible(false);
220 this.items_.forEach(function(item) {
221 if (item.isModified())
222 values[item.id] = item.selectedValue;
225 this.printTicketStore_.vendorItems.updateValue(values);
231 AdvancedSettings: AdvancedSettings