-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathvalidate-custom-attribute.js
45 lines (44 loc) · 1.53 KB
/
validate-custom-attribute.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
import {inject} from 'aurelia-dependency-injection';
import {customAttribute} from 'aurelia-templating';
@customAttribute('validate')
@inject(Element)
export class ValidateCustomAttribute {
constructor(element) {
this.element = element;
this.processedValidation = null;
this.viewStrategy = null;
}
valueChanged(newValue) {
if (this.value === null || this.value === undefined) {
return;
}
this.processedValidation = this.value;
if (typeof (this.value) !== 'string') {
//binding to a validation instance
this.subscribeChangedHandlers(this.element);
}
return; //this is just to tell the real validation instance (higher in the DOM) the exact property-path to bind to
}
subscribeChangedHandlers(currentElement) {
let viewStrategy = this.value.config.getViewStrategy();
let validationProperty = viewStrategy.getValidationProperty(this.value, currentElement);
let children = currentElement.children;
this.viewStrategy = viewStrategy;
if (validationProperty !== null && validationProperty !== undefined) {
this.viewStrategy.prepareElement(validationProperty, currentElement);
validationProperty.onValidate(
(vp) => {
this.viewStrategy.updateElement(vp, currentElement);
}
);
}
for (let i = 0; i < children.length; i++) {
this.subscribeChangedHandlers(children[i]);
}
}
attached() {
if (this.processedValidation === null || this.processedValidation === undefined) {
this.valueChanged(this.value);
}
}
}