summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/print_preview/data/margins.js
blob: 3facd5a82cda13a84125ac312de47a374d0bf6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('print_preview.ticket_items', function() {
  'use strict';

  /**
   * Enumeration of the orientations of margins.
   * @enum {string}
   */
  const CustomMarginsOrientation = {
    TOP: 'top',
    RIGHT: 'right',
    BOTTOM: 'bottom',
    LEFT: 'left',
  };

  /**
   * Must be kept in sync with the C++ MarginType enum in
   * printing/print_job_constants.h.
   * @enum {number}
   */
  const MarginsTypeValue = {
    DEFAULT: 0,
    NO_MARGINS: 1,
    MINIMUM: 2,
    CUSTOM: 3,
  };

  return {
    CustomMarginsOrientation: CustomMarginsOrientation,
    MarginsTypeValue: MarginsTypeValue,
  };
});

cr.define('print_preview', function() {
  'use strict';

  /**
   * Keep in sync with the C++ kSettingMargin... values in
   * printing/print_job_constants.h.
   * @typedef {{
   *   marginTop: number,
   *   marginRight: number,
   *   marginBottom: number,
   *   marginLeft: number,
   * }}
   */
  let MarginsSetting;

  class Margins {
    /**
     * Creates a Margins object that holds four margin values in points.
     * @param {number} top The top margin in pts.
     * @param {number} right The right margin in pts.
     * @param {number} bottom The bottom margin in pts.
     * @param {number} left The left margin in pts.
     */
    constructor(top, right, bottom, left) {
      /**
       * Backing store for the margin values in points.
       * @type {!Object<
       *     !print_preview.ticket_items.CustomMarginsOrientation, number>}
       * @private
       */
      this.value_ = {};
      this.value_[print_preview.ticket_items.CustomMarginsOrientation.TOP] =
          top;
      this.value_[print_preview.ticket_items.CustomMarginsOrientation.RIGHT] =
          right;
      this.value_[print_preview.ticket_items.CustomMarginsOrientation.BOTTOM] =
          bottom;
      this.value_[print_preview.ticket_items.CustomMarginsOrientation.LEFT] =
          left;
    }

    /**
     * Parses a margins object from the given serialized state.
     * @param {Object} state Serialized representation of the margins created by
     *     the {@code serialize} method.
     * @return {!print_preview.Margins} New margins instance.
     */
    static parse(state) {
      return new print_preview.Margins(
          state[print_preview.ticket_items.CustomMarginsOrientation.TOP] || 0,
          state[print_preview.ticket_items.CustomMarginsOrientation.RIGHT] || 0,
          state[print_preview.ticket_items.CustomMarginsOrientation.BOTTOM] ||
              0,
          state[print_preview.ticket_items.CustomMarginsOrientation.LEFT] || 0);
    }

    /**
     * @param {!print_preview.ticket_items.CustomMarginsOrientation}
     *     orientation Specifies the margin value to get.
     * @return {number} Value of the margin of the given orientation.
     */
    get(orientation) {
      return this.value_[orientation];
    }

    /**
     * @param {!print_preview.ticket_items.CustomMarginsOrientation}
     *     orientation Specifies the margin to set.
     * @param {number} value Updated value of the margin in points to modify.
     * @return {!print_preview.Margins} A new copy of |this| with the
     *     modification made to the specified margin.
     */
    set(orientation, value) {
      const newValue = this.clone_();
      newValue[orientation] = value;
      return new Margins(
          newValue[print_preview.ticket_items.CustomMarginsOrientation.TOP],
          newValue[print_preview.ticket_items.CustomMarginsOrientation.RIGHT],
          newValue[print_preview.ticket_items.CustomMarginsOrientation.BOTTOM],
          newValue[print_preview.ticket_items.CustomMarginsOrientation.LEFT]);
    }

    /**
     * @param {print_preview.Margins} other The other margins object to compare
     *     against.
     * @return {boolean} Whether this margins object is equal to another.
     */
    equals(other) {
      if (other == null) {
        return false;
      }
      for (const orientation in this.value_) {
        if (this.value_[orientation] != other.value_[orientation]) {
          return false;
        }
      }
      return true;
    }

    /** @return {Object} A serialized representation of the margins. */
    serialize() {
      return this.clone_();
    }

    /**
     * @return {Object} Cloned state of the margins.
     * @private
     */
    clone_() {
      const clone = {};
      for (const o in this.value_) {
        clone[o] = this.value_[o];
      }
      return clone;
    }
  }

  // Export
  return {
    Margins: Margins,
    MarginsSetting: MarginsSetting,
  };
});