1 // Copyright (c) 2012 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.Destination} destination Destination data object to
14 * @param {RegExp} query Active filter query.
16 * @extends {print_preview.Component}
18 function DestinationListItem(eventTarget, destination, query) {
19 print_preview.Component.call(this);
22 * Event target to dispatch selection events to.
23 * @type {!cr.EventTarget}
26 this.eventTarget_ = eventTarget;
29 * Destination that the list item renders.
30 * @type {!print_preview.Destination}
33 this.destination_ = destination;
36 * Active filter query text.
43 * FedEx terms-of-service widget or {@code null} if this list item does not
44 * render the FedEx Office print destination.
45 * @type {print_preview.FedexTos}
48 this.fedexTos_ = null;
52 * Event types dispatched by the destination list item.
55 DestinationListItem.EventType = {
56 // Dispatched when the list item is activated.
57 SELECT: 'print_preview.DestinationListItem.SELECT',
58 REGISTER_PROMO_CLICKED:
59 'print_preview.DestinationListItem.REGISTER_PROMO_CLICKED'
62 DestinationListItem.prototype = {
63 __proto__: print_preview.Component.prototype,
66 createDom: function() {
67 this.setElementInternal(this.cloneTemplateInternal(
68 'destination-list-item-template'));
73 enterDocument: function() {
74 print_preview.Component.prototype.enterDocument.call(this);
75 this.tracker.add(this.getElement(), 'click', this.onActivate_.bind(this));
77 this.getElement(), 'keydown', this.onKeyDown_.bind(this));
79 this.getChildElement('.register-promo-button'),
81 this.onRegisterPromoClicked_.bind(this));
84 /** @return {!print_preiew.Destination} */
86 return this.destination_;
90 * Updates the list item UI state.
91 * @param {RegExp} query Active filter query.
93 update: function(query) {
99 * Initializes the element with destination's info.
102 updateUi_: function() {
103 var iconImg = this.getChildElement('.destination-list-item-icon');
104 iconImg.src = this.destination_.iconUrl;
106 var nameEl = this.getChildElement('.destination-list-item-name');
107 var textContent = this.destination_.displayName;
109 nameEl.textContent = '';
110 // When search query is specified, make it obvious why this particular
111 // printer made it to the list. Display name is always visible, even if
112 // it does not match the search query.
113 this.addTextWithHighlight_(nameEl, textContent);
114 // Show the first matching property.
115 this.destination_.extraPropertiesToMatch.some(function(property) {
116 if (property.match(this.query_)) {
117 var hintSpan = document.createElement('span');
118 hintSpan.className = 'search-hint';
119 nameEl.appendChild(hintSpan);
120 this.addTextWithHighlight_(hintSpan, property);
121 // Add the same property to the element title.
122 textContent += ' (' + property + ')';
127 // Show just the display name and nothing else to lessen visual clutter.
128 nameEl.textContent = textContent;
130 nameEl.title = textContent;
132 // Initialize the element which renders the destination's offline status.
133 this.getElement().classList.toggle('stale', this.destination_.isOffline);
134 var offlineStatusEl = this.getChildElement('.offline-status');
135 offlineStatusEl.textContent = this.destination_.offlineStatusText;
136 setIsVisible(offlineStatusEl, this.destination_.isOffline);
138 // Initialize registration promo element for Privet unregistered printers.
140 this.getChildElement('.register-promo'),
141 this.destination_.connectionStatus ==
142 print_preview.Destination.ConnectionStatus.UNREGISTERED);
146 * Adds text to parent element wrapping search query matches in highlighted
148 * @param {!Element} parent Element to build the text in.
149 * @param {string} text The text string to highlight segments in.
152 addTextWithHighlight_: function(parent, text) {
153 var sections = text.split(this.query_);
154 for (var i = 0; i < sections.length; ++i) {
156 parent.appendChild(document.createTextNode(sections[i]));
158 var span = document.createElement('span');
159 span.className = 'destination-list-item-query-highlight';
160 span.textContent = sections[i];
161 parent.appendChild(span);
167 * Called when the destination item is activated. Dispatches a SELECT event
168 * on the given event target.
171 onActivate_: function() {
172 if (this.destination_.id ==
173 print_preview.Destination.GooglePromotedId.FEDEX &&
174 !this.destination_.isTosAccepted) {
175 if (!this.fedexTos_) {
176 this.fedexTos_ = new print_preview.FedexTos();
177 this.fedexTos_.render(this.getElement());
180 print_preview.FedexTos.EventType.AGREE,
181 this.onTosAgree_.bind(this));
183 this.fedexTos_.setIsVisible(true);
184 } else if (this.destination_.connectionStatus !=
185 print_preview.Destination.ConnectionStatus.UNREGISTERED) {
186 var selectEvt = new Event(DestinationListItem.EventType.SELECT);
187 selectEvt.destination = this.destination_;
188 this.eventTarget_.dispatchEvent(selectEvt);
193 * Called when the key is pressed on the destination item. Dispatches a
194 * SELECT event when Enter is pressed.
195 * @param {KeyboardEvent} e Keyboard event to process.
198 onKeyDown_: function(e) {
199 if (!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
200 if (e.keyCode == 13) {
201 var activeElementTag = document.activeElement ?
202 document.activeElement.tagName.toUpperCase() : '';
203 if (activeElementTag == 'LI') {
213 * Called when the user agrees to the print destination's terms-of-service.
214 * Selects the print destination that was agreed to.
217 onTosAgree_: function() {
218 var selectEvt = new Event(DestinationListItem.EventType.SELECT);
219 selectEvt.destination = this.destination_;
220 this.eventTarget_.dispatchEvent(selectEvt);
224 * Called when the registration promo is clicked.
227 onRegisterPromoClicked_: function() {
228 var promoClickedEvent = new Event(
229 DestinationListItem.EventType.REGISTER_PROMO_CLICKED);
230 promoClickedEvent.destination = this.destination_;
231 this.eventTarget_.dispatchEvent(promoClickedEvent);
237 DestinationListItem: DestinationListItem