UNPKG

@asoftwareworld/form-builder-pro

Version:

ASW Form Builder Pro helps you with rapid development and designed web forms which includes several controls. The key feature of Form Builder is to make your content attractive and effective. We can customize our control at run time and preview the same b

626 lines (614 loc) 23 kB
/** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ function base64ToFile(base64Image) { const split = base64Image.split(','); const type = split[0].replace('data:', '').replace(';base64', ''); const byteString = atob(split[1]); const ab = new ArrayBuffer(byteString.length); const ia = new Uint8Array(ab); for (let i = 0; i < byteString.length; i += 1) { ia[i] = byteString.charCodeAt(i); } return new Blob([ab], { type }); } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ // Black 2x1 JPEG, with the following meta information set: // - EXIF Orientation: 6 (Rotated 90° CCW) // Source: https://github.com/blueimp/JavaScript-Load-Image const testAutoOrientationImageURL = 'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' + 'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' + 'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' + 'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' + 'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' + 'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='; function supportsAutomaticRotation() { return new Promise((resolve) => { const img = new Image(); img.onload = () => { // Check if browser supports automatic image orientation: const supported = img.width === 1 && img.height === 2; resolve(supported); }; img.src = testAutoOrientationImageURL; }); } function getTransformationsFromExifData(exifRotationOrArrayBuffer) { if (typeof exifRotationOrArrayBuffer === 'object') { exifRotationOrArrayBuffer = getExifRotation(exifRotationOrArrayBuffer); } switch (exifRotationOrArrayBuffer) { case 2: return { rotate: 0, flip: true }; case 3: return { rotate: 2, flip: false }; case 4: return { rotate: 2, flip: true }; case 5: return { rotate: 1, flip: true }; case 6: return { rotate: 1, flip: false }; case 7: return { rotate: 3, flip: true }; case 8: return { rotate: 3, flip: false }; default: return { rotate: 0, flip: false }; } } function getExifRotation(arrayBuffer) { const view = new DataView(arrayBuffer); if (view.getUint16(0, false) !== 0xffd8) { return -2; } const length = view.byteLength; let offset = 2; while (offset < length) { if (view.getUint16(offset + 2, false) <= 8) return -1; const marker = view.getUint16(offset, false); offset += 2; if (marker == 0xffe1) { if (view.getUint32((offset += 2), false) !== 0x45786966) { return -1; } const little = view.getUint16((offset += 6), false) == 0x4949; offset += view.getUint32(offset + 4, little); const tags = view.getUint16(offset, little); offset += 2; for (let i = 0; i < tags; i++) { if (view.getUint16(offset + i * 12, little) == 0x0112) { return view.getUint16(offset + i * 12 + 8, little); } } } else if ((marker & 0xff00) !== 0xff00) { break; } else { offset += view.getUint16(offset, false); } } return -1; } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ function getPositionForKey(key) { switch (key) { case 'ArrowUp': return 'top'; case 'ArrowRight': return 'right'; case 'ArrowDown': return 'bottom'; case 'ArrowLeft': default: return 'left'; } } function getInvertedPositionForKey(key) { switch (key) { case 'ArrowUp': return 'bottom'; case 'ArrowRight': return 'left'; case 'ArrowDown': return 'top'; case 'ArrowLeft': default: return 'right'; } } function getEventForKey(key, stepSize) { switch (key) { case 'ArrowUp': return { clientX: 0, clientY: stepSize * -1 }; case 'ArrowRight': return { clientX: stepSize, clientY: 0 }; case 'ArrowDown': return { clientX: 0, clientY: stepSize }; case 'ArrowLeft': default: return { clientX: stepSize * -1, clientY: 0 }; } } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ function percentage(percent, totalValue) { return (percent / 100) * totalValue; } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ function resizeCanvas(canvas, width, height) { const WIDTH_SOURCE = canvas.width; const HEIGHT_SOURCE = canvas.height; width = Math.round(width); height = Math.round(height); const RATIO_W = WIDTH_SOURCE / width; const RATIO_H = HEIGHT_SOURCE / height; const RATIO_W_HALF = Math.ceil(RATIO_W / 2); const RATIO_H_HALF = Math.ceil(RATIO_H / 2); const ctx = canvas.getContext('2d'); if (ctx) { const img = ctx.getImageData(0, 0, WIDTH_SOURCE, HEIGHT_SOURCE); const img2 = ctx.createImageData(width, height); const data = img.data; const data2 = img2.data; for (let j = 0; j < height; j++) { for (let i = 0; i < width; i++) { const x2 = (i + j * width) * 4; const CENTER_Y = j * RATIO_H; let weight = 0; let weights = 0; let WEIGHTS_ALPHA = 0; let GX_R = 0; let GX_G = 0; let GX_B = 0; let GX_A = 0; const XX_START = Math.floor(i * RATIO_W); const YY_START = Math.floor(j * RATIO_H); let XX_STOP = Math.ceil((i + 1) * RATIO_W); let YY_STOP = Math.ceil((j + 1) * RATIO_H); XX_STOP = Math.min(XX_STOP, WIDTH_SOURCE); YY_STOP = Math.min(YY_STOP, HEIGHT_SOURCE); for (let yy = YY_START; yy < YY_STOP; yy++) { const dy = Math.abs(CENTER_Y - yy) / RATIO_H_HALF; const CENTER_X = i * RATIO_W; const w0 = dy * dy; // pre-calc part of w for (let xx = XX_START; xx < XX_STOP; xx++) { const dx = Math.abs(CENTER_X - xx) / RATIO_W_HALF; const w = Math.sqrt(w0 + dx * dx); if (w >= 1) { // pixel too far continue; } // hermite filter weight = 2 * w * w * w - 3 * w * w + 1; const POS_X = 4 * (xx + yy * WIDTH_SOURCE); // alpha GX_A += weight * data[POS_X + 3]; WEIGHTS_ALPHA += weight; // colors if (data[POS_X + 3] < 255) { weight = (weight * data[POS_X + 3]) / 250; } GX_R += weight * data[POS_X]; GX_G += weight * data[POS_X + 1]; GX_B += weight * data[POS_X + 2]; weights += weight; } } data2[x2] = GX_R / weights; data2[x2 + 1] = GX_G / weights; data2[x2 + 2] = GX_B / weights; data2[x2 + 3] = GX_A / WEIGHTS_ALPHA; } } canvas.width = width; canvas.height = height; // draw ctx.putImageData(img2, 0, 0); } } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ function isDefined(value) { return value !== undefined && value !== null; } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ class ObjectUtils { static keyPressNumbersWithDecimal(event) { const charCode = event.which ? event.which : event.keyCode; if (charCode !== 46 && charCode > 31 && (charCode < 48 || charCode > 57)) { event.preventDefault(); return false; } return true; } static resetForm(formContainer) { formContainer.forEach((control) => { control.value = ''; if (control.controlType === 'select' || control.controlType === 'multi-select' || control.controlType === 'radio' || control.controlType === 'checkbox' || control.controlType === 'autocomplete') { control.options.forEach((option) => { option.isChecked = false; }); } }); this.resetChildControls(formContainer); return formContainer; } static resetChildControls(formContainer) { const columnControls = formContainer.filter((x) => x.controlType === 'columns'); columnControls.forEach((control) => { control.columns.forEach((column) => { column.components.forEach((component) => { if (component.controlType === 'columns') { this.resetChildControls(column.components); } else { component.value = ''; if (component.controlType === 'select' || component.controlType === 'multi-select' || component.controlType === 'radio' || component.controlType === 'checkbox' || component.controlType === 'autocomplete') { component.options.forEach((option) => { option.isChecked = false; }); } } }); }); }); } static generateUUID(control) { const id = this.UUID(); if (control) { control.guid = control.guid ? control.guid : id; control.id = control.id ? control.id : id; } return control; } static validateForm(formContainer) { const data = formContainer.filter((x) => x.isRequired); let requiredControls = []; data.forEach((control) => { if (!control.value?.length) { requiredControls.push(control); } }); this.validateFormChilds(formContainer, requiredControls); requiredControls = [...new Set(requiredControls.map((control) => control))]; const labels = requiredControls.map((control) => control.label); return { isFormValid: labels.length ? false : true, labels }; } static validateFormChilds(formContainer, requiredControls) { const columnControls = formContainer.filter((x) => x.controlType === 'columns'); columnControls.forEach((control) => { control.columns.forEach((column) => { column.components.forEach((component) => { if (component.controlType === 'columns') { this.validateFormChilds(column.components, requiredControls); } if (component.isRequired && !component.value?.length) { requiredControls.push(component); } }); }); }); } static calculateValue(operations) { if (operations.length) { let sum = Number(operations[0].value); operations.forEach((operation, index) => { if (index !== 0) { switch (operation.operationValue) { case '+': sum += Number(operation.value); break; case '-': sum -= operation.value; break; case '*': sum *= operation.value; break; case '/': sum /= operation.value; break; case 'x̄': sum /= index; break; } } }); return sum; } return 0; } static missingOrEmpty(value) { return value == null || value.length === 0; } static exists(value, allowEmptyString = false) { return value != null && (value !== '' || allowEmptyString); } static stringToUtf8Arr(sDOMStr) { let nChr; let nArrLen = 0; const nStrLen = sDOMStr.length; /* mapping... */ for (let nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) { nChr = sDOMStr.charCodeAt(nMapIdx); nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6; } const aBytes = new Uint8Array(nArrLen); /* transcription... */ for (let nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) { nChr = sDOMStr.charCodeAt(nChrIdx); if (nChr < 128) { /* one byte */ aBytes[nIdx++] = nChr; } else if (nChr < 0x800) { /* two bytes */ aBytes[nIdx++] = 192 + (nChr >>> 6); aBytes[nIdx++] = 128 + (nChr & 63); } else if (nChr < 0x10000) { /* three bytes */ aBytes[nIdx++] = 224 + (nChr >>> 12); aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63); aBytes[nIdx++] = 128 + (nChr & 63); } else if (nChr < 0x200000) { /* four bytes */ aBytes[nIdx++] = 240 + (nChr >>> 18); aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63); aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63); aBytes[nIdx++] = 128 + (nChr & 63); } else if (nChr < 0x4000000) { /* five bytes */ aBytes[nIdx++] = 248 + (nChr >>> 24); aBytes[nIdx++] = 128 + ((nChr >>> 18) & 63); aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63); aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63); aBytes[nIdx++] = 128 + (nChr & 63); } /* if (nChr <= 0x7fffffff) */ else { /* six bytes */ aBytes[nIdx++] = 252 + (nChr >>> 30); aBytes[nIdx++] = 128 + ((nChr >>> 24) & 63); aBytes[nIdx++] = 128 + ((nChr >>> 18) & 63); aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63); aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63); aBytes[nIdx++] = 128 + (nChr & 63); } } return aBytes; } /** * Converst string to ArrayBuffer */ static stringToArrayBuffer(dataString) { const data = new ArrayBuffer(dataString.length); const dataView = new Uint8Array(data); for (let i = 0; i < dataString.length; i++) { dataView[i] = dataString.charCodeAt(i); } return data; } /** * Converts Uint8Array to a string */ static utf8ArrToString(aBytes) { let sView = ''; for (let nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) { nPart = aBytes[nIdx]; sView += String.fromCharCode(nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */ /* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */ (nPart - 252) * 1073741824 + ((aBytes[++nIdx] - 128) << 24) + ((aBytes[++nIdx] - 128) << 18) + ((aBytes[++nIdx] - 128) << 12) + ((aBytes[++nIdx] - 128) << 6) + aBytes[++nIdx] - 128 : nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */ ((nPart - 248) << 24) + ((aBytes[++nIdx] - 128) << 18) + ((aBytes[++nIdx] - 128) << 12) + ((aBytes[++nIdx] - 128) << 6) + aBytes[++nIdx] - 128 : nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */ ((nPart - 240) << 18) + ((aBytes[++nIdx] - 128) << 12) + ((aBytes[++nIdx] - 128) << 6) + aBytes[++nIdx] - 128 : nPart > 223 && nPart < 240 && nIdx + 2 < nLen /* three bytes */ ? ((nPart - 224) << 12) + ((aBytes[++nIdx] - 128) << 6) + aBytes[++nIdx] - 128 : nPart > 191 && nPart < 224 && nIdx + 1 < nLen /* two bytes */ ? ((nPart - 192) << 6) + aBytes[++nIdx] - 128 : /* nPart < 127 ? */ /* one byte */ nPart); } return sView; } /** * Returns stringified jwk. */ static getSortedObjectString(obj) { return JSON.stringify(obj, Object.keys(obj).sort()); } static findDeviceSize(containerWidth) { if (containerWidth >= 1400) { return 0.375; } else if (containerWidth >= 1200) { return 0.775; } else if (containerWidth >= 992) { return 0.775; } else if (containerWidth >= 768) { return 0.775; } else if (containerWidth >= 576) { return 0.775; } else if (containerWidth < 576) { return 0.775; } else { return 0.775; } } static UUID() { if (typeof window !== 'undefined' && typeof window.crypto !== 'undefined' && typeof window.crypto.getRandomValues !== 'undefined') { // If we have a cryptographically secure PRNG, use that // http://stackoverflow.com/questions/6906916/collisions-when-generating-uuids-in-javascript const buf = new Uint16Array(8); window.crypto.getRandomValues(buf); return this.pad4(buf[0]) + this.pad4(buf[1]) + '-' + this.pad4(buf[2]) + '-' + this.pad4(buf[3]) + '-' + this.pad4(buf[4]) + '-' + this.pad4(buf[5]) + this.pad4(buf[6]) + this.pad4(buf[7]); } else { // Otherwise, just use Math.random // https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript // https://stackoverflow.com/questions/11605068/why-does-jshint-argue-against-bitwise-operators-how-should-i-express-this-code return this.random4() + this.random4() + '-' + this.random4() + '-' + this.random4() + '-' + this.random4() + '-' + this.random4() + this.random4() + this.random4(); } } static pad4(num) { let ret = num.toString(16); while (ret.length < 4) { ret = '0' + ret; } return ret; } static random4() { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ function equals(o1, o2) { if (o1 === o2) { return true; } if (o1 === null || o2 === null) { return false; } if (o1 !== o1 && o2 !== o2) { return true; } // NaN === NaN const t1 = typeof o1; const t2 = typeof o2; let length; let key; let keySet; if (t1 === t2 && t1 === 'object') { if (Array.isArray(o1)) { if (!Array.isArray(o2)) { return false; } const val = (length = o1.length); if (val === o2.length) { for (key = 0; key < length; key++) { if (!equals(o1[key], o2[key])) { return false; } } return true; } } else { if (Array.isArray(o2)) { return false; } keySet = Object.create(null); // tslint:disable-next-line:forin for (key in o1) { if (!equals(o1[key], o2[key])) { return false; } keySet[key] = true; } for (key in o2) { if (!(key in keySet) && typeof o2[key] !== 'undefined') { return false; } } return true; } } return false; } function isObject(item) { return item && typeof item === 'object' && !Array.isArray(item); } function mergeDeep(target, source) { const output = Object.assign({}, target); if (isObject(target) && isObject(source)) { Object.keys(source).forEach((key) => { if (isObject(source[key])) { if (!(key in target)) { Object.assign(output, { [key]: source[key] }); } else { output[key] = mergeDeep(target[key], source[key]); } } else { Object.assign(output, { [key]: source[key] }); } }); } return output; } /** * @license * Copyright ASW (A Software World) All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ /** * Generated bundle index. Do not edit. */ export { ObjectUtils, base64ToFile, equals, getEventForKey, getInvertedPositionForKey, getPositionForKey, getTransformationsFromExifData, isDefined, isObject, mergeDeep, percentage, resizeCanvas, supportsAutomaticRotation }; //# sourceMappingURL=asoftwareworld-form-builder-pro-utils.mjs.map