-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlform.min.js
26 lines (26 loc) · 8.73 KB
/
urlform.min.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
"use strict";const QueryLocationQuery="query",QueryLocationFragment="fragment",DefaultFormOptions={FormParameters:[],prefix:"",shareURL:"#shareURL",shareURLBtn:"#shareURLBtn",shareURLArea:"#shareURLArea",clearBtn:"#clearBtn",defaultQueryLocation:QueryLocationFragment,callback:null,cleanURL:!1,localStorageNamespace:"URLFormJS_",Sanitized:!1,Inited:!1,ShareURLBtnElement:HTMLButtonElement,ClearBtnElement:HTMLButtonElement,formID:"",FormMode:!1,FormElement:HTMLFormElement};function Init(e){console.log("Initializing URLFormJS: ",e);let t={};return t=sanitizeFormOptions(e),t.ShareURLBtnElement=document.querySelector(t.shareURLBtn),t.ShareURLBtnElement!=null&&t.ShareURLBtnElement.addEventListener("click",()=>ShareURI(t)),t.ClearBtnElement=document.querySelector(t.clearBtn),t.ClearBtnElement!=null&&t.ClearBtnElement.addEventListener("click",()=>{Clear(t)}),isEmpty(t.formID)||(t.FormElement=document.getElementById(t.formID),t.FormElement!==null&&(t.FormMode=!0)),window.addEventListener("hashchange",function(){window.location.reload()}),t.Inited=!0,t}function Populate(e){let t={};for(let a of e.FormParameters)a.saveSetting&&(t[a.name]=getSavedSetting(a.name,e));let r=GetQuagParts(e).pairs,l={...t,...r};PopulateFromValues(l,e)}function PopulateFromValues(e,t){if(!t.Inited)throw new Error("URLFormJS: Init() must be called first to initialize the URLFormJS module.");SetForm(e,t),ShareURI(t)}async function SetForm(e,t){try{for(let r of t.FormParameters){let l=r.id,a=r.name,n="";isEmpty(l)&&(l=t.prefix+a),r.defaultValue!==void 0&&(n=r.defaultValue),e["-"+a]!==void 0&&r.type=="bool"&&(n=!1),a in e&&(n=e[a]),n===!0&&(n="true"),r.type==="bool"&&n===""&&e[a]!==void 0&&(n="true");let u=document.getElementById(l);u!==null&&(r.type=="bool"&&n=="true"&&(u.checked=!0),!isEmpty(n)&&r.type!=="bool"&&(u.value=n),r.saveSetting&&u.addEventListener("input",f=>{r.type=="bool"?setSavedSetting(a,f.target.checked,t):setSavedSetting(a,f.target.value,t)})),isEmpty(r.func)||await r.func(n),r.type=="bool"&&n==="true"&&(isEmpty(r.funcTrue)||await r.funcTrue(n))}}finally{isEmpty(t.callback)||t.callback()}}function getSavedSetting(e,t){return localStorage.getItem(t.localStorageNamespace+t.prefix+e)}function setSavedSetting(e,t,r){return localStorage.setItem(r.localStorageNamespace+r.prefix+e,t)}function sanitizeFormOptions(e){let t={...DefaultFormOptions};if(isEmpty(e))return t;if(!(!isEmpty(e.Sanitized)&&e.Sanitized===!0)){isEmpty(e.formID)||(t.formID=e.formID),isEmpty(e.prefix)||(t.prefix=e.prefix),isEmpty(e.clearBtn)||(t.clearBtn=e.clearBtn),isEmpty(e.shareURLArea)||(t.shareURLArea=e.shareURLArea),isEmpty(e.shareURL)||(t.shareURL=e.shareURL),isEmpty(e.shareURLBtn)||(t.shareURLBtn=e.shareURLBtn),isEmpty(e.defaultQueryLocation)||(t.defaultQueryLocation=e.defaultQueryLocation),isEmpty(e.callback)||(t.callback=e.callback),isEmpty(e.cleanURL)||(t.cleanURL=e.cleanURL),isEmpty(e.localStorageNamespace)||(t.localStorageNamespace=e.localStorageNamespace),e.defaultQueryLocation!==QueryLocationQuery&&(t.defaultQueryLocation=QueryLocationFragment);for(let r of e.FormParameters)r.queryLocation!==QueryLocationFragment&&r.queryLocation!==QueryLocationQuery&&(r.queryLocation=t.defaultQueryLocation),t.FormParameters.push(r);return t.Sanitized=!0,t}}function setShareURL(e,t){let r=document.querySelector(t.shareURL);r!==null&&(r.innerHTML=e.link(e));let l=document.querySelector(t.shareURLArea);l!==null&&(l.innerHTML=e)}async function ShareURI(e){let t=GetForm(e),r=GetQuagParts(e);var l=getURIBase();for(let a of e.FormParameters){let n=t[a.name];n===void 0&&(n=""),a.type=="bool"&&(a.defaultValue===!0&&n==""&&(n="false"),a.nonFormValue===!0&&a.name in r.pairs&&(n="true")),a.queryLocation===QueryLocationFragment?(l.searchParams.delete(a.name),r.fragment.pairs[a.name]=n):r.query.pairs[a.name]=n}return l.search=buildQueryString(r.query.pairs,r.query.extras,e),l.hash=fragmentToString(r.fragment,e),setShareURL(l.href,e),l}function getURIBase(){var e=new URL(window.location.origin+window.location.pathname);return e.search="",e.hash="",e}function fragmentToString(e,t){let r="#"+e.before;return r+=buildQueryString(e.pairs,e.extras,t),r+=e.after,r=="#"||r=="#?"?"":r}function buildQueryString(e,t,r){let l="?",a=!0;if(Object.keys(e).length!==0)for(let n in e){let i=e[n];if(i==="")continue;a?a=!1:l+="&";let u="=";r.FormParameters.find(c=>c.name==n).type==="bool"&&(i="",u="",e[n]==="false"&&(n="-"+n)),l+=n+u+i}if(Object.keys(t).length>0&&!r.cleanURL)for(let n in t)a?a=!1:l+="&",l+=n+"="+t[n];return l=="?"?"":l}function getPairs(e){if(isEmpty(e))return{};let t={},r=e.split("&");for(const l in r){let a=r[l].split("="),n=a[0],i=a[1];n.at(0)==="-"&&(n=removeNegativeFlag(n),i="false"),!isEmpty(n)&&(i==null&&(i=""),t[n]=decodeURIComponent(i))}return t}function GetQuagParts(e){function t(){let a={string:getFragmentString(),pairs:{},extras:{},before:"",query:"",after:""},n=a.string.split("?");if(n.length==0?a.query=n[0]:(a.before=n[0],a.query=n[1]),!isEmpty(a.query)){let i=a.query.split(":~:");i.length>1&&(a.query=i[0],a.after=":~:"+i[1])}return a.pairs=getPairs(a.query),JSON.parse(JSON.stringify(a))}let r={query:{string:decodeURIComponent(window.location.search.substring(1)),pairs:getPairs(window.location.search.substring(1)),extras:{}},fragment:t()};r.pairs={...r.query.pairs,...r.fragment.pairs};let l=[];for(let a of e.FormParameters)l.push(a.name);for(let a of Object.keys(r.query.pairs))a=removeNegativeFlag(a),l.includes(a)||(r.query.extras[a]=r.query.pairs[a],delete r.query.pairs[a]);for(let a of Object.keys(r.fragment.pairs))a=removeNegativeFlag(a),l.includes(a)||(r.fragment.extras[a]=r.fragment.pairs[a],delete r.fragment.pairs[a]);return r}function removeNegativeFlag(e){return e.at(0)==="-"?e.substring(1):e}function GetURLKeyValue(e){return isEmpty(e)&&(e=GetDefaultFormOptions()),GetQuagParts(e).pairs}function Serialize(e){return JSON.stringify(GetForm(e))}function GetForm(e,t){if(!e.Inited)throw new Error("URLFormJS: Init() must be called first to initialize the URLFormJS module.");let r={};if(!e.FormMode){for(let a of e.FormParameters){let n,i=a.name;isEmpty(a.id)||(i=a.id);let u=document.getElementById(e.prefix+i);if(u!==null)switch(a.type){default:n=u.value,isEmpty(n)&&(n="");break;case"bool":n=u.checked;break;case"number":n=Number(u.value),isEmpty(n)&&(n=0);break}isEmpty(n)?t&&(r[a.name]=n):r[a.name]=n}return r}let l=new FormData(e.FormElement);for(let[a,n]of l)(n=="true"||n=="on")&&(n=!0),(n=="false"||n=="unchecked")&&(n=!1),isEmpty(e.prefix)||(a=a.substring(e.prefix.length)),isEmpty(n)?t&&(r[a]=n):r[a]=n;return r}function GetFormElements(e){let t={};for(let r of e.FormParameters)t[r.name]=document.getElementById(e.prefix+r.name);return t}function getFragmentString(){let e=window.location.hash.split("#");return window.location.protocol!=="file:"&&!navigator.userAgent.includes("Firefox")&&(e=performance.getEntriesByType("navigation")[0].name.split("#")),e.length==1?"":decodeURIComponent(e[1])}function Clear(e){if(!e.Inited)throw new Error("URLFormJS: Init() must be called first to initialize the URLFormJS module.");if(e.FormMode){for(let r of FormOptions.FormElement.elements)r.type==="checkbox"?r.checked=!1:r.value="";return}for(let r of e.FormParameters){let l=e.prefix+r.name,a=r.id;if(isEmpty(a)&&(a=l),r.type=="bool"){let i=document.getElementById(a);i!=null&&(i.checked=!1);continue}let n=document.getElementById(a);n!=null&&(n.value="")}var t=new URL(window.location.origin+window.location.pathname);setShareURL(t.href,e)}function GetDefaultFormOptions(){return DefaultFormOptions}function SetURLNoReload(e){window.history.pushState({},"",e)}function IsEmpty(e){return isEmpty(GetForm(e))}function isEmpty(e){return typeof e=="function"?!1:Array.isArray(e)&&e.length==0?!0:e===Object(e)?Object.keys(e).length===0:!isBool(e)}function isBool(e){return!(e===!1||e==="false"||e===void 0||e==="undefined"||e===""||e===0||e==="0"||e===null||e==="null"||e==="NaN"||Number.isNaN(e)||e===Object(e))}
//# sourceMappingURL=urlform.min.js.map
// UMD export see https://github.com/Cyphrme/UMD_tutorial
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.URLForm = {}))
})(this, (function (exports) {
exports.Init = Init
exports.SetURLNoReload = SetURLNoReload
exports.PopulateFromValues = PopulateFromValues
exports.Populate = Populate
exports.Serialize = Serialize
exports.GetForm = GetForm
exports.GetFormElements = GetFormElements
exports.GetURLKeyValue = GetURLKeyValue
exports.GetQuagParts = GetQuagParts
exports.SetForm = SetForm
exports.Clear = Clear
exports.IsEmpty = IsEmpty
exports.GetDefaultFormOptions = GetDefaultFormOptions
exports.ShareURI = ShareURI
Object.defineProperty(exports, '__esModule', {
value: true
})
}))