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
15 * @extends {print_preview.Component}
17 function DestinationListItem(eventTarget, destination) {
18 print_preview.Component.call(this);
21 * Event target to dispatch selection events to.
22 * @type {!cr.EventTarget}
25 this.eventTarget_ = eventTarget;
28 * Destination that the list item renders.
29 * @type {!print_preview.Destination}
32 this.destination_ = destination;
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}
40 this.fedexTos_ = null;
44 * Event types dispatched by the destination list item.
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'
55 * CSS classes used by the destination list item.
59 DestinationListItem.Classes_ = {
60 ICON: 'destination-list-item-icon',
61 NAME: 'destination-list-item-name',
65 DestinationListItem.prototype = {
66 __proto__: print_preview.Component.prototype,
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_();
87 enterDocument: function() {
88 print_preview.Component.prototype.enterDocument.call(this);
89 this.tracker.add(this.getElement(), 'click', this.onActivate_.bind(this));
93 * Initializes the element which renders the print destination's
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';
111 offlineMessageId = 'offline';
113 var offlineStatusEl = this.getChildElement('.offline-status');
114 offlineStatusEl.textContent = localStrings.getString(offlineMessageId);
115 setIsVisible(offlineStatusEl, true);
120 * Initialize registration promo element for Privet unregistered printers.
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);
135 * Called when the destination item is activated. Dispatches a SELECT event
136 * on the given event target.
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());
148 print_preview.FedexTos.EventType.AGREE,
149 this.onTosAgree_.bind(this));
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);
161 * Called when the user agrees to the print destination's terms-of-service.
162 * Selects the print destination that was agreed to.
165 onTosAgree_: function() {
166 var selectEvt = new Event(DestinationListItem.EventType.SELECT);
167 selectEvt.destination = this.destination_;
168 this.eventTarget_.dispatchEvent(selectEvt);
172 * Called when the registration promo is clicked.
175 onRegisterPromoClicked_: function() {
176 var promoClickedEvent = new Event(
177 DestinationListItem.EventType.REGISTER_PROMO_CLICKED);
178 promoClickedEvent.destination = this.destination_;
179 this.eventTarget_.dispatchEvent(promoClickedEvent);
185 DestinationListItem: DestinationListItem