|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2021 Vaadin Ltd. |
| 4 | + * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
| 5 | + */ |
| 6 | +import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js'; |
| 7 | + |
| 8 | +const DelegateStateMixinImplementation = (superclass) => |
| 9 | + class DelegateStateMixinClass extends superclass { |
| 10 | + /** |
| 11 | + * An array of the host attributes to delegate to the target element. |
| 12 | + */ |
| 13 | + static get delegateAttrs() { |
| 14 | + return []; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * An array of the host properties to delegate to the target element. |
| 19 | + */ |
| 20 | + static get delegateProps() { |
| 21 | + return []; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * A target element to which attributes and properties are delegated. |
| 26 | + */ |
| 27 | + get _delegateStateTarget() { |
| 28 | + console.warn(`Please implement the '_delegateStateTarget' property in <${this.localName}>`); |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + /** @protected */ |
| 33 | + ready() { |
| 34 | + super.ready(); |
| 35 | + |
| 36 | + this._createDelegateAttrsObserver(); |
| 37 | + this._ensureAttrsDelegated(); |
| 38 | + |
| 39 | + this._createDelegatePropsObserver(); |
| 40 | + this._ensurePropsDelegated(); |
| 41 | + } |
| 42 | + |
| 43 | + /** @protected */ |
| 44 | + _createDelegateAttrsObserver() { |
| 45 | + this._createMethodObserver(`_delegateAttrsChanged(${this.constructor.delegateAttrs.join(', ')})`); |
| 46 | + } |
| 47 | + |
| 48 | + /** @protected */ |
| 49 | + _createDelegatePropsObserver() { |
| 50 | + this._createMethodObserver(`_delegatePropsChanged(${this.constructor.delegateProps.join(', ')})`); |
| 51 | + } |
| 52 | + |
| 53 | + /** @protected */ |
| 54 | + _ensureAttrsDelegated() { |
| 55 | + this.constructor.delegateAttrs.forEach((name) => { |
| 56 | + this._delegateAttribute(name, this[name]); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** @protected */ |
| 61 | + _ensurePropsDelegated() { |
| 62 | + this.constructor.delegateProps.forEach((name) => { |
| 63 | + this._delegateProperty(name, this[name]); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + /** @protected */ |
| 68 | + _delegateAttrsChanged(...values) { |
| 69 | + this.constructor.delegateAttrs.forEach((name, index) => { |
| 70 | + this._delegateAttribute(name, values[index]); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** @protected */ |
| 75 | + _delegatePropsChanged(...values) { |
| 76 | + this.constructor.delegateProps.forEach((name, index) => { |
| 77 | + this._delegateProperty(name, values[index]); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** @protected */ |
| 82 | + _delegateAttribute(name, value) { |
| 83 | + if (!this._delegateStateTarget) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (name === 'invalid') { |
| 88 | + this._delegateAttribute('aria-invalid', value ? 'true' : false); |
| 89 | + } |
| 90 | + |
| 91 | + if (typeof value === 'boolean') { |
| 92 | + this._delegateStateTarget.toggleAttribute(name, value); |
| 93 | + } else if (value) { |
| 94 | + this._delegateStateTarget.setAttribute(name, value); |
| 95 | + } else { |
| 96 | + this._delegateStateTarget.removeAttribute(name); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** @protected */ |
| 101 | + _delegateProperty(name, value) { |
| 102 | + if (!this._delegateStateTarget) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + this._delegateStateTarget[name] = value; |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | +/** |
| 111 | + * A mixin to delegate properties and attributes to a target element. |
| 112 | + */ |
| 113 | +export const DelegateStateMixin = dedupingMixin(DelegateStateMixinImplementation); |
0 commit comments