export { forceDownload } from './forcedownload'; /** * * @param obj: simple javascript object, for example, the result returned from formObjToObj() */ export function objToParameterUrl(obj) { let parameter, parameterArray = []; for (const p in obj) { if (Object.prototype.hasOwnProperty.call(obj, p)) { const v = obj[p]; if (Array.isArray(v)) { for (let i = 0; i < v.length; i++) { parameter = p + '=' + encodeURIComponent(v[i]); parameterArray.push(parameter); } } else { parameter = p + '=' + encodeURIComponent(v); parameterArray.push(parameter); } } } return parameterArray.join('&'); } /** * @param obj: an object returned from formToObj() method, which adds additional function such as 'hasValue()' to the object * return: an uri with special characters encoded, the uri represents all the parameters sent from the form */ export function formObjToParameterUrl(obj) { let parameter, parameterArray = []; for (const p in obj) { if (Object.prototype.hasOwnProperty.call(obj, p)) { const v = obj[p]; if (Array.isArray(v)) { for (let i = 0; i < v.length; i++) { if (v[i].hasValue()) { parameter = p + '=' + v[i].value; parameterArray.push(parameter); } } } else { if (v.hasValue()) { parameter = p + '=' + v.value; parameterArray.push(parameter); } } } } return encodeURI(parameterArray.join('&')); } export function getFutureDateInDays(days) { // returns a date that is the number of provided days in the future const d = new Date(); d.setTime(d.getTime() + days * 24 * 60 * 60 * 1000); return d; } export function getFutureDateInMinutes(minutes) { // returns a date that is the number of provided minutes in the future const d = new Date(); d.setTime(d.getTime() + minutes * 60 * 1000); return d; } export function setCookie( cname, cvalue, path = '/', expirationDate = undefined ) { /** no return value * @cname: {string} cookie name * @cvalue {string} cookie value * @path: {string} what path the cookie belongs to, by default it belongs to current page * @expirationDate {date} */ let cookie = `${cname}=${cvalue};`; if (expirationDate) { cookie = cookie + 'expires=' + expirationDate.toUTCString(); } cookie = cookie + `;path=${path}`; document.cookie = cookie; } export function getCookie(cname) { /** return cookie value or empty string if nothing found * @cname: {string} cookie name */ let i, c, name = cname + '=', ca = document.cookie.split(';'); for (i = 0; i < ca.length; i++) { c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ''; } export function getCookieLayoutObj() { const layoutCookie = getCookie('layout'); let layoutObj; try { layoutObj = JSON.parse(layoutCookie); } catch (e) { console.error('Error parsing layout cookie.'); } if (!layoutObj) { layoutObj = {}; } return layoutObj; } export function setCookieLayoutObj(layoutObj, expirationInMinutes = 30) { const expiration = getFutureDateInMinutes(expirationInMinutes); setCookie('layout', JSON.stringify(layoutObj), undefined, expiration); }