UNPKG

@tripetto/block-date

Version:

Date block for Tripetto.

3 lines (2 loc) 12.8 kB
/*! Tripetto Date Block 4.0.3 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */ import{definition as e,affects as t,tripetto as i,pgettext as o,editor as a,ConditionBlock as l,Slots as d,isNumberFinite as s,L10n as n,isString as r,lookupVariable as u,Forms as m,populateVariables as b,slots as c,conditions as h,NodeBlock as g,DateTime as M,insertVariable as A,isBoolean as D,isNumber as I,each as w}from"@tripetto/builder";function p(e,t,i,o){var a,l=arguments.length,d=l<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,i):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)d=Reflect.decorate(e,t,i,o);else for(var s=e.length-1;s>=0;s--)(a=e[s])&&(d=(l<3?a(d):l>3?a(t,i,d):a(t,i))||d);return l>3&&d&&Object.defineProperty(t,i,d),d}let x=class extends l{constructor(){super(...arguments),this.allowMarkdown=!0,this.mode="equal"}get label(){return""}get name(){const e=this.slot;if(e instanceof d.Date){const t=this.parse(e,this.value);switch(this.mode){case"between":return`${t} ≤ @${e.id} ≤ ${this.parse(e,this.to)}`;case"not-between":return`@${e.id} < ${t} ${o("block:date","or")} @${e.id} > ${this.parse(e,this.to)}`;case"defined":return`@${e.id} ${o("block:date","not empty")}`;case"undefined":return`@${e.id} ${o("block:date","empty")}`;case"before":case"after":case"equal":case"not-equal":return`@${e.id} ${"after"===this.mode?">":"before"===this.mode?"<":"not-equal"===this.mode?"≠":"="} ${t}`}}return this.type.label}get title(){var e;const t=this.node;return(null==t?void 0:t.block)instanceof L&&t.label+(t.block.range?" / "+(null===(e=this.slot)||void 0===e?void 0:e.label):"")||void 0}parse(e,t){var i;return s(t)?e.supportsTime?n.locale.dateTimeShort(e.toValue(t),!0):n.locale.dateShort(e.toValue(t),!0):r(t)?t&&(null===(i=u(this,t))||void 0===i?void 0:i.label)?`@${t}`:"\\_\\_":"`"+o("block:date","Now").toUpperCase()+"`"}defineEditor(){const e=this.slot instanceof d.Date&&this.slot.supportsTime;this.editor.form({title:o("block:date","Compare mode"),controls:[new m.Radiobutton([{label:o("block:date","Date is equal to"),value:"equal"},{label:o("block:date","Date is not equal to"),value:"not-equal"},{label:o("block:date","Date is before"),value:"before"},{label:o("block:date","Date is after"),value:"after"},{label:o("block:date","Date is between"),value:"between"},{label:o("block:date","Date is not between"),value:"not-between"},{label:o("block:date","Date is not empty"),value:"defined"},{label:o("block:date","Date is empty"),value:"undefined"}],m.Radiobutton.bind(this,"mode","equal")).on((e=>{switch(i.visible("defined"!==e.value&&"undefined"!==e.value),a.visible("between"===e.value||"not-between"===e.value),e.value){case"equal":i.title=o("block:date","If date equals");break;case"not-equal":i.title=o("block:date","If date not equals");break;case"before":i.title=o("block:date","If date is before");break;case"after":i.title=o("block:date","If date is after");break;case"between":i.title=o("block:date","If date is between");break;case"not-between":i.title=o("block:date","If date is not between")}}))]});const t=(t,i,a)=>{var l;const n=this[t],u=b(this,(e=>e instanceof d.Date),r(n)?n:void 0,!0,null===(l=this.slot)||void 0===l?void 0:l.id),c=new m.DateTime(s(n)?n:L.getToday("to"===t?"end":"begin")).label(o("block:date",e?"Use fixed date/time":"Use fixed date")).features(1|(e?8:32)).years((new Date).getFullYear()-150,(new Date).getFullYear()+50).zone("UTC").width("full").required().on((e=>{e.isFormVisible&&e.isObservable&&(this[t]=e.value)})),h=new m.Dropdown(u,r(n)?n:"").label(o("block:date","Use value of")).width("full").on((e=>{e.isFormVisible&&e.isObservable&&(this[t]=e.value||"")}));return this.editor.form({title:i,controls:[new m.Radiobutton([{label:o("block:date",e?"Current date/time":"Current date"),value:"current"},{label:o("block:date",e?"Fixed date/time":"Fixed date"),value:"date"},{label:o("block:date","Value"),value:"variable",disabled:0===u.length}],r(n)?"variable":s(n)?"date":"current").on((e=>{c.visible("date"===e.value),h.visible("variable"===e.value),"current"===e.value&&(this[t]=void 0)})),c,h]}).visible(a)},i=t("value",o("block:date","If date equals"),"defined"!==this.mode&&"undefined"!==this.mode),a=t("to",o("block:date","And"),"between"===this.mode||"not-between"===this.mode)}};p([e,t("#name")],x.prototype,"mode",void 0),p([e,t("#name")],x.prototype,"value",void 0),p([e,t("#name")],x.prototype,"to",void 0),p([a],x.prototype,"defineEditor",null),x=p([i({type:"condition",legacyBlock:!0,context:"@tripetto/block-date",identifier:"@tripetto/block-date",version:"4.0.3",icon:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCI+PHBhdGggZD0iTTE4LjUgMkgxNnYtLjVhLjUuNSAwIDAgMC0xIDBWMkg1di0uNWEuNS41IDAgMCAwLTEgMFYySDEuNUMuNjczIDIgMCAyLjY3MyAwIDMuNXYxNGMwIC44MjcuNjczIDEuNSAxLjUgMS41aDE3Yy44MjcgMCAxLjUtLjY3MyAxLjUtMS41di0xNGMwLS44MjctLjY3My0xLjUtMS41LTEuNXptLTE3IDFINHYxLjVhLjUuNSAwIDAgMCAxIDBWM2gxMHYxLjVhLjUuNSAwIDAgMCAxIDBWM2gyLjVhLjUuNSAwIDAgMSAuNS41VjZIMVYzLjVhLjUuNSAwIDAgMSAuNS0uNXptMTcgMTVoLTE3YS41LjUgMCAwIDEtLjUtLjVWN2gxOHYxMC41YS41LjUgMCAwIDEtLjUuNXoiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PHBhdGggZD0iTTguNSAxNS41YS41MDIuNTAyIDAgMCAxLS4zNTQtLjE0NmwtMi0yYS41LjUgMCAwIDEgLjcwNy0uNzA3bDEuNjQ2IDEuNjQ2IDQuNjQ2LTQuNjQ2YS41LjUgMCAwIDEgLjcwNy43MDdsLTUgNWEuNDk4LjQ5OCAwIDAgMS0uMzU0LjE0NnoiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PC9zdmc+",get label(){return o("block:date","Verify date")}})],x);var S;let L=S=class extends g{get label(){return this.range?o("block:date","Date range"):this.time?o("block:date","Date with time"):this.type.label}static getToday(e){return M.UTCToday+("end"===e?86399999:0)}defineSlot(){if(this.dateSlot=this.slots.static({type:d.Date,reference:"date",label:this.range?o("block:date","Date from"):this.time?o("block:date","Date with time"):S.label,required:this.required,alias:this.alias,exportable:this.exportable,exchange:["required","alias","exportable","precision","minimum","maximum"]}),this.dateSlot.precision=this.time?"minutes":"days",this.dateSlot.minimum=this.minimum,this.dateSlot.maximum=this.maximum,this.range){const e=this.slots.static({type:d.Date,reference:"to",label:o("block:date","Date to"),required:this.required,alias:this.alias,exportable:this.exportable,exchange:["required","alias","exportable","precision","minimum","maximum"],pipe:"to"});e.precision=this.dateSlot.precision,e.minimum=this.dateSlot.minimum,e.maximum=this.dateSlot.maximum}else this.slots.delete("to","static")}defineEditor(){const e=e=>1|(e?8:32),t=new m.Text("singleline",m.Text.bind(this,"placeholder",void 0,"")).placeholder(o("block:date","Type placeholder for the range to date field...")).action("@",A(this)).visible(this.range||!1);this.editor.name(),this.editor.description(),this.editor.placeholder(t),this.editor.explanation(),this.editor.groups.settings(),this.editor.option({name:o("block:date","Time"),form:{title:o("block:date","Time"),controls:[new m.Checkbox(o("block:date","Allow a time to be set"),m.Checkbox.bind(this,"time",void 0,!0)).on((t=>{i.features(e(t.isFeatureEnabled&&t.isChecked)),a.features(e(t.isFeatureEnabled&&t.isChecked))})),new m.Static(o("block:date","When enabled this will allow the user to set a specific time along with the date."))]},activated:D(this.time)}),this.editor.option({name:o("block:date","Range"),form:{title:o("block:date","Range"),controls:[new m.Checkbox(o("block:date","Enable date range"),m.Checkbox.bind(this,"range",void 0,!0)).on((()=>{t.visible(this.range||!1)})),new m.Static(o("block:date","When enabled this will display two date input fields. One for the *from* date and another one for the *to* date.")).markdown()]},activated:D(this.range)});const i=new m.DateTime(I(this.minimum)?this.minimum:void 0).zone("UTC").features(e(this.time)).disabled(!0===this.minimum).years((new Date).getFullYear()-150,(new Date).getFullYear()+50).width("full").label(o("block:date","Minimum")).placeholder(o("block:date","Not set")).on((()=>{i.isFeatureEnabled?!0!==this.minimum&&(this.minimum=i.value):this.minimum=void 0})),a=new m.DateTime(I(this.maximum)?this.maximum:void 0).zone("UTC").features(e(this.time)).disabled(!0===this.maximum).years((new Date).getFullYear()-150,(new Date).getFullYear()+50).width("full").label(o("block:date","Maximum")).placeholder(o("block:date","Not set")).on((()=>{a.isFeatureEnabled?!0!==this.maximum&&(this.maximum=a.value):this.maximum=void 0}));this.editor.option({name:o("block:date","Limits"),form:{title:o("block:date","Limits"),controls:[i,a,new m.Static(o("block:date","Options")),new m.Checkbox(o("block:date","Date must be in the future"),!0===this.minimum).on((e=>{e.isFeatureEnabled&&e.isChecked?this.minimum=!0:!0===this.minimum&&(this.minimum=void 0,i.refresh()),i.disabled(!0===this.minimum)})),new m.Checkbox(o("block:date","Date must be in the past"),!0===this.maximum).on((e=>{e.isFeatureEnabled&&e.isChecked?this.maximum=!0:!0===this.maximum&&(this.maximum=void 0,a.refresh()),a.disabled(!0===this.maximum)}))]},activated:I(this.minimum)||!0===this.minimum||I(this.maximum)||!0===this.maximum}),this.editor.groups.options(),this.editor.required(this),this.editor.visibility(),this.editor.alias(this),this.editor.exportable(this)}defineCondition(){const e=this.slots.select("date"),t=this.slots.select("to"),i=[{mode:"equal",label:o("block:date","Is equal to")},{mode:"not-equal",label:o("block:date","Is not equal to")},{mode:"before",label:o("block:date","Is before")},{mode:"after",label:o("block:date","Is after")},{mode:"between",label:o("block:date","Is between")},{mode:"not-between",label:o("block:date","Is not between")},{mode:"defined",label:o("block:date","Is not empty")},{mode:"undefined",label:o("block:date","Is empty")}];if(e){const a=t?this.conditions.group(e.label||o("block:date","Date from")):this.conditions;w(i,(t=>{a.template({condition:x,label:t.label,autoOpen:"defined"!==t.mode&&"undefined"!==t.mode,props:{slot:e,mode:t.mode,value:this.time?M.UTC:M.UTCToday,to:"between"===t.mode||"not-between"===t.mode?this.time?M.UTC:M.UTCToday:void 0}})}))}if(t){const e=this.conditions.group(t.label||o("block:date","Date to"));w(i,(i=>{e.template({condition:x,label:i.label,autoOpen:"defined"!==i.mode&&"undefined"!==i.mode,props:{slot:t,mode:i.mode,value:M.UTC,to:"between"===i.mode||"not-between"===i.mode?M.UTC:void 0}})}))}}};p([e("boolean","optional"),t("#label"),t("#slots")],L.prototype,"time",void 0),p([e("boolean","optional"),t("#label"),t("#slots")],L.prototype,"range",void 0),p([e("number","optional"),t("#slots")],L.prototype,"minimum",void 0),p([e("number","optional"),t("#slots")],L.prototype,"maximum",void 0),p([e("boolean","optional"),t("#slots")],L.prototype,"required",void 0),p([e("string","optional"),t("#slots")],L.prototype,"alias",void 0),p([e("boolean","optional"),t("#slots")],L.prototype,"exportable",void 0),p([e("string","optional")],L.prototype,"placeholder",void 0),p([c],L.prototype,"defineSlot",null),p([a],L.prototype,"defineEditor",null),p([h],L.prototype,"defineCondition",null),L=S=p([i({type:"node",legacyBlock:!0,identifier:"@tripetto/block-date",version:"4.0.3",icon:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCI+PHBhdGggZD0iTTE4LjUgMkgxNnYtLjVhLjUuNSAwIDAgMC0xIDBWMkg1di0uNWEuNS41IDAgMCAwLTEgMFYySDEuNUMuNjczIDIgMCAyLjY3MyAwIDMuNXYxNGMwIC44MjcuNjczIDEuNSAxLjUgMS41aDE3Yy44MjcgMCAxLjUtLjY3MyAxLjUtMS41di0xNGMwLS44MjctLjY3My0xLjUtMS41LTEuNXptLTE3IDFINHYxLjVhLjUuNSAwIDAgMCAxIDBWM2gxMHYxLjVhLjUuNSAwIDAgMCAxIDBWM2gyLjVhLjUuNSAwIDAgMSAuNS41VjZIMVYzLjVhLjUuNSAwIDAgMSAuNS0uNXptMTcgMTVoLTE3YS41LjUgMCAwIDEtLjUtLjVWN2gxOHYxMC41YS41LjUgMCAwIDEtLjUuNXoiIGNsYXNzPSJ0cmlwZXR0by1maWxsIi8+PHBhdGggZ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