voc-lib-js 1.0.4 → 1.0.5
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/dist/main.global.js +202 -0
- package/package.json +5 -3
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var FormsLib = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/main.ts
|
|
22
|
+
var main_exports = {};
|
|
23
|
+
__export(main_exports, {
|
|
24
|
+
FormsLib: () => FormsLib
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// src/forms-lib/form-submit.ts
|
|
28
|
+
function submitForm(form) {
|
|
29
|
+
console.log("Form submitted:", form);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/forms-lib/form-render.ts
|
|
33
|
+
function renderForm(element, data) {
|
|
34
|
+
let containers = [];
|
|
35
|
+
if (typeof element === "string") {
|
|
36
|
+
containers = Array.from(document.querySelectorAll(element));
|
|
37
|
+
} else {
|
|
38
|
+
containers = [element];
|
|
39
|
+
}
|
|
40
|
+
if (containers.length === 0) return null;
|
|
41
|
+
function buildForm() {
|
|
42
|
+
const form = document.createElement("form");
|
|
43
|
+
form.id = data.id;
|
|
44
|
+
const title = document.createElement("h2");
|
|
45
|
+
title.textContent = data.form_name;
|
|
46
|
+
form.appendChild(title);
|
|
47
|
+
function applyValidation(el, validation) {
|
|
48
|
+
if (!validation) return;
|
|
49
|
+
if (validation.min_length !== void 0) {
|
|
50
|
+
el.minLength = validation.min_length;
|
|
51
|
+
}
|
|
52
|
+
if (validation.max_length !== void 0) {
|
|
53
|
+
el.maxLength = validation.max_length;
|
|
54
|
+
}
|
|
55
|
+
if (validation.regex) {
|
|
56
|
+
if (el instanceof HTMLInputElement) {
|
|
57
|
+
el.pattern = validation.regex;
|
|
58
|
+
} else {
|
|
59
|
+
el.dataset.pattern = validation.regex;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function renderField(field) {
|
|
64
|
+
if (!field.key && !field.label) return null;
|
|
65
|
+
const wrapper = document.createElement("div");
|
|
66
|
+
wrapper.className = "form-group";
|
|
67
|
+
if (field.label) {
|
|
68
|
+
const label = document.createElement("label");
|
|
69
|
+
label.htmlFor = field.key || "";
|
|
70
|
+
label.textContent = field.label + (field.required ? " *" : "");
|
|
71
|
+
wrapper.appendChild(label);
|
|
72
|
+
}
|
|
73
|
+
let input = null;
|
|
74
|
+
switch (field.type) {
|
|
75
|
+
case "textarea": {
|
|
76
|
+
const textarea = document.createElement("textarea");
|
|
77
|
+
if (field.key) textarea.name = field.key;
|
|
78
|
+
if (field.required) textarea.required = true;
|
|
79
|
+
applyValidation(textarea, field.validation);
|
|
80
|
+
input = textarea;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "select": {
|
|
84
|
+
const select = document.createElement("select");
|
|
85
|
+
if (field.key) select.name = field.key;
|
|
86
|
+
if (field.required) select.required = true;
|
|
87
|
+
if (field.options) {
|
|
88
|
+
field.options.forEach((opt) => {
|
|
89
|
+
const option = document.createElement("option");
|
|
90
|
+
option.value = opt.value;
|
|
91
|
+
option.textContent = opt.label;
|
|
92
|
+
if (field.default === opt.value) option.selected = true;
|
|
93
|
+
select.appendChild(option);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
input = select;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case "radio": {
|
|
100
|
+
const radioGroup = document.createElement("div");
|
|
101
|
+
radioGroup.className = "radio-group " + (field.direction || "vertical");
|
|
102
|
+
if (field.options) {
|
|
103
|
+
field.options.forEach((opt) => {
|
|
104
|
+
const radioWrapper = document.createElement("label");
|
|
105
|
+
radioWrapper.style.display = field.direction === "horizontal" ? "inline-block" : "block";
|
|
106
|
+
const radio = document.createElement("input");
|
|
107
|
+
radio.type = "radio";
|
|
108
|
+
radio.name = field.key || "";
|
|
109
|
+
radio.value = opt.value;
|
|
110
|
+
radioWrapper.appendChild(radio);
|
|
111
|
+
radioWrapper.appendChild(
|
|
112
|
+
document.createTextNode(opt.label)
|
|
113
|
+
);
|
|
114
|
+
radioGroup.appendChild(radioWrapper);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
input = radioGroup;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
default: {
|
|
121
|
+
const inp = document.createElement("input");
|
|
122
|
+
inp.type = field.type || "text";
|
|
123
|
+
if (field.key) inp.name = field.key;
|
|
124
|
+
if (field.required) inp.required = true;
|
|
125
|
+
applyValidation(inp, field.validation);
|
|
126
|
+
input = inp;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (input) wrapper.appendChild(input);
|
|
131
|
+
if (field.hint_text) {
|
|
132
|
+
const hint = document.createElement("small");
|
|
133
|
+
hint.className = "hint";
|
|
134
|
+
hint.textContent = field.hint_text;
|
|
135
|
+
wrapper.appendChild(hint);
|
|
136
|
+
}
|
|
137
|
+
return wrapper;
|
|
138
|
+
}
|
|
139
|
+
data.fields.forEach((field) => {
|
|
140
|
+
if (field.type === "row") {
|
|
141
|
+
const rowWrapper = document.createElement("div");
|
|
142
|
+
rowWrapper.className = "form-row";
|
|
143
|
+
if (field.label) {
|
|
144
|
+
const rowLabel = document.createElement("h3");
|
|
145
|
+
rowLabel.textContent = field.label;
|
|
146
|
+
rowWrapper.appendChild(rowLabel);
|
|
147
|
+
}
|
|
148
|
+
field.items.forEach((item) => {
|
|
149
|
+
const rendered = renderField(item);
|
|
150
|
+
if (rendered) rowWrapper.appendChild(rendered);
|
|
151
|
+
});
|
|
152
|
+
form.appendChild(rowWrapper);
|
|
153
|
+
} else {
|
|
154
|
+
const rendered = renderField(field);
|
|
155
|
+
if (rendered) form.appendChild(rendered);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
const submitBtn = document.createElement("button");
|
|
159
|
+
submitBtn.type = "submit";
|
|
160
|
+
submitBtn.textContent = "Submit";
|
|
161
|
+
form.appendChild(submitBtn);
|
|
162
|
+
return form;
|
|
163
|
+
}
|
|
164
|
+
const formElement = buildForm();
|
|
165
|
+
containers[0].appendChild(formElement);
|
|
166
|
+
return {
|
|
167
|
+
element: formElement,
|
|
168
|
+
// this method validates the form and returns an array of errors
|
|
169
|
+
validate: () => {
|
|
170
|
+
const errors = [];
|
|
171
|
+
const invalidElements = formElement.querySelectorAll(":invalid");
|
|
172
|
+
invalidElements.forEach((element2) => {
|
|
173
|
+
const message = element2.validationMessage;
|
|
174
|
+
errors.push({ element: element2, message });
|
|
175
|
+
});
|
|
176
|
+
return errors;
|
|
177
|
+
},
|
|
178
|
+
// this method submits the form to the portal vocphone server
|
|
179
|
+
submitForm: () => {
|
|
180
|
+
submitForm(formElement);
|
|
181
|
+
},
|
|
182
|
+
// This method gets the data from the form.
|
|
183
|
+
getValues: () => {
|
|
184
|
+
const formData = new FormData(formElement);
|
|
185
|
+
const values = {};
|
|
186
|
+
formData.forEach((value, key) => {
|
|
187
|
+
values[key] = value;
|
|
188
|
+
});
|
|
189
|
+
return values;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/forms-lib/index.ts
|
|
195
|
+
var forms_lib_default = {
|
|
196
|
+
renderForm
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// src/main.ts
|
|
200
|
+
var FormsLib = forms_lib_default;
|
|
201
|
+
return __toCommonJS(main_exports);
|
|
202
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "voc-lib-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "A JavaScript library for VocPhone",
|
|
5
5
|
"main": "./dist/main.cjs",
|
|
6
6
|
"module": "./dist/main.mjs",
|
|
7
|
+
"unpkg": "./dist/main.iife.js",
|
|
8
|
+
"jsdelivr": "./dist/main.iife.js",
|
|
7
9
|
"types": "./dist/main.d.ts",
|
|
8
10
|
"scripts": {
|
|
9
|
-
"build": "tsup src/main.ts --format cjs,esm --dts",
|
|
10
|
-
"up": "rimraf dist && npm run build && npm publish"
|
|
11
|
+
"build": "tsup src/main.ts --format cjs,esm,iife --global-name FormsLib --dts",
|
|
12
|
+
"up": "npm version patch && rimraf dist && npm run build && npm publish"
|
|
11
13
|
},
|
|
12
14
|
"repository": {
|
|
13
15
|
"type": "git",
|