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', function() {
6  'use strict';
7
8  /**
9   * Creates a LayoutSettings object. This object encapsulates all settings and
10   * logic related to layout mode (portrait/landscape).
11   * @param {!print_preview.ticket_items.Landscape} landscapeTicketItem Used to
12   *     get the layout written to the print ticket.
13   * @constructor
14   * @extends {print_preview.Component}
15   */
16  function LayoutSettings(landscapeTicketItem) {
17    print_preview.Component.call(this);
18
19    /**
20     * Used to get the layout written to the print ticket.
21     * @type {!print_preview.ticket_items.Landscape}
22     * @private
23     */
24    this.landscapeTicketItem_ = landscapeTicketItem;
25  };
26
27  /**
28   * CSS classes used by the layout settings.
29   * @enum {string}
30   * @private
31   */
32  LayoutSettings.Classes_ = {
33    LANDSCAPE_RADIO: 'layout-settings-landscape-radio',
34    PORTRAIT_RADIO: 'layout-settings-portrait-radio'
35  };
36
37  LayoutSettings.prototype = {
38    __proto__: print_preview.Component.prototype,
39
40    /** @param {boolean} isEnabled Whether this component is enabled. */
41    set isEnabled(isEnabled) {
42      this.landscapeRadioButton_.disabled = !isEnabled;
43      this.portraitRadioButton_.disabled = !isEnabled;
44    },
45
46    /** @override */
47    enterDocument: function() {
48      print_preview.Component.prototype.enterDocument.call(this);
49      this.tracker.add(
50          this.portraitRadioButton_,
51          'click',
52          this.onLayoutButtonClick_.bind(this));
53      this.tracker.add(
54          this.landscapeRadioButton_,
55          'click',
56          this.onLayoutButtonClick_.bind(this));
57      this.tracker.add(
58          this.landscapeTicketItem_,
59          print_preview.ticket_items.TicketItem.EventType.CHANGE,
60          this.onLandscapeTicketItemChange_.bind(this));
61    },
62
63    /**
64     * @return {HTMLInputElement} The portrait orientation radio button.
65     * @private
66     */
67    get portraitRadioButton_() {
68      return this.getElement().getElementsByClassName(
69          LayoutSettings.Classes_.PORTRAIT_RADIO)[0];
70    },
71
72    /**
73     * @return {HTMLInputElement} The landscape orientation radio button.
74     * @private
75     */
76    get landscapeRadioButton_() {
77      return this.getElement().getElementsByClassName(
78          LayoutSettings.Classes_.LANDSCAPE_RADIO)[0];
79    },
80
81    /**
82     * Called when one of the radio buttons is clicked. Updates the print ticket
83     * store.
84     * @private
85     */
86    onLayoutButtonClick_: function() {
87      this.landscapeTicketItem_.updateValue(this.landscapeRadioButton_.checked);
88    },
89
90    /**
91     * Called when the print ticket store changes state. Updates the state of
92     * the radio buttons and hides the setting if necessary.
93     * @private
94     */
95    onLandscapeTicketItemChange_: function() {
96      if (this.landscapeTicketItem_.isCapabilityAvailable()) {
97        var isLandscapeEnabled = this.landscapeTicketItem_.getValue();
98        this.portraitRadioButton_.checked = !isLandscapeEnabled;
99        this.landscapeRadioButton_.checked = isLandscapeEnabled;
100        fadeInOption(this.getElement());
101      } else {
102        fadeOutOption(this.getElement());
103      }
104    }
105  };
106
107  // Export
108  return {
109    LayoutSettings: LayoutSettings
110  };
111});
112