Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / search / destination_list_item.js
blob40f228e048d9ba0018d713bfe958305e366a61f0
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() {
6   'use strict';
8   /**
9    * Component that renders a destination item in a destination list.
10    * @param {!cr.EventTarget} eventTarget Event target to dispatch selection
11    *     events to.
12    * @param {!print_preview.Destination} destination Destination data object to
13    *     render.
14    * @constructor
15    * @extends {print_preview.Component}
16    */
17   function DestinationListItem(eventTarget, destination) {
18     print_preview.Component.call(this);
20     /**
21      * Event target to dispatch selection events to.
22      * @type {!cr.EventTarget}
23      * @private
24      */
25     this.eventTarget_ = eventTarget;
27     /**
28      * Destination that the list item renders.
29      * @type {!print_preview.Destination}
30      * @private
31      */
32     this.destination_ = destination;
34     /**
35      * FedEx terms-of-service widget or {@code null} if this list item does not
36      * render the FedEx Office print destination.
37      * @type {print_preview.FedexTos}
38      * @private
39      */
40     this.fedexTos_ = null;
41   };
43   /**
44    * Event types dispatched by the destination list item.
45    * @enum {string}
46    */
47   DestinationListItem.EventType = {
48     // Dispatched when the list item is activated.
49     SELECT: 'print_preview.DestinationListItem.SELECT',
50     REGISTER_PROMO_CLICKED:
51         'print_preview.DestinationListItem.REGISTER_PROMO_CLICKED'
52   };
54   /**
55    * CSS classes used by the destination list item.
56    * @enum {string}
57    * @private
58    */
59   DestinationListItem.Classes_ = {
60     ICON: 'destination-list-item-icon',
61     NAME: 'destination-list-item-name',
62     STALE: 'stale'
63   };
65   DestinationListItem.prototype = {
66     __proto__: print_preview.Component.prototype,
68     /** @override */
69     createDom: function() {
70       this.setElementInternal(this.cloneTemplateInternal(
71           'destination-list-item-template'));
73       var iconImg = this.getElement().getElementsByClassName(
74           print_preview.DestinationListItem.Classes_.ICON)[0];
75       iconImg.src = this.destination_.iconUrl;
77       var nameEl = this.getElement().getElementsByClassName(
78           DestinationListItem.Classes_.NAME)[0];
79       nameEl.textContent = this.destination_.displayName;
80       nameEl.title = this.destination_.displayName;
82       this.initializeOfflineStatusElement_();
83       this.initializeRegistrationPromoElement_();
84     },
86     /** @override */
87     enterDocument: function() {
88       print_preview.Component.prototype.enterDocument.call(this);
89       this.tracker.add(this.getElement(), 'click', this.onActivate_.bind(this));
90     },
92     /**
93      * Initializes the element which renders the print destination's
94      * offline status.
95      * @private
96      */
97     initializeOfflineStatusElement_: function() {
98       if (arrayContains([print_preview.Destination.ConnectionStatus.OFFLINE,
99                          print_preview.Destination.ConnectionStatus.DORMANT],
100                         this.destination_.connectionStatus)) {
101         this.getElement().classList.add(DestinationListItem.Classes_.STALE);
102         var offlineDurationMs = Date.now() - this.destination_.lastAccessTime;
103         var offlineMessageId;
104         if (offlineDurationMs > 31622400000.0) { // One year.
105           offlineMessageId = 'offlineForYear';
106         } else if (offlineDurationMs > 2678400000.0) { // One month.
107           offlineMessageId = 'offlineForMonth';
108         } else if (offlineDurationMs > 604800000.0) { // One week.
109           offlineMessageId = 'offlineForWeek';
110         } else {
111           offlineMessageId = 'offline';
112         }
113         var offlineStatusEl = this.getChildElement('.offline-status');
114         offlineStatusEl.textContent = localStrings.getString(offlineMessageId);
115         setIsVisible(offlineStatusEl, true);
116       }
117     },
119     /**
120      * Initialize registration promo element for Privet unregistered printers.
121      */
122     initializeRegistrationPromoElement_: function() {
123       if (this.destination_.connectionStatus ==
124           print_preview.Destination.ConnectionStatus.UNREGISTERED) {
125         var registerBtnEl = this.getChildElement('.register-promo-button');
126         registerBtnEl.addEventListener('click',
127                                        this.onRegisterPromoClicked_.bind(this));
129         var registerPromoEl = this.getChildElement('.register-promo');
130         setIsVisible(registerPromoEl, true);
131       }
132     },
134     /**
135      * Called when the destination item is activated. Dispatches a SELECT event
136      * on the given event target.
137      * @private
138      */
139     onActivate_: function() {
140       if (this.destination_.id ==
141               print_preview.Destination.GooglePromotedId.FEDEX &&
142           !this.destination_.isTosAccepted) {
143         if (!this.fedexTos_) {
144           this.fedexTos_ = new print_preview.FedexTos();
145           this.fedexTos_.render(this.getElement());
146           this.tracker.add(
147               this.fedexTos_,
148               print_preview.FedexTos.EventType.AGREE,
149               this.onTosAgree_.bind(this));
150         }
151         this.fedexTos_.setIsVisible(true);
152       } else if (this.destination_.connectionStatus !=
153                      print_preview.Destination.ConnectionStatus.UNREGISTERED) {
154         var selectEvt = new Event(DestinationListItem.EventType.SELECT);
155         selectEvt.destination = this.destination_;
156         this.eventTarget_.dispatchEvent(selectEvt);
157       }
158     },
160     /**
161      * Called when the user agrees to the print destination's terms-of-service.
162      * Selects the print destination that was agreed to.
163      * @private
164      */
165     onTosAgree_: function() {
166       var selectEvt = new Event(DestinationListItem.EventType.SELECT);
167       selectEvt.destination = this.destination_;
168       this.eventTarget_.dispatchEvent(selectEvt);
169     },
171     /**
172      * Called when the registration promo is clicked.
173      * @private
174      */
175     onRegisterPromoClicked_: function() {
176       var promoClickedEvent = new Event(
177           DestinationListItem.EventType.REGISTER_PROMO_CLICKED);
178       promoClickedEvent.destination = this.destination_;
179       this.eventTarget_.dispatchEvent(promoClickedEvent);
180     }
181   };
183   // Export
184   return {
185     DestinationListItem: DestinationListItem
186   };