(function(){const component={name:"submit",version:[6,7,2],ccm:"https://ccmjs.github.io/ccm/versions/ccm-19.0.0.js",config:{html:{entry:{class:"form-group"},label:{tag:"label",class:"item-label",inner:"%label%"},info:{tag:"span",class:"info",inner:[{tag:"input",type:"checkbox",id:"%id%"},{tag:"label",for:"%id%",inner:{tag:"span",class:"info-icon glyphicon glyphicon-info-sign"}},{tag:"span",class:"alert alert-info",inner:"%info%"}]},section:{class:"section",inner:[{class:"items"},{class:"buttons btn-group",inner:[{tag:"a",class:"add btn btn-default btn-sm",role:"button",inner:{tag:"span",class:"glyphicon glyphicon-plus"},onclick:"%add%"},{tag:"a",class:"del btn btn-default btn-sm",role:"button",inner:{tag:"span",class:"glyphicon glyphicon-minus"},onclick:"%del%"}]}]}},css:["ccm.load",{context:"head",url:"https://ccmjs.github.io/akless-components/libs/bootstrap/css/font-face.css"},"https://ccmjs.github.io/akless-components/libs/bootstrap/css/bootstrap.css","https://ccmjs.github.io/akless-components/submit/resources/default.css"],data:{store:["ccm.store"]}},Instance:function(){const self=this;let $;let inputs;let element;this.ready=(async()=>{$=this.ccm.helper;this.submit=this.component;this.submit.config=$.integrate(JSON.parse(this.config),this.submit.config);delete this.submit.config.inner;delete this.submit.config.data;if(this.entries&&!this.inner)convertData($.clone(this.entries));this.logger&&this.logger.log("ready",$.privatize(this,true));function convertData(data){self.inner=document.createDocumentFragment();data.forEach((entry,i)=>self.inner.appendChild(newItem(entry,i)));function newItem(entry,i){entry=$.format(entry,{nr:"<nr>"});if(!$.isObject(entry)||!entry.type)return $.html(entry);entry.id="item-"+(i+1);let entry_elem=$.html(self.html.entry);if(entry.label){entry_elem.appendChild($.html(self.html.label,entry));if(entry.info)entry_elem.querySelector(".item-label").appendChild($.html(self.html.info,entry))}delete entry.info;delete entry.id;switch(entry.type){case"color":case"date":case"datetime-local":case"email":case"file":case"hidden":case"month":case"number":case"password":case"range":case"search":case"tel":case"text":case"time":case"url":case"week":delete entry.label;entry.tag="input";entry.class="form-control";entry_elem.appendChild($.html(entry));break;case"checkbox":delete entry.label;entry.tag="input";entry_elem.appendChild($.html({class:"checkbox",inner:{tag:"label",inner:entry}}));break;case"multi-checkbox":entry.type="checkbox";case"radio":delete entry.label;entry.tag="input";entry.items.forEach(item=>{const label=item.label;delete item.label;item=$.integrate(entry,item);delete item.items;entry_elem.appendChild($.html({class:entry.type,inner:{tag:"label",inner:[item,label]}}))});break;case"multi-select":entry.multiple=true;case"select":delete entry.label;entry.tag="select";delete entry.type;entry.class="form-control";const items=entry.items;delete entry.items;const select_elem=$.html(entry);items.forEach(item=>{item.tag="option";if(item.label&&!item.inner){item.inner=item.label;delete item.label}select_elem.appendChild($.html(item))});entry_elem.appendChild(select_elem);break;case"textarea":delete entry.label;entry.tag="textarea";delete entry.type;entry.class="form-control";entry_elem.appendChild($.html(entry));break;case"contenteditable":delete entry.label;delete entry.type;entry.contenteditable=true;entry_elem.appendChild($.html(entry));break;case"object":entry.type="several";entry.items=[{label:"Key",name:"property",type:"text"},{label:"Value",name:"value",type:"text"}];case"several":if(entry.name){const callout_elem=$.html({class:"callout callout-primary"});const several_elem=$.html({tag:"several",name:entry.name});entry.items.forEach((item,i)=>callout_elem.appendChild(newItem(item,i)));several_elem.appendChild(callout_elem);entry_elem.appendChild(several_elem)}else{const several_elem=$.html({tag:"several"});const item_elem=newItem(entry.item);item_elem.classList.add("callout","callout-primary");several_elem.appendChild(item_elem);entry_elem.appendChild(several_elem)}break;case"submit":delete entry.label;entry.tag="input";entry.class="form-control";entry_elem.appendChild($.html(entry.name?{class:"callout",inner:entry}:entry));break;default:delete entry.label;entry.tag="input";entry.class="form-control";entry_elem.appendChild($.html({class:"callout",inner:entry}));break}return entry_elem}}});this.start=(async()=>{if(!this.inner)return;let dataset=await $.dataset(this.data);if(this.ignore)dataset=$.integrate(this.ignore.defaults,dataset,true);this.logger&&this.logger.log("start",$.clone(dataset));inputs=[];const inner=$.html(this.inner);await $.asyncForEach([...inner.querySelectorAll("several")],handleSeveral);await $.asyncForEach([...inner.querySelectorAll("input")],handleSpecial);$.fillForm(inner,dataset);const content=this.content&&await this.content.start({inner:inner,css:JSON.parse(this.config).css});$.setContent(this.element,content?content.root:inner);element=content?content.element:this.element;element.classList.add("main");const form=document.createElement("form");element.parentNode.replaceChild(form,element);form.appendChild(element);[...element.querySelectorAll("[name]")].forEach(elem=>{elem.onchange=(event=>{$.wait(0,()=>{if(!form.reportValidity())return;const value=$.deepValue(this.getValue(),elem.name);this.logger&&this.logger.log("change",{name:elem.name,value:$.clone(value)});this.onchange&&this.onchange.call(this,{name:elem.name,value:$.clone(value),event:event})})});if(this.disabled)elem.disabled=true});const submit=element.querySelector("input[type=submit]");submit&&this.no_submit_button&&$.removeElement(submit);if(!submit)return;form.onsubmit=(async event=>{event.preventDefault();this.user&&await this.user.login();let results=this.getValue();this.logger&&this.logger.log("submit",$.clone(results));this.onfinish&&await $.onFinish(this,$.clone(results))});async function handleSeveral(several){if(several.hasAttribute("name")){const input=$.html({tag:"input",type:"submit",name:several.getAttribute("name")});input.setAttribute("inner",several.innerHTML);$.setContent(several,input)}let template=$.html({class:"item"});while(several.childNodes.length)template.appendChild(several.childNodes[0]);template=template.cloneNode(true);const section=$.html(self.html.section,{add:addItem,del:delItem});$.replace(several,section);const name=template.querySelector("[name]").getAttribute("name");if(!dataset[name])return addItem(true);if($.isObject(dataset[name])){const obj=dataset[name];dataset[name]=[];for(const key in obj)dataset[name].push({property:key,value:obj[key]})}dataset[name].forEach((value,i)=>dataset[name+"."+i]=value);for(let i=0;i<dataset[name].length;i++)await addItem(true);delete dataset[name];async function addItem(initial){const items=section.querySelector(".items");const item=template.cloneNode(true);const input=item.querySelector("[name]");$.append(items,item);input.setAttribute("name",input.getAttribute("name")+"."+(items.childElementCount-1));input.onchange=(event=>{const value=$.deepValue(self.getValue(),input.name);self.logger&&self.logger.log("change",{name:input.name,value:$.clone(value)});self.onchange&&self.onchange.call(self,{name:input.name,value:$.clone(value),event:event})});[...item.querySelectorAll("nr")].forEach(nr=>$.replace(nr,$.html({tag:"span",inner:items.childElementCount})));if(input.tagName==="SEVERAL")await handleSeveral(input);await handleSpecial(input);initial!==true&&self.onchange&&self.onchange.call(self,{name:input.name,value:$.deepValue(self.getValue(),input.name)})}function delItem(){const items=section.querySelector(".items");if(!items.hasChildNodes())return;inputs=inputs.filter(input=>!items.lastChild.querySelector("[id]")||input.instance.index!==items.lastChild.querySelector("[id]").id);items.childElementCount&&items.removeChild(items.lastChild);self.onchange&&self.onchange.call(self)}}async function handleSpecial(input){if(!input||!input.type)return;const type=input.getAttribute("type");switch(type){case"hidden":if(!input.value)input.value=$.generateKey();case"button":case"checkbox":case"color":case"date":case"datetime-local":case"email":case"file":case"image":case"month":case"number":case"password":case"radio":case"range":case"reset":case"search":case"tel":case"text":case"time":case"url":case"week":break;default:if(!$.deepValue(self,type))return;if(type==="submit"&&!input.getAttribute("name"))return;let instance;instance=await $.deepValue(self,type).start($.integrate({data:{store:["ccm.store",{config:$.deepValue($.solveDotNotation(dataset),input.name)}],key:"config"},onchange:function(){const value=this.getValue();delete value.key;instance&&self.logger&&self.logger.log("change",{name:input.name,value:$.clone(value),builder:instance});instance&&self.onchange&&self.onchange.call(self,{name:input.name,value:$.clone(value),builder:instance})}},$.generateConfig(input)));$.replace(input,instance.root);inputs.push({instance:instance,name:input.name})}}});this.getValue=(()=>{let results=$.formData(element);inputs.forEach(input=>{const result=input.instance.getValue();delete result.key;const keys=Object.keys(result);if(keys.length===2&&keys.every(key=>["property","value"].includes(key)))result.property&&$.deepValue(results,input.name.substr(0,input.name.lastIndexOf("."))+"."+result.property,result.value);else $.deepValue(results,input.name,result)});return $.clone(results)})}};let b="ccm."+component.name+(component.version?"-"+component.version.join("."):"")+".js";if(window.ccm&&null===window.ccm.files[b])return window.ccm.files[b]=component;(b=window.ccm&&window.ccm.components[component.name])&&b.ccm&&(component.ccm=b.ccm);"string"===typeof component.ccm&&(component.ccm={url:component.ccm});let c=(component.ccm.url.match(/(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/)||["latest"])[0];if(window.ccm&&window.ccm[c])window.ccm[c].component(component);else{var a=document.createElement("script");document.head.appendChild(a);component.ccm.integrity&&a.setAttribute("integrity",component.ccm.integrity);component.ccm.crossorigin&&a.setAttribute("crossorigin",component.ccm.crossorigin);a.onload=function(){window.ccm[c].component(component);document.head.removeChild(a)};a.src=component.ccm.url}})();