Home | History | Annotate | Download | only in data
      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    * Creates a Margins object that holds four margin values in points.
     10    * @param {number} top The top margin in pts.
     11    * @param {number} right The right margin in pts.
     12    * @param {number} bottom The bottom margin in pts.
     13    * @param {number} left The left margin in pts.
     14    * @constructor
     15    */
     16   function Margins(top, right, bottom, left) {
     17     /**
     18      * Backing store for the margin values in points.
     19      * @type {!Object.<
     20      *     !print_preview.ticket_items.CustomMargins.Orientation, number>}
     21      * @private
     22      */
     23     this.value_ = {};
     24     this.value_[print_preview.ticket_items.CustomMargins.Orientation.TOP] = top;
     25     this.value_[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] =
     26         right;
     27     this.value_[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] =
     28         bottom;
     29     this.value_[print_preview.ticket_items.CustomMargins.Orientation.LEFT] =
     30         left;
     31   };
     32 
     33   /**
     34    * Parses a margins object from the given serialized state.
     35    * @param {Object} state Serialized representation of the margins created by
     36    *     the {@code serialize} method.
     37    * @return {!print_preview.Margins} New margins instance.
     38    */
     39   Margins.parse = function(state) {
     40     return new print_preview.Margins(
     41         state[print_preview.ticket_items.CustomMargins.Orientation.TOP] || 0,
     42         state[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] || 0,
     43         state[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] || 0,
     44         state[print_preview.ticket_items.CustomMargins.Orientation.LEFT] || 0);
     45   };
     46 
     47   Margins.prototype = {
     48     /**
     49      * @param {!print_preview.ticket_items.CustomMargins.Orientation}
     50      *     orientation Specifies the margin value to get.
     51      * @return {number} Value of the margin of the given orientation.
     52      */
     53     get: function(orientation) {
     54       return this.value_[orientation];
     55     },
     56 
     57     /**
     58      * @param {!print_preview.ticket_items.CustomMargins.Orientation}
     59      *     orientation Specifies the margin to set.
     60      * @param {number} value Updated value of the margin in points to modify.
     61      * @return {!print_preview.Margins} A new copy of |this| with the
     62      *     modification made to the specified margin.
     63      */
     64     set: function(orientation, value) {
     65       var newValue = this.clone_();
     66       newValue[orientation] = value;
     67       return new Margins(
     68           newValue[print_preview.ticket_items.CustomMargins.Orientation.TOP],
     69           newValue[print_preview.ticket_items.CustomMargins.Orientation.RIGHT],
     70           newValue[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM],
     71           newValue[print_preview.ticket_items.CustomMargins.Orientation.LEFT]);
     72     },
     73 
     74     /**
     75      * @param {print_preview.Margins} other The other margins object to compare
     76      *     against.
     77      * @return {boolean} Whether this margins object is equal to another.
     78      */
     79     equals: function(other) {
     80       if (other == null) {
     81         return false;
     82       }
     83       for (var orientation in this.value_) {
     84         if (this.value_[orientation] != other.value_[orientation]) {
     85           return false;
     86         }
     87       }
     88       return true;
     89     },
     90 
     91     /** @return {Object} A serialized representation of the margins. */
     92     serialize: function() {
     93       return this.clone_();
     94     },
     95 
     96     /**
     97      * @return {Object} Cloned state of the margins.
     98      * @private
     99      */
    100     clone_: function() {
    101       var clone = {};
    102       for (var o in this.value_) {
    103         clone[o] = this.value_[o];
    104       }
    105       return clone;
    106     }
    107   };
    108 
    109   // Export
    110   return {
    111     Margins: Margins
    112   };
    113 });
    114