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
5cr.define('print_preview.ticket_items', function() {
6  'use strict';
7
8  /**
9   * Duplex ticket item whose value is a {@code boolean} that indicates whether
10   * the document should be duplex printed.
11   * @param {!print_preview.AppState} appState App state used to persist collate
12   *     selection.
13   * @param {!print_preview.DestinationStore} destinationStore Destination store
14   *     used determine if a destination has the collate capability.
15   * @constructor
16   * @extends {print_preview.ticket_items.TicketItem}
17   */
18  function Duplex(appState, destinationStore) {
19    print_preview.ticket_items.TicketItem.call(
20        this,
21        appState,
22        print_preview.AppState.Field.IS_DUPLEX_ENABLED,
23        destinationStore);
24  };
25
26  Duplex.prototype = {
27    __proto__: print_preview.ticket_items.TicketItem.prototype,
28
29    /** @override */
30    wouldValueBeValid: function(value) {
31      return true;
32    },
33
34    /** @override */
35    isCapabilityAvailable: function() {
36      var cap = this.getDuplexCapability_();
37      if (!cap) {
38        return false;
39      }
40      var hasLongEdgeOption = false;
41      var hasSimplexOption = false;
42      cap.option.forEach(function(option) {
43        hasLongEdgeOption = hasLongEdgeOption || option.type == 'LONG_EDGE';
44        hasSimplexOption = hasSimplexOption || option.type == 'NO_DUPLEX';
45      });
46      return hasLongEdgeOption && hasSimplexOption;
47    },
48
49    /** @override */
50    getDefaultValueInternal: function() {
51      var cap = this.getDuplexCapability_();
52      var defaultOptions = cap.option.filter(function(option) {
53        return option.is_default;
54      });
55      return defaultOptions.length == 0 ? false :
56                                          defaultOptions[0].type == 'LONG_EDGE';
57    },
58
59    /** @override */
60    getCapabilityNotAvailableValueInternal: function() {
61      return false;
62    },
63
64    /**
65     * @return {Object} Duplex capability of the selected destination.
66     * @private
67     */
68    getDuplexCapability_: function() {
69      var dest = this.getSelectedDestInternal();
70      return (dest &&
71              dest.capabilities &&
72              dest.capabilities.printer &&
73              dest.capabilities.printer.duplex) ||
74             null;
75    }
76  };
77
78  // Export
79  return {
80    Duplex: Duplex
81  };
82});
83