-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathformGenerator.vue
370 lines (305 loc) · 8.25 KB
/
formGenerator.vue
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<template lang="jade">
div
fieldset.vue-form-generator(v-if="schema != null")
template(v-for="field in fields")
.form-group(v-if="fieldVisible(field)", :class="getFieldRowClasses(field)")
label {{ field.label }}
span.help(v-if="field.help")
i.icon
.helpText {{{field.help}}}
.field-wrap
component(:is="getFieldType(field)", :disabled="fieldDisabled(field)", :model.sync="model", :schema.sync="field")
.buttons(v-if="field.buttons && field.buttons.length > 0")
button(v-for="btn in field.buttons", @click="btn.onclick(model, field)", :class="btn.classes") {{ btn.label }}
.hint(v-if="field.hint") {{ field.hint }}
.errors(v-if="field.errors && field.errors.length > 0")
span(v-for="error in field.errors", track-by="$index") {{ error }}
</template>
<script>
import Vue from "vue";
import {each, isFunction, isNil, isArray, isString} from "lodash";
// Load all fields from '../fields' folder
let Fields = require.context("./fields/", false, /^\.\/field([\w-_]+)\.vue$/);
let fieldComponents = {};
each(Fields.keys(), (key) => {
let compName = Vue.util.classify(key.replace(/^\.\//, "").replace(/\.vue/, ""));
fieldComponents[compName] = Fields(key);
});
export default {
components: fieldComponents,
props: [
"schema",
"options",
"model",
"multiple",
"isNewModel"
],
data () {
return {
errors: [] // Validation errors
};
},
computed: {
fields() {
let res = [];
if (this.schema) {
each(this.schema.fields, (field) => {
if (!this.multiple || field.multi === true)
res.push(field);
});
}
return res;
}
},
watch: {
// new model loaded
model: function(newModel, oldModel) {
if (oldModel == newModel) // model got a new property, skip
return;
// Model changed!
if (this.options.validateAfterLoad === true && this.isNewModel !== true)
this.validate();
else
this.clearValidationErrors();
}
},
compiled() {
// First load, running validation if neccessary
if (this.options && this.options.validateAfterLoad === true && this.isNewModel !== true)
this.validate();
else
this.clearValidationErrors();
},
methods: {
// Get style classes of field
getFieldRowClasses(field) {
let baseClasses = {
error: field.errors && field.errors.length > 0,
disabled: this.fieldDisabled(field),
readonly: field.readonly,
featured: field.featured,
required: field.required
};
if (isArray(field.styleClasses)) {
each(field.styleClasses, (c) => baseClasses[c] = true);
}
else if (isString(field.styleClasses)) {
baseClasses[field.styleClasses] = true;
}
baseClasses["field-" + field.type] = true;
return baseClasses;
},
// Get type of field 'field-xxx'. It'll be the name of HTML element
getFieldType(fieldSchema) {
return "field-" + fieldSchema.type;
},
// Get disabled attr of field
fieldDisabled(field) {
if (isFunction(field.disabled))
return field.disabled(this.model);
if (isNil(field.disabled))
return false;
return field.disabled;
},
// Get visible prop of field
fieldVisible(field) {
if (isFunction(field.visible))
return field.visible(this.model);
if (isNil(field.visible))
return true;
return field.visible;
},
// Validating the model properties
validate() {
this.clearValidationErrors();
each(this.$children, (child) => {
if (isFunction(child.validate))
{
let err = child.validate();
each(err, (err) => {
this.errors.push({
field: child.schema,
error: err
});
});
}
});
return this.errors.length == 0;
},
// Clear validation errors
clearValidationErrors() {
this.errors.splice(0);
each(this.$children, (child) => {
child.clearValidationErrors();
});
}
}
};
</script>
<style lang="sass">
$errorColor: #F00;
fieldset.vue-form-generator {
* {
box-sizing: border-box;
}
.form-control {
// Default Bootstrap .form-control style
display: block;
width: 100%;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
} // .form-control
span.help {
margin-left: 0.3em;
position: relative;
.icon {
display: inline-block;
width: 16px;
height: 14px;
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAA+UlEQVQ4ja3TS0oDQRAG4C8+lq7ceICICoLGK7iXuNBbeAMJuPVOIm7cqmDiIncIggg+cMZFaqCnZyYKWtB0df31V1VXdfNH6S2wD9CP8xT3KH8T9BiTcE7XBMOfyBcogvCFO9ziLWwFRosyV+QxthNsA9dJkEYlvazsQdi3sBv6Ol6TBLX+HWT3fcQZ3vGM5fBLk+ynAU41m1biCXvhs4OPBDuBpa6GxF0P8YAj3GA1d1qJfdoS4DOIcIm1DK9x8iaWeDF/SP3QU6zRROpjLDFLsFlibx1jJaMkSIGrWKntvItcyTBKzCcybsvc9ZmYz3kz9Ooz/b98A8yvW13B3ch6AAAAAElFTkSuQmCC');
background-repeat: no-repeat;
background-position: center center;
} // .icon
.helpText {
background-color: #444;
bottom: 30px;
color: #fff;
display: block;
left: 0px;
//margin-bottom: 15px;
opacity: 0;
padding: 20px;
pointer-events: none;
position: absolute;
text-align: justify;
width: 300px;
//transform: translateY(10%);
transition: all .25s ease-out;
box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.5);
border-radius: 6px;
a {
font-weight: bold;
text-decoration: underline;
} // a
} // .helpText
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.helpText:before {
bottom: -20px;
content: " ";
display: block;
height: 20px;
left: 0;
position: absolute;
width: 100%;
}
&:hover .helpText {
opacity: 1;
pointer-events: auto;
transform: translateY(0px);
}
} // span.help
.field-wrap {
display: flex;
.buttons {
white-space: nowrap;
margin-left: 4px;
}
button, input[type=submit] {
// Default Bootstrap button style
display: inline-block;
padding: 6px 12px;
margin: 0px;
font-size: 14px;
font-weight: normal;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
touch-action: manipulation;
cursor: pointer;
user-select: none;
color: #333;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
&:not(:last-child) {
margin-right: 4px;
}
&:hover {
color: #333;
background-color: #e6e6e6;
border-color: #adadad;
}
&:active {
color: #333;
background-color: #d4d4d4;
border-color: #8c8c8c;
outline: 0;
box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
}
} // button, input[submit]
} // .field-wrap
.hint {
font-style: italic;
font-size: 0.8em;
} // .hint
.form-group {
display: inline-block;
vertical-align: top;
width: 100%;
// margin: 0.5rem 0.26rem;
margin-bottom: 1rem;
label {
font-weight: 400;
}
&.featured {
label {
font-weight: bold;
}
}
&.required {
label:after {
content: "*";
font-weight: normal;
color: Red;
position: absolute;
padding-left: 0.2em;
font-size: 1em;
}
}
&.disabled {
label {
color: #666;
font-style: italic;
}
}
&.error {
input:not([type="checkbox"]), textarea, select {
border: 1px solid $errorColor;
background-color: rgba($errorColor, 0.15);
}
.errors {
color: $errorColor;
font-size: 0.80em;
span {
display: block;
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAiklEQVR4Xt2TMQoCQQxF3xdhu72MpZU3GU/meBFLOztPYrVWsQmEWSaMsIXgK8P8RyYkMjO2sAN+K9gTIAmDAlzoUzE7p4IFytvDCQWJKSStYB2efcAvqZFM0BcstMx5naSDYFzfLhh/4SmRM+6Agw/xIX0tKEDFufeDNRUc4XqLRz3qabVIf3BMHwl6Ktexn3nmAAAAAElFTkSuQmCC');
background-repeat: no-repeat;
padding-left: 17px;
padding-top: 0px;
margin-top: 0.2em;
font-weight: 600;
}
} // .errors
} // .error
} // .form-group
} // fieldset
</style>