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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 2x 1x 1x 1x 1x 1x 2x | import CustomElement from "../../custom-element/CustomElement";
import defineCustomElement from "../../custom-element/helpers/defineCustomElement";
import SubmitableMixin from "../../custom-element/mixins/data/SubmitableMixin";
import ErrorableMixin from "../../custom-element/mixins/components/errorable/ErrorableMixin";
import LoadableMixin from "../../custom-element/mixins/data/LoadableMixin";
import html from "../../renderer/html";
import DataRecord from "../../utils/data/record/DataRecord";
import Field, { changeEvent, fieldAddedEvent } from "../fields/Field";
import { NodePatchingData } from "../../renderer/NodePatcher";
import ValidatableMixin from "../../custom-element/mixins/components/validatable/ValidatableMixin";
import { ValidationContext } from "../../utils/validation/Interfaces";
import styles from "./Form.css";
import { CustomElementPropertyMetadata } from "../../custom-element/interfaces";
export default class Form extends
SubmitableMixin(
ValidatableMixin(
LoadableMixin(
ErrorableMixin(
CustomElement
)
)
)
) {
private _fields: Map<string, Field> = new Map<string, Field>();
private _record: DataRecord = new DataRecord();
constructor() {
super();
this.handleFieldAdded = this.handleFieldAdded.bind(this);
this.handleChange = this.handleChange.bind(this);
}
static get styles(): string {
return styles as any;
}
static get properties(): Record<string, CustomElementPropertyMetadata> {
return {
/**
* The width of the labels of the form
*/
labelWidth: {
attribute: 'label-width',
type: String,
value: '50%',
},
/**
* Content justification
*/
justifyLabelContent: {
attribute: 'justify-label-content',
type: String,
value: 'space-evenly',
options: ['start', 'center', 'space-around', 'space-between', 'space-evenly'],
reflect: true
}
};
}
render(): NodePatchingData {
const {
labelWidth,
justifyLabelContent
} = this;
return html`<form>
${this.renderLoading()}
${this.renderSubmitting()}
${this.renderError()}
<slot label-width=${labelWidth} justify-label-content=${justifyLabelContent}key="form-fields"></slot>
${this._renderButton()}
</form>`;
}
private _renderButton(): NodePatchingData {
// Doing onClick=${this.submit} binds the button instead of the form to the submit function
return html`<gcl-button key="submit-button" kind="primary" variant="contained" click=${() => this.submit()}>
<gcl-localized-text resource-key="submit">Submit</gcl-localized-text>
<gcl-icon name="box-arrow-right"></gcl-icon>
</gcl-button>`;
}
getSubmitData() {
const data = this._record.getData();
console.log(JSON.stringify(data));
return data;
}
submit() {
if (this.validate()) {
super.submit();
}
}
createValidationContext(): ValidationContext {
return {
warnings: [],
errors: []
}
}
/**
* Handles the data that was loaded from the server
* @param data The data returned by the server
*/
handleLoadedData(data: Record<string, any>) {
console.log(JSON.stringify(data));
const d = data.payload ?? data;
this._record.initialize(d); // Fill the record without seting any modified fields
this._populateFields(d); // Update the form with the returned values
}
/**
* Called when a response from a submission is received from a server
* @param data The data returned by the server
*/
handleSubmitResponse(data: Record<string, any>) {
console.log(JSON.stringify(data));
const d = data.payload ?? data;
this._record.setData(d); // Fill the record without seting any modified fields
this._populateFields(d); // Update the form with the returned values
}
private _populateFields(data: any) {
for (const key in data) {
if (data.hasOwnProperty(key)) {
const field = this._fields.get(key);
if (field !== undefined) {
field.value = data[key];
}
else { // The field does not need to exist for the given data member but let the programmer know it is missing
console.warn(`Field of name: '${key}' was not found for data member with same name`);
}
}
}
}
initializeValidator(validator: string) {
switch (validator) {
default: throw new Error(`initializeValidator is not implemented for validator: '${validator}'`);
}
}
validate(): boolean {
let valid = super.validate();
this._fields.forEach(field => {
const v = field.validate();
if (valid === true) {
valid = v;
}
});
return valid;
}
connectedCallback() {
super.connectedCallback?.();
this.addEventListener(fieldAddedEvent, this.handleFieldAdded);
this.addEventListener(changeEvent, this.handleChange);
}
disconnectedCallback() {
super.disconnectedCallback?.();
this.removeEventListener(fieldAddedEvent, this.handleFieldAdded);
this.removeEventListener(changeEvent, this.handleChange);
}
handleFieldAdded(event: CustomEvent): void {
const {
field
} = event.detail;
const {
name,
type,
value
} = field;
this._fields.set(name, field); // Add the field to the form
const dataField = this._record.addField({ // Add the field to the record
name,
type,
value
});
field.dataField = dataField; // Set the data field in the field so it "knows" if its value changed from the initial one
event.stopPropagation();
}
handleChange(event: CustomEvent): void {
const {
name,
newValue
} = event.detail;
console.log('valueChanged: ' + JSON.stringify(event.detail));
this._record.setData({
[name]: newValue
});
event.stopPropagation();
}
}
defineCustomElement('gcl-form', Form); |