@tripetto/block-checkboxes
Version:
Checkboxes block for Tripetto.
3 lines (2 loc) • 6.69 kB
JavaScript
/*! Tripetto Checkboxes Block 7.1.1 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */
import{tripetto as e,condition as t,ConditionBlock as o,isString as s,Slots as i,isNumberFinite as r,Num as a,NodeBlock as n,filter as l,map as c,each as u,Str as h,reduce as d,findFirst as v,validator as p}from"@tripetto/runner";function f(e,t,o,s){var i,r=arguments.length,a=r<3?t:null===s?s=Object.getOwnPropertyDescriptor(t,o):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,s);else for(var n=e.length-1;n>=0;n--)(i=e[n])&&(a=(r<3?i(a):r>3?i(t,o,a):i(t,o))||a);return r>3&&a&&Object.defineProperty(t,o,a),a}let m=class extends o{isChecked(){const e=this.valueOf();return e&&e.value===this.props.checked||!1}};f([t],m.prototype,"isChecked",null),m=f([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-checkboxes"})],m);let b=class extends o{allUnchecked(){const e=this.slots;return!e||!e.each((e=>{const t=this.valueOf(e);return t&&t.value||!1}))}};f([t],b.prototype,"allUnchecked",null),b=f([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-checkboxes:unchecked"})],b);let x=class extends o{getValue(e,t){if(s(t)&&e instanceof i.Number){const o=this.variableFor(t);return o&&o.hasValue?e.toValue(o.value):void 0}return r(t)?t:void 0}verify(){const e=this.valueOf();if(e){const t=this.getValue(e.slot,this.props.value);switch(this.props.mode){case"equal":return(e.hasValue?e.value:void 0)===t;case"not-equal":return(e.hasValue?e.value:void 0)!==t;case"below":return r(t)&&e.hasValue&&e.value<t;case"above":return r(t)&&e.hasValue&&e.value>t;case"between":case"not-between":const o=this.getValue(e.slot,this.props.to);return r(t)&&r(o)&&(e.hasValue&&e.value>=a.min(t,o)&&e.value<=a.max(t,o))===("between"===this.props.mode)}}return!1}};f([t],x.prototype,"verify",null),x=f([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-checkboxes:counter"})],x);let k=class extends o{getValue(e,t){if(s(t)&&e instanceof i.Numeric){const o=this.variableFor(t);return o&&o.hasValue?e.toValue(o.value):void 0}return r(t)?t:void 0}verify(){const e=this.valueOf();if(e){const t=this.getValue(e.slot,this.props.value);switch(this.props.mode){case"equal":return(e.hasValue?e.value:void 0)===t;case"not-equal":return(e.hasValue?e.value:void 0)!==t;case"below":return r(t)&&e.hasValue&&e.value<t;case"above":return r(t)&&e.hasValue&&e.value>t;case"between":case"not-between":const o=this.getValue(e.slot,this.props.to);return r(t)&&r(o)&&(e.hasValue&&e.value>=a.min(t,o)&&e.value<=a.max(t,o))===("between"===this.props.mode);case"defined":return e.hasValue;case"undefined":return!e.hasValue}}return!1}};f([t],k.prototype,"verify",null),k=f([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-checkboxes:score"})],k);class g extends n{constructor(){super(...arguments),this.counterSlot=this.valueOf("counter"),this.concatenationSlot=this.valueOf("concatenation","feature"),this.scoreSlot=this.valueOf("score","feature")}get required(){return this.props.required||!1}transform(){const e=l(c(this.props.checkboxes,(e=>({id:e.id,label:e.value||e.name||"",exclusive:e.exclusive,valueRef:this.valueOf(e.id)}))),(e=>{var t;return!0===(null===(t=e.valueRef)||void 0===t?void 0:t.value)})).sort(((e,t)=>{var o,s;return((null===(o=t.valueRef)||void 0===o?void 0:o.time)||0)-((null===(s=e.valueRef)||void 0===s?void 0:s.time)||0)})),t=e.length&&e[0];if(t&&e.forEach((e=>{e.id!==t.id&&(t.exclusive||e.exclusive)&&(e.valueRef.value=!1)})),this.props.max){const t=this.props.max;let o=0;e.forEach((e=>{var s;!0===(null===(s=e.valueRef)||void 0===s?void 0:s.value)&&(o++,o>t&&(e.valueRef.value=!1))}))}if(this.counterSlot){const t=l(e,(e=>{var t;return!0===(null===(t=e.valueRef)||void 0===t?void 0:t.value)})).length;this.counterSlot.set(t)}if(this.concatenationSlot){const e=[];let t="",o=0;if(u(this.props.checkboxes,(s=>{var i;const r=s.value||s.name||"";if(r&&!0===(null===(i=this.valueOf(s.id))||void 0===i?void 0:i.value))switch(this.props.formatSeparator){case"space":t+=(""===t?"":" ")+r;break;case"list":t+=(""===t?"":"\n")+r;break;case"bullets":t+=(""===t?"":"\n")+"- "+r;break;case"numbers":t+=(""===t?"":"\n")+`${++o}. ${r}`;break;case"conjunction":case"disjunction":e.push(r);break;case"custom":t+=(""===t?"":this.props.formatSeparatorCustom||"")+r;break;default:t+=(""===t?"":", ")+r}})),"conjunction"===this.props.formatSeparator||"disjunction"===this.props.formatSeparator)try{const o=new Intl.ListFormat(this.context.l10n.current||"en",{type:this.props.formatSeparator});t=o.format(e)}catch(o){t=h.iterateToString(e,", ")}this.concatenationSlot.set(t)}}score(e){this.scoreSlot&&r(e.score)&&this.scoreSlot.set(d(this.props.checkboxes,((e,t)=>{var o;return e+(!0===(null===(o=this.valueOf(t.id))||void 0===o?void 0:o.value)&&t.score||0)}),0))}checkboxSlot(e){return this.valueOf(e.id,"dynamic",{confirm:!0,onChange:()=>{this.transform(),this.score(e)},onContext:(t,o)=>{var s;this.scoreSlot&&(null===(s=o.contextualValueOf(this.scoreSlot))||void 0===s||s.set(!0===t.value?e.score||0:void 0))}})}isChecked(e){const t=this.checkboxSlot(e);return(null==t?void 0:t.value)||!1}check(e,t){const o=this.checkboxSlot(e);return!!o&&(o.value=t,o.value)}toggle(e){const t=this.checkboxSlot(e);t&&(t.value=!t.value)}checkboxes(e){var t;let o=0;const s=this,i=(null===(t=this.props.checkboxes)||void 0===t?void 0:t.map((t=>{const i=this.checkboxSlot(t);return this.props.max&&!0===(null==i?void 0:i.value)&&o++,{id:t.id,score:t.score,exclusive:t.exclusive,label:e.markdownifyToJSX(t.name,!1),description:t.description&&e.markdownifyToJSX(t.description,!0)||void 0,value:i,tabIndex:e.tabIndex,get disabled(){return s.props.max&&o>=s.props.max&&!0!==(null==i?void 0:i.value)||!1}}})))||[];if(this.props.randomize&&i.length>1){if(!this.randomized||this.randomized.length!==i.length||v(this.randomized,(e=>{var t;return(null===(t=i[e.index])||void 0===t?void 0:t.id)!==e.id}))){this.randomized=i.map(((e,t)=>({index:t,id:e.id})));let e=this.randomized.length;for(;--e;){const t=Math.floor(Math.random()*e),o=this.randomized[e];this.randomized[e]=this.randomized[t],this.randomized[t]=o}}return this.randomized.map((e=>i[e.index]))}return this.randomized&&(this.randomized=void 0),i}validate(){if(this.props.min||this.props.max){const e=l(this.props.checkboxes,(e=>{var t;return!0===(null===(t=this.checkboxSlot(e))||void 0===t?void 0:t.value)})).length;if(this.props.min&&e<this.props.min||this.props.max&&e>this.props.max)return!1}return!this.props.required||!!v(this.props.checkboxes,(e=>{var t;return!0===(null===(t=this.valueOf(e.id))||void 0===t?void 0:t.value)}))}}f([p],g.prototype,"validate",null);export{g as Checkboxes};