|
1 |
| -import {each, isFunction, isString, isArray, isUndefined} from "lodash"; |
| 1 | +import { each, isFunction, isString, isArray, isUndefined } from "lodash"; |
2 | 2 |
|
3 | 3 | export default {
|
4 |
| - props: [ |
5 |
| - "model", |
6 |
| - "schema", |
7 |
| - "disabled" |
8 |
| - ], |
9 |
| - |
10 |
| - computed: { |
11 |
| - value: { |
12 |
| - cache: false, |
13 |
| - get() { |
14 |
| - let val; |
15 |
| - if (isFunction(this.schema.get)) |
16 |
| - val = this.schema.get(this.model); |
17 |
| - |
18 |
| - else if (this.model && this.schema.model) |
19 |
| - val = this.$get("model." + this.schema.model); |
20 |
| - |
21 |
| - if (isFunction(this.formatValueToField)) |
22 |
| - val = this.formatValueToField(val); |
23 |
| - |
24 |
| - return val; |
25 |
| - }, |
26 |
| - |
27 |
| - set(newValue) { |
28 |
| - if (isFunction(this.formatValueToModel)) |
29 |
| - newValue = this.formatValueToModel(newValue); |
30 |
| - |
31 |
| - if (isFunction(this.schema.set)) |
32 |
| - this.schema.set(this.model, newValue); |
33 |
| - |
34 |
| - else if (this.schema.model) |
35 |
| - this.$set("model." + this.schema.model, newValue); |
36 |
| - } |
37 |
| - } |
38 |
| - }, |
39 |
| - |
40 |
| - watch: { |
41 |
| - value: function(newVal, oldVal) { |
42 |
| - // console.log("Changed", newVal, oldVal); |
43 |
| - if (isFunction(this.schema.onChanged)) { |
44 |
| - this.schema.onChanged(this.model, newVal, oldVal, this.schema); |
45 |
| - } |
46 |
| - |
47 |
| - if (this.$parent.options && this.$parent.options.validateAfterChanged === true) |
48 |
| - this.validate(); |
49 |
| - } |
50 |
| - }, |
51 |
| - |
52 |
| - methods: { |
53 |
| - validate() { |
54 |
| - this.clearValidationErrors(); |
55 |
| - |
56 |
| - if (this.schema.validator && this.schema.readonly !== true && this.disabled !== true) { |
57 |
| - |
58 |
| - let validators = []; |
59 |
| - if (!isArray(this.schema.validator)) { |
60 |
| - validators.push(this.schema.validator.bind(this)); |
61 |
| - } else { |
62 |
| - each(this.schema.validator, (validator) => { |
63 |
| - validators.push(validator.bind(this)); |
64 |
| - }); |
65 |
| - } |
66 |
| - |
67 |
| - each(validators, (validator) => { |
68 |
| - let err = validator(this.value, this.schema, this.model); |
69 |
| - if (err) { |
70 |
| - if (isArray(err)) |
71 |
| - Array.prototype.push.apply(this.schema.errors, err); |
72 |
| - else if (isString(err)) |
73 |
| - this.schema.errors.push(err); |
74 |
| - } |
75 |
| - }); |
76 |
| - |
77 |
| - } |
78 |
| - |
79 |
| - if (isFunction(this.schema.onValidated)) { |
80 |
| - this.schema.onValidated(this.model, this.schema.errors, this.schema); |
81 |
| - } |
82 |
| - |
83 |
| - return this.schema.errors; |
84 |
| - }, |
85 |
| - |
86 |
| - clearValidationErrors() { |
87 |
| - if (isUndefined(this.schema.errors)) |
88 |
| - this.$set("schema.errors", []); // Be reactive |
89 |
| - else |
90 |
| - this.schema.errors.splice(0); // Clear |
91 |
| - } |
92 |
| - } |
| 4 | + props: [ |
| 5 | + "model", |
| 6 | + "schema", |
| 7 | + "disabled" |
| 8 | + ], |
| 9 | + |
| 10 | + computed: { |
| 11 | + value: { |
| 12 | + cache: false, |
| 13 | + get() { |
| 14 | + let val; |
| 15 | + if (isFunction(this.schema.get)) |
| 16 | + val = this.schema.get(this.model); |
| 17 | + |
| 18 | + else if (this.model && this.schema.model) |
| 19 | + val = this.model[this.schema.model]; |
| 20 | + |
| 21 | + if (isFunction(this.formatValueToField)) |
| 22 | + val = this.formatValueToField(val); |
| 23 | + |
| 24 | + return val; |
| 25 | + }, |
| 26 | + |
| 27 | + set(newValue) { |
| 28 | + |
| 29 | + if (isFunction(this.formatValueToModel)) |
| 30 | + newValue = this.formatValueToModel(newValue); |
| 31 | + |
| 32 | + if (isFunction(this.schema.set)) { |
| 33 | + this.schema.set(this.model, newValue); |
| 34 | + console.log("model-updated via schema", this.model[this.schema.model]); |
| 35 | + this.$emit("model-updated", this.model[this.schema.model], this.schema.model); |
| 36 | + |
| 37 | + } else if (this.schema.model) { |
| 38 | + this.$set(this.model, this.schema.model, newValue); |
| 39 | + console.log("model-updated via normal", this.model[this.schema.model]); |
| 40 | + this.$emit("model-updated", this.model[this.schema.model], this.schema.model); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + |
| 46 | + watch: { |
| 47 | + value: function(newVal, oldVal) { |
| 48 | + // console.log("Changed", newVal, oldVal); |
| 49 | + if (isFunction(this.schema.onChanged)) { |
| 50 | + this.schema.onChanged(this.model, newVal, oldVal, this.schema); |
| 51 | + } |
| 52 | + |
| 53 | + if (this.$parent.options && this.$parent.options.validateAfterChanged === true){ |
| 54 | + this.validate(); |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + |
| 59 | + methods: { |
| 60 | + validate() { |
| 61 | + this.clearValidationErrors(); |
| 62 | + |
| 63 | + if (this.schema.validator && this.schema.readonly !== true && this.disabled !== true) { |
| 64 | + |
| 65 | + let validators = []; |
| 66 | + if (!isArray(this.schema.validator)) { |
| 67 | + validators.push(this.schema.validator.bind(this)); |
| 68 | + } else { |
| 69 | + each(this.schema.validator, (validator) => { |
| 70 | + validators.push(validator.bind(this)); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + each(validators, (validator) => { |
| 75 | + let err = validator(this.value, this.schema, this.model); |
| 76 | + if (err) { |
| 77 | + if (isArray(err)) |
| 78 | + Array.prototype.push.apply(this.schema.errors, err); |
| 79 | + else if (isString(err)) |
| 80 | + this.schema.errors.push(err); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + if (isFunction(this.schema.onValidated)) { |
| 87 | + this.schema.onValidated(this.model, this.schema.errors, this.schema); |
| 88 | + } |
| 89 | + |
| 90 | + return this.schema.errors; |
| 91 | + }, |
| 92 | + |
| 93 | + clearValidationErrors() { |
| 94 | + if (isUndefined(this.schema.errors)) |
| 95 | + this.$set(this.schema, "errors", []); // Be reactive |
| 96 | + else |
| 97 | + this.schema.errors.splice(0); // Clear |
| 98 | + } |
| 99 | + } |
93 | 100 | };
|
0 commit comments