vue-ethiopian-calendar
Version:
A Vue 2 component for Ethiopian (Amete Alem) calendar with Gregorian date conversion
2 lines • 9.95 kB
JavaScript
(function(e,t){"object"===typeof exports&&"object"===typeof module?module.exports=t():"function"===typeof define&&define.amd?define([],t):"object"===typeof exports?exports["EthiopianCalendar"]=t():e["EthiopianCalendar"]=t()})("undefined"!==typeof self?self:this,(function(){return function(e){var t={};function a(r){if(t[r])return t[r].exports;var n=t[r]={i:r,l:!1,exports:{}};return e[r].call(n.exports,n,n.exports,a),n.l=!0,n.exports}return a.m=e,a.c=t,a.d=function(e,t,r){a.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},a.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,t){if(1&t&&(e=a(e)),8&t)return e;if(4&t&&"object"===typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(a.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)a.d(r,n,function(t){return e[t]}.bind(null,n));return r},a.n=function(e){var t=e&&e.__esModule?function(){return e["default"]}:function(){return e};return a.d(t,"a",t),t},a.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},a.p="",a(a.s="fb15")}({"17dd":function(e,t,a){"use strict";a("4792")},4792:function(e,t,a){},fb15:function(e,t,a){"use strict";if(a.r(t),"undefined"!==typeof window){var r=window.document.currentScript,n=r&&r.src.match(/(.+\/)[^/]+\.js(\?.*)?$/);n&&(a.p=n[1])}var s=function(){var e=this,t=e._self._c;return t("div",{staticClass:"calendar-container",style:e.cssVariables},[t("div",{staticClass:"calendar-header"},[t("button",{on:{click:e.prevYear}},[e._v("«")]),"yyyy"!==e.scope?t("button",{on:{click:e.prevMonth}},[e._v("◀")]):e._e(),"yyyy"===e.scope?t("span",[e._v(e._s(e.visibleYears[0])+" - "+e._s(e.visibleYears[e.visibleYears.length-1]))]):(e.scope,t("span",[e._v(e._s(e.monthNames[e.selectedMonth-1])+" "+e._s(e.selectedYear))])),"yyyy"!==e.scope?t("button",{on:{click:e.nextMonth}},[e._v("▶")]):e._e(),t("button",{on:{click:e.nextYear}},[e._v("»")])]),"yyyy"===e.scope?t("div",{staticClass:"calendar-years"},e._l(e.visibleYears,(function(a){return t("div",{key:a,staticClass:"calendar-year",class:{selected:a===e.selectedYear},on:{click:function(t){return e.selectYear(a)}}},[e._v(" "+e._s(a)+" ")])})),0):"yyyy-mm"===e.scope?t("div",{staticClass:"calendar-months"},e._l(e.monthNames,(function(a,r){return t("div",{key:a,staticClass:"calendar-month",class:{selected:r+1===e.selectedMonth},on:{click:function(t){return e.selectMonth(r+1)}}},[e._v(" "+e._s(a)+" ")])})),0):[t("div",{staticClass:"calendar-weekdays"},e._l(e.weekdayNames,(function(a){return t("div",{key:a},[e._v(e._s(a))])})),0),t("div",{staticClass:"calendar-days"},[e._l(e.firstDayOffset,(function(e){return t("div",{key:"blank"+e,staticClass:"calendar-day empty"})})),e._l(e.daysInMonth,(function(a){return t("div",{key:a,staticClass:"calendar-day",class:{selected:a===e.selectedDay},on:{click:function(t){return e.selectDate(a)}}},[e._v(" "+e._s(a)+" ")])}))],2)],"yyyy-mm"===e.scope?t("div",{staticClass:"calendar-footer"},[e._v(" "+e._s(e.monthNames[e.selectedMonth-1])+" "+e._s(e.selectedYear)+" ")]):"yyyy"===e.scope?t("div",{staticClass:"calendar-footer"},[e._v(" "+e._s(e.selectedYear)+" ")]):t("div",{staticClass:"calendar-footer"},[e._v(" "+e._s(e.selectedDay)+" "+e._s(e.monthNames[e.selectedMonth-1])+" "+e._s(e.selectedYear)+" ")])],2)},o=[],i={props:{language:{type:String,default:"am",validator:e=>["am","en"].includes(e)},primaryColor:{type:String,default:"#ffffff"},hoverColor:{type:String,default:"#ffe08a"},todayColor:{type:String,default:"#1fe08a"},selectedColor:{type:String,default:"#ccdefe"},textColor:{type:String,default:"#2d3748"},backgroundColor:{type:String,default:"#ffffff"},borderColor:{type:String,default:"#ffffff"},headerBg:{type:String,default:"#f7fafc"},weekdayColor:{type:String,default:"#718096"},fontStack:{type:String,default:"-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans Ethiopic', sans-serif"},scope:{type:String,default:"yyyy-mm-dd",validator:e=>["yyyy","yyyy-mm","yyyy-mm-dd"].includes(e)},value:{type:[String,Object],default:null,validator:function(e){if(!e)return!0;if("string"===typeof e)return!!/^\d{4}$/.test(e)||(!!/^\d{4}-\d{2}$/.test(e)||/^\d{4}-\d{2}-\d{2}$/.test(e));if("object"===typeof e){const t=!!e.year,a=void 0!==e.month,r=void 0!==e.day;return t||a||r}return!1}}},data(){const e=new Date,t=this.gregorianToEthiopian(e.getFullYear(),e.getMonth()+1,e.getDate());let a;if(this.value)if("string"===typeof this.value){const e=this.value.split("-");a={year:parseInt(e[0]),month:e[1]?parseInt(e[1]):1,day:e[2]?parseInt(e[2]):1}}else a={...this.value};else a=t;return{selectedYear:a.year,selectedMonth:a.month,selectedDay:a.day,internalValue:{...a},defaultDate:t,months:{am:["መስከረም","ጥቅምት","ኅዳር","ታኅሳስ","ጥር","የካቲት","መጋቢት","ሚያዝያ","ግንቦት","ሰኔ","ሀምሌ","ነሐሴ","ጳጉሜ"],en:["Meskerem","Tikimt","Hidar","Tahsas","Tir","Yekatit","Megabit","Miyazya","Ginbot","Sene","Hamle","Nehase","Pagume"]},weekdays:{am:["እሑድ","ሰኞ","ማክሰኞ","ረቡዕ","ሐሙስ","ዓርብ","ቅዳሜ"],en:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]}}},computed:{monthNames(){return this.months[this.language]},weekdayNames(){return this.weekdays[this.language]},daysInMonth(){return 13===this.selectedMonth?this.isEthiopianLeapYear(this.selectedYear)?6:5:30},firstDayOffset(){const e=this.ethiopianToGregorian(this.selectedYear,this.selectedMonth,1);return e.getDay()},cssVariables(){return{"--primary-color":this.primaryColor,"--hover-color":this.hoverColor,"--today-color":this.todayColor,"--selected-color":this.selectedColor,"--text-color":this.textColor,"--background-color":this.backgroundColor,"--border-color":this.borderColor,"--header-bg":this.headerBg,"--weekday-color":this.weekdayColor,"--font-stack":this.fontStack}},visibleYears(){const e="yyyy"===this.scope?12*Math.floor(this.selectedYear/12):this.selectedYear-Math.floor(6);return Array.from({length:12},(t,a)=>e+a)}},methods:{startDayOfEthiopian(e){let t=Math.floor(e/100)-Math.floor(e/400)-4;return(e-1)%4===3&&(t+=1),t},isEthiopianLeapYear(e){return e%4===3},ethiopianToGregorian(e,t,a){if(e<=0||t<=0||a<=0)throw new Error("Malformed input can't be converted.");const r=this.startDayOfEthiopian(e);let n=e+7;const s=[0,30,31,30,31,31,28,31,30,31,30,31,31,30],o=n+1;(o%4===0&&o%100!==0||o%400===0)&&(s[6]=29);let i=30*(t-1)+a;i<=37&&e<=1575?(i+=28,s[0]=31):i+=r-1,(e-1)%4===3&&(i+=1);let l=0,c=0;for(l=0;l<s.length;l++){if(i<=s[l]){c=i;break}i-=s[l]}l>4&&(n+=1);const d=[8,9,10,11,12,1,2,3,4,5,6,7,8,9],y=d[l];return new Date(n,y-1,c)},gregorianToEthiopian(e,t,a){if(e<=0||t<=0||a<=0)throw new Error("Malformed input can't be converted.");if(5===t&&a>=5&&a<=14&&1582===e)throw new Error("Invalid Date between 5-14 May 1582.");const r=[0,31,28,31,30,31,30,31,31,30,31,30,31];(e%4===0&&e%100!==0||e%400===0)&&(r[2]=29);let n=e-8;const s=[0,30,30,30,30,30,30,30,30,30,5,30,30,30,30];s[10]=n%4===3?6:5;const o=this.startDayOfEthiopian(n);let i,l=0;for(let f=1;f<t;f++)l+=r[f];l+=a,i=n%4===0?26:25,e<1582||l<=277&&1582===e?(s[1]=0,s[2]=i):(i=o-3,s[1]=i);let c=0,d=0;for(c=1;c<s.length;c++){if(l<=s[c]){d=1===c||0===s[c]?l+(30-i):l;break}l-=s[c]}c>10&&(n+=1);const y=[0,4,5,6,7,8,9,10,11,12,13,1,2,3,4],h=y[c];return{year:n,month:h,day:d}},selectDate(e){this.selectedDay=e,this.emitSelectedDate()},selectMonth(e){this.selectedMonth=e,"yyyy-mm"===this.scope&&this.emitSelectedDate()},selectYear(e){this.selectedYear=e,"yyyy"===this.scope&&this.emitSelectedDate()},emitSelectedDate(){const e=this.defaultDate||{year:this.selectedYear,month:1,day:1};let t;switch(this.scope){case"yyyy":t={year:this.selectedYear||e.year,month:1,day:1};break;case"yyyy-mm":t={year:this.selectedYear||e.year,month:this.selectedMonth||e.month,day:1};break;default:t={year:this.selectedYear||e.year,month:this.selectedMonth||e.month,day:this.selectedDay||e.day}}t.year=t.year||e.year,t.month=t.month||e.month,t.day=t.day||e.day;const a=this.ethiopianToGregorian(t.year,t.month,t.day),r={year:a.getFullYear(),month:a.getMonth()+1,day:a.getDate()};this.$emit("date-selected",{ethiopianDate:t,gregorianDate:r,formatted:this.formatDateString(t)})},formatDateString(e){const t=e.year,a=e.month.toString().padStart(2,"0"),r=e.day.toString().padStart(2,"0");switch(this.scope){case"yyyy":return""+t;case"yyyy-mm":return`${t}-${a}`;default:return`${t}-${a}-${r}`}},prevMonth(e){e.preventDefault(),this.selectedDay=null,1===this.selectedMonth?(this.selectedMonth=13,this.selectedYear-=1):this.selectedMonth-=1},nextMonth(e){e.preventDefault(),this.selectedDay=null,13===this.selectedMonth?(this.selectedMonth=1,this.selectedYear+=1):this.selectedMonth+=1},prevYear(e){e.preventDefault(),this.selectedDay=null,"yyyy"===this.scope?this.selectedYear-=12:(this.scope,this.selectedYear-=1)},nextYear(e){e.preventDefault(),this.selectedDay=null,"yyyy"===this.scope?this.selectedYear+=12:(this.scope,this.selectedYear+=1)}}},l=i;a("17dd");function c(e,t,a,r,n,s,o,i){var l,c="function"===typeof e?e.options:e;if(t&&(c.render=t,c.staticRenderFns=a,c._compiled=!0),r&&(c.functional=!0),s&&(c._scopeId="data-v-"+s),o?(l=function(e){e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,e||"undefined"===typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),n&&n.call(this,e),e&&e._registeredComponents&&e._registeredComponents.add(o)},c._ssrRegister=l):n&&(l=i?function(){n.call(this,(c.functional?this.parent:this).$root.$options.shadowRoot)}:n),l)if(c.functional){c._injectStyles=l;var d=c.render;c.render=function(e,t){return l.call(t),d(e,t)}}else{var y=c.beforeCreate;c.beforeCreate=y?[].concat(y,l):[l]}return{exports:e,options:c}}var d=c(l,s,o,!1,null,"b75e1bd2",null),y=d.exports;t["default"]=y}})["default"]}));
//# sourceMappingURL=EthiopianCalendar.umd.min.js.map