f1a25d6056bf03cc986de64f34de2d3d3f23a50a
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / settings / layout_settings.js
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 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       fadeOutOption(this.getElement(), true);
50       this.tracker.add(
51           this.portraitRadioButton_,
52           'click',
53           this.onLayoutButtonClick_.bind(this));
54       this.tracker.add(
55           this.landscapeRadioButton_,
56           'click',
57           this.onLayoutButtonClick_.bind(this));
58       this.tracker.add(
59           this.landscapeTicketItem_,
60           print_preview.ticket_items.TicketItem.EventType.CHANGE,
61           this.onLandscapeTicketItemChange_.bind(this));
62     },
63
64     /**
65      * @return {HTMLInputElement} The portrait orientation radio button.
66      * @private
67      */
68     get portraitRadioButton_() {
69       return this.getElement().getElementsByClassName(
70           LayoutSettings.Classes_.PORTRAIT_RADIO)[0];
71     },
72
73     /**
74      * @return {HTMLInputElement} The landscape orientation radio button.
75      * @private
76      */
77     get landscapeRadioButton_() {
78       return this.getElement().getElementsByClassName(
79           LayoutSettings.Classes_.LANDSCAPE_RADIO)[0];
80     },
81
82     /**
83      * Called when one of the radio buttons is clicked. Updates the print ticket
84      * store.
85      * @private
86      */
87     onLayoutButtonClick_: function() {
88       this.landscapeTicketItem_.updateValue(this.landscapeRadioButton_.checked);
89     },
90
91     /**
92      * Called when the print ticket store changes state. Updates the state of
93      * the radio buttons and hides the setting if necessary.
94      * @private
95      */
96     onLandscapeTicketItemChange_: function() {
97       if (this.landscapeTicketItem_.isCapabilityAvailable()) {
98         var isLandscapeEnabled = this.landscapeTicketItem_.getValue();
99         this.portraitRadioButton_.checked = !isLandscapeEnabled;
100         this.landscapeRadioButton_.checked = isLandscapeEnabled;
101         fadeInOption(this.getElement());
102       } else {
103         fadeOutOption(this.getElement());
104       }
105     }
106   };
107
108   // Export
109   return {
110     LayoutSettings: LayoutSettings
111   };
112 });