@live-change/vue3-components 0.2.9 → 0.2.12

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.
@@ -164,6 +164,7 @@
164
164
  const node = this.getNodeIfExists(name)
165
165
  if(node) node.unobserveError(observer)
166
166
  },
167
+
167
168
  addValidator(name, validator) {
168
169
  this.getNode(name).validators.push(validator)
169
170
  },
@@ -185,6 +186,20 @@
185
186
  clearValidation() {
186
187
  this.$refs.defined.clearValidation()
187
188
  },
189
+
190
+ waitForFieldBarriers(name, context) {
191
+ return this.$refs.defined.waitForFieldBarriers(name, context)
192
+ },
193
+ waitForBarriers(context) {
194
+ return this.$refs.defined.waitForBarriers(context)
195
+ },
196
+ addBarrier(name, validator) {
197
+ this.$refs.defined.addBarrier(name, validator)
198
+ },
199
+ removeBarrier(name, validator) {
200
+ this.$refs.defined.removeBarrier(name, validator)
201
+ },
202
+
188
203
  addElementToArray(propName, initialValue) {
189
204
  this.getNode(propName).addElement(initialValue)
190
205
  },
@@ -209,88 +224,94 @@
209
224
 
210
225
  analytics.emit('form', { service: this.service, action: this.action, parameters: analyticsParameters })
211
226
 
