Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 13x 37x 18x 2x 2x 18x 2x 4x 4x 4x | import CustomElement from "../../custom-element/CustomElement";
import mergeStyles from "../../custom-element/helpers/mergeStyles";
import { CustomElementPropertyMetadata } from "../../custom-element/interfaces";
import SizableMixin from "../../custom-element/mixins/components/sizable/SizableMixin";
import ValidatableMixin from "../../custom-element/mixins/components/validatable/ValidatableMixin";
import RequiredValidator from "../../utils/validation/validators/field/RequiredValidator";
import LocalizedText from "../localized-text/LocalizedText";
import styles from "./Field.css";
export const inputEvent = "inputEvent";
export const changeEvent = "changeEvent";
export const fieldAddedEvent = "fieldAddedEvent";
export default abstract class Field extends
SizableMixin(
ValidatableMixin(
CustomElement
)
) {
// The temporary value being validated on input
// Since it is not the final one there is no need to refresh
private _tempValue: any = undefined;
// Marker to mark the field as such so it can be filtered out from other components
isField = true;
static get styles(): string {
return mergeStyles(super.styles, styles);
}
static get properties(): Record<string, CustomElementPropertyMetadata> {
return {
/**
* The name of the field
*/
name: {
type: String,
required: true
},
/**
* The initial value of the field
*/
value: {
type: [String, Object], // Ideally is a string but could be a more complex object
mutable: true,
reflect: true
},
disabled: {
type: Boolean,
mutable: true,
reflect: true
},
required: {
type: Boolean,
inherit: true,
mutable: true,
reflect: true
}
};
}
attributeChangedCallback(attributeName: string, oldValue: string, newValue: string) {
if (attributeName === 'required') {
Iif (newValue !== "false") { // Add a required validator
if (!this.hasRequiredValidator()) {
const {
validators = []
} = this;
this.validators = [...validators, new RequiredValidator()];
}
}
else { // remove any existing required validator
Iif (this.hasRequiredValidator()) {
const {
validators
} = this;
const requiredValidator = validators.filter(v => v instanceof RequiredValidator)[0];
if (requiredValidator !== undefined) {
const index = validators.indexOf(requiredValidator);
validators.splice(index, 1);
this.validators = validators;
}
}
}
}
super.attributeChangedCallback(attributeName, oldValue, newValue);
}
hasRequiredValidator(): boolean {
return this.validators.filter(v => v instanceof RequiredValidator).length > 1;
}
didAdoptChildCallback(parent, child) {
super.didAdoptChildCallback?.(parent, child);
Iif (child !== this) { // Not a field
return;
}
this.dispatchCustomEvent(fieldAddedEvent, {
field: child
});
}
handleBlur(event) {
//this.validate();
}
/**
* Called every time the input changes
* Perform validation to give instantaneous feedback but do not update the current value since it might keep changing
* @param event
* @returns
*/
handleInput(event) {
this._tempValue = this.getNewValue(event.target);
this.validate(); // Validate the field on input
this.dispatchCustomEvent(inputEvent, {
modified: !this.dataField.hasSameInitialValue(this._tempValue)
});
}
createValidationContext() /*: ValidationContext */ {
const label = this.getLabel();
const value = this._tempValue ?? this.value;
return {
label,
value,
warnings: [],
errors: []
};
}
initializeValidator(validator: string) {
switch(validator) {
case 'required': return new RequiredValidator();
default: throw new Error(`initializeValidator is not implemented for validator: '${validator}'`);
}
}
getLabel() : string {
const {
adoptingParent
} = this;
const lt = Array.from(adoptingParent.children).filter(c => c instanceof LocalizedText);
if (lt.length > 0) {
return (lt[0] as LocalizedText).innerHTML;
}
else {
throw new Error('Not implemented');
}
}
handleChange(event): void {
// Reset the temporary value
this._tempValue = undefined;
// Retrieve the new value
const target = event.target as HTMLInputElement;
const oldValue = this.value;
this.value = this.getNewValue(target);
const {
name,
value
} = this;
this.dispatchCustomEvent(changeEvent, {
name,
oldValue,
newValue: value
});
}
getNewValue(input: HTMLInputElement): any {
let value: any;
switch (input.type) {
case 'file':
{
const {
files
} = input;
if (files.length === 0) { // No files selected
return value;
}
if (input.multiple === true) {
value = Array.from(files).map(f => {
return {
name: f.name,
type: f.type,
size: f.size,
content: URL.createObjectURL(f)
};
});
}
else {
const f = files[0];
value = {
name: f.name,
type: f.type,
size: f.size,
content: URL.createObjectURL(f)
};
}
}
break;
default:
{
value = input.value;
}
break;
}
return value;
}
} |