Rolling forward https://codereview.chromium.org/1155683008/ which was
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components-chromium / iron-behaviors / iron-control-state-extracted.js
blob5e4ed8e997f3938af333cfd74d64a84eb7f9fbf3
3   /** @polymerBehavior */
5   Polymer.IronControlState = {
7     properties: {
9       /**
10        * If true, the element currently has focus.
11        *
12        * @attribute focused
13        * @type boolean
14        * @default false
15        */
16       focused: {
17         type: Boolean,
18         value: false,
19         notify: true,
20         readOnly: true,
21         reflectToAttribute: true
22       },
24       /**
25        * If true, the user cannot interact with this element.
26        *
27        * @attribute disabled
28        * @type boolean
29        * @default false
30        */
31       disabled: {
32         type: Boolean,
33         value: false,
34         notify: true,
35         observer: '_disabledChanged',
36         reflectToAttribute: true
37       },
39       _oldTabIndex: {
40         type: Number
41       }
42     },
44     observers: [
45       '_changedControlState(focused, disabled)'
46     ],
48     listeners: {
49       focus: '_focusHandler',
50       blur: '_blurHandler'
51     },
53     ready: function() {
54       // TODO(sjmiles): ensure read-only property is valued so the compound
55       // observer will fire
56       if (this.focused === undefined) {
57         this._setFocused(false);
58       }
59     },
61     _focusHandler: function() {
62       this._setFocused(true);
63     },
65     _blurHandler: function() {
66       this._setFocused(false);
67     },
69     _disabledChanged: function(disabled, old) {
70       this.setAttribute('aria-disabled', disabled ? 'true' : 'false');
71       this.style.pointerEvents = disabled ? 'none' : '';
72       if (disabled) {
73         this._oldTabIndex = this.tabIndex;
74         this.focused = false;
75         this.tabIndex = -1;
76       } else if (this._oldTabIndex !== undefined) {
77         this.tabIndex = this._oldTabIndex;
78       }
79     },
81     _changedControlState: function() {
82       // _controlStateChanged is abstract, follow-on behaviors may implement it
83       if (this._controlStateChanged) {
84         this._controlStateChanged();
85       }
86     }
88   };