@tripetto/block-dropdown
Version:
Dropdown block for Tripetto.
3 lines (2 loc) • 13.1 kB
JavaScript
/*! Tripetto Dropdown Block 7.0.0 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */
import{Collection as o,pgettext as e,Forms as t,insertVariable as i,isString as l,definition as n,name as d,alias as r,score as s,editor as a,affects as c,collection as u,tripetto as p,ConditionBlock as b,isFilledString as w,markdownifyToString as h,Markdown as g,Slots as N,isNumberFinite as S,L10n as m,lookupVariable as v,populateVariables as A,supplies as I,metadata as M,NodeBlock as D,npgettext as f,isBoolean as j,each as L,slots as k,conditions as y}from"@tripetto/builder";function x(o,e,t,i){var l,n=arguments.length,d=n<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,t):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)d=Reflect.decorate(o,e,t,i);else for(var r=o.length-1;r>=0;r--)(l=o[r])&&(d=(n<3?l(d):n>3?l(e,t,d):l(e,t))||d);return n>3&&d&&Object.defineProperty(e,t,d),d}class C extends o.Item{constructor(){super(...arguments),this.name=""}defineEditor(){this.editor.option({name:e("block:dropdown","Name"),form:{title:e("block:dropdown","Option name"),controls:[new t.Text("singleline",t.Text.bind(this,"name","")).action("@",i(this,"exclude")).autoFocus().autoSelect().enter(this.editor.close).escape(this.editor.close)]},locked:!0}),this.editor.group(e("block:dropdown","Options")),this.editor.option({name:e("block:dropdown","Identifier"),form:{title:e("block:dropdown","Identifier"),controls:[new t.Text("singleline",t.Text.bind(this,"value",void 0)),new t.Static(e("block:dropdown","If an option identifier is set, this identifier will be used as selected option value instead of the option label."))]},activated:l(this.value)});const o=this.ref.slots.select("score","feature");this.editor.option({name:e("block:dropdown","Score"),form:{title:e("block:dropdown","Score"),controls:[new t.Numeric(t.Numeric.bind(this,"score",void 0)).precision((null==o?void 0:o.precision)||0).digits((null==o?void 0:o.digits)||0).decimalSign((null==o?void 0:o.decimal)||"").thousands(!!(null==o?void 0:o.separator),(null==o?void 0:o.separator)||"").prefix((null==o?void 0:o.prefix)||"").prefixPlural((null==o?void 0:o.prefixPlural)||void 0).suffix((null==o?void 0:o.suffix)||"").suffixPlural((null==o?void 0:o.suffixPlural)||void 0)]},activated:!0,locked:!!o,disabled:!o})}}x([n("string"),d],C.prototype,"name",void 0),x([n("string","optional"),r],C.prototype,"value",void 0),x([n("number","optional"),s],C.prototype,"score",void 0),x([a],C.prototype,"defineEditor",null);var E="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCI+PHBhdGggZD0iTTE0LjUgNS41YS41MDIuNTAyIDAgMCAxLS4zNTQtLjE0NmwtMi0yYS41LjUgMCAwIDEgLjcwNy0uNzA3bDEuNjQ2IDEuNjQ2IDEuNjQ2LTEuNjQ2YS41LjUgMCAwIDEgLjcwNy43MDdsLTIgMmEuNDk4LjQ5OCAwIDAgMS0uMzU0LjE0NnoiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PHBhdGggZD0iTTE4LjUgMGgtMTdDLjY3MyAwIDAgLjY3MyAwIDEuNXY1QzAgNy4zMjcuNjczIDggMS41IDhINHYxMC41YzAgLjgyNy42NzMgMS41IDEuNSAxLjVoMTNjLjgyNyAwIDEuNS0uNjczIDEuNS0xLjV2LTE3YzAtLjgyNy0uNjczLTEuNS0xLjUtMS41ek0xIDYuNXYtNWEuNS41IDAgMCAxIC41LS41aDE3YS41LjUgMCAwIDEgLjUuNXY1YS41LjUgMCAwIDEtLjUuNWgtMTdhLjUuNSAwIDAgMS0uNS0uNXpNMTguNSAxOWgtMTNhLjUuNSAwIDAgMS0uNS0uNVY4aDEzLjVjLjE3NSAwIC4zNDMtLjAzMS41LS4wODZWMTguNWEuNS41IDAgMCAxLS41LjV6IiBjbGFzcz0idHJpcGV0dG8tZmlsbCIvPjxwYXRoIGQ9Ik0xNi41IDExaC05YS41LjUgMCAwIDEgMC0xaDlhLjUuNSAwIDAgMSAwIDF6bTAgM2gtOWEuNS41IDAgMCAxIDAtMWg5YS41LjUgMCAwIDEgMCAxem0wIDNoLTlhLjUuNSAwIDAgMSAwLTFoOWEuNS41IDAgMCAxIDAgMXoiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PC9zdmc+",U="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCI+PHBhdGggZD0ibTEwLjcwNyAxMC41IDUuNjQ2LTUuNjQ2YS41LjUgMCAwIDAtLjcwNy0uNzA3TDEwIDkuNzkzIDQuMzU0IDQuMTQ3YS41LjUgMCAwIDAtLjcwNy43MDdMOS4yOTMgMTAuNWwtNS42NDYgNS42NDZhLjUuNSAwIDAgMCAuNzA4LjcwN2w1LjY0Ni01LjY0NiA1LjY0NiA1LjY0NmEuNDk4LjQ5OCAwIDAgMCAuNzA4IDAgLjUuNSAwIDAgMCAwLS43MDdMMTAuNzA5IDEwLjV6IiBjbGFzcz0idHJpcGV0dG8tZmlsbCIvPjwvc3ZnPg==";let T=class extends b{get name(){return(this.option?this.option.name:e("block:dropdown","Nothing selected"))||this.type.label}get icon(){return this.option?E:U}get options(){return this.node&&this.node.block instanceof P&&this.node.block.options||void 0}defineEditor(){if(this.node&&this.options){const o=[];this.options.each((e=>{w(e.name)&&o.push({label:h(e.name,g.MarkdownFeatures.None),value:e})})),o.push({label:e("block:dropdown","Nothing selected"),value:void 0}),this.editor.form({title:this.node.label,controls:[new t.Dropdown(o,t.Dropdown.bind(this,"option",void 0))]})}}};x([c("#name"),c("#condition"),u("#options")],T.prototype,"option",void 0),x([a],T.prototype,"defineEditor",null),T=x([p({type:"condition",legacyBlock:!0,context:"@tripetto/block-dropdown",identifier:"@tripetto/block-dropdown",version:"7.0.0",icon:E,get label(){return e("block:dropdown","Dropdown option")}})],T);var z="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCI+PHBhdGggZD0iTTE4LjUgOEgxM1YzLjVhLjUuNSAwIDAgMC0uNS0uNWgtNmEuNS41IDAgMCAwLS41LjVWNkguNWEuNS41IDAgMCAwLS41LjV2MTBhLjUuNSAwIDAgMCAuNS41aDE4YS41LjUgMCAwIDAgLjUtLjV2LThhLjUuNSAwIDAgMC0uNS0uNXpNNyA0aDV2MTJIN1Y0ek0xIDdoNXY5SDFWN3ptMTcgOWgtNVY5aDV2N3oiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PHBhdGggZD0iTTkuNSAxMGEuNS41IDAgMCAxLS41LS41di00YS41LjUgMCAwIDEgMSAwdjRhLjUuNSAwIDAgMS0uNS41em0tNSAzaC0yYS41LjUgMCAwIDEtLjUtLjV2LTJhLjUuNSAwIDAgMSAuNS0uNUg0VjlIMi41YS41LjUgMCAwIDEgMC0xaDJhLjUuNSAwIDAgMSAuNS41djJhLjUuNSAwIDAgMS0uNS41SDN2MWgxLjVhLjUuNSAwIDAgMSAwIDF6bTEyLTNoLTJhLjUuNSAwIDAgMCAwIDFIMTZ2MWgtMS41YS41LjUgMCAwIDAgMCAxSDE2djFoLTEuNWEuNS41IDAgMCAwIDAgMWgyYS41LjUgMCAwIDAgLjUtLjV2LTRhLjUuNSAwIDAgMC0uNS0uNXoiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PC9zdmc+";let Y=class extends b{constructor(){super(...arguments),this.allowMarkdown=!0,this.mode="equal"}get label(){return""}get name(){const o=this.slot;if(o instanceof N.Numeric){const t=this.parse(o,this.value);switch(this.mode){case"between":return`${t} ≤ @${o.id} ≤ ${this.parse(o,this.to)}`;case"not-between":return`@${o.id} < ${t} ${e("block:dropdown","or")} @${o.id} > ${this.parse(o,this.to)}`;case"defined":return`@${o.id} ${e("block:dropdown","calculated")}`;case"undefined":return`@${o.id} ${e("block:dropdown","not calculated")}`;case"not-equal":return`@${o.id} ≠ ${t}`;case"above":case"below":case"equal":return`@${o.id} ${"above"===this.mode?">":"below"===this.mode?"<":"="} ${t}`}}return this.type.label}get title(){var o,e;return(null===(o=this.slot)||void 0===o?void 0:o.label)||(null===(e=this.node)||void 0===e?void 0:e.label)}parse(o,e){var t;return S(e)?o.toString(e,((o,e)=>m.locale.number(o,e,!1))):l(e)&&e&&(null===(t=v(this,e))||void 0===t?void 0:t.label)?`@${e}`:"\\_\\_"}defineEditor(){this.editor.form({title:e("block:dropdown","Compare mode"),controls:[new t.Radiobutton([{label:e("block:dropdown","Score is equal to"),value:"equal"},{label:e("block:dropdown","Score is not equal to"),value:"not-equal"},{label:e("block:dropdown","Score is lower than"),value:"below"},{label:e("block:dropdown","Score is higher than"),value:"above"},{label:e("block:dropdown","Score is between"),value:"between"},{label:e("block:dropdown","Score is not between"),value:"not-between"},{label:e("block:dropdown","Score is calculated"),value:"defined"},{label:e("block:dropdown","Score is not calculated"),value:"undefined"}],t.Radiobutton.bind(this,"mode","equal")).on((o=>{switch(i.visible("defined"!==o.value&&"undefined"!==o.value),n.visible("between"===o.value||"not-between"===o.value),o.value){case"equal":i.title=e("block:dropdown","If score equals");break;case"not-equal":i.title=e("block:dropdown","If score not equals");break;case"below":i.title=e("block:dropdown","If score is lower than");break;case"above":i.title=e("block:dropdown","If score is higher than");break;case"between":i.title=e("block:dropdown","If score is between");break;case"not-between":i.title=e("block:dropdown","If score is not between")}}))]});const o=(o,i,n)=>{var d;const r=this[o],s=this.slot,a=new t.Numeric(S(r)?r:0).label(e("block:dropdown","Use fixed number")).precision((null==s?void 0:s.precision)||0).digits((null==s?void 0:s.digits)||0).decimalSign((null==s?void 0:s.decimal)||"").thousands(!!(null==s?void 0:s.separator),(null==s?void 0:s.separator)||"").prefix((null==s?void 0:s.prefix)||"").prefixPlural((null==s?void 0:s.prefixPlural)||void 0).suffix((null==s?void 0:s.suffix)||"").suffixPlural((null==s?void 0:s.suffixPlural)||void 0).autoFocus("value"===o).escape(this.editor.close).enter((()=>("between"!==this.mode&&"not-between"!==this.mode||"to"===o)&&this.editor.close())).on((e=>{e.isFormVisible&&e.isObservable&&(this[o]=e.value)})),c=A(this,(o=>o instanceof N.Number||o instanceof N.Numeric),l(r)?r:void 0,!0,null===(d=this.slot)||void 0===d?void 0:d.id),u=new t.Dropdown(c,l(r)?r:"").label(e("block:dropdown","Use value of")).width("full").on((e=>{e.isFormVisible&&e.isObservable&&(this[o]=e.value||"")}));return this.editor.form({title:i,controls:[new t.Radiobutton([{label:e("block:dropdown","Number"),value:"number"},{label:e("block:dropdown","Value"),value:"variable",disabled:0===c.length}],l(r)?"variable":"number").on((o=>{a.visible("number"===o.value),u.visible("variable"===o.value),a.isObservable&&a.focus()})),a,u]}).visible(n)},i=o("value",e("block:dropdown","If score equals"),"defined"!==this.mode&&"undefined"!==this.mode),n=o("to",e("block:dropdown","And"),"between"===this.mode||"not-between"===this.mode)}};x([n,c("#name")],Y.prototype,"mode",void 0),x([n,c("#name")],Y.prototype,"value",void 0),x([n,c("#name")],Y.prototype,"to",void 0),x([a],Y.prototype,"defineEditor",null),Y=x([p({type:"condition",legacyBlock:!0,context:"@tripetto/block-dropdown",identifier:"@tripetto/block-dropdown:score",version:"7.0.0",icon:z,get label(){return e("block:dropdown","Verify score")}})],Y);let P=class extends D{constructor(){super(...arguments),this.options=o.of(C,this)}get label(){return f("block:dropdown","%2 (%1 option)","%2 (%1 options)",this.options.count,this.type.label)}get calculator(){return{option:{allowCastToNumber:!0}}}defineSlots(){this.dropdownSlot=this.slots.static({type:N.String,reference:"option",label:e("block:dropdown","Selected option"),exchange:["required","alias","exportable"]})}defineEditor(){this.editor.groups.general(),this.editor.name(),this.editor.description(),this.editor.placeholder(),this.editor.explanation();const o=this.editor.collection({collection:this.options,title:e("block:dropdown","Dropdown options"),placeholder:e("block:dropdown","Unnamed option"),autoOpen:!0,allowVariables:!0,allowImport:!0,allowExport:!0,allowDedupe:!0,showAliases:!0,sorting:"manual",emptyMessage:e("block:dropdown","Click the + button to add an option...")});this.editor.groups.settings(),this.editor.option({name:e("block:dropdown","Randomization"),form:{title:e("block:dropdown","Randomization"),controls:[new t.Checkbox(e("block:dropdown","Randomize the options (using [Fisher-Yates shuffle](%1))","https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle"),t.Checkbox.bind(this,"randomize",void 0,!0)).markdown()]},activated:j(this.randomize)}),this.editor.groups.options(),this.editor.required(this.dropdownSlot),this.editor.visibility(),this.editor.scores({target:this,collection:o,description:e("block:dropdown","Generates a score based on the selected option. Open the settings panel for each option to set the score.")}),this.editor.alias(this.dropdownSlot),this.editor.exportable(this.dropdownSlot)}defineConditions(){this.options.each((o=>{o.name&&this.conditions.template({condition:T,label:o.name,burst:"branch",props:{slot:this.dropdownSlot,option:o}})})),this.conditions.template({condition:T,label:e("block:dropdown","Nothing selected"),icon:U,separator:!0,props:{slot:this.dropdownSlot,option:void 0}});const o=this.slots.select("score","feature");if(o&&o.label){const t=this.conditions.group(o.label,z);L([{mode:"equal",label:e("block:dropdown","Score is equal to")},{mode:"not-equal",label:e("block:dropdown","Score is not equal to")},{mode:"below",label:e("block:dropdown","Score is lower than")},{mode:"above",label:e("block:dropdown","Score is higher than")},{mode:"between",label:e("block:dropdown","Score is between")},{mode:"not-between",label:e("block:dropdown","Score is not between")},{mode:"defined",label:e("block:dropdown","Score is calculated")},{mode:"undefined",label:e("block:dropdown","Score is not calculated")}],(e=>{t.template({condition:Y,label:e.label,autoOpen:"defined"!==e.mode&&"undefined"!==e.mode,props:{slot:o,mode:e.mode,value:0,to:"between"===e.mode||"not-between"===e.mode?0:void 0}})}))}}};x([n("items"),c("#label"),I("#slot","option")],P.prototype,"options",void 0),x([n("boolean","optional")],P.prototype,"randomize",void 0),x([M("calculator")],P.prototype,"calculator",null),x([k],P.prototype,"defineSlots",null),x([a],P.prototype,"defineEditor",null),x([y],P.prototype,"defineConditions",null),P=x([p({type:"node",legacyBlock:!0,identifier:"@tripetto/block-dropdown",version:"7.0.0",icon:E,get label(){return e("block:dropdown","Dropdown (single-select)")}})],P);export{P as Dropdown};