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   * Header-footer ticket item whose value is a {@code boolean} that indicates
10   * whether the document should be printed with headers and footers.
11   * @param {!print_preview.AppState} appState App state used to persist whether
12   *     header-footer is enabled.
13   * @param {!print_preview.DocumentInfo} documentInfo Information about the
14   *     document to print.
15   * @param {!print_preview.ticket_items.MarginsType} marginsType Ticket item
16   *     that stores which predefined margins to print with.
17   * @param {!print_preview.ticket_items.CustomMargins} customMargins Ticket
18   *     item that stores custom margin values.
19   * @constructor
20   * @extends {print_preview.ticket_items.TicketItem}
21   */
22  function HeaderFooter(appState, documentInfo, marginsType, customMargins) {
23    print_preview.ticket_items.TicketItem.call(
24        this,
25        appState,
26        print_preview.AppState.Field.IS_HEADER_FOOTER_ENABLED,
27        null /*destinationStore*/,
28        documentInfo);
29
30    /**
31     * Ticket item that stores which predefined margins to print with.
32     * @type {!print_preview.ticket_items.MarginsType}
33     * @private
34     */
35    this.marginsType_ = marginsType;
36
37    /**
38     * Ticket item that stores custom margin values.
39     * @type {!print_preview.ticket_items.CustomMargins}
40     * @private
41     */
42    this.customMargins_ = customMargins;
43
44    this.addEventListeners_();
45  };
46
47  HeaderFooter.prototype = {
48    __proto__: print_preview.ticket_items.TicketItem.prototype,
49
50    /** @override */
51    wouldValueBeValid: function(value) {
52      return true;
53    },
54
55    /** @override */
56    isCapabilityAvailable: function() {
57      if (!this.getDocumentInfoInternal().isModifiable) {
58        return false;
59      } else if (this.marginsType_.getValue() ==
60          print_preview.ticket_items.MarginsType.Value.NO_MARGINS) {
61        return false;
62      } else if (this.marginsType_.getValue() ==
63          print_preview.ticket_items.MarginsType.Value.MINIMUM) {
64        return true;
65      }
66      var margins;
67      if (this.marginsType_.getValue() ==
68          print_preview.ticket_items.MarginsType.Value.CUSTOM) {
69        if (!this.customMargins_.isValid()) {
70          return false;
71        }
72        margins = this.customMargins_.getValue();
73      } else {
74        margins = this.getDocumentInfoInternal().margins;
75      }
76      var orientEnum = print_preview.ticket_items.CustomMargins.Orientation;
77      return margins == null ||
78             margins.get(orientEnum.TOP) > 0 ||
79             margins.get(orientEnum.BOTTOM) > 0;
80    },
81
82    /** @override */
83    getDefaultValueInternal: function() {
84      return true;
85    },
86
87    /** @override */
88    getCapabilityNotAvailableValueInternal: function() {
89      return false;
90    },
91
92    /**
93     * Adds CHANGE listeners to dependent ticket items.
94     * @private
95     */
96    addEventListeners_: function() {
97      this.getTrackerInternal().add(
98          this.marginsType_,
99          print_preview.ticket_items.TicketItem.EventType.CHANGE,
100          this.dispatchChangeEventInternal.bind(this));
101      this.getTrackerInternal().add(
102          this.customMargins_,
103          print_preview.ticket_items.TicketItem.EventType.CHANGE,
104          this.dispatchChangeEventInternal.bind(this));
105    }
106  };
107
108  // Export
109  return {
110    HeaderFooter: HeaderFooter
111  };
112});
113