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 * Component that renders a destination item in a destination list.
10 * @param {!cr.EventTarget} eventTarget Event target to dispatch selection
12 * @param {!print_preview.PrintTicketStore} printTicketStore Contains the
13 * print ticket to print.
14 * @param {!Object} capability Capability to render.
16 * @extends {print_preview.Component}
18 function AdvancedSettingsItem(eventTarget, printTicketStore, capability) {
19 print_preview.Component.call(this);
22 * Event target to dispatch selection events to.
23 * @private {!cr.EventTarget}
25 this.eventTarget_ = eventTarget;
28 * Contains the print ticket to print.
29 * @private {!print_preview.PrintTicketStore}
31 this.printTicketStore_ = printTicketStore;
34 * Capability this component renders.
37 this.capability_ = capability;
40 * Value selected by user. {@code null}, if user has not changed the default
41 * value yet (still, the value can be the default one, if it is what user
45 this.selectedValue_ = null;
48 * Active filter query.
54 * Search hint for the control.
55 * @private {print_preview.SearchBubble}
57 this.searchBubble_ = null;
59 /** @private {!EventTracker} */
60 this.tracker_ = new EventTracker();
63 AdvancedSettingsItem.prototype = {
64 __proto__: print_preview.Component.prototype,
67 createDom: function() {
68 this.setElementInternal(this.cloneTemplateInternal(
69 'advanced-settings-item-template'));
72 this.select_, 'change', this.onSelectChange_.bind(this));
73 this.tracker_.add(this.text_, 'input', this.onTextInput_.bind(this));
75 this.initializeValue_();
77 this.renderCapability_();
81 * ID of the corresponding vendor capability.
85 return this.capability_.id;
89 * Currently selected value.
93 return this.selectedValue_ || '';
97 * Whether the corresponding ticket item was changed or not.
100 isModified: function() {
101 return !!this.selectedValue_;
104 /** @param {RegExp} query Query to update the filter with. */
105 updateSearchQuery: function(query) {
107 this.renderCapability_();
110 get searchBubbleShown() {
111 return getIsVisible(this.getElement()) && !!this.searchBubble_;
115 * @return {HTMLSelectElement} Select element.
119 return this.getChildElement(
120 '.advanced-settings-item-value-select-control');
124 * @return {HTMLSelectElement} Text element.
128 return this.getChildElement('.advanced-settings-item-value-text-control');
132 * Called when the select element value is changed.
135 onSelectChange_: function() {
136 this.selectedValue_ = this.select_.value;
137 this.capability_.select_cap.option.some(function(option) {
138 if (this.select_.value == option.value && option.is_default)
139 this.selectedValue_ = null;
140 return this.select_.value == option.value || option.is_default;
145 * Called when the text element value is changed.
148 onTextInput_: function() {
149 this.selectedValue_ = this.text_.value || null;
152 var optionMatches = (this.selectedValue_ || '').match(this.query_);
153 // Even if there's no match anymore, keep the item visible to do not
156 this.showSearchBubble_(optionMatches[0]);
158 this.hideSearchBubble_();
163 * Renders capability properties according to the current state.
166 renderCapability_: function() {
167 var textContent = this.capability_.display_name;
168 var nameMatches = this.query_ ? !!textContent.match(this.query_) : true;
169 var optionMatches = null;
171 if (this.capability_.type == 'SELECT') {
172 this.capability_.select_cap.option.some(function(option) {
173 optionMatches = (option.display_name || '').match(this.query_);
174 return !!optionMatches;
177 optionMatches = (this.text_.value || '').match(this.query_);
180 var matches = nameMatches || optionMatches;
182 if (!matches || !optionMatches)
183 this.hideSearchBubble_();
185 setIsVisible(this.getElement(), matches);
189 var nameEl = this.getChildElement('.advanced-settings-item-label');
191 nameEl.textContent = '';
192 this.addTextWithHighlight_(nameEl, textContent);
194 nameEl.textContent = textContent;
196 nameEl.title = textContent;
199 this.showSearchBubble_(optionMatches[0]);
203 * Shows search bubble for this element.
204 * @param {string} text Text to show in the search bubble.
207 showSearchBubble_: function(text) {
209 this.capability_.type == 'SELECT' ? this.select_ : this.text_;
210 if (!this.searchBubble_) {
211 this.searchBubble_ = new print_preview.SearchBubble(text);
212 this.searchBubble_.attachTo(element);
214 this.searchBubble_.content = text;
219 * Hides search bubble associated with this element.
222 hideSearchBubble_: function() {
223 if (this.searchBubble_) {
224 this.searchBubble_.dispose();
225 this.searchBubble_ = null;
230 * Initializes the element's value control.
233 initializeValue_: function() {
234 this.selectedValue_ =
235 this.printTicketStore_.vendorItems.ticketItems[this.id] || null;
237 if (this.capability_.type == 'SELECT')
238 this.initializeSelectValue_();
240 this.initializeTextValue_();
244 * Initializes the select element.
247 initializeSelectValue_: function() {
249 this.getChildElement('.advanced-settings-item-value-select'), true);
250 var selectEl = this.select_;
251 var indexToSelect = 0;
252 this.capability_.select_cap.option.forEach(function(option, index) {
253 var item = document.createElement('option');
254 item.text = option.display_name;
255 item.value = option.value;
256 if (option.is_default)
257 indexToSelect = index;
258 selectEl.appendChild(item);
260 for (var i = 0, option; option = selectEl.options[i]; i++) {
261 if (option.value == this.selectedValue_) {
266 selectEl.selectedIndex = indexToSelect;
270 * Initializes the text element.
273 initializeTextValue_: function() {
275 this.getChildElement('.advanced-settings-item-value-text'), true);
276 this.text_.value = this.selectedValue;
280 * Adds text to parent element wrapping search query matches in highlighted
282 * @param {!Element} parent Element to build the text in.
283 * @param {string} text The text string to highlight segments in.
286 addTextWithHighlight_: function(parent, text) {
287 text.split(this.query_).forEach(function(section, i) {
289 parent.appendChild(document.createTextNode(section));
291 var span = document.createElement('span');
292 span.className = 'advanced-settings-item-query-highlight';
293 span.textContent = section;
294 parent.appendChild(span);
302 AdvancedSettingsItem: AdvancedSettingsItem