choices.field.js
1 lines
| 1 | (()=>{"use strict";function t(t){return t?.classList?.contains("jet-form-builder-choice")}function e(t){return t.closest(".jet-form-builder-choice--item")}const{InputData:i,ReactiveSet:n,ReactiveHook:o}=JetFormBuilderAbstract,{getParsedName:s}=JetFormBuilderFunctions;function r(){i.call(this),this.isSupported=t,this.setNode=function(t){t.jfbSync=this,this.nodes=Array.from(t.querySelectorAll(".jet-form-builder-choice--item input")),this.rawName=this.nodes[0].name,this.name=s(this.rawName),this.inputType="choice"},this.setValue=function(){this.value.current=this.isArray()?this.getMultipleValue():this.getSingleValue()},this.addListeners=function(){for(const t of this.nodes)t.addEventListener("change",()=>this.setValue()),this.addListenerForChoice(t);this.isArray()&&this.sanitize(t=>Array.isArray(t)?t:[t])},this.getMultipleValue=function(){return Array.from(this.nodes).filter(t=>t.checked).map(t=>t.value)},this.getSingleValue=function(){for(const t of this.nodes)if(t.checked)return t.value;return""},this.addListenerForChoice=function(t){const i=e(t);i.addEventListener("click",e=>this.toggleChoice(t,e)),this.enterKey=new o,this.isNativeControl(t)?t.addEventListener("keydown",this.handleEnterKey.bind(this)):i.addEventListener("keydown",e=>{this.handleEnterKey(e),this.handleSpaceKey(e,t),this.isArray()||(this.handleNextKey(e,t),this.handlePrevKey(e,t))})},this.toggleChoice=function(t,e){e.target.closest(".jet-form-builder-choice--item-control label")||(this.isArray()?this.value.toggle(t.value):this.value.current!==t.value?this.value.current=t.value:this.value.current="")},this.hasAutoScroll=function(){return this.isNativeControl(this.nodes[0])},this.focusRaw=function(){const[t]=this.nodes;this.isNativeControl(t)?i.prototype.focusRaw.call(this):e(t).focus({preventScroll:!0})}}r.prototype=Object.create(i.prototype),r.prototype.getReactive=function(){return new n},r.prototype.isNativeControl=function(t){return t.classList.contains("jet-form-builder-choice--item-control-input")},r.prototype.handleNextKey=function(t,e){if(!["ArrowRight","ArrowDown"].includes(t.key))return;t.preventDefault();const i=this.nextNode(e);this.switchChoice(t,i)},r.prototype.handlePrevKey=function(t,e){if(!["ArrowUp","ArrowLeft"].includes(t.key))return;t.preventDefault();const i=this.prevNode(e);this.switchChoice(t,i)},r.prototype.handleSpaceKey=function(t,e){["Spacebar"," "].includes(t.key)&&(t.preventDefault(),this.toggleChoice(e,t))},r.prototype.switchChoice=function(t,i){const n=e(i);this.toggleChoice(i,t),n.focus({preventScroll:!0})},r.prototype.nextNode=function(t){for(const[e,i]of this.nodes.entries())if(i===t)return this.nodes.hasOwnProperty(+e+1)?this.nodes[+e+1]:this.nodes[0]},r.prototype.prevNode=function(t){for(const[e,i]of this.nodes.entries())if(i===t)return this.nodes.at(e-1)};const c=r,{BaseSignal:a}=JetFormBuilderAbstract,{isEmpty:h}=JetFormBuilderFunctions;function u(){a.call(this),this.isRadio=null,this.indexedFirst=!1,this.isSupported=function(t,i){return!!e(t)},this.runSignal=function(){this.input.calcValue=0,this.indexedFirst=!1;for(const i of this.input.nodes){var t;i.checked=this.isRadio?this.input.value.current===i.value:this.input.value.current?.includes(i.value);const n=e(i);this.toggleTabIndex(i,n),n.classList.toggle("is-checked",i.checked),n.ariaChecked=i.checked,this.toggleImageControl(n,i.checked),i.checked&&(this.input.calcValue+=parseFloat(null!==(t=i.dataset?.calculate)&&void 0!==t?t:i.value))}},this.setInput=function(t){a.prototype.setInput.call(this,t),this.isRadio=!this.input.isArray()}}u.prototype=Object.create(a.prototype),u.prototype.toggleImageControl=function(t,e){const i=t.querySelectorAll(".jet-form-builder-choice--item-control-img");for(const t of i)t.style.display=e?t.classList.contains("checked")?"":"none":t.classList.contains("checked")?"none":""},u.prototype.toggleTabIndex=function(t,e){if(this.isRadio&&!this.input.isNativeControl(t))return h(this.input.value.current)?(this.setRawTabIndex(e,this.indexedFirst?-1:0),void(this.indexedFirst=!0)):void this.setRawTabIndex(e,t.checked?0:-1)},u.prototype.setRawTabIndex=function(t,e){e!==t.tabIndex&&(t.tabIndex=e)};const l=u,{addFilter:d}=JetPlugins.hooks;d("jet.fb.inputs","jet-form-builder/choices-field",function(t){return[c,...t]}),d("jet.fb.signals","jet-form-builder/choices-field",function(t){return[l,...t]},20)})(); |