UNPKG

@tripetto/block-multiple-choice

Version:

Multiple choice block for Tripetto.

3 lines (2 loc) 7.81 kB
/*! Tripetto Multiple Choice Block 7.0.1 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */ import{tripetto as e,condition as t,ConditionBlock as i,isBoolean as o,isString as s,Slots as r,isNumberFinite as a,Num as l,NodeBlock as n,findFirst as c,filter as u,map as h,each as p,Str as d,reduce as v,markdownifyToString as f,MarkdownFeatures as m,markdownifyToURL as g,validator as b}from"@tripetto/runner";function x(e,t,i,o){var s,r=arguments.length,a=r<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,i):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,i,o);else for(var l=e.length-1;l>=0;l--)(s=e[l])&&(a=(r<3?s(a):r>3?s(t,i,a):s(t,i))||a);return r>3&&a&&Object.defineProperty(t,i,a),a}let y=class extends i{isChosen(){const e=this.valueOf(this.props.choice)||this.valueOf("choice");return!!e&&(o(e.value)?e.value:e.reference===this.props.choice)}};x([t],y.prototype,"isChosen",null),y=x([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-multiple-choice",alias:"multiple-choice"})],y);let S=class extends i{isUndefined(){const e=this.valueOf("choice");if(e)return!e.value;{const e=this.slots;return!e||!e.each((e=>{const t=this.valueOf(e);return t&&t.value||!1}))}}};x([t],S.prototype,"isUndefined",null),S=x([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-multiple-choice:undefined"})],S);let V=class extends i{getValue(e,t){if(s(t)&&e instanceof r.Number){const i=this.variableFor(t);return i&&i.hasValue?e.toValue(i.value):void 0}return a(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 a(t)&&e.hasValue&&e.value<t;case"above":return a(t)&&e.hasValue&&e.value>t;case"between":case"not-between":const i=this.getValue(e.slot,this.props.to);return a(t)&&a(i)&&(e.hasValue&&e.value>=l.min(t,i)&&e.value<=l.max(t,i))===("between"===this.props.mode)}}return!1}};x([t],V.prototype,"verify",null),V=x([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-multiple-choice:counter"})],V);let k=class extends i{getValue(e,t){if(s(t)&&e instanceof r.Numeric){const i=this.variableFor(t);return i&&i.hasValue?e.toValue(i.value):void 0}return a(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 a(t)&&e.hasValue&&e.value<t;case"above":return a(t)&&e.hasValue&&e.value>t;case"between":case"not-between":const i=this.getValue(e.slot,this.props.to);return a(t)&&a(i)&&(e.hasValue&&e.value>=l.min(t,i)&&e.value<=l.max(t,i))===("between"===this.props.mode);case"defined":return e.hasValue;case"undefined":return!e.hasValue}}return!1}};x([t],k.prototype,"verify",null),k=x([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-multiple-choice:score"})],k);class w extends n{constructor(){super(...arguments),this.counterSlot=this.valueOf("counter"),this.concatenationSlot=this.valueOf("concatenation","feature"),this.scoreSlot=this.valueOf("score","feature"),this.singleChoiceSlot=this.valueOf("choice","static",{modifier:e=>{if(e.value){if(!e.reference){const t=c(this.props.choices,(t=>t.value===e.value))||c(this.props.choices,(t=>t.id===e.value))||c(this.props.choices,(t=>t.name===e.value))||c(this.props.choices,(t=>t.name.toLowerCase()===e.value.toLowerCase()));return{value:t&&(t.value||t.name),reference:t&&t.id,display:null==t?void 0:t.name}}if(!c(this.props.choices,(t=>t.id===e.reference)))return{value:void 0,reference:void 0,display:void 0}}},onChange:e=>{if(this.scoreSlot){const t=c(this.props.choices,(t=>t.id===e.reference));this.scoreSlot.set(t&&((null==t?void 0:t.score)||0))}}})}get required(){return this.props.required||!1}transform(){if(this.props.multiple){const e=u(h(this.props.choices,(e=>({id:e.id,exclusive:e.exclusive,valueRef:this.choiceSlot(e)}))),(e=>{var t;return!0===(null===(t=e.valueRef)||void 0===t?void 0:t.value)})).sort(((e,t)=>{var i,o;return((null===(i=t.valueRef)||void 0===i?void 0:i.time)||0)-((null===(o=e.valueRef)||void 0===o?void 0:o.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 i=0;e.forEach((e=>{var o;!0===(null===(o=e.valueRef)||void 0===o?void 0:o.value)&&(i++,i>t&&(e.valueRef.value=!1))}))}if(this.counterSlot){const t=u(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="",i=0;if(p(this.props.choices,(o=>{var s;const r=o.value||o.name||"";if(r&&!0===(null===(s=this.valueOf(o.id))||void 0===s?void 0:s.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")+`${++i}. ${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 i=new Intl.ListFormat(this.context.l10n.current||"en",{type:this.props.formatSeparator});t=i.format(e)}catch(i){t=d.iterateToString(e,", ")}this.concatenationSlot.set(t)}}}score(e){this.scoreSlot&&a(e.score)&&this.scoreSlot.set(v(this.props.choices,((e,t)=>{var i;return e+(!0===(null===(i=this.valueOf(t.id))||void 0===i?void 0:i.value)&&t.score||0)}),0))}choiceSlot(e){return this.props.multiple&&this.valueOf(e.id,"dynamic",{confirm:!0,onChange:()=>{this.transform(),this.score(e)},onContext:(t,i)=>{var o;this.scoreSlot&&(null===(o=i.contextualValueOf(this.scoreSlot))||void 0===o||o.set(!0===t.value?e.score||0:void 0))}})||void 0}choices(e){var t;let i=0;const o=this,s=(null===(t=this.props.choices)||void 0===t?void 0:t.map((t=>{const s=this.choiceSlot(t),r=this.context;return this.props.multiple&&this.props.max&&!0===(null==s?void 0:s.value)&&i++,{id:t.id,get name(){return f(t.name,r,void 0,!1,m.Formatting|m.Hyperlinks)},get label(){return e.markdownifyToJSX(t.name,!1)},get description(){return t.description&&e.markdownifyToJSX(t.description,!0)||void 0},get descriptionString(){return t.description&&f(t.description,r,void 0,!0,m.Formatting|m.Hyperlinks)||void 0},url:t.url&&g(t.url,this.context)||void 0,target:t.target,value:t.value,score:t.score,exclusive:t.exclusive,color:t.color,slot:s,tabIndex:e.tabIndex,get disabled(){return o.props.max&&i>=o.props.max&&!0!==(null==s?void 0:s.value)||!1}}})))||[];if(this.props.randomize&&s.length>1){if(!this.randomized||this.randomized.length!==s.length||c(this.randomized,(e=>{var t;return(null===(t=s[e.index])||void 0===t?void 0:t.id)!==e.id}))){this.randomized=s.map(((e,t)=>({index:t,id:e.id})));let e=this.randomized.length;for(;--e;){const t=Math.floor(Math.random()*e),i=this.randomized[e];this.randomized[e]=this.randomized[t],this.randomized[t]=i}}return this.randomized.map((e=>s[e.index]))}return this.randomized&&(this.randomized=void 0),s}validate(){var e;if(this.props.multiple&&(this.props.min||this.props.max)){const e=u(this.props.choices,(e=>{var t;return!0===(null===(t=this.choiceSlot(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||(this.props.multiple?!!c(this.props.choices,(e=>{var t;return!0===(null===(t=this.choiceSlot(e))||void 0===t?void 0:t.value)})):(null===(e=this.singleChoiceSlot)||void 0===e?void 0:e.hasValue)||!1)}}x([b],w.prototype,"validate",null);export{w as MultipleChoice};