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   * Color ticket item whose value is a {@code boolean} that indicates whether
10   * the document should be printed in color.
11   * @param {!print_preview.AppState} appState App state persistence object to
12   *     save the state of the color selection.
13   * @param {!print_preview.DestinationStore} destinationStore Used to determine
14   *     whether color printing should be available.
15   * @constructor
16   * @extends {print_preview.ticket_items.TicketItem}
17   */
18  function Color(appState, destinationStore) {
19    print_preview.ticket_items.TicketItem.call(
20        this,
21        appState,
22        print_preview.AppState.Field.IS_COLOR_ENABLED,
23        destinationStore);
24  };
25
26  /*
27   * @private {!Array.<string>} List of capability types considered color.
28   * @const
29   */
30  Color.COLOR_TYPES_ = ['STANDARD_COLOR', 'CUSTOM_COLOR'];
31
32  /*
33   * @private {!Array.<string>} List of capability types considered monochrome.
34   * @const
35   */
36  Color.MONOCHROME_TYPES_ = ['STANDARD_MONOCHROME', 'CUSTOM_MONOCHROME'];
37
38  Color.prototype = {
39    __proto__: print_preview.ticket_items.TicketItem.prototype,
40
41    /** @override */
42    wouldValueBeValid: function(value) {
43      return true;
44    },
45
46    /** @override */
47    isCapabilityAvailable: function() {
48      var capability = this.capability;
49      if (!capability) {
50        return false;
51      }
52      var hasColor = false;
53      var hasMonochrome = false;
54      capability.option.forEach(function(option) {
55        hasColor = hasColor || (Color.COLOR_TYPES_.indexOf(option.type) >= 0);
56        hasMonochrome = hasMonochrome ||
57            (Color.MONOCHROME_TYPES_.indexOf(option.type) >= 0);
58      });
59      return hasColor && hasMonochrome;
60    },
61
62    /** @return {Object} Color capability of the selected destination. */
63    get capability() {
64      var dest = this.getSelectedDestInternal();
65      return (dest &&
66              dest.capabilities &&
67              dest.capabilities.printer &&
68              dest.capabilities.printer.color) ||
69             null;
70    },
71
72    /** @return {Object} Color option corresponding to the current value. */
73    getSelectedOption: function() {
74      var capability = this.capability;
75      var options = capability ? capability.option : null;
76      if (options) {
77        var typesToLookFor =
78            this.getValue() ? Color.COLOR_TYPES_ : Color.MONOCHROME_TYPES_;
79        for (var i = 0; i < typesToLookFor.length; i++) {
80          var matchingOptions = options.filter(function(option) {
81            return option.type == typesToLookFor[i];
82          });
83          if (matchingOptions.length > 0) {
84            return matchingOptions[0];
85          }
86        }
87      }
88      return null;
89    },
90
91    /** @override */
92    getDefaultValueInternal: function() {
93      var capability = this.capability;
94      var defaultOption = capability ?
95          this.getDefaultColorOption_(capability.option) : null;
96      return defaultOption &&
97          (Color.COLOR_TYPES_.indexOf(defaultOption.type) >= 0);
98    },
99
100    /** @override */
101    getCapabilityNotAvailableValueInternal: function() {
102      // TODO(rltoscano): Get rid of this check based on destination ID. These
103      // destinations should really update their CDDs to have only one color
104      // option that has type 'STANDARD_COLOR'.
105      var dest = this.getSelectedDestInternal();
106      if (dest) {
107        if (dest.id == print_preview.Destination.GooglePromotedId.DOCS ||
108            dest.id == print_preview.Destination.GooglePromotedId.FEDEX ||
109            dest.type == print_preview.Destination.Type.MOBILE) {
110          return true;
111        }
112      }
113      return this.getDefaultValueInternal();
114    },
115
116    /**
117     * @param options {!Array.<!Object.<{type: string=, is_default: boolean=}>>
118     * @return {Object.<{type: string=, is_default: boolean=}>} Default color
119     *     option of the given list.
120     * @private
121     */
122    getDefaultColorOption_: function(options) {
123      var defaultOptions = options.filter(function(option) {
124        return option.is_default;
125      });
126      return (defaultOptions.length == 0) ? null : defaultOptions[0];
127    }
128  };
129
130  // Export
131  return {
132    Color: Color
133  };
134});
135