diff --git a/README.md b/README.md index da853a92..203fb4fb 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![vanilla-calendar preview](https://vanilla-calendar.frontend.uvarov.tech/preview.png?v2)](https://vanilla-calendar.frontend.uvarov.tech/) -# Vanilla JS Calendar v1.5.4 +# Vanilla JS Calendar v1.5.5 A simple yet feature rich calendar with no dependencies and no «input» tag. A lightweight date and time picker written in pure JavaScript. The size of the minified file .js is approximately **30kb** and **7.2kb** gzip. @@ -14,7 +14,7 @@ If you like the plugin please give it a star. ## Implementation -Because Vanilla Calendar is written in pure JavaScript, it can be used with any framework or library, such as Vue, React, or Angular, if desired. You can get the plugin by [downloading](https://vanilla-calendar.frontend.uvarov.tech/vanilla-calendar-v1.5.4.zip) it manually, via [cdn](https://cdn.jsdelivr.net/npm/@uvarov.frontend/vanilla-calendar@1.5.4/build/) or via a package manager like npm: +Because Vanilla Calendar is written in pure JavaScript, it can be used with any framework or library, such as Vue, React, or Angular, if desired. You can get the plugin by [downloading](https://vanilla-calendar.frontend.uvarov.tech/vanilla-calendar-v1.5.5.zip) it manually, via [cdn](https://cdn.jsdelivr.net/npm/@uvarov.frontend/vanilla-calendar@1.5.5/build/) or via a package manager like npm: ```sh npm install @uvarov.frontend/vanilla-calendar diff --git a/build/vanilla-calendar.min.js b/build/vanilla-calendar.min.js index 8cb68e87..ad466741 100644 --- a/build/vanilla-calendar.min.js +++ b/build/vanilla-calendar.min.js @@ -1,6 +1,6 @@ // ==UserScript== // @name @uvarov.frontend/vanilla-calendar -// @version 1.5.4 +// @version 1.5.5 // @homepage https://vanilla-calendar.frontend.uvarov.tech/ // ==/UserScript== -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).VanillaCalendar=t()}(this,(function(){"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){for(var a=0;ae.length)&&(t=e.length);for(var a=0,n=new Array(t);a=0&&this.settings.selected.month<12?this.selectedMonth=this.settings.selected.month:this.selectedMonth=this.date.today.getUTCMonth(),null!==this.settings.selected.year&&this.settings.selected.year>=0&&this.settings.selected.year<=9999?this.selectedYear=this.settings.selected.year:this.selectedYear=this.date.today.getUTCFullYear(),this.viewYear=this.selectedYear,this.dateMin=this.settings.visibility.disabled?new Date(this.date.min):new Date(this.settings.range.min),this.dateMax=this.settings.visibility.disabled?new Date(this.date.max):new Date(this.settings.range.max);var t=!0===this.settings.selection.time||12===this.settings.selection.time;if(t||24===this.settings.selection.time){if("string"==typeof this.settings.selected.time){var a=t?/^([0-9]|0[1-9]|1[0-2]):([0-5][0-9])|(AM|PM)/g:/^([0-1]?[0-9]|2[0-3]):([0-5][0-9])/g;this.settings.selected.time.replace(a,(function(a,n,l,s){n&&l&&(e.userTime=!0,e.selectedHours=n,e.selectedMinutes=l),s&&t?e.selectedKeeping=s:t&&(e.selectedKeeping="AM")}))}!this.userTime&&t?(this.selectedHours=this.transformTime12(this.date.today.getHours()),this.selectedMinutes=this.date.today.getMinutes(),this.selectedKeeping=Number(this.date.today.getHours())>12?"PM":"AM"):this.userTime||(this.selectedHours=this.date.today.getHours(),this.selectedMinutes=this.date.today.getMinutes()),this.selectedHours=Number(this.selectedHours)<10?"0".concat(Number(this.selectedHours)):"".concat(this.selectedHours),this.selectedMinutes=Number(this.selectedMinutes)<10?"0".concat(Number(this.selectedMinutes)):"".concat(this.selectedMinutes),this.selectedTime="".concat(this.selectedHours,":").concat(this.selectedMinutes).concat(this.selectedKeeping?" ".concat(this.selectedKeeping):"")}else this.settings.selection.time&&(this.settings.selection.time=null,console.error("The value of the time property can be: false, true, 12 or 24."))}},{key:"createDOM",value:function(){"default"===this.currentType?(this.HTMLElement.classList.add("vanilla-calendar_default"),this.HTMLElement.classList.remove("vanilla-calendar_month"),this.HTMLElement.classList.remove("vanilla-calendar_year"),this.HTMLElement.innerHTML='\n\t\t\t
\n\t\t\t\t\n\t\t\t\t
\n\t\t\t\t\n\t\t\t
\n\t\t\t'.concat(this.settings.visibility.weekNumbers?'\n\t\t\t
\n\t\t\t\t#\n\t\t\t\t
\n\t\t\t
\n\t\t\t':"",'\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t').concat(this.settings.selection.time?'\n\t\t\t\t
\n\t\t\t\t':"","\n\t\t\t
\n\t\t")):"month"===this.currentType?(this.HTMLElement.classList.remove("vanilla-calendar_default"),this.HTMLElement.classList.add("vanilla-calendar_month"),this.HTMLElement.classList.remove("vanilla-calendar_year"),this.HTMLElement.innerHTML='\n\t\t\t
\n\t\t\t\t
\n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t
'):"year"===this.currentType&&(this.HTMLElement.classList.remove("vanilla-calendar_default"),this.HTMLElement.classList.remove("vanilla-calendar_month"),this.HTMLElement.classList.add("vanilla-calendar_year"),this.HTMLElement.innerHTML='\n\t\t\t
\n\t\t\t\t\n\t\t\t\t
\n\t\t\t\t\n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t
')}},{key:"createHeader",value:function(){var e=this.HTMLElement.querySelector(".vanilla-calendar-header__content"),t=this.settings.selection.month?"":" vanilla-calendar-month_disabled",a=this.settings.selection.year?"":" vanilla-calendar-year_disabled",n='\n\t\t").replace(/[\n\t]/g,""),l='\n\t\t").replace(/[\n\t]/g,""),s=this.settings.visibility.templateHeader.replace("%M",n);s=s.replace("%Y",l),e.innerHTML=s}},{key:"controlArrows",value:function(){var e=this;if(["default","year"].includes(this.currentType)){var t=this.HTMLElement.querySelector(".vanilla-calendar-arrow_prev"),a=this.HTMLElement.querySelector(".vanilla-calendar-arrow_next");!function(){if("default"===e.currentType){var n=e.selectedMonth===e.dateMin.getUTCMonth(),l=e.selectedMonth===e.dateMax.getUTCMonth(),s=!e.settings.selection.year||e.selectedYear===e.dateMin.getUTCFullYear(),i=!e.settings.selection.year||e.selectedYear===e.dateMax.getUTCFullYear();n&&s||!e.settings.selection.month?t.style.visibility="hidden":t.style.visibility=null,l&&i||!e.settings.selection.month?a.style.visibility="hidden":a.style.visibility=null}}(),"year"===e.currentType&&(e.dateMin.getUTCFullYear()&&e.viewYear-7<=e.dateMin.getUTCFullYear()?t.style.visibility="hidden":t.style.visibility=null,e.dateMax.getUTCFullYear()&&e.viewYear+7>=e.dateMax.getUTCFullYear()?a.style.visibility="hidden":a.style.visibility=null)}}},{key:"createWeek",value:function(){var e=this.HTMLElement.querySelector(".vanilla-calendar-week"),t=document.createElement("b");t.className="vanilla-calendar-week__day";var n=a(this.locale.weekday);this.settings.iso8601&&n.push(n.shift()),e.innerHTML="";for(var l=0;l'.concat(n.html,"")}}},{key:"createDays",value:function(){var e=this,t=new Date(Date.UTC(this.selectedYear,this.selectedMonth,1)),a=new Date(Date.UTC(this.selectedYear,this.selectedMonth+1,0)).getUTCDate(),n=Number(t.getUTCDay());this.settings.iso8601&&(n=Number((0!==t.getUTCDay()?t.getUTCDay():7)-1));var l=this.HTMLElement.querySelector(".vanilla-calendar-days"),s=document.createElement("div"),i=document.createElement("button");s.className="vanilla-calendar-day",i.className="vanilla-calendar-day__btn",i.type="button",["single","multiple","multiple-ranged"].includes(this.settings.selection.day)&&l.classList.add("vanilla-calendar-days_selecting"),l.innerHTML="";var c=function(t,a,n,c,r){var d=s.cloneNode(!0),o=i.cloneNode(!0);r&&o.classList.add(r),o.innerText=t,o.dataset.calendarDay=n,function(t,a,n,l){!e.settings.visibility.weekend||0!==a&&6!==a||t.classList.add("vanilla-calendar-day__btn_weekend"),Array.isArray(e.settings.selected.holidays)&&e.settings.selected.holidays.forEach((function(e){e===n&&t.classList.add("vanilla-calendar-day__btn_holiday")}));var s=e.date.today.getUTCDate(),i=e.date.today.getUTCMonth()+1;s=s<10?"0".concat(s):s,i=i<10?"0".concat(i):i;var c="".concat(e.date.today.getUTCFullYear(),"-").concat(i,"-").concat(s);e.settings.visibility.today&&t.dataset.calendarDay===c&&t.classList.add("vanilla-calendar-day__btn_today"),0===e.selectedDates.indexOf(n)||e.selectedDates[0]&&e.selectedDates.indexOf(n)===e.selectedDates.length-1?t.classList.add("vanilla-calendar-day__btn_selected"):"multiple-ranged"===e.settings.selection.day&&e.selectedDates.indexOf(n)>0?(t.classList.add("vanilla-calendar-day__btn_selected"),t.classList.add("vanilla-calendar-day__btn_intermediate")):e.selectedDates.indexOf(n)>0&&t.classList.add("vanilla-calendar-day__btn_selected"),(e.settings.range.min>n||e.settings.range.maxthis.dateMax.getUTCFullYear()&&l.classList.add("vanilla-calendar-years__year_disabled"),l.dataset.calendarYear=n,l.innerText="".concat(n),e.append(l)}}},{key:"createMonths",value:function(){this.currentType="month",this.createDOM(),this.createHeader();var e=this.HTMLElement.querySelector(".vanilla-calendar-months");this.settings.selection.month&&e.classList.add("vanilla-calendar-months_selecting");var t=document.createElement("button");t.type="button",t.className="vanilla-calendar-months__month";for(var a=0;athis.dateMax.getUTCMonth()&&this.selectedYear===this.dateMax.getUTCFullYear()&&l.classList.add("vanilla-calendar-months__month_disabled"),l.dataset.calendarMonth=a,l.title="".concat(n),l.innerText="".concat(this.settings.visibility.monthShort?n.substring(0,3):n),e.append(l)}}},{key:"controlTime",value:function(e){var t=this,a=this.HTMLElement.querySelector('.vanilla-calendar-time__range input[name="hours"]'),n=this.HTMLElement.querySelector('.vanilla-calendar-time__range input[name="minutes"]'),l=this.HTMLElement.querySelector('.vanilla-calendar-time__hours input[name="hours"]'),s=this.HTMLElement.querySelector('.vanilla-calendar-time__minutes input[name="minutes"]'),i=this.HTMLElement.querySelector(".vanilla-calendar-time__keeping"),c=function(e,t){e.addEventListener("mouseover",(function(){return t.classList.add("is-focus")}))},r=function(e,t){e.addEventListener("mouseout",(function(){return t.classList.remove("is-focus")}))},d=function(e,a,n){"hours"===n?t.selectedHours="".concat(a):"minutes"===n&&(t.selectedMinutes="".concat(a)),t.selectedTime="".concat(t.selectedHours,":").concat(t.selectedMinutes).concat(t.selectedKeeping?" ".concat(t.selectedKeeping):""),t.settings.selected.time=t.selectedTime,t.actions.changeTime&&t.actions.changeTime(e,t.selectedTime,t.selectedHours,t.selectedMinutes,t.selectedKeeping)},o=function(e,a,n,l){e.addEventListener("input",(function(e){var s=Number(e.target.value);s=s<10?"0".concat(s):"".concat(s),"hours"===n&&12===l?Number(e.target.value)0?(a.value=s,t.selectedKeeping="AM",i.innerText=t.selectedKeeping,d(e,s,n)):(0===Number(e.target.value)?(t.selectedKeeping="AM",i.innerText="AM"):(t.selectedKeeping="PM",i.innerText="PM"),a.value=t.transformTime12(e.target.value),d(e,t.transformTime12(e.target.value),n)):(a.value=s,d(e,s,n))}))},u=function(e,a,n,l){a.addEventListener("change",(function(a){var s=Number(a.target.value);s=s<10?"0".concat(s):"".concat(s),"hours"===n&&12===l?a.target.value&&Number(a.target.value)<=l&&Number(a.target.value)>0?(a.target.value=s,e.value=t.transformTime24(s,t.selectedKeeping),d(a,s,n)):a.target.value&&Number(a.target.value)<24&&(Number(a.target.value)>l||0===Number(a.target.value))?(0===Number(a.target.value)?(t.selectedKeeping="AM",i.innerText="AM"):(t.selectedKeeping="PM",i.innerText="PM"),a.target.value=t.transformTime12(a.target.value),e.value=s,d(a,t.transformTime12(a.target.value),n)):a.target.value=t.selectedHours:a.target.value&&Number(a.target.value)<=l&&Number(a.target.value)>=0?(a.target.value=s,e.value=s,d(a,s,n)):"hours"===n?a.target.value=t.selectedHours:"minutes"===n&&(a.target.value=t.selectedMinutes)}))};c(a,l),c(n,s),r(a,l),r(n,s),o(a,l,"hours",24===e?23:e),o(n,s,"minutes"),u(a,l,"hours",24===e?23:e),u(n,s,"minutes",59),i&&i.addEventListener("click",(function(e){i.innerText.includes("AM")?t.selectedKeeping="PM":t.selectedKeeping="AM",a.value=t.transformTime24(t.selectedHours,t.selectedKeeping),d(e,t.selectedHours,"hours"),i.innerText=t.selectedKeeping}))}},{key:"createTime",value:function(){var e=this.HTMLElement.querySelector(".vanilla-calendar-time");if(e){var t=!0===this.settings.selection.time?12:this.settings.selection.time,a="range"===this.settings.selection.controlTime;e.innerHTML='\n\t\t
\n\t\t\t\n\t\t\t\n\t\t\t").concat(12===t?'\n\t\t\t\n\t\t\t"):"",'\n\t\t
\n\t\t
\n\t\t\t\n\t\t\t\n\t\t
'),this.controlTime(t)}}},{key:"getLocale",value:function(){if("define"!==this.settings.lang){this.locale.weekday=[];for(var e=0;e<7;e++){var t=new Date(0,0,e).toLocaleString(this.settings.lang,{weekday:"short"});t=(t="".concat(t.charAt(0).toUpperCase()).concat(t.substring(1,t.length))).replace(/\./,""),this.locale.weekday.push(t)}this.locale.months=[];for(var a=0;a<12;a++){var n=new Date(0,a).toLocaleString(this.settings.lang,{month:"long"});n=(n="".concat(n.charAt(0).toUpperCase()).concat(n.substring(1,n.length))).replace(/\./,""),this.locale.months.push(n)}}}},{key:"update",value:function(){this.setVariablesDates(),this.getLocale(),this.createDOM(),this.createHeader(),this.controlArrows(),this.createTime(),"default"===this.currentType?(this.createWeek(),this.createDays()):"month"===this.currentType?this.createMonths():"year"===this.currentType&&this.createYears()}},{key:"click",value:function(){var e=this;this.HTMLElement.addEventListener("click",(function(t){var a=t.target.closest(".vanilla-calendar-arrow"),n=t.target.closest(".vanilla-calendar-arrow_prev"),l=t.target.closest(".vanilla-calendar-arrow_next"),s=t.target.closest(".vanilla-calendar-day__btn"),i=t.target.closest(".vanilla-calendar-day__btn_prev"),c=t.target.closest(".vanilla-calendar-day__btn_next"),r=t.target.closest(".vanilla-calendar-year"),d=t.target.closest(".vanilla-calendar-years__year"),o=t.target.closest(".vanilla-calendar-month"),u=t.target.closest(".vanilla-calendar-months__month");a&&"year"!==e.currentType&&"month"!==e.currentType&&e.changeMonth(t.target.dataset.calendarArrow),function(){if(["single","multiple","multiple-ranged"].includes(e.settings.selection.day)&&s){switch(e.settings.selection.day){case"single":s.classList.contains("vanilla-calendar-day__btn_selected")?e.selectedDates.splice(e.selectedDates.indexOf(s.dataset.calendarDay),1):(e.selectedDates=[],e.selectedDates.push(s.dataset.calendarDay));break;case"multiple":s.classList.contains("vanilla-calendar-day__btn_selected")?e.selectedDates.splice(e.selectedDates.indexOf(s.dataset.calendarDay),1):e.selectedDates.push(s.dataset.calendarDay);break;case"multiple-ranged":!function(){if(e.selectedDates.length>1&&(e.selectedDates=[]),e.selectedDates.push(s.dataset.calendarDay),e.selectedDates[1]){var t=new Date(Date.UTC(new Date(e.selectedDates[0]).getUTCFullYear(),new Date(e.selectedDates[0]).getUTCMonth(),new Date(e.selectedDates[0]).getUTCDate())),a=new Date(Date.UTC(new Date(e.selectedDates[1]).getUTCFullYear(),new Date(e.selectedDates[1]).getUTCMonth(),new Date(e.selectedDates[1]).getUTCDate())),n=function(t){var a=e.generateDate(t);e.settings.range.disabled&&e.settings.range.disabled.includes(a)||e.selectedDates.push(a)};if(e.selectedDates=[],a>t)for(var l=t;l<=a;l.setUTCDate(l.getUTCDate()+1))n(l);else for(var i=t;i>=a;i.setUTCDate(i.getUTCDate()-1))n(i)}}()}e.actions.clickDay&&e.actions.clickDay(t,e.selectedDates),e.settings.selected.dates=e.selectedDates,i?e.changeMonth("prev"):c?e.changeMonth("next"):e.createDays()}}(),e.settings.selection.year&&(a&&"year"===e.currentType?(l?e.viewYear+=15:n&&(e.viewYear-=15),e.createYears()):"year"!==e.currentType&&r?e.createYears():"year"===e.currentType&&r?(e.currentType=e.type,e.update()):d&&(e.selectedYear=Number(d.dataset.calendarYear),e.currentType=e.type,e.selectedMonthe.dateMax.getUTCMonth()&&e.selectedYear===e.dateMax.getUTCFullYear()&&(e.settings.selected.month=e.dateMax.getUTCMonth()),e.actions.clickYear&&e.actions.clickYear(t,e.selectedYear),e.settings.selected.year=e.selectedYear,e.update())),e.settings.selection.month&&("month"!==e.currentType&&o?e.createMonths():"month"===e.currentType&&o?(e.currentType=e.type,e.update()):u&&(e.selectedMonth=Number(u.dataset.calendarMonth),e.currentType=e.type,e.actions.clickMonth&&e.actions.clickMonth(t,e.selectedMonth),e.settings.selected.month=e.selectedMonth,e.update()))}))}},{key:"init",value:function(){this.HTMLElement&&(this.update(),this.click())}}])&&t(l.prototype,s),i&&t(l,i),Object.defineProperty(l,"prototype",{writable:!1}),n}()})); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).VanillaCalendar=t()}(this,(function(){"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){for(var a=0;ae.length)&&(t=e.length);for(var a=0,n=new Array(t);a=0&&this.settings.selected.month<12?this.selectedMonth=this.settings.selected.month:this.selectedMonth=this.date.today.getUTCMonth(),null!==this.settings.selected.year&&this.settings.selected.year>=0&&this.settings.selected.year<=9999?this.selectedYear=this.settings.selected.year:this.selectedYear=this.date.today.getUTCFullYear(),this.viewYear=this.selectedYear,this.dateMin=this.settings.visibility.disabled?new Date(this.date.min):new Date(this.settings.range.min),this.dateMax=this.settings.visibility.disabled?new Date(this.date.max):new Date(this.settings.range.max);var t=!0===this.settings.selection.time||12===this.settings.selection.time;if(t||24===this.settings.selection.time){if("string"==typeof this.settings.selected.time){var a=t?/^([0-9]|0[1-9]|1[0-2]):([0-5][0-9])|(AM|PM)/g:/^([0-1]?[0-9]|2[0-3]):([0-5][0-9])/g;this.settings.selected.time.replace(a,(function(a,n,l,s){n&&l&&(e.userTime=!0,e.selectedHours=n,e.selectedMinutes=l),s&&t?e.selectedKeeping=s:t&&(e.selectedKeeping="AM")}))}!this.userTime&&t?(this.selectedHours=this.transformTime12(this.date.today.getHours()),this.selectedMinutes=this.date.today.getMinutes(),this.selectedKeeping=Number(this.date.today.getHours())>12?"PM":"AM"):this.userTime||(this.selectedHours=this.date.today.getHours(),this.selectedMinutes=this.date.today.getMinutes()),this.selectedHours=Number(this.selectedHours)<10?"0".concat(Number(this.selectedHours)):"".concat(this.selectedHours),this.selectedMinutes=Number(this.selectedMinutes)<10?"0".concat(Number(this.selectedMinutes)):"".concat(this.selectedMinutes),this.selectedTime="".concat(this.selectedHours,":").concat(this.selectedMinutes).concat(this.selectedKeeping?" ".concat(this.selectedKeeping):"")}else this.settings.selection.time&&(this.settings.selection.time=null,console.error("The value of the time property can be: false, true, 12 or 24."))}},{key:"createDOM",value:function(){"default"===this.currentType?(this.HTMLElement.classList.add("vanilla-calendar_default"),this.HTMLElement.classList.remove("vanilla-calendar_month"),this.HTMLElement.classList.remove("vanilla-calendar_year"),this.HTMLElement.innerHTML='\n\t\t\t
\n\t\t\t\t\n\t\t\t\t
\n\t\t\t\t\n\t\t\t
\n\t\t\t'.concat(this.settings.visibility.weekNumbers?'\n\t\t\t
\n\t\t\t\t#\n\t\t\t\t
\n\t\t\t
\n\t\t\t':"",'\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t').concat(this.settings.selection.time?'\n\t\t\t\t
\n\t\t\t\t':"","\n\t\t\t
\n\t\t")):"month"===this.currentType?(this.HTMLElement.classList.remove("vanilla-calendar_default"),this.HTMLElement.classList.add("vanilla-calendar_month"),this.HTMLElement.classList.remove("vanilla-calendar_year"),this.HTMLElement.innerHTML='\n\t\t\t
\n\t\t\t\t
\n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t
'):"year"===this.currentType&&(this.HTMLElement.classList.remove("vanilla-calendar_default"),this.HTMLElement.classList.remove("vanilla-calendar_month"),this.HTMLElement.classList.add("vanilla-calendar_year"),this.HTMLElement.innerHTML='\n\t\t\t
\n\t\t\t\t\n\t\t\t\t
\n\t\t\t\t\n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t
')}},{key:"createHeader",value:function(){var e=this.HTMLElement.querySelector(".vanilla-calendar-header__content"),t=this.settings.selection.month?"":" vanilla-calendar-month_disabled",a=this.settings.selection.year?"":" vanilla-calendar-year_disabled",n='\n\t\t").replace(/[\n\t]/g,""),l='\n\t\t").replace(/[\n\t]/g,""),s=this.settings.visibility.templateHeader.replace("%M",n);s=s.replace("%Y",l),e.innerHTML=s}},{key:"controlArrows",value:function(){var e=this;if(["default","year"].includes(this.currentType)){var t=this.HTMLElement.querySelector(".vanilla-calendar-arrow_prev"),a=this.HTMLElement.querySelector(".vanilla-calendar-arrow_next");!function(){if("default"===e.currentType){var n=e.selectedMonth===e.dateMin.getUTCMonth(),l=e.selectedMonth===e.dateMax.getUTCMonth(),s=!e.settings.selection.year||e.selectedYear===e.dateMin.getUTCFullYear(),i=!e.settings.selection.year||e.selectedYear===e.dateMax.getUTCFullYear();n&&s||!e.settings.selection.month?t.style.visibility="hidden":t.style.visibility=null,l&&i||!e.settings.selection.month?a.style.visibility="hidden":a.style.visibility=null}}(),"year"===e.currentType&&(e.dateMin.getUTCFullYear()&&e.viewYear-7<=e.dateMin.getUTCFullYear()?t.style.visibility="hidden":t.style.visibility=null,e.dateMax.getUTCFullYear()&&e.viewYear+7>=e.dateMax.getUTCFullYear()?a.style.visibility="hidden":a.style.visibility=null)}}},{key:"createWeek",value:function(){var e=this.HTMLElement.querySelector(".vanilla-calendar-week"),t=document.createElement("b");t.className="vanilla-calendar-week__day";var n=a(this.locale.weekday);this.settings.iso8601&&n.push(n.shift()),e.innerHTML="";for(var l=0;l'.concat(n.html,"")}}}},{key:"createDays",value:function(){var e=this,t=new Date(Date.UTC(this.selectedYear,this.selectedMonth,1)),a=new Date(Date.UTC(this.selectedYear,this.selectedMonth+1,0)).getUTCDate(),n=Number(t.getUTCDay());this.settings.iso8601&&(n=Number((0!==t.getUTCDay()?t.getUTCDay():7)-1));var l=this.HTMLElement.querySelector(".vanilla-calendar-days"),s=document.createElement("div"),i=document.createElement("button");s.className="vanilla-calendar-day",i.className="vanilla-calendar-day__btn",i.type="button",["single","multiple","multiple-ranged"].includes(this.settings.selection.day)&&l.classList.add("vanilla-calendar-days_selecting"),l.innerHTML="";var c=function(t,a,n,c,r){var d=s.cloneNode(!0),o=i.cloneNode(!0);r&&o.classList.add(r),o.innerText=t,o.dataset.calendarDay=n,function(t,a,n,l){!e.settings.visibility.weekend||0!==a&&6!==a||t.classList.add("vanilla-calendar-day__btn_weekend"),Array.isArray(e.settings.selected.holidays)&&e.settings.selected.holidays.forEach((function(e){e===n&&t.classList.add("vanilla-calendar-day__btn_holiday")}));var s=e.date.today.getUTCDate(),i=e.date.today.getUTCMonth()+1;s=s<10?"0".concat(s):s,i=i<10?"0".concat(i):i;var c="".concat(e.date.today.getUTCFullYear(),"-").concat(i,"-").concat(s);e.settings.visibility.today&&t.dataset.calendarDay===c&&t.classList.add("vanilla-calendar-day__btn_today"),0===e.selectedDates.indexOf(n)||e.selectedDates[0]&&e.selectedDates.indexOf(n)===e.selectedDates.length-1?t.classList.add("vanilla-calendar-day__btn_selected"):"multiple-ranged"===e.settings.selection.day&&e.selectedDates.indexOf(n)>0?(t.classList.add("vanilla-calendar-day__btn_selected"),t.classList.add("vanilla-calendar-day__btn_intermediate")):e.selectedDates.indexOf(n)>0&&t.classList.add("vanilla-calendar-day__btn_selected"),(e.settings.range.min>n||e.settings.range.maxthis.dateMax.getUTCFullYear()&&l.classList.add("vanilla-calendar-years__year_disabled"),l.dataset.calendarYear=n,l.innerText="".concat(n),e.append(l)}}},{key:"createMonths",value:function(){this.currentType="month",this.createDOM(),this.createHeader();var e=this.HTMLElement.querySelector(".vanilla-calendar-months");this.settings.selection.month&&e.classList.add("vanilla-calendar-months_selecting");var t=document.createElement("button");t.type="button",t.className="vanilla-calendar-months__month";for(var a=0;athis.dateMax.getUTCMonth()&&this.selectedYear===this.dateMax.getUTCFullYear()&&l.classList.add("vanilla-calendar-months__month_disabled"),l.dataset.calendarMonth=a,l.title="".concat(n),l.innerText="".concat(this.settings.visibility.monthShort?n.substring(0,3):n),e.append(l)}}},{key:"controlTime",value:function(e){var t=this,a=this.HTMLElement.querySelector('.vanilla-calendar-time__range input[name="hours"]'),n=this.HTMLElement.querySelector('.vanilla-calendar-time__range input[name="minutes"]'),l=this.HTMLElement.querySelector('.vanilla-calendar-time__hours input[name="hours"]'),s=this.HTMLElement.querySelector('.vanilla-calendar-time__minutes input[name="minutes"]'),i=this.HTMLElement.querySelector(".vanilla-calendar-time__keeping"),c=function(e,t){e.addEventListener("mouseover",(function(){return t.classList.add("is-focus")}))},r=function(e,t){e.addEventListener("mouseout",(function(){return t.classList.remove("is-focus")}))},d=function(e,a,n){"hours"===n?t.selectedHours="".concat(a):"minutes"===n&&(t.selectedMinutes="".concat(a)),t.selectedTime="".concat(t.selectedHours,":").concat(t.selectedMinutes).concat(t.selectedKeeping?" ".concat(t.selectedKeeping):""),t.settings.selected.time=t.selectedTime,t.actions.changeTime&&t.actions.changeTime(e,t.selectedTime,t.selectedHours,t.selectedMinutes,t.selectedKeeping)},o=function(e,a,n,l){e.addEventListener("input",(function(e){var s=Number(e.target.value);s=s<10?"0".concat(s):"".concat(s),"hours"===n&&12===l?Number(e.target.value)0?(a.value=s,t.selectedKeeping="AM",i.innerText=t.selectedKeeping,d(e,s,n)):(0===Number(e.target.value)?(t.selectedKeeping="AM",i.innerText="AM"):(t.selectedKeeping="PM",i.innerText="PM"),a.value=t.transformTime12(e.target.value),d(e,t.transformTime12(e.target.value),n)):(a.value=s,d(e,s,n))}))},u=function(e,a,n,l){a.addEventListener("change",(function(a){var s=Number(a.target.value);s=s<10?"0".concat(s):"".concat(s),"hours"===n&&12===l?a.target.value&&Number(a.target.value)<=l&&Number(a.target.value)>0?(a.target.value=s,e.value=t.transformTime24(s,t.selectedKeeping),d(a,s,n)):a.target.value&&Number(a.target.value)<24&&(Number(a.target.value)>l||0===Number(a.target.value))?(0===Number(a.target.value)?(t.selectedKeeping="AM",i.innerText="AM"):(t.selectedKeeping="PM",i.innerText="PM"),a.target.value=t.transformTime12(a.target.value),e.value=s,d(a,t.transformTime12(a.target.value),n)):a.target.value=t.selectedHours:a.target.value&&Number(a.target.value)<=l&&Number(a.target.value)>=0?(a.target.value=s,e.value=s,d(a,s,n)):"hours"===n?a.target.value=t.selectedHours:"minutes"===n&&(a.target.value=t.selectedMinutes)}))};c(a,l),c(n,s),r(a,l),r(n,s),o(a,l,"hours",24===e?23:e),o(n,s,"minutes"),u(a,l,"hours",24===e?23:e),u(n,s,"minutes",59),i&&i.addEventListener("click",(function(e){i.innerText.includes("AM")?t.selectedKeeping="PM":t.selectedKeeping="AM",a.value=t.transformTime24(t.selectedHours,t.selectedKeeping),d(e,t.selectedHours,"hours"),i.innerText=t.selectedKeeping}))}},{key:"createTime",value:function(){var e=this.HTMLElement.querySelector(".vanilla-calendar-time");if(e){var t=!0===this.settings.selection.time?12:this.settings.selection.time,a="range"===this.settings.selection.controlTime;e.innerHTML='\n\t\t
\n\t\t\t\n\t\t\t\n\t\t\t").concat(12===t?'\n\t\t\t\n\t\t\t"):"",'\n\t\t
\n\t\t
\n\t\t\t\n\t\t\t\n\t\t
'),this.controlTime(t)}}},{key:"getLocale",value:function(){if("define"!==this.settings.lang){this.locale.weekday=[];for(var e=0;e<7;e++){var t=new Date(0,0,e).toLocaleString(this.settings.lang,{weekday:"short"});t=(t="".concat(t.charAt(0).toUpperCase()).concat(t.substring(1,t.length))).replace(/\./,""),this.locale.weekday.push(t)}this.locale.months=[];for(var a=0;a<12;a++){var n=new Date(0,a).toLocaleString(this.settings.lang,{month:"long"});n=(n="".concat(n.charAt(0).toUpperCase()).concat(n.substring(1,n.length))).replace(/\./,""),this.locale.months.push(n)}}}},{key:"update",value:function(){this.setVariablesDates(),this.getLocale(),this.createDOM(),this.createHeader(),this.controlArrows(),this.createTime(),"default"===this.currentType?(this.createWeek(),this.createDays()):"month"===this.currentType?this.createMonths():"year"===this.currentType&&this.createYears()}},{key:"click",value:function(){var e=this;this.HTMLElement.addEventListener("click",(function(t){var a=t.target.closest(".vanilla-calendar-arrow"),n=t.target.closest(".vanilla-calendar-arrow_prev"),l=t.target.closest(".vanilla-calendar-arrow_next"),s=t.target.closest(".vanilla-calendar-day__btn"),i=t.target.closest(".vanilla-calendar-day__btn_prev"),c=t.target.closest(".vanilla-calendar-day__btn_next"),r=t.target.closest(".vanilla-calendar-year"),d=t.target.closest(".vanilla-calendar-years__year"),o=t.target.closest(".vanilla-calendar-month"),u=t.target.closest(".vanilla-calendar-months__month");a&&"year"!==e.currentType&&"month"!==e.currentType&&e.changeMonth(t.target.dataset.calendarArrow),function(){if(["single","multiple","multiple-ranged"].includes(e.settings.selection.day)&&s){switch(e.settings.selection.day){case"single":s.classList.contains("vanilla-calendar-day__btn_selected")?e.selectedDates.splice(e.selectedDates.indexOf(s.dataset.calendarDay),1):(e.selectedDates=[],e.selectedDates.push(s.dataset.calendarDay));break;case"multiple":s.classList.contains("vanilla-calendar-day__btn_selected")?e.selectedDates.splice(e.selectedDates.indexOf(s.dataset.calendarDay),1):e.selectedDates.push(s.dataset.calendarDay);break;case"multiple-ranged":!function(){if(e.selectedDates.length>1&&(e.selectedDates=[]),e.selectedDates.push(s.dataset.calendarDay),e.selectedDates[1]){var t=new Date(Date.UTC(new Date(e.selectedDates[0]).getUTCFullYear(),new Date(e.selectedDates[0]).getUTCMonth(),new Date(e.selectedDates[0]).getUTCDate())),a=new Date(Date.UTC(new Date(e.selectedDates[1]).getUTCFullYear(),new Date(e.selectedDates[1]).getUTCMonth(),new Date(e.selectedDates[1]).getUTCDate())),n=function(t){var a=e.generateDate(t);e.settings.range.disabled&&e.settings.range.disabled.includes(a)||e.selectedDates.push(a)};if(e.selectedDates=[],a>t)for(var l=t;l<=a;l.setUTCDate(l.getUTCDate()+1))n(l);else for(var i=t;i>=a;i.setUTCDate(i.getUTCDate()-1))n(i)}}()}e.actions.clickDay&&e.actions.clickDay(t,e.selectedDates),e.settings.selected.dates=e.selectedDates,i?e.changeMonth("prev"):c?e.changeMonth("next"):e.createDays()}}(),e.settings.selection.year&&(a&&"year"===e.currentType?(l?e.viewYear+=15:n&&(e.viewYear-=15),e.createYears()):"year"!==e.currentType&&r?e.createYears():"year"===e.currentType&&r?(e.currentType=e.type,e.update()):d&&(e.selectedYear=Number(d.dataset.calendarYear),e.currentType=e.type,e.selectedMonthe.dateMax.getUTCMonth()&&e.selectedYear===e.dateMax.getUTCFullYear()&&(e.settings.selected.month=e.dateMax.getUTCMonth()),e.actions.clickYear&&e.actions.clickYear(t,e.selectedYear),e.settings.selected.year=e.selectedYear,e.update())),e.settings.selection.month&&("month"!==e.currentType&&o?e.createMonths():"month"===e.currentType&&o?(e.currentType=e.type,e.update()):u&&(e.selectedMonth=Number(u.dataset.calendarMonth),e.currentType=e.type,e.actions.clickMonth&&e.actions.clickMonth(t,e.selectedMonth),e.settings.selected.month=e.selectedMonth,e.update()))}))}},{key:"init",value:function(){this.HTMLElement&&(this.update(),this.click())}}])&&t(l.prototype,s),i&&t(l,i),Object.defineProperty(l,"prototype",{writable:!1}),n}()})); diff --git a/package.json b/package.json index f1cf72db..465fbb62 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uvarov.frontend/vanilla-calendar", - "version": "1.5.4", + "version": "1.5.5", "description": "Vanilla JS calendar without using additional packages. A lightweight date and time picker written in pure JavaScript.", "homepage": "https://vanilla-calendar.frontend.uvarov.tech/", "keywords": [