summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/settings/controls/settings_boolean_control_behavior.js
blob: b69ab4e25930f178318ec917da1e01fa993cc87e (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
// Copyright 2016 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.

/**
 * @fileoverview
 * A behavior to help controls that handle a boolean preference, such as
 * checkbox and toggle button.
 */

/** @polymerBehavior SettingsBooleanControlBehavior */
const SettingsBooleanControlBehaviorImpl = {
  properties: {
    /** Whether the control should represent the inverted value. */
    inverted: {
      type: Boolean,
      value: false,
    },

    /** Whether the control is checked. */
    checked: {
      type: Boolean,
      value: false,
      notify: true,
      reflectToAttribute: true,
    },

    /** Disabled property for the element. */
    disabled: {
      type: Boolean,
      value: false,
      notify: true,
      reflectToAttribute: true,
    },

    /**
     * If true, do not automatically set the preference value. This allows the
     * container to confirm the change first then call either sendPrefChange
     * or resetToPrefValue accordingly.
     */
    noSetPref: {
      type: Boolean,
      value: false,
    },

    /** The main label. */
    label: {
      type: String,
      value: '',
    },

    /** Additional (optional) sub-label. */
    subLabel: {
      type: String,
      value: '',
    },

    /**
     * For numeric prefs only, the integer value equivalent to the unchecked
     * state. This is the value sent to prefs if the user unchecks the control.
     * During initialization, the control is unchecked if and only if the pref
     * value is equal to the this value. (Values 2, 3, 4, etc. all are checked.)
     */
    numericUncheckedValue: {
      type: Number,
      value: 0,
    }
  },

  observers: [
    'prefValueChanged_(pref.value)',
  ],

  notifyChangedByUserInteraction: function() {
    this.fire('settings-boolean-control-change');

    if (!this.pref || this.noSetPref) {
      return;
    }
    this.sendPrefChange();
  },

  /** Reset the checked state to match the current pref value. */
  resetToPrefValue: function() {
    this.checked = this.getNewValue_(this.pref.value);
  },

  /** Update the pref to the current |checked| value. */
  sendPrefChange: function() {
    // Ensure that newValue is the correct type for the pref type, either
    // a boolean or a number.
    if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) {
      assert(!this.inverted);
      this.set('pref.value', this.checked ? 1 : this.numericUncheckedValue);
      return;
    }
    this.set('pref.value', this.inverted ? !this.checked : this.checked);
  },

  /**
   * Polymer observer for pref.value.
   * @param {*} prefValue
   * @private
   */
  prefValueChanged_: function(prefValue) {
    this.checked = this.getNewValue_(prefValue);
  },

  /**
   * @param {*} value
   * @return {boolean} The value as a boolean, inverted if |inverted| is true.
   * @private
   */
  getNewValue_: function(value) {
    // For numeric prefs, the control is only false if the value is exactly
    // equal to the unchecked-equivalent value.
    if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) {
      assert(!this.inverted);
      return value != this.numericUncheckedValue;
    }
    return this.inverted ? !value : !!value;
  },

  /**
   * @return {boolean} Whether the control should be disabled.
   * @protected
   */
  controlDisabled: function() {
    return this.disabled || this.isPrefEnforced();
  },
};

/** @polymerBehavior */
const SettingsBooleanControlBehavior = [
  CrPolicyPrefBehavior,
  PrefControlBehavior,
  SettingsBooleanControlBehaviorImpl,
];