-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvalidation-custom-mixin.js
71 lines (60 loc) · 1.68 KB
/
validation-custom-mixin.js
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
import { isCustomFormElement } from '../form/form-helper.js';
export const ValidationCustomMixin = superclass => class extends superclass {
static get properties() {
return {
failureText: { type: String, attribute: 'failure-text' },
for: { type: String }
};
}
constructor() {
super();
this._forElement = null;
}
get forElement() {
return this._forElement;
}
connectedCallback() {
super.connectedCallback();
this._updateForElement();
this.dispatchEvent(new CustomEvent('d2l-validation-custom-connected', { bubbles: true }));
}
disconnectedCallback() {
super.disconnectedCallback();
if (isCustomFormElement(this._forElement)) {
this._forElement.validationCustomDisconnected(this);
}
this._forElement = null;
this.dispatchEvent(new CustomEvent('d2l-validation-custom-disconnected'));
}
updated(changedProperties) {
super.updated(changedProperties);
changedProperties.forEach((_, prop) => {
if (prop === 'for') {
this._updateForElement();
}
});
}
async validate() {
throw new Error('ValidationCustomMixin requires validate to be overridden');
}
_updateForElement() {
const oldForElement = this._forElement;
if (this.for) {
const root = this.getRootNode();
this._forElement = root.getElementById(this.for);
if (!this._forElement) {
throw new Error(`validation-custom failed to find element with id ${this.for}`);
}
} else {
this._forElement = null;
}
if (this._forElement !== oldForElement) {
if (isCustomFormElement(oldForElement)) {
oldForElement.validationCustomDisconnected(this);
}
if (isCustomFormElement(this._forElement)) {
this._forElement.validationCustomConnected(this);
}
}
}
};