ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components-chromium / iron-behaviors / iron-control-state-extracted.js
blobd84e4eeda12c65b8e070b4578d315059b27a1e72
3   /**
4    * @demo demo/index.html
5    * @polymerBehavior
6    */
7   Polymer.IronControlState = {
9     properties: {
11       /**
12        * If true, the element currently has focus.
13        */
14       focused: {
15         type: Boolean,
16         value: false,
17         notify: true,
18         readOnly: true,
19         reflectToAttribute: true
20       },
22       /**
23        * If true, the user cannot interact with this element.
24        */
25       disabled: {
26         type: Boolean,
27         value: false,
28         notify: true,
29         observer: '_disabledChanged',
30         reflectToAttribute: true
31       },
33       _oldTabIndex: {
34         type: Number
35       },
37       _boundFocusBlurHandler: {
38         type: Function,
39         value: function() {
40           return this._focusBlurHandler.bind(this);
41         }
42       }
44     },
46     observers: [
47       '_changedControlState(focused, disabled)'
48     ],
50     ready: function() {
51       // TODO(sjmiles): ensure read-only property is valued so the compound
52       // observer will fire
53       if (this.focused === undefined) {
54         this._setFocused(false);
55       }
56       this.addEventListener('focus', this._boundFocusBlurHandler, true);
57       this.addEventListener('blur', this._boundFocusBlurHandler, true);
58     },
60     _focusBlurHandler: function(event) {
61       var target = event.path ? event.path[0] : event.target;
62       if (target === this) {
63         var focused = event.type === 'focus';
64         this._setFocused(focused);
65       } else if (!this.shadowRoot) {
66         event.stopPropagation();
67         this.fire(event.type, {sourceEvent: event}, {
68           node: this,
69           bubbles: event.bubbles,
70           cancelable: event.cancelable
71         });
72       }
73     },
75     _disabledChanged: function(disabled, old) {
76       this.setAttribute('aria-disabled', disabled ? 'true' : 'false');
77       this.style.pointerEvents = disabled ? 'none' : '';
78       if (disabled) {
79         this._oldTabIndex = this.tabIndex;
80         this.focused = false;
81         this.tabIndex = -1;
82       } else if (this._oldTabIndex !== undefined) {
83         this.tabIndex = this._oldTabIndex;
84       }
85     },
87     _changedControlState: function() {
88       // _controlStateChanged is abstract, follow-on behaviors may implement it
89       if (this._controlStateChanged) {
90         this._controlStateChanged();
91       }
92     }
94   };