@tripetto/block-multiple-choice
Version:
Multiple choice block for Tripetto.
2 lines • 14.4 kB
JavaScript
/*! Tripetto Multiple Choice Block 7.0.1 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("TripettoRunner")):"function"==typeof define&&define.amd?define("TripettoBlockMultipleChoice",["TripettoRunner"],t):"object"==typeof exports?exports.TripettoBlockMultipleChoice=t(require("TripettoRunner")):e.TripettoBlockMultipleChoice=t(e.TripettoRunner)}(this,(function(e){return function(){"use strict";var t={974:function(e,t,o){var r,i=this&&this.__extends||(r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},r(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function o(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(o.prototype=t.prototype,new o)}),n=this&&this.__decorate||function(e,t,o,r){var i,n=arguments.length,u=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(u=(n<3?i(u):n>3?i(t,o,u):i(t,o))||u);return n>3&&u&&Object.defineProperty(t,o,u),u};Object.defineProperty(t,"__esModule",{value:!0}),t.MultipleChoiceCondition=void 0;var u=o(294),c=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.isChosen=function(){var e=this.valueOf(this.props.choice)||this.valueOf("choice");return!!e&&((0,u.isBoolean)(e.value)?e.value:e.reference===this.props.choice)},n([u.condition],t.prototype,"isChosen",null),t=n([(0,u.tripetto)({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-multiple-choice",alias:"multiple-choice"})],t)}(u.ConditionBlock);t.MultipleChoiceCondition=c},924:function(e,t,o){var r,i=this&&this.__extends||(r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},r(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function o(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(o.prototype=t.prototype,new o)}),n=this&&this.__decorate||function(e,t,o,r){var i,n=arguments.length,u=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(u=(n<3?i(u):n>3?i(t,o,u):i(t,o))||u);return n>3&&u&&Object.defineProperty(t,o,u),u};Object.defineProperty(t,"__esModule",{value:!0}),t.CounterCondition=void 0;var u=o(294),c=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.getValue=function(e,t){if((0,u.isString)(t)&&e instanceof u.Slots.Number){var o=this.variableFor(t);return o&&o.hasValue?e.toValue(o.value):void 0}return(0,u.isNumberFinite)(t)?t:void 0},t.prototype.verify=function(){var e=this.valueOf();if(e){var 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(0,u.isNumberFinite)(t)&&e.hasValue&&e.value<t;case"above":return(0,u.isNumberFinite)(t)&&e.hasValue&&e.value>t;case"between":case"not-between":var o=this.getValue(e.slot,this.props.to);return(0,u.isNumberFinite)(t)&&(0,u.isNumberFinite)(o)&&(e.hasValue&&e.value>=u.Num.min(t,o)&&e.value<=u.Num.max(t,o))===("between"===this.props.mode)}}return!1},n([u.condition],t.prototype,"verify",null),t=n([(0,u.tripetto)({type:"condition",legacyBlock:!0,identifier:"".concat("@tripetto/block-multiple-choice",":counter")})],t)}(u.ConditionBlock);t.CounterCondition=c},502:function(e,t,o){var r,i=this&&this.__extends||(r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},r(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function o(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(o.prototype=t.prototype,new o)}),n=this&&this.__decorate||function(e,t,o,r){var i,n=arguments.length,u=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(u=(n<3?i(u):n>3?i(t,o,u):i(t,o))||u);return n>3&&u&&Object.defineProperty(t,o,u),u};Object.defineProperty(t,"__esModule",{value:!0}),t.ScoreCondition=void 0;var u=o(294),c=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.getValue=function(e,t){if((0,u.isString)(t)&&e instanceof u.Slots.Numeric){var o=this.variableFor(t);return o&&o.hasValue?e.toValue(o.value):void 0}return(0,u.isNumberFinite)(t)?t:void 0},t.prototype.verify=function(){var e=this.valueOf();if(e){var 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(0,u.isNumberFinite)(t)&&e.hasValue&&e.value<t;case"above":return(0,u.isNumberFinite)(t)&&e.hasValue&&e.value>t;case"between":case"not-between":var o=this.getValue(e.slot,this.props.to);return(0,u.isNumberFinite)(t)&&(0,u.isNumberFinite)(o)&&(e.hasValue&&e.value>=u.Num.min(t,o)&&e.value<=u.Num.max(t,o))===("between"===this.props.mode);case"defined":return e.hasValue;case"undefined":return!e.hasValue}}return!1},n([u.condition],t.prototype,"verify",null),t=n([(0,u.tripetto)({type:"condition",legacyBlock:!0,identifier:"".concat("@tripetto/block-multiple-choice",":score")})],t)}(u.ConditionBlock);t.ScoreCondition=c},955:function(e,t,o){var r,i=this&&this.__extends||(r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},r(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function o(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(o.prototype=t.prototype,new o)}),n=this&&this.__decorate||function(e,t,o,r){var i,n=arguments.length,u=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(u=(n<3?i(u):n>3?i(t,o,u):i(t,o))||u);return n>3&&u&&Object.defineProperty(t,o,u),u};Object.defineProperty(t,"__esModule",{value:!0}),t.MultipleChoiceUndefinedCondition=void 0;var u=o(294),c=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.isUndefined=function(){var e=this,t=this.valueOf("choice");if(t)return!t.value;var o=this.slots;return!o||!o.each((function(t){var o=e.valueOf(t);return o&&o.value||!1}))},n([u.condition],t.prototype,"isUndefined",null),t=n([(0,u.tripetto)({type:"condition",legacyBlock:!0,identifier:"".concat("@tripetto/block-multiple-choice",":undefined")})],t)}(u.ConditionBlock);t.MultipleChoiceUndefinedCondition=c},724:function(e,t,o){var r,i=this&&this.__extends||(r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])},r(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function o(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(o.prototype=t.prototype,new o)}),n=this&&this.__decorate||function(e,t,o,r){var i,n=arguments.length,u=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(i=e[c])&&(u=(n<3?i(u):n>3?i(t,o,u):i(t,o))||u);return n>3&&u&&Object.defineProperty(t,o,u),u};Object.defineProperty(t,"__esModule",{value:!0}),t.MultipleChoice=void 0;var u=o(294);o(974);var c=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.counterSlot=t.valueOf("counter"),t.concatenationSlot=t.valueOf("concatenation","feature"),t.scoreSlot=t.valueOf("score","feature"),t.singleChoiceSlot=t.valueOf("choice","static",{modifier:function(e){if(e.value){if(!e.reference){var o=(0,u.findFirst)(t.props.choices,(function(t){return t.value===e.value}))||(0,u.findFirst)(t.props.choices,(function(t){return t.id===e.value}))||(0,u.findFirst)(t.props.choices,(function(t){return t.name===e.value}))||(0,u.findFirst)(t.props.choices,(function(t){return t.name.toLowerCase()===e.value.toLowerCase()}));return{value:o&&(o.value||o.name),reference:o&&o.id,display:null==o?void 0:o.name}}if(!(0,u.findFirst)(t.props.choices,(function(t){return t.id===e.reference})))return{value:void 0,reference:void 0,display:void 0}}},onChange:function(e){if(t.scoreSlot){var o=(0,u.findFirst)(t.props.choices,(function(t){return t.id===e.reference}));t.scoreSlot.set(o&&((null==o?void 0:o.score)||0))}}}),t}return i(t,e),Object.defineProperty(t.prototype,"required",{get:function(){return this.props.required||!1},enumerable:!1,configurable:!0}),t.prototype.transform=function(){var e=this;if(this.props.multiple){var t=(0,u.filter)((0,u.map)(this.props.choices,(function(t){return{id:t.id,exclusive:t.exclusive,valueRef:e.choiceSlot(t)}})),(function(e){var t;return!0===(null===(t=e.valueRef)||void 0===t?void 0:t.value)})).sort((function(e,t){var o,r;return((null===(o=t.valueRef)||void 0===o?void 0:o.time)||0)-((null===(r=e.valueRef)||void 0===r?void 0:r.time)||0)})),o=t.length&&t[0];if(o&&t.forEach((function(e){e.id!==o.id&&(o.exclusive||e.exclusive)&&(e.valueRef.value=!1)})),this.props.max){var r=this.props.max,i=0;t.forEach((function(e){var t;!0===(null===(t=e.valueRef)||void 0===t?void 0:t.value)&&++i>r&&(e.valueRef.value=!1)}))}if(this.counterSlot){var n=(0,u.filter)(t,(function(e){var t;return!0===(null===(t=e.valueRef)||void 0===t?void 0:t.value)})).length;this.counterSlot.set(n)}if(this.concatenationSlot){var c=[],a="",l=0;if((0,u.each)(this.props.choices,(function(t){var o,r=t.value||t.name||"";if(r&&!0===(null===(o=e.valueOf(t.id))||void 0===o?void 0:o.value))switch(e.props.formatSeparator){case"space":a+=(""===a?"":" ")+r;break;case"list":a+=(""===a?"":"\n")+r;break;case"bullets":a+=(""===a?"":"\n")+"- "+r;break;case"numbers":a+=(""===a?"":"\n")+"".concat(++l,". ").concat(r);break;case"conjunction":case"disjunction":c.push(r);break;case"custom":a+=(""===a?"":e.props.formatSeparatorCustom||"")+r;break;default:a+=(""===a?"":", ")+r}})),"conjunction"===this.props.formatSeparator||"disjunction"===this.props.formatSeparator)try{var s=new Intl.ListFormat(this.context.l10n.current||"en",{type:this.props.formatSeparator});a=s.format(c)}catch(e){a=u.Str.iterateToString(c,", ")}this.concatenationSlot.set(a)}}},t.prototype.score=function(e){var t=this;this.scoreSlot&&(0,u.isNumberFinite)(e.score)&&this.scoreSlot.set((0,u.reduce)(this.props.choices,(function(e,o){var r;return e+(!0===(null===(r=t.valueOf(o.id))||void 0===r?void 0:r.value)&&o.score||0)}),0))},t.prototype.choiceSlot=function(e){var t=this;return this.props.multiple&&this.valueOf(e.id,"dynamic",{confirm:!0,onChange:function(){t.transform(),t.score(e)},onContext:function(o,r){var i;t.scoreSlot&&(null===(i=r.contextualValueOf(t.scoreSlot))||void 0===i||i.set(!0===o.value?e.score||0:void 0))}})||void 0},t.prototype.choices=function(e){var t,o=this,r=0,i=this,n=(null===(t=this.props.choices)||void 0===t?void 0:t.map((function(t){var n=o.choiceSlot(t),c=o.context;return o.props.multiple&&o.props.max&&!0===(null==n?void 0:n.value)&&r++,{id:t.id,get name(){return(0,u.markdownifyToString)(t.name,c,void 0,!1,u.MarkdownFeatures.Formatting|u.MarkdownFeatures.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&&(0,u.markdownifyToString)(t.description,c,void 0,!0,u.MarkdownFeatures.Formatting|u.MarkdownFeatures.Hyperlinks)||void 0},url:t.url&&(0,u.markdownifyToURL)(t.url,o.context)||void 0,target:t.target,value:t.value,score:t.score,exclusive:t.exclusive,color:t.color,slot:n,tabIndex:e.tabIndex,get disabled(){return i.props.max&&r>=i.props.max&&!0!==(null==n?void 0:n.value)||!1}}})))||[];if(this.props.randomize&&n.length>1){if(!this.randomized||this.randomized.length!==n.length||(0,u.findFirst)(this.randomized,(function(e){var t;return(null===(t=n[e.index])||void 0===t?void 0:t.id)!==e.id}))){this.randomized=n.map((function(e,t){return{index:t,id:e.id}}));for(var c=this.randomized.length;--c;){var a=Math.floor(Math.random()*c),l=this.randomized[c];this.randomized[c]=this.randomized[a],this.randomized[a]=l}}return this.randomized.map((function(e){return n[e.index]}))}return this.randomized&&(this.randomized=void 0),n},t.prototype.validate=function(){var e,t=this;if(this.props.multiple&&(this.props.min||this.props.max)){var o=(0,u.filter)(this.props.choices,(function(e){var o;return!0===(null===(o=t.choiceSlot(e))||void 0===o?void 0:o.value)})).length;if(this.props.min&&o<this.props.min||this.props.max&&o>this.props.max)return!1}return!this.props.required||(this.props.multiple?!!(0,u.findFirst)(this.props.choices,(function(e){var o;return!0===(null===(o=t.choiceSlot(e))||void 0===o?void 0:o.value)})):(null===(e=this.singleChoiceSlot)||void 0===e?void 0:e.hasValue)||!1)},n([u.validator],t.prototype,"validate",null),t}(u.NodeBlock);t.MultipleChoice=c},294:function(t){t.exports=e}},o={};function r(e){var i=o[e];if(void 0!==i)return i.exports;var n=o[e]={exports:{}};return t[e].call(n.exports,n,n.exports,r),n.exports}var i={};return function(){var e=i;Object.defineProperty(e,"__esModule",{value:!0}),e.MultipleChoice=void 0,r(974),r(955),r(924),r(502);var t=r(724);Object.defineProperty(e,"MultipleChoice",{enumerable:!0,get:function(){return t.MultipleChoice}})}(),i}()}));