@pacem/pacem-scaffolding 1.0.0-abel
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/NOTICE +4 -0
- package/dist/browser/pacem-scaffolding.js +10430 -0
- package/dist/browser/pacem-scaffolding.js.map +1 -0
- package/dist/browser/pacem-scaffolding.min.js +2 -0
- package/dist/browser/pacem-scaffolding.min.js.map +1 -0
- package/dist/bundle/pacem-scaffolding.min.mjs +380 -0
- package/dist/bundle/pacem-scaffolding.mjs +10046 -0
- package/dist/bundle/pacem-scaffolding.mjs.map +7 -0
- package/dist/esm/calendar-picker.js +478 -0
- package/dist/esm/char-counter.js +43 -0
- package/dist/esm/checkbox-list.js +75 -0
- package/dist/esm/checkbox.js +112 -0
- package/dist/esm/childform.js +237 -0
- package/dist/esm/contenteditable.js +326 -0
- package/dist/esm/datetime-picker.js +310 -0
- package/dist/esm/form.js +630 -0
- package/dist/esm/formfield.js +1065 -0
- package/dist/esm/geolocate.js +197 -0
- package/dist/esm/hierarchy.js +509 -0
- package/dist/esm/index-components-scaffolding.js +39 -0
- package/dist/esm/index-components.js +1 -0
- package/dist/esm/index-iife.js +3 -0
- package/dist/esm/index-root.js +2 -0
- package/dist/esm/index-scaffolding.js +3 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/input-chars.js +185 -0
- package/dist/esm/input-color.js +80 -0
- package/dist/esm/input-email.js +32 -0
- package/dist/esm/input-file.js +691 -0
- package/dist/esm/input-hidden.js +25 -0
- package/dist/esm/input-image.js +391 -0
- package/dist/esm/input-number.js +58 -0
- package/dist/esm/input-password.js +71 -0
- package/dist/esm/input-search.js +27 -0
- package/dist/esm/input-tel.js +31 -0
- package/dist/esm/input-text.js +36 -0
- package/dist/esm/input-url.js +32 -0
- package/dist/esm/input.js +215 -0
- package/dist/esm/l10n.js +66 -0
- package/dist/esm/latlng.js +178 -0
- package/dist/esm/namevalue-list.js +139 -0
- package/dist/esm/property-picker.js +84 -0
- package/dist/esm/radio-list.js +70 -0
- package/dist/esm/radio.js +79 -0
- package/dist/esm/select.js +126 -0
- package/dist/esm/slider.js +284 -0
- package/dist/esm/snapshot.js +325 -0
- package/dist/esm/suggest.js +545 -0
- package/dist/esm/tags.js +174 -0
- package/dist/esm/textarea-markdown.js +75 -0
- package/dist/esm/textarea.js +51 -0
- package/dist/esm/types-components.js +611 -0
- package/dist/esm/types.js +8 -0
- package/dist/esm/validators.js +409 -0
- package/package.json +38 -0
- package/typings/index.d.ts +2283 -0
package/dist/esm/form.js
ADDED
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { P, PCSS, CustomElement, Watch, Utils, PropertyConverters, CustomTypedEvent, CommandEventName, Components, Net, Debounce, avoidHandler, PropertyChangeEvent, PropertyChangeEventName, DeferPromise, Logging, CustomElementUtils } from '@pacem/pacem-core';
|
|
8
|
+
import { PacemModelElement } from './types-components';
|
|
9
|
+
import { FieldChangeEventName } from './types';
|
|
10
|
+
export const FormSubmitEventName = 'submit';
|
|
11
|
+
export const FormResetEventName = 'reset';
|
|
12
|
+
export class FormSubmitEvent extends CustomTypedEvent {
|
|
13
|
+
constructor(args) {
|
|
14
|
+
super(FormSubmitEventName, args, { bubbles: true, cancelable: true });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class FormResetEvent extends Event {
|
|
18
|
+
constructor() {
|
|
19
|
+
super(FormResetEventName, { bubbles: true, cancelable: true });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const EMITTABLE_EVENT_TYPES = ['download' /* < dedicated, for the upload custom element */, FieldChangeEventName /* expose */, CommandEventName];
|
|
23
|
+
const SUBMIT_CANCELLATION_TOKEN = false;
|
|
24
|
+
export class FormEventEmitter {
|
|
25
|
+
constructor(_element) {
|
|
26
|
+
this._element = _element;
|
|
27
|
+
this._itemEmitHandler = (e) => {
|
|
28
|
+
// handle the actual event first
|
|
29
|
+
this._element.handle(e);
|
|
30
|
+
if (e.type === CommandEventName) {
|
|
31
|
+
// then command-specific event, name being 'item' + command name in lowercase (e.g. 'itemdelete', 'itemselect', ...)
|
|
32
|
+
const evt = e;
|
|
33
|
+
this._element.handle(new CustomEvent('item' + evt.detail.commandName.toLowerCase(), { detail: evt.detail.commandArgument }));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
start() {
|
|
38
|
+
EMITTABLE_EVENT_TYPES.forEach(t => this._element.addEventListener(t, this._itemEmitHandler, false));
|
|
39
|
+
}
|
|
40
|
+
stop() {
|
|
41
|
+
EMITTABLE_EVENT_TYPES.forEach(t => this._element.removeEventListener(t, this._itemEmitHandler, false));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class PacemFormRelevantElement extends Components.PacemElement {
|
|
45
|
+
viewActivatedCallback() {
|
|
46
|
+
super.viewActivatedCallback();
|
|
47
|
+
this.form = CustomElementUtils.findAncestorOfType(this, PacemFormElement);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
__decorate([
|
|
51
|
+
Watch({ converter: PropertyConverters.Element })
|
|
52
|
+
], PacemFormRelevantElement.prototype, "form", void 0);
|
|
53
|
+
let PacemFormElement = class PacemFormElement extends PacemFormRelevantElement {
|
|
54
|
+
constructor() {
|
|
55
|
+
super('form');
|
|
56
|
+
this.method = Net.HttpMethod.Post;
|
|
57
|
+
this._emitter = null;
|
|
58
|
+
this._buttons = { submit: undefined, reset: undefined };
|
|
59
|
+
this.#fetching = false;
|
|
60
|
+
this._keyupHandler = (evt) => {
|
|
61
|
+
if (evt.keyCode === /* Enter */ 13
|
|
62
|
+
// no modifiers
|
|
63
|
+
&& !evt.shiftKey && !evt.ctrlKey && !evt.altKey && !evt.metaKey) {
|
|
64
|
+
let btn = evt.currentTarget.querySelector(P + '-button[type=submit]');
|
|
65
|
+
if (!Utils.isNull(btn) && Utils.isVisible(btn) && !btn.disabled) {
|
|
66
|
+
btn.click();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
this._submit = (fetcher, evt) => {
|
|
71
|
+
if (!Utils.isNull(evt)) {
|
|
72
|
+
avoidHandler(evt);
|
|
73
|
+
}
|
|
74
|
+
Promise.all(this._validateAllFields()).then(p => {
|
|
75
|
+
if (p.findIndex(f => f === false) === -1) {
|
|
76
|
+
this.submit(fetcher).then(_ => {
|
|
77
|
+
// successful submission here
|
|
78
|
+
this.setPristine();
|
|
79
|
+
}, _ => {
|
|
80
|
+
// catch rejected/canceled
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
this._reset = (evt) => {
|
|
86
|
+
if (!Utils.isNull(evt))
|
|
87
|
+
avoidHandler(evt);
|
|
88
|
+
var resetEvt = new FormResetEvent();
|
|
89
|
+
this.dispatchEvent(resetEvt);
|
|
90
|
+
if (resetEvt.defaultPrevented) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// reset
|
|
94
|
+
// fields first
|
|
95
|
+
for (var field in this._fields) {
|
|
96
|
+
var fld = this._fields[field];
|
|
97
|
+
if (fld instanceof PacemModelElement)
|
|
98
|
+
fld.reset();
|
|
99
|
+
}
|
|
100
|
+
// then sub-forms
|
|
101
|
+
for (var form of this._subForms) {
|
|
102
|
+
form._reset();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
this._fields = {};
|
|
106
|
+
this._validators = {};
|
|
107
|
+
this._subForms = [];
|
|
108
|
+
this._fieldPropertyChanged = (evt) => {
|
|
109
|
+
switch (evt.detail.propertyName) {
|
|
110
|
+
case 'value':
|
|
111
|
+
// validate:
|
|
112
|
+
let model = evt.target;
|
|
113
|
+
this._checkFieldValidity(model.name);
|
|
114
|
+
break;
|
|
115
|
+
case 'dirty':
|
|
116
|
+
this._checkDirtyness();
|
|
117
|
+
break;
|
|
118
|
+
case 'valid':
|
|
119
|
+
this._checkValidity();
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
this._validatorPropertyChanged = (evt) => {
|
|
124
|
+
const validator = evt.target, name = validator.watch;
|
|
125
|
+
if (evt.detail.propertyName === 'invalid') {
|
|
126
|
+
let model = this._fields[validator.watch];
|
|
127
|
+
if (validator.invalid)
|
|
128
|
+
model.valid = false;
|
|
129
|
+
else
|
|
130
|
+
this._checkFieldValidity(name);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
this._checkFieldValidity(name);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
/** Gets or sets whether to hint invalid fields even if still pristine (default 'true').
|
|
137
|
+
* When 'true' - and form is autogenerated - the submit button status is tightly bound to the form valid state. */
|
|
138
|
+
this.suddenValidation = true;
|
|
139
|
+
/** Gets or sets whether the form should show - when autogenerated - the reset button. */
|
|
140
|
+
this.resettable = true;
|
|
141
|
+
this._emitter = new FormEventEmitter(this);
|
|
142
|
+
}
|
|
143
|
+
propertyChangedCallback(name, old, val, first) {
|
|
144
|
+
super.propertyChangedCallback(name, old, val, first);
|
|
145
|
+
if ((name === 'method' && this.autogenerate === true && !Utils.isNullOrEmpty(this.metadata)) ||
|
|
146
|
+
(name === 'metadata' && this.autogenerate === true) ||
|
|
147
|
+
(name === 'autogenerate' && val === true && this.metadata && (this.metadata['props'] || this.metadata).length > 0)) {
|
|
148
|
+
this._buildUpForm();
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
switch (name) {
|
|
152
|
+
case 'readonly':
|
|
153
|
+
//this._lockSubForms(val);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
viewActivatedCallback() {
|
|
159
|
+
super.viewActivatedCallback();
|
|
160
|
+
this.form && this.form.registerSubForm(this);
|
|
161
|
+
this._checkValidity();
|
|
162
|
+
this.addEventListener('keyup', this._keyupHandler, false);
|
|
163
|
+
this._emitter.start();
|
|
164
|
+
}
|
|
165
|
+
disconnectedCallback() {
|
|
166
|
+
this._emitter.stop();
|
|
167
|
+
this.removeEventListener('keyup', this._keyupHandler, false);
|
|
168
|
+
this.form && this.form.unregisterSubForm(this);
|
|
169
|
+
super.disconnectedCallback();
|
|
170
|
+
}
|
|
171
|
+
_buildUpForm() {
|
|
172
|
+
const key = '_' + Utils.uniqueCode();
|
|
173
|
+
const uid = this.id = (this.id || key);
|
|
174
|
+
const f_uid = 'fetch' + key;
|
|
175
|
+
//
|
|
176
|
+
const form = document.createElement('form');
|
|
177
|
+
form.id = "form" + key;
|
|
178
|
+
form.className = PCSS + '-form';
|
|
179
|
+
form.setAttribute('pacem', '');
|
|
180
|
+
form.setAttribute('novalidate', '');
|
|
181
|
+
const html = `<${P}-repeater datasource="{{ #${uid}.metadata && (#${uid}.metadata.props || #${uid}.metadata) }}">
|
|
182
|
+
<${P}-panel css="{{ #${uid}.metadata.display && #${uid}.metadata.display.css }}" css-class="{{ (#${uid}.metadata.display && #${uid}.metadata.display.cssClass || []).concat(#${uid}.suddenValidation ? [] : ['lazy-validation']) }}" class="${PCSS}-animatable-list ${PCSS}-list-appear">
|
|
183
|
+
<template>
|
|
184
|
+
<${P}-form-field fetch-credentials="{{ #${uid}.fetchCredentials }}" fetch-headers="{{ #${uid}.fetchHeaders }}"
|
|
185
|
+
logger="{{ #${uid}.logger }}" entity="{{ #${uid}.entity, twoway }}"
|
|
186
|
+
metadata="{{ ^item }}" readonly="{{ #${uid}.readonly || ^item.isReadOnly }}"></${P}-form-field>
|
|
187
|
+
</template>
|
|
188
|
+
</${P}-panel>
|
|
189
|
+
</${P}-repeater>
|
|
190
|
+
<${P}-fetch logger="{{ #${uid}.logger }}" id="${f_uid}" method="${this.method}" credentials="{{ #${uid}.fetchCredentials }}" headers="{{ #${uid}.fetchHeaders }}"></${P}-fetch>
|
|
191
|
+
<div class="${PCSS}-buttonset buttons">
|
|
192
|
+
<div class="buttonset-left">
|
|
193
|
+
<${P}-button logger="{{ #${uid}.logger }}" on-click="#${uid}._submit(#${uid}.fetcher || #${f_uid}, $event)" type="submit" hide="{{ #${uid}.readonly || $pacem.isNullOrEmpty(#${uid}.action) || !$pacem.isNull(#${uid}.form) }}"
|
|
194
|
+
class="button primary button-size size-small"
|
|
195
|
+
css-class="{{ {'buttonset-last': !#${uid}.resettable || !#${uid}.dirty || !$pacem.isNull(#${uid}.form) } }}"
|
|
196
|
+
disabled="{{ (#${uid}.suddenValidation && (!(#${uid}.valid && #${uid}.dirty))) || #${uid}.fetching }}">
|
|
197
|
+
<${P}-panel class="submit-spinner ${PCSS}-anim anim-pop" hide="{{ !#${uid}.fetching }}"><i class="${PCSS}-icon ${PCSS}-anim anim-rotate rotate-uniform display-block">refresh</i></${P}-panel>
|
|
198
|
+
<${P}-text text="{{ #${uid}.submitCaption || 'OK' }}"></${P}-text>
|
|
199
|
+
</${P}-button>
|
|
200
|
+
<${P}-button logger="{{ #${uid}.logger }}" on-click="#${uid}._reset($event)" type="reset" class="button button-size size-small" css-class="{{ {'buttonset-first': #${uid}.readonly || $pacem.isNullOrEmpty(#${uid}.action) || !$pacem.isNull(#${uid}.form)} }}" hide="{{ !#${uid}.resettable || #${uid}.readonly || !#${uid}.dirty || !Pacem.Utils.isNull(#${uid}.form) }}" disabled="{{ #${uid}.fetching }}">
|
|
201
|
+
<${P}-text text="{{ #${uid}.resetCaption || 'Reset' }}"></${P}-text>
|
|
202
|
+
</${P}-button>
|
|
203
|
+
</div></div>`;
|
|
204
|
+
// buttons are kept hidden if...
|
|
205
|
+
form.innerHTML = html;
|
|
206
|
+
this.innerHTML = '';
|
|
207
|
+
this.appendChild(form);
|
|
208
|
+
form.addEventListener('submit', avoidHandler, false);
|
|
209
|
+
const old = this._buttons;
|
|
210
|
+
const val = this._buttons = {
|
|
211
|
+
submit: form.lastElementChild.firstElementChild.firstElementChild,
|
|
212
|
+
reset: form.lastElementChild.firstElementChild.lastElementChild
|
|
213
|
+
};
|
|
214
|
+
this.dispatchEvent(new PropertyChangeEvent({ propertyName: 'formButtons', currentValue: val, oldValue: old }));
|
|
215
|
+
}
|
|
216
|
+
/** Gets the 'submit' and 'reset' form buttons. */
|
|
217
|
+
get formButtons() {
|
|
218
|
+
return this._buttons;
|
|
219
|
+
}
|
|
220
|
+
#fetching;
|
|
221
|
+
get fetching() {
|
|
222
|
+
return this.#fetching;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Submits the form data via a provided fetcher.
|
|
226
|
+
* @param fetcher The fetching delegate.
|
|
227
|
+
*/
|
|
228
|
+
submit(fetcher) {
|
|
229
|
+
return new Promise((resolve, reject) => {
|
|
230
|
+
if (Utils.isNull(fetcher)) {
|
|
231
|
+
reject(`Fetcher cannot be null while submitting a form.`);
|
|
232
|
+
}
|
|
233
|
+
this._submitInternally(fetcher).then(result => {
|
|
234
|
+
this.dispatchEvent(new CustomEvent("success", { detail: result }));
|
|
235
|
+
resolve(result);
|
|
236
|
+
}, err => {
|
|
237
|
+
if (err !== SUBMIT_CANCELLATION_TOKEN) {
|
|
238
|
+
this.dispatchEvent(new CustomEvent("fail", { detail: err }));
|
|
239
|
+
}
|
|
240
|
+
reject(err);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/** Resets the form to its pristine values. */
|
|
245
|
+
reset() {
|
|
246
|
+
this._reset();
|
|
247
|
+
}
|
|
248
|
+
_submitInternally(fetcher, args) {
|
|
249
|
+
if (Utils.isNull(fetcher))
|
|
250
|
+
throw `Fetcher cannot be null while submitting a form.`;
|
|
251
|
+
var deferred = DeferPromise.defer();
|
|
252
|
+
const entity = this.entity, entityName = this.entityName;
|
|
253
|
+
var model = {};
|
|
254
|
+
if (!Utils.isNullOrEmpty(entityName)) {
|
|
255
|
+
model[entityName] = entity;
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
model = entity;
|
|
259
|
+
}
|
|
260
|
+
var args = { parameters: model, fields: this._getAllFields() };
|
|
261
|
+
// allow to inject some logic while submitting...
|
|
262
|
+
var submitEvt = new FormSubmitEvent(args);
|
|
263
|
+
this.dispatchEvent(submitEvt);
|
|
264
|
+
if (submitEvt.defaultPrevented) {
|
|
265
|
+
// rejection using `false` identifies a canceled submission.
|
|
266
|
+
deferred.reject(SUBMIT_CANCELLATION_TOKEN);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
// ...use the output parameters
|
|
270
|
+
fetcher.parameters = Utils.extend({}, submitEvt.detail.parameters);
|
|
271
|
+
// a) fetcher 'success' event listener
|
|
272
|
+
const fnSuccess = (evt) => {
|
|
273
|
+
fetcher.removeEventListener(Net.FetchSuccessEventName, fnSuccess, false);
|
|
274
|
+
fetcher.removeEventListener(Net.FetchErrorEventName, fnError, false);
|
|
275
|
+
const r = evt.detail, empty = Net.Fetcher.isEmpty(r);
|
|
276
|
+
if (empty) {
|
|
277
|
+
// if no content gets fetched (e.g. '204') then resolve right here (because it won't enter 'fnResult')
|
|
278
|
+
fetcher.removeEventListener(Net.FetchResultEventName, fnResult, false);
|
|
279
|
+
this.success = true;
|
|
280
|
+
deferred.resolve({});
|
|
281
|
+
}
|
|
282
|
+
switch (r.status) {
|
|
283
|
+
case 205 /*'Reset Content'*/:
|
|
284
|
+
this._reset();
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
// b) fetcher 'fetchresult' event listener
|
|
289
|
+
const fnResult = (evt) => {
|
|
290
|
+
// remove listeners (others have already been removed in 'fnSuccess')
|
|
291
|
+
fetcher.removeEventListener(Net.FetchResultEventName, fnResult, false);
|
|
292
|
+
//
|
|
293
|
+
var result = evt.detail;
|
|
294
|
+
if (typeof (result) === 'object' && result.hasOwnProperty('success')) {
|
|
295
|
+
// DEPRECATED code branch
|
|
296
|
+
if (result.success === true) {
|
|
297
|
+
this.success = true;
|
|
298
|
+
deferred.resolve(result.result || {});
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
this.fail = true;
|
|
302
|
+
deferred.reject(result.error);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
this.success = true;
|
|
307
|
+
deferred.resolve(result);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
// c) fetcher 'error' event listener
|
|
311
|
+
const fnError = (evt) => {
|
|
312
|
+
// remove listeners
|
|
313
|
+
fetcher.removeEventListener(Net.FetchSuccessEventName, fnSuccess, false);
|
|
314
|
+
fetcher.removeEventListener(Net.FetchResultEventName, fnResult, false);
|
|
315
|
+
fetcher.removeEventListener(Net.FetchErrorEventName, fnError, false);
|
|
316
|
+
this.fail = true;
|
|
317
|
+
const response = evt.detail;
|
|
318
|
+
deferred.reject(response);
|
|
319
|
+
};
|
|
320
|
+
// fetcher propertychange event listener
|
|
321
|
+
const fnPropChange = (evt) => {
|
|
322
|
+
switch (evt.detail.propertyName) {
|
|
323
|
+
case 'fetching':
|
|
324
|
+
if (evt.detail.currentValue === true) {
|
|
325
|
+
Utils.addClass(this, PCSS + '-fetching');
|
|
326
|
+
this.#fetching = true;
|
|
327
|
+
this.dispatchEvent(new PropertyChangeEvent({ currentValue: true, oldValue: false, propertyName: 'fetching' }));
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
this.#fetching = false;
|
|
331
|
+
this.dispatchEvent(new PropertyChangeEvent({ currentValue: false, oldValue: true, propertyName: 'fetching' }));
|
|
332
|
+
Utils.removeClass(this, PCSS + '-fetching');
|
|
333
|
+
fetcher.removeEventListener(PropertyChangeEventName, fnPropChange, false);
|
|
334
|
+
}
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
fetcher.addEventListener(PropertyChangeEventName, fnPropChange, false);
|
|
339
|
+
fetcher.addEventListener(Net.FetchSuccessEventName, fnSuccess, false);
|
|
340
|
+
fetcher.addEventListener(Net.FetchResultEventName, fnResult, false);
|
|
341
|
+
fetcher.addEventListener(Net.FetchErrorEventName, fnError, false);
|
|
342
|
+
if (!Utils.isNullOrEmpty(this.action)) {
|
|
343
|
+
fetcher.url = this.action;
|
|
344
|
+
}
|
|
345
|
+
// is it an 'inert' PacemFetchElement?
|
|
346
|
+
if (fetcher instanceof Components.PacemFetchElement && !fetcher.autofetch) {
|
|
347
|
+
fetcher.fetch();
|
|
348
|
+
}
|
|
349
|
+
this.success = this.fail = false;
|
|
350
|
+
}
|
|
351
|
+
//
|
|
352
|
+
return deferred.promise;
|
|
353
|
+
}
|
|
354
|
+
_getAllFields(accumulator = {}) {
|
|
355
|
+
Utils.extend(accumulator, this._fields);
|
|
356
|
+
for (var form of this._subForms) {
|
|
357
|
+
form._getAllFields(accumulator);
|
|
358
|
+
}
|
|
359
|
+
return accumulator;
|
|
360
|
+
}
|
|
361
|
+
/** Modifies the state of the current entity programmatically. */
|
|
362
|
+
apply(state) {
|
|
363
|
+
if (Utils.isNullOrEmpty(state)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
this.entity = Utils.extend({}, this.entity, state);
|
|
367
|
+
for (let name in state) {
|
|
368
|
+
this.setDirty(name);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/** Sets the current form (or the fields of it having the specified names) in a pristine state. */
|
|
372
|
+
setPristine(...names) {
|
|
373
|
+
this._setAs(false, ...names);
|
|
374
|
+
}
|
|
375
|
+
/** Sets the current form (or the fields of it having the specified names) in a dirty state. */
|
|
376
|
+
setDirty(...names) {
|
|
377
|
+
this._setAs(true, ...names);
|
|
378
|
+
}
|
|
379
|
+
_setAs(dirty, ...names) {
|
|
380
|
+
const hasNames = !Utils.isEmpty(names);
|
|
381
|
+
for (let field in this._fields) {
|
|
382
|
+
if (!hasNames || names.indexOf(field) >= 0) {
|
|
383
|
+
const fld = this._fields[field];
|
|
384
|
+
if (fld instanceof PacemModelElement) {
|
|
385
|
+
dirty ? fld.setDirty() : fld.setPristine();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
for (let form of this._subForms) {
|
|
390
|
+
dirty ? form.setDirty(...names) : form.setPristine(...names);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
_checkFieldValidity(name) {
|
|
394
|
+
if (!this.suddenValidation) {
|
|
395
|
+
return Promise.resolve(null);
|
|
396
|
+
}
|
|
397
|
+
if (name in this._fields) {
|
|
398
|
+
return this._validateField(name, false);
|
|
399
|
+
}
|
|
400
|
+
return Promise.resolve(false);
|
|
401
|
+
}
|
|
402
|
+
// @Concurrent()
|
|
403
|
+
validate(name) {
|
|
404
|
+
if (!Utils.isNullOrEmpty(name)) {
|
|
405
|
+
return this._validateField(name, false);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
return Promise.all(this._validateAllFields()).then(b => !b.some(b0 => b0 === false));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
_validateAllFields(setAsDirty = true) {
|
|
412
|
+
const tasks = [];
|
|
413
|
+
for (let field in this._fields) {
|
|
414
|
+
tasks.push(this._validateField(field, setAsDirty));
|
|
415
|
+
}
|
|
416
|
+
for (let sub of this._subForms) {
|
|
417
|
+
Array.prototype.push.apply(tasks, sub._validateAllFields(setAsDirty));
|
|
418
|
+
}
|
|
419
|
+
return tasks;
|
|
420
|
+
}
|
|
421
|
+
// Remove Concurrent from here, better check the closure for params in @Concurrent
|
|
422
|
+
//@Concurrent()
|
|
423
|
+
_validateField(name, setAsDirty) {
|
|
424
|
+
return new Promise((resolve, _) => {
|
|
425
|
+
var model = this._fields[name];
|
|
426
|
+
if (model) {
|
|
427
|
+
var validators = this._validators[model.name];
|
|
428
|
+
if (validators && validators.length > 0) {
|
|
429
|
+
this.log(Logging.LogLevel.Log, `Computing "${model.name}" validity.`);
|
|
430
|
+
Promise.all(validators.map(v => v.validate(model.value)
|
|
431
|
+
.then(_ => !(v.invalid = !_))))
|
|
432
|
+
.then(results => {
|
|
433
|
+
let valid = true;
|
|
434
|
+
for (var result of results) {
|
|
435
|
+
if (result === false) {
|
|
436
|
+
valid = false;
|
|
437
|
+
// set dirty only when not valid
|
|
438
|
+
if (setAsDirty) {
|
|
439
|
+
model.setDirty();
|
|
440
|
+
}
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
this.log(Logging.LogLevel.Log, `Property "${model.name}" turns out to be ${(valid ? '' : 'not ')}valid.`);
|
|
445
|
+
resolve(model.valid = valid);
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
this.log(Logging.LogLevel.Log, `Property "${model.name}" has no validators.`);
|
|
450
|
+
resolve(model.valid = true);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
this.log(Logging.LogLevel.Log, `Property "${name}" has no fields in this form.`);
|
|
455
|
+
resolve(true);
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
// #region FORM HIERARCHY validity/dirtyness
|
|
460
|
+
_checkDirtyness() {
|
|
461
|
+
let dirty = false;
|
|
462
|
+
for (var model in this._fields) {
|
|
463
|
+
if (this._fields[model].dirty === true) {
|
|
464
|
+
dirty = true;
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if ( /* still */dirty === false) {
|
|
469
|
+
for (var form of this._subForms) {
|
|
470
|
+
if (form.dirty) {
|
|
471
|
+
dirty = true;
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
this.dirty = dirty;
|
|
477
|
+
}
|
|
478
|
+
;
|
|
479
|
+
_checkValidity() {
|
|
480
|
+
let valid = true;
|
|
481
|
+
for (var model in this._fields) {
|
|
482
|
+
this.log(Logging.LogLevel.Log, `Checking "${model}" validity.`);
|
|
483
|
+
if (this._fields[model].valid === false) {
|
|
484
|
+
this.log(Logging.LogLevel.Log, `"${model}" is not valid.`);
|
|
485
|
+
valid = false;
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if ( /* still */valid === true) {
|
|
490
|
+
for (var form of this._subForms) {
|
|
491
|
+
if (!form.valid) {
|
|
492
|
+
valid = false;
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
this.valid = valid;
|
|
498
|
+
}
|
|
499
|
+
;
|
|
500
|
+
registerSubForm(form) {
|
|
501
|
+
let arr = this._subForms;
|
|
502
|
+
if (arr.indexOf(form) == -1) {
|
|
503
|
+
form.addEventListener(PropertyChangeEventName, this._fieldPropertyChanged, false);
|
|
504
|
+
arr.push(form);
|
|
505
|
+
this._checkValidity();
|
|
506
|
+
this._checkDirtyness();
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
unregisterSubForm(form) {
|
|
510
|
+
let arr = this._subForms, ndx = arr.indexOf(form);
|
|
511
|
+
if (ndx >= 0) {
|
|
512
|
+
form.removeEventListener(PropertyChangeEventName, this._fieldPropertyChanged, false);
|
|
513
|
+
arr.splice(ndx, 1);
|
|
514
|
+
this._checkValidity();
|
|
515
|
+
this._checkDirtyness();
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
registerField(name, model) {
|
|
519
|
+
var container = this._fields;
|
|
520
|
+
if (container[name] != model) {
|
|
521
|
+
this.unregisterField(name, false);
|
|
522
|
+
container[name] = model;
|
|
523
|
+
this.log(Logging.LogLevel.Log, `Registering "${name}" field.`);
|
|
524
|
+
if (model instanceof HTMLElement) {
|
|
525
|
+
model.addEventListener(PropertyChangeEventName, this._fieldPropertyChanged, false);
|
|
526
|
+
}
|
|
527
|
+
this._checkFieldValidity(name);
|
|
528
|
+
this._checkDirtyness();
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
unregisterField(name, check = true) {
|
|
532
|
+
var container = this._fields, current = container[name];
|
|
533
|
+
if (current instanceof HTMLElement) {
|
|
534
|
+
current.removeEventListener(PropertyChangeEventName, this._fieldPropertyChanged, false);
|
|
535
|
+
}
|
|
536
|
+
delete container[name];
|
|
537
|
+
if (current && check === true) {
|
|
538
|
+
this._checkValidity();
|
|
539
|
+
this._checkDirtyness();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
registerValidator(name, validator) {
|
|
543
|
+
var container = this._validators[name] = this._validators[name] || [], index = container.indexOf(validator);
|
|
544
|
+
if (index == -1) {
|
|
545
|
+
container.push(validator);
|
|
546
|
+
if (validator instanceof HTMLElement) {
|
|
547
|
+
validator.addEventListener(PropertyChangeEventName, this._validatorPropertyChanged, false);
|
|
548
|
+
}
|
|
549
|
+
this.log(Logging.LogLevel.Log, `Calling "${name}" validity check after validator registration.`);
|
|
550
|
+
// most likely, at this point, the corresponding field isn't available yet.
|
|
551
|
+
this._checkFieldValidity(name);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
unregisterValidator(name, validator) {
|
|
555
|
+
this._unregisterValidator(name, validator, true);
|
|
556
|
+
}
|
|
557
|
+
_unregisterValidator(name, validator, check) {
|
|
558
|
+
var container = this._validators[name] = this._validators[name] || [], index = container.indexOf(validator);
|
|
559
|
+
if (index >= 0) {
|
|
560
|
+
if (validator instanceof HTMLElement) {
|
|
561
|
+
validator.removeEventListener(PropertyChangeEventName, this._validatorPropertyChanged, false);
|
|
562
|
+
}
|
|
563
|
+
container.splice(index, 1);
|
|
564
|
+
if (check) {
|
|
565
|
+
this._checkFieldValidity(name);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
__decorate([
|
|
571
|
+
Watch({ emit: false, converter: PropertyConverters.Element })
|
|
572
|
+
], PacemFormElement.prototype, "fetcher", void 0);
|
|
573
|
+
__decorate([
|
|
574
|
+
Watch({ emit: false, converter: PropertyConverters.String })
|
|
575
|
+
], PacemFormElement.prototype, "fetchCredentials", void 0);
|
|
576
|
+
__decorate([
|
|
577
|
+
Watch({ emit: false, converter: PropertyConverters.Json })
|
|
578
|
+
], PacemFormElement.prototype, "fetchHeaders", void 0);
|
|
579
|
+
__decorate([
|
|
580
|
+
Watch({ reflectBack: true, converter: PropertyConverters.String })
|
|
581
|
+
], PacemFormElement.prototype, "method", void 0);
|
|
582
|
+
__decorate([
|
|
583
|
+
Watch({ reflectBack: true, converter: PropertyConverters.String })
|
|
584
|
+
], PacemFormElement.prototype, "submitCaption", void 0);
|
|
585
|
+
__decorate([
|
|
586
|
+
Watch({ reflectBack: true, converter: PropertyConverters.String })
|
|
587
|
+
], PacemFormElement.prototype, "resetCaption", void 0);
|
|
588
|
+
__decorate([
|
|
589
|
+
Debounce(100)
|
|
590
|
+
], PacemFormElement.prototype, "_buildUpForm", null);
|
|
591
|
+
__decorate([
|
|
592
|
+
Watch({ reflectBack: true, converter: PropertyConverters.Boolean })
|
|
593
|
+
], PacemFormElement.prototype, "valid", void 0);
|
|
594
|
+
__decorate([
|
|
595
|
+
Watch({ reflectBack: true, converter: PropertyConverters.Boolean })
|
|
596
|
+
], PacemFormElement.prototype, "dirty", void 0);
|
|
597
|
+
__decorate([
|
|
598
|
+
Watch({ reflectBack: true, converter: PropertyConverters.Boolean })
|
|
599
|
+
], PacemFormElement.prototype, "readonly", void 0);
|
|
600
|
+
__decorate([
|
|
601
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
602
|
+
], PacemFormElement.prototype, "success", void 0);
|
|
603
|
+
__decorate([
|
|
604
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
605
|
+
], PacemFormElement.prototype, "fail", void 0);
|
|
606
|
+
__decorate([
|
|
607
|
+
Watch({ converter: PropertyConverters.Json })
|
|
608
|
+
], PacemFormElement.prototype, "entity", void 0);
|
|
609
|
+
__decorate([
|
|
610
|
+
Watch({ emit: false, converter: PropertyConverters.String })
|
|
611
|
+
], PacemFormElement.prototype, "entityName", void 0);
|
|
612
|
+
__decorate([
|
|
613
|
+
Watch()
|
|
614
|
+
], PacemFormElement.prototype, "metadata", void 0);
|
|
615
|
+
__decorate([
|
|
616
|
+
Watch({ emit: false, converter: PropertyConverters.Boolean })
|
|
617
|
+
], PacemFormElement.prototype, "autogenerate", void 0);
|
|
618
|
+
__decorate([
|
|
619
|
+
Watch({ converter: PropertyConverters.Boolean })
|
|
620
|
+
], PacemFormElement.prototype, "suddenValidation", void 0);
|
|
621
|
+
__decorate([
|
|
622
|
+
Watch({ emit: false, converter: PropertyConverters.Boolean })
|
|
623
|
+
], PacemFormElement.prototype, "resettable", void 0);
|
|
624
|
+
__decorate([
|
|
625
|
+
Watch({ converter: PropertyConverters.String })
|
|
626
|
+
], PacemFormElement.prototype, "action", void 0);
|
|
627
|
+
PacemFormElement = __decorate([
|
|
628
|
+
CustomElement({ tagName: P + '-form' })
|
|
629
|
+
], PacemFormElement);
|
|
630
|
+
export { PacemFormElement };
|