import TemporalUtils from './temporal-utils'; export interface FormUtilsField { name: string; value: string; } export default class FormUtils { static nonAjaxPost(url: string, obj: any, newWindow?: boolean): void; static nonAjaxPost(url: string, fieldArr: FormUtilsField[], newWindow?: boolean): void; static nonAjaxPost( url: string, fieldArrOrObj: FormUtilsField[] | any, newWindow?: boolean, ): void { let arr: FormUtilsField[]; if (fieldArrOrObj.constructor === Array) { arr = fieldArrOrObj; } else { arr = []; for (const key in fieldArrOrObj) { if (Object.prototype.hasOwnProperty.call(fieldArrOrObj, key)) { arr.push({ name: key, value: fieldArrOrObj[key], }); } } } const formMethod = 'POST'; const formName = `frm${TemporalUtils.dateNowMs()}`; const form = document.createElement('form'); form.id = formName; form.method = formMethod; form.action = url; if (newWindow == true) { form.target = '_blank'; } // Set value via the property (not the attribute) so quotes / control chars // in the data are preserved exactly the way the original jQuery `.val()` set // them, instead of being HTML-encoded into the markup. arr.forEach((field) => { const input = document.createElement('input'); input.type = 'hidden'; input.name = field.name; input.value = field.value; form.appendChild(input); }); document.body.appendChild(form); form.submit(); setTimeout(() => { form.remove(); }, 50); } }