UNPKG

tbxforms

Version:

A Torchbox-flavoured template pack for django-crispy-forms, adapted from crispy-forms-gds

2 lines (1 loc) 1.93 kB
(function(i,n){typeof exports=="object"&&typeof module!="undefined"?module.exports=n():typeof define=="function"&&define.amd?define(n):(i=typeof globalThis!="undefined"?globalThis:i||self,i.TbxForms=n())})(this,function(){"use strict";var i="";function n(e,c,t){let s=!1;if(c.length>1)c.forEach(function(a){a.checked&&t.includes(a.value)&&(s=!0)});else{const a=c.item(0);s=t.includes(a.value)||t.includes(Number(a.value))}e.hidden=!s,c.forEach(a=>a.setAttribute("aria-expanded",s.toString()))}function o(e){this.form=e;const c=this;e.querySelectorAll("*").forEach(function(t){if(t.dataset.conditionalFieldName&&t.dataset.conditionalFieldValues){const s=t.closest(".tbxforms-form-group")?t.closest(".tbxforms-form-group"):t,a=e.querySelectorAll('[name="'+t.dataset.conditionalFieldName+'"]');let l;try{l=JSON.parse(t.dataset.conditionalFieldValues)}catch(u){throw"Invalid JSON: "+u}s.classList.add("tbxforms-conditional"),a.forEach(function(u){u.addEventListener("change",function(){n(s,a,l)})}),n(s,a,l)}}),e.addEventListener("submit",function(){e.querySelectorAll("[hidden]").forEach(function(t){c.clearInput(t)})})}return o.prototype.clearInput=function(e){const c=this;switch(e.tagName){case"INPUT":switch(e.type){case"color":case"date":case"datetime-local":case"email":case"file":case"hidden":case"image":case"month":case"number":case"password":case"range":case"reset":case"search":case"tel":case"text":case"time":case"url":case"week":e.value="";break;case"radio":case"checkbox":e.checked=!1;break;default:console.debug(`Skipping unsupported node.type '${e.type}' while trying to clearInput().`)}break;case"TEXTAREA":e.value="";break;case"SELECT":e.selectedIndex=-1;break;case"DIV":case"FIELDSET":e.querySelectorAll("*").forEach(function(t){c.clearInput(t)});break;default:console.debug(`Skipping unsupported node.tagName '${e.tagName}' while trying to clearInput().`)}},o.selector=function(){return"form.tbxforms"},o});