|
| 1 | +import { dedupeMixin } from '@lion/core'; |
| 2 | +import { formRegistrarManager } from './formRegistrarManager.js'; |
| 3 | +import { FormRegisteringMixin } from './FormRegisteringMixin.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * This allows an element to become the manager of a register |
| 7 | + */ |
| 8 | +export const FormRegistrarMixin = dedupeMixin( |
| 9 | + superclass => |
| 10 | + // eslint-disable-next-line no-shadow, no-unused-vars |
| 11 | + class FormRegistrarMixin extends FormRegisteringMixin(superclass) { |
| 12 | + get formElements() { |
| 13 | + return this.__formElements; |
| 14 | + } |
| 15 | + |
| 16 | + set formElements(value) { |
| 17 | + this.__formElements = value; |
| 18 | + } |
| 19 | + |
| 20 | + get formElementsArray() { |
| 21 | + return this.__formElements; |
| 22 | + } |
| 23 | + |
| 24 | + constructor() { |
| 25 | + super(); |
| 26 | + this.formElements = []; |
| 27 | + this.__readyForRegistration = false; |
| 28 | + this.registrationReady = new Promise(resolve => { |
| 29 | + this.__resolveRegistrationReady = resolve; |
| 30 | + }); |
| 31 | + formRegistrarManager.add(this); |
| 32 | + |
| 33 | + this._onRequestToAddFormElement = this._onRequestToAddFormElement.bind(this); |
| 34 | + this.addEventListener('form-element-register', this._onRequestToAddFormElement); |
| 35 | + } |
| 36 | + |
| 37 | + isRegisteredFormElement(el) { |
| 38 | + return this.formElementsArray.some(exitingEl => exitingEl === el); |
| 39 | + } |
| 40 | + |
| 41 | + firstUpdated(changedProperties) { |
| 42 | + super.firstUpdated(changedProperties); |
| 43 | + this.__resolveRegistrationReady(); |
| 44 | + this.__readyForRegistration = true; |
| 45 | + formRegistrarManager.becomesReady(this); |
| 46 | + } |
| 47 | + |
| 48 | + addFormElement(child) { |
| 49 | + // This is a way to let the child element (a lion-fieldset or lion-field) know, about its parent |
| 50 | + // eslint-disable-next-line no-param-reassign |
| 51 | + child.__parentFormGroup = this; |
| 52 | + |
| 53 | + this.formElements.push(child); |
| 54 | + } |
| 55 | + |
| 56 | + removeFormElement(child) { |
| 57 | + const index = this.formElements.indexOf(child); |
| 58 | + if (index > -1) { |
| 59 | + this.formElements.splice(index, 1); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + _onRequestToAddFormElement(ev) { |
| 64 | + const child = ev.detail.element; |
| 65 | + if (child === this) { |
| 66 | + // as we fire and listen - don't add ourselves |
| 67 | + return; |
| 68 | + } |
| 69 | + if (this.isRegisteredFormElement(child)) { |
| 70 | + // do not readd already existing elements |
| 71 | + return; |
| 72 | + } |
| 73 | + ev.stopPropagation(); |
| 74 | + this.addFormElement(child); |
| 75 | + } |
| 76 | + |
| 77 | + _onRequestToRemoveFormElement(ev) { |
| 78 | + const child = ev.detail.element; |
| 79 | + if (child === this) { |
| 80 | + // as we fire and listen - don't add ourselves |
| 81 | + return; |
| 82 | + } |
| 83 | + if (!this.isRegisteredFormElement(child)) { |
| 84 | + // do not readd already existing elements |
| 85 | + return; |
| 86 | + } |
| 87 | + ev.stopPropagation(); |
| 88 | + |
| 89 | + this.removeFormElement(child); |
| 90 | + } |
| 91 | + }, |
| 92 | +); |
0 commit comments