This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
installer.js
129 lines (108 loc) · 3.75 KB
/
installer.js
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
import {FormIcon} from "@/libraries/icon-facade";
import FormBuilder from "@/components/FormBuilder";
import FormRenderer from "@/components/FormRenderer";
import {CONTROLS} from "@/configs/controls";
import {STYLES} from "@/configs/styles";
import {VALIDATION_RULES} from "@/configs/validation";
import {IRegisterProperties} from "@/interfaces/register-properties.interface.ts";
const VueFormBuilderInstaller = function(
Vue,
properties = {}
) {
if (VueFormBuilderInstaller.installed) {
return
}
/**
*
* @type {IRegisterProperties}
*/
const defaultProperties = {
globalInjection : true,
validationErrorShowAlert: true,
validationErrorAlertText: "Your form got error(s), please fix it and submit again"
};
Object.assign(properties, defaultProperties);
// DI for Form-Builder
const formDI = {
getIcon: FormIcon.getSVG, // a method to get icon from IconFacade
};
// control extend?
if (properties.hasOwnProperty('controls')) {
extendingControls(properties.controls)
}
// style override?
if (properties.hasOwnProperty('styles')) {
Object.assign(STYLES, properties.styles)
}
// validation extend?
if (properties.hasOwnProperty('validations')) {
extendingValidations(properties.validations)
}
// validation closures
if (properties.hasOwnProperty('validationClosures')) {
formDI.validationClosures = properties.validationClosures
}
// show alert or not?
formDI.validationErrorShowAlert = properties.validationErrorShowAlert || true
formDI.validationErrorAlertText = properties.validationErrorAlertText
// disable control?
if (properties.disableControls && properties.disableControls.length) {
disableControls(properties.disableControls)
}
// For Event-Bus purpose
Vue.prototype.$formEvent = new Vue()
Vue.prototype.$form = formDI
// Register Form-Components
if (!properties.hasOwnProperty('globalInjection') || properties.globalInjection) {
Vue.component('FormBuilder', FormBuilder);
Vue.component('FormRenderer', FormRenderer);
}
// Mark as registered
VueFormBuilderInstaller.installed = true;
}
/**
* Extending Control from the users
* @param {Object} moreControlObject
*/
const extendingControls = function(moreControlObject) {
// validation if it does conflict or not
const allKeys = Object.keys(moreControlObject)
for (let iKey = 0; iKey < allKeys.length; iKey++) {
let key = allKeys[iKey]
// duplicated => error
if (CONTROLS.hasOwnProperty(key)) {
throw new TypeError(`Extend-Control-Error: Your '${key}' control is duplicated with our build-in Controls. Please change to another key name instead.`);
}
}
// eligible to extend now
Object.assign(CONTROLS, moreControlObject)
}
/**
* Extending Validation
* @param {Object} validationObj
*/
const extendingValidations = function (validationObj) {
// validation if it does conflict or not
const allKeys = Object.keys(validationObj)
for (let iKey = 0; iKey < allKeys.length; iKey++) {
let key = allKeys[iKey]
// duplicated => error
if (VALIDATION_RULES.hasOwnProperty(key)) {
throw new TypeError(`Extend-Validation-Error: Your '${key}' validation is duplicated with our build-in Validation. Please change to another key name instead.`);
}
}
// eligible to extend now
Object.assign(VALIDATION_RULES, validationObj)
}
/**
* Disable a list of controls by key
* @param {String[]} controlKeys
*/
const disableControls = function(controlKeys) {
controlKeys.forEach(
controlKey => CONTROLS[controlKey].isHidden = true
);
}
export {
VueFormBuilderInstaller
}