212
- return this.validate({ parameters: {...this.parameters, ...additionalParameters} }).then(validationError => {
213
- debug("VALIDATION ERROR?", validationError)
214
- if(validationError) {
215
- analytics.emit('formError', {
216
- service: this.service, action: this.action, parameters: analyticsParameters, error: validationError
217
- })
218
- this.workingZone.finished(this.workingTask)
219
- this.state = 'ready'
220
- this.scrollToError()
221
- return;
222
- }
227
+ const barrierResult = await this.waitForBarriers({
228
+ parameters: { ...this.parameters, ...additionalParameters }
229
+ })
223
230
 
224
- let parameters = this.$refs.defined.formRoot.getValue()
225
- parameters = { ...parameters, ...this.parameters, ...(additionalParameters || {}), _commandId }
226
- //console.trace("SUBMIT!")
227
- debug("SUBMIT DATA:\n"+JSON.stringify(parameters, null, " "))
231
+ const validationError = await this.validate({
232
+ parameters: { ...this.parameters, ...additionalParameters }
233
+ })
228
234
 
229
- this.$emit("submit", { parameters })
235
+ debug("VALIDATION ERROR?", validationError)
236
+ if(validationError) {
237
+ analytics.emit('formError', {
238
+ service: this.service, action: this.action, parameters: analyticsParameters, error: validationError
239
+ })
240
+ this.workingZone.finished(this.workingTask)
241
+ this.state = 'ready'
242
+ this.scrollToError()
243
+ return;
244
+ }
230
245
 
231
- return this.$api.request([this.service, this.action], parameters).then((result) => {
232
- debug("DATA SUBMITED")
233
- analytics.emit('formDone', {
234
- service: this.service, action: this.action, parameters: analyticsParameters, error: validationError
235
- })
236
- if(this.resetOnDone) {
237
- this.state = 'ready'
238
- this.reset()
239
- } else if(this.keepOnDone) {
246
+ let parameters = this.$refs.defined.formRoot.getValue()
247
+ parameters = { ...parameters, ...this.parameters, ...(additionalParameters || {}), _commandId }
248
+ //console.trace("SUBMIT!")
249
+ debug("SUBMIT DATA:\n"+JSON.stringify(parameters, null, " "))
250
+
251
+ this.$emit("submit", { parameters })
252
+
253
+ return this.$api.request([this.service, this.action], parameters).then((result) => {
254
+ debug("DATA SUBMITED")
255
+ analytics.emit('formDone', {
256
+ service: this.service, action: this.action, parameters: analyticsParameters, error: validationError
257
+ })
258
+ if(this.resetOnDone) {
259
+ this.state = 'ready'
260
+ this.reset()
261
+ } else if(this.keepOnDone) {
262
+ this.state = 'ready'
263
+ } else {
264
+ this.state = 'done'
265
+ }
266
+ this.$emit('done', { result , parameters })
267
+ this.workingZone.finished(this.workingTask)
268
+ }).catch((error) => {
269
+ console.error("FORM ERROR", error)
270
+ analytics.emit('formError', {
271
+ service: this.service, action: this.action, parameters: analyticsParameters, error
272
+ })
273
+ this.$refs.defined.formRoot.afterError(this.initialValues)
274
+ if(error.properties) {
275
+ for(let propName in error.properties) {
276
+ let node = this.getNode(propName)
277
+ if(!node) {
278
+ this.state = 'error'
279
+ this.error = `protocol mismatch, field ${propName} not found`
280
+ return
281
+ }
282
+ node.setError(error.properties[propName])
240
283
  this.state = 'ready'
241
- } else {
242
- this.state = 'done'
243
284
  }
244
- this.$emit('done', { result , parameters })
285
+ this.scrollToError()
245
286
  this.workingZone.finished(this.workingTask)
246
- }).catch((error) => {
247
- console.error("FORM ERROR", error)
248
- analytics.emit('formError', {
249
- service: this.service, action: this.action, parameters: analyticsParameters, error
250
- })
251
- this.$refs.defined.formRoot.afterError(this.initialValues)
252
- if(error.properties) {
253
- for(let propName in error.properties) {
254
- let node = this.getNode(propName)
255
- if(!node) {
256
- this.state = 'error'
257
- this.error = `protocol mismatch, field ${propName} not found`
258
- return
259
- }
260
- node.setError(error.properties[propName])
261
- this.state = 'ready'
262
- }
263
- this.scrollToError()
264
- this.workingZone.finished(this.workingTask)
265
- } else if(this.ignoreError) {
266
- if((typeof this.ignoreError) == 'function') {
267
- if(this.ignoreError(error)) {
268
- this.workingZone.finished(this.workingTask)
269
- } else {
270
- this.state = 'error'
271
- this.error = error
272
- this.workingZone.failed(this.workingTask, error)
273
- }
274
- } else if(Array.isArray(this.ignoreError)) {
275
- if(this.ignoreError.indexOf(error) != -1) {
276
- this.workingZone.finished(this.workingTask)
277
- } else {
278
- this.state = 'error'
279
- this.error = error
280
- this.workingZone.failed(this.workingTask, error)
281
- }
287
+ } else if(this.ignoreError) {
288
+ if((typeof this.ignoreError) == 'function') {
289
+ if(this.ignoreError(error)) {
290
+ this.workingZone.finished(this.workingTask)
282
291
  } else {
292
+ this.state = 'error'
293
+ this.error = error
294
+ this.workingZone.failed(this.workingTask, error)
295
+ }
296
+ } else if(Array.isArray(this.ignoreError)) {
297
+ if(this.ignoreError.indexOf(error) != -1) {
283
298
  this.workingZone.finished(this.workingTask)
299
+ } else {
300
+ this.state = 'error'
301
+ this.error = error
302
+ this.workingZone.failed(this.workingTask, error)
284
303
  }
285
- } else if(this.externalErrorsProcessing) {
286
- // ...
287
304
  } else {
288
- this.state = 'error'
289
- this.error = error
290
- this.workingZone.failed(this.workingTask, error)
305
+ this.workingZone.finished(this.workingTask)
291
306
  }
292
- this.$emit('error', { error, parameters, task: this.workingTask })
293
- })
307
+ } else if(this.externalErrorsProcessing) {
308
+ // ...
309
+ } else {
310
+ this.state = 'error'
311
+ this.error = error
312
+ this.workingZone.failed(this.workingTask, error)
313
+ }
314
+ this.$emit('error', { error, parameters, task: this.workingTask })
294
315
  })
295
316
  },
296
317
  handleSubmitEvent(ev) {
@@ -20,6 +20,8 @@
20
20
  if(!this.component) throw new Error("no component parameter")
21
21
 
22
22
  this.validators = []
23
+ this.barriers = []
24
+
23
25
  if(definition.validation) {
24
26
  let validations = Array.isArray(definition.validation) ? definition.validation : [definition.validation]
25
27
  const context = {
@@ -109,6 +111,14 @@
109
111
  clearValidation() {
110
112
  this.setError(null)
111
113
  }
114
+
115
+ async waitForBarriers(context) {
116
+ let promises = []
117
+ for(let barrier of this.barriers) {
118
+ promises.push(barrier(this.value, context))
119
+ }
120
+ await Promise.all(promises)
121
+ }
112
122
  }
113
123
 
114
124
  class FormObject extends FormValue {
@@ -196,6 +206,20 @@
196
206
  }
197
207
  }
198
208
 
209
+ async waitForBarriers(context) {
210
+ let promises = [ super.waitForBarriers(context).then(error => ['root', error]) ]
211
+ for(let propName in this.properties) {
212
+ if(context.parameters && context.parameters[propName]) continue;
213
+ promises.push(
214
+ this.properties[propName].waitForBarriers({
215
+ ...context,
216
+ propName: context.propName ? context.propName+'.'+propName : propName
217
+ })
218
+ )
219
+ }
220
+ await Promise.all(promises)
221
+ }
222
+
199
223
  getValue() {
200
224
  let obj = { ...this.value }
201
225
  for(let propName in this.properties) {
@@ -250,10 +274,12 @@
250
274
  if(!this.data[this.property]) this.data[this.property] = []
251
275
  this.object = this.data[this.property]
252
276
  }
277
+
253
278
  setProperty(name) {
254
279
  this.property = name
255
280
  this.object = this.data[this.property]
256
281
  }
282
+
257
283
  newElement(index) {
258
284
  if(this.elementDefinition.type == "Object") {
259
285
  return new FormObject(this.elementDefinition, this.component, this.object, index)
@@ -263,6 +289,7 @@
263
289
  return new FormValue(this.elementDefinition, this.component, this.object, index)
264
290
  }
265
291
  }
292
+
266
293
  reset(initialValue) {
267
294
  initialValue = initialValue || this.definition.defaultValue || []
268
295
  this.data[this.property] = new Array(initialValue.length)
@@ -274,12 +301,14 @@
274
301
  }
275
302
  super.setValue(initialValue)
276
303
  }
304
+
277
305
  afterError(initialValue) {
278
306
  if(this.definition.singleUse) return this.reset()
279
307
  for(let i = 0; i < this.elements.length; i++) {
280
308
  this.elements[i].afterError(initialValue && initialValue[i])
281
309
  }
282
310
  }
311
+
283
312
  validate(context) {
284
313
  let promises = [super.validate(context)]
285
314
  for(let propName in this.elements) {
@@ -298,6 +327,7 @@
298
327
  return anyError && results
299
328
  })
300
329
  }
330
+
301
331
  clearValidation() {
302
332
  super.clearValidation()
303
333
  for(let element of this.elements) {
@@ -306,6 +336,14 @@
306
336
  }
307
337
  }
308
338
 
339
+ async waitForBarriers(context) {
340
+ let promises = [super.waitForBarriers(context)]
341
+ for(let propName in this.elements) {
342
+ promises.push(this.elements[propName].waitForBarriers({ ...context, propName: context.propName+'.'+propName }))
343
+ }
344
+ await Promise.all(promises)
345
+ }
346
+
309
347
  getValue() {
310
348
  let arr = new Array(this.elements.length)
311
349
  arr.length = this.elements.length
@@ -405,6 +443,7 @@
405
443
  setFieldError: (name, value) => this.setFieldError(name, value),
406
444
  getValue: () => this.formRoot.getValue(),
407
445
  reset: () => this.reset(),
446
+
408
447
  addValidator: (propName, validator) => this.addValidator(propName, validator),
409
448
  removeValidator: (propName, validator) => this.addValidator(propName, validator),
410
449
  validateField: (propName) => this.validateField(propName),
@@ -412,6 +451,11 @@
412
451
  clearFieldValidation: (propName) => this.clearFieldValidation(propName),
413
452
  clearValidation: () => this.clearValidation(),
414
453
 
454
+ addBarrier: (propName, validator) => this.addBarrier(propName, validator),
455
+ removeBarrier: (propName, validator) => this.removeBarrier(propName, validator),
456
+ waitForFieldBarriers: (propName) => this.waitForFieldBarriers(propName),
457
+ waitForBarriers: (propName) => this.waitForBarriers(),
458
+
415
459
  addElementToArray: (propName, initialValue) => this.addElementToArray(propName, initialValue),
416
460
  removeElementFromArray: (propName, index) => this.removeElementFromArray(propName, index)
417
461
  }
@@ -435,6 +479,7 @@
435
479
  let np = name.split('.')
436
480
  let node = this.formRoot
437
481
  for(let p of np) {
482
+ if(!p) continue
438
483
  if(node.properties) node = node.properties[p]
439
484
  else if(node.elements) node = node.elements[p]
440
485
  if(!node) throw new Error(`form field ${name} not found`)//return null
@@ -445,6 +490,7 @@
445
490
  let np = name.split('.')
446
491
  let node = this.formRoot
447
492
  for(let p of np) {
493
+ if(!p) continue
448
494
  if(node.properties) node = node.properties[p]
449
495
  else if(node.elements) node = node.elements[p]
450
496
  if(!node) return node
@@ -483,6 +529,18 @@
483
529
  this.formRoot.reset(this.initialValues)
484
530
  //console.log("Form after reset", JSON.stringify(this.formRoot.getValue(), null, ' '))
485
531
  },
532
+
533
+ getValidationContext(srcContext) {
534
+ const context = {
535
+ ...(srcContext || { parameters: this.parameters }),
536
+ ...this.provided,
537
+ source: this.definition,
538
+ props: this.formRoot.getValue(),
539
+ form: this
540
+ }
541
+ return context
542
+ },
543
+
486
544
  addValidator(name, validator) {
487
545
  this.getNode(name).validators.push(validator)
488
546
  },
@@ -492,18 +550,11 @@
492
550
  if(id == -1) throw new Error("validator not found")
493
551
  validators.splice(id)
494
552
  },
495
- validateField(name) {
496
- return this.getNode(name).validate(this.formRoot.properties, name, this.definition)
553
+ validateField(name, context) {
554
+ return this.getNode(name).validate(this.getValidationContext(context))
497
555
  },
498
556
  validate(context) {
499
- context = {
500
- ...(context || { parameters: this.parameters }),
501
- ...this.provided,
502
- source: this.definition,
503
- props: this.formRoot.getValue(),
504
- form: this
505
- }
506
- return this.formRoot.validate(context)
557
+ return this.formRoot.validate(this.getValidationContext(context))
507
558
  },
508
559
  clearFieldValidation(name) {
509
560
  this.getNode(name).clearValidation()
@@ -511,6 +562,24 @@
511
562
  clearValidation() {
512
563
  this.formRoot.clearValidation()
513
564
  },
565
+
566
+ waitForFieldBarriers(name, context) {
567
+ return this.getNode(name).waitForBarriers(this.getValidationContext(context))
568
+ },
569
+ waitForBarriers(context) {
570
+ return this.formRoot.waitForBarriers(this.getValidationContext(context))
571
+ },
572
+ addBarrier(name, barrier) {
573
+ this.getNode(name).barriers.push(barrier)
574
+ },
575
+ removeBarrier(name, barrier) {
576
+ let barriers = this.getNode(name).barriers
577
+ let id = barriers.indexOf(barrier)
578
+ if(id == -1) throw new Error("barrier not found")
579
+ barriers.splice(id)
580
+ },
581
+
582
+
514
583
  addElementToArray(propName, initialValue) {
515
584
  this.getNode(propName).addElement(initialValue)
516
585
  },
@@ -59,7 +59,7 @@ function synchronized(options) {
59
59
  onChange()
60
60
  if(autoSave) throttledSave()
61
61
  })
62
- console.log("WATCH SOURCE VALUE", source.value)
62
+ //console.log("WATCH SOURCE VALUE", source.value)
63
63
  watch(() => JSON.stringify(source.value), sourceJson => {
64
64
  const sourceData = JSON.parse(sourceJson)
65
65
  if(sourceData) {
@@ -63,16 +63,16 @@ function synchronizedList(options) {
63
63
  const locallyDeleted = ref([])
64
64
 
65
65
  function createSynchronizedElement(sourceData) {
66
- console.log("CREATE SYNCHRONIZED", JSON.stringify(sourceData))
66
+ //console.log("CREATE SYNCHRONIZED", JSON.stringify(sourceData))
67
67
  const elementSource = ref(sourceData)
68
68
  const synchronizedElement = mapper(elementSource)
69
- console.log("SYNC ELEMENT", synchronizedElement)
69
+ //console.log("SYNC ELEMENT", synchronizedElement)
70
70
  synchronizedElement.source = elementSource
71
71
  synchronizedElement.id = sourceData.id
72
72
  return synchronizedElement
73
73
  }
74
74
  function synchronizeFromSource() {
75
- console.log("SYNCHRONIZE FROM SOURCE!")
75
+ //console.log("SYNCHRONIZE FROM SOURCE!", source.value, "!!!")
76
76
  let obsoleteLocallyAdded = new Set()
77
77
  let obsoleteLocallyDeleted = new Set()
78
78
  let newSynchronized = sortedArraysMerge(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/vue3-components",
3
- "version": "0.2.9",
3
+ "version": "0.2.12",
4
4
  "description": "Live Change Framework - vue components",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,10 +21,10 @@
21
21
  },
22
22
  "homepage": "https://github.com/live-change/live-change-framework-vue3",
23
23
  "dependencies": {
24
- "@live-change/vue3-ssr": "^0.2.9",
24
+ "@live-change/vue3-ssr": "0.2.10",
25
25
  "debug": "^4.3.4",
26
26
  "mitt": "3.0.0",
27
27
  "vue": "^3.2.33"
28
28
  },
29
- "gitHead": "efcb8e4afc03219f588d6e782f29e4f3f74af3cf"
29
+ "gitHead": "3120e4e5c5c96e017cf66bb8172a3f6f96baea84"
30
30
  }