Home | History | Annotate | Download | only in search
      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.
      4 
      5 cr.define('print_preview', function() {
      6   'use strict';
      7 
      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);
     19 
     20     /**
     21      * Event target to dispatch selection events to.
     22      * @type {!cr.EventTarget}
     23      * @private
     24      */
     25     this.eventTarget_ = eventTarget;
     26 
     27     /**
     28      * Destination that the list item renders.
     29      * @type {!print_preview.Destination}
     30      * @private
     31      */
     32     this.destination_ = destination;
     33 
     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   };
     42 
     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   };
     51 
     52   /**
     53    * CSS classes used by the destination list item.
     54    * @enum {string}
     55    * @private
     56    */
     57   DestinationListItem.Classes_ = {
     58     ICON: 'destination-list-item-icon',
     59     NAME: 'destination-list-item-name',
     60     STALE: 'stale'
     61   };
     62 
     63   DestinationListItem.prototype = {
     64     __proto__: print_preview.Component.prototype,
     65 
     66     /** @override */
     67     createDom: function() {
     68       this.setElementInternal(this.cloneTemplateInternal(
     69           'destination-list-item-template'));
     70 
     71       var iconImg = this.getElement().getElementsByClassName(
     72           print_preview.DestinationListItem.Classes_.ICON)[0];
     73       iconImg.src = this.destination_.iconUrl;
     74 
     75       var nameEl = this.getElement().getElementsByClassName(
     76           DestinationListItem.Classes_.NAME)[0];
     77       nameEl.textContent = this.destination_.displayName;
     78       nameEl.title = this.destination_.displayName;
     79 
     80       this.initializeOfflineStatusElement_();
     81     },
     82 
     83     /** @override */
     84     enterDocument: function() {
     85       print_preview.Component.prototype.enterDocument.call(this);
     86       this.tracker.add(this.getElement(), 'click', this.onActivate_.bind(this));
     87     },
     88 
     89     /**
     90      * Initializes the element which renders the print destination's
     91      * offline status.
     92      * @private
     93      */
     94     initializeOfflineStatusElement_: function() {
     95       if (arrayContains([print_preview.Destination.ConnectionStatus.OFFLINE,
     96                          print_preview.Destination.ConnectionStatus.DORMANT],
     97                         this.destination_.connectionStatus)) {
     98         this.getElement().classList.add(DestinationListItem.Classes_.STALE);
     99         var offlineDurationMs = Date.now() - this.destination_.lastAccessTime;
    100         var offlineMessageId;
    101         if (offlineDurationMs > 31622400000.0) { // One year.
    102           offlineMessageId = 'offlineForYear';
    103         } else if (offlineDurationMs > 2678400000.0) { // One month.
    104           offlineMessageId = 'offlineForMonth';
    105         } else if (offlineDurationMs > 604800000.0) { // One week.
    106           offlineMessageId = 'offlineForWeek';
    107         } else {
    108           offlineMessageId = 'offline';
    109         }
    110         var offlineStatusEl = this.getElement().querySelector(
    111             '.offline-status');
    112         offlineStatusEl.textContent = localStrings.getString(offlineMessageId);
    113         setIsVisible(offlineStatusEl, true);
    114       }
    115     },
    116 
    117     /**
    118      * Called when the destination item is activated. Dispatches a SELECT event
    119      * on the given event target.
    120      * @private
    121      */
    122     onActivate_: function() {
    123       if (this.destination_.id ==
    124               print_preview.Destination.GooglePromotedId.FEDEX &&
    125           !this.destination_.isTosAccepted) {
    126         if (!this.fedexTos_) {
    127           this.fedexTos_ = new print_preview.FedexTos();
    128           this.fedexTos_.render(this.getElement());
    129           this.tracker.add(
    130               this.fedexTos_,
    131               print_preview.FedexTos.EventType.AGREE,
    132               this.onTosAgree_.bind(this));
    133         }
    134         this.fedexTos_.setIsVisible(true);
    135       } else {
    136         var selectEvt = new cr.Event(DestinationListItem.EventType.SELECT);
    137         selectEvt.destination = this.destination_;
    138         this.eventTarget_.dispatchEvent(selectEvt);
    139       }
    140     },
    141 
    142     /**
    143      * Called when the user agrees to the print destination's terms-of-service.
    144      * Selects the print destination that was agreed to.
    145      * @private
    146      */
    147     onTosAgree_: function() {
    148       var selectEvt = new cr.Event(DestinationListItem.EventType.SELECT);
    149       selectEvt.destination = this.destination_;
    150       this.eventTarget_.dispatchEvent(selectEvt);
    151     }
    152   };
    153 
    154   // Export
    155   return {
    156     DestinationListItem: DestinationListItem
    157   };
    158 });
    159