UNPKG

simple-jscalendar

Version:
46 lines (44 loc) 9.15 kB
/* * jsCalendar extension * DatePicker v1.1.1 * * * MIT License * * Copyright (c) 2021 Grammatopoulos Athanasios-Vasileios * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ (function(h){if(!h)throw"Error jsCalendar library was not found";var d=function(){0!==arguments.length&&this._construct(arguments)};d.version="v1.1.1";d.options={format:"DD/MM/YYYY",autoValueRead:!1,yearsLine:!1,offsetTop:2,offsetLeft:-4,close_keycodes:[9,27,13],"class":"jsCalendar-datepicker-wrappper"};d.prototype._construct=function(a){a=this._parseArguments(a);this._setTarget(a.target);a=this._parseOptions(a.options);this._visible=!1;this._wrapper=document.createElement("div");this._wrapper.className= this._options["class"];this._wrapper.style.display="none";this._wrapper.style.position="absolute";document.body.appendChild(this._wrapper);a.target=this._wrapper;this.jsCalendar=new h(a);this._options.yearsLine&&this._creatYearsLine();var b=this;this.jsCalendar.onDateClick(function(c,e){b._onPick(e)});this._target.addEventListener("focus",function(){b._onFocus()},!1);this._target.addEventListener("keydown",function(c){0<=b._options.close_keycodes.indexOf(c.keyCode)&&b.hide()},!1);this._options.autoValueRead&& (this._target.addEventListener("keyup",function(){b.readInput()},!1),this._target.addEventListener("change",function(){b.readInput()},!1));this.jsCalendar._elements.table.addEventListener("click",function(c){c.stopPropagation()},!1);window.addEventListener("click",function(c){c.target!==b._target&&b.hide()},!1);window.addEventListener("resize",function(){b._visible&&b.position()},!1);window.addEventListener("scroll",function(){b._visible&&b.position()},!1);a.hasOwnProperty("date")?this.set(a.date): (a=new Date(b.jsCalendar._date.getTime()),this.set(a))};d.prototype._parseArguments=function(a){var b={target:null,options:{}};if(0===a.length)throw Error("jsCalendar-datepicker: No parameters were given.");if(1===a.length)if(("object"===typeof HTMLElement?a[0]instanceof HTMLElement:a[0])&&"object"===typeof a[0]&&null!==a[0]&&1===a[0].nodeType&&"string"===typeof a[0].nodeName||"string"===typeof a[0])b.target=a[0];else if(b.options=a[0],"undefined"!==typeof a[0].target)b.target=a[0].target;else throw Error("jsCalendar-datepicker: Not target was given."); else b.target=a[0],3<=a.length&&(b.options=a[2]),2<=a.length&&(b.options.date=a[1]);return b};d.prototype._setTarget=function(a){if(a=h.tools.getElement(a))this._target=a;else throw Error("jsCalendar-datepicker: Target was not found.");};d.prototype._parseOptions=function(a){this._options={};var b={};for(c in d.options)d.options.hasOwnProperty(c)&&(this._options[c]=d.options[c]instanceof Array?d.options[c].slice(0):d.options[c]),a.hasOwnProperty(c)?b[c]=a[c]:this._target.dataset.hasOwnProperty(c)&& (b[c]=this._target.dataset[c]);"undefined"!==typeof b.format&&(this._options.format=b.format);if("undefined"!==typeof b.offsetTop)if("number"===typeof b.offsetTop)this._options.offsetTop=b.offsetTop;else if("string"===typeof b.offsetTop){var c=parseInt(b.offsetTop,10);isNaN(c)||(this._options.offsetTop=c)}"undefined"!==typeof b.offsetLeft&&("number"===typeof b.offsetLeft?this._options.offsetLeft=b.offsetLeft:"string"===typeof b.offsetLeft&&(c=parseInt(b.offsetLeft,10),isNaN(c)||(this._options.offsetLeft= c)));"string"===typeof b["class"]&&(this._options["class"]=b["class"]);"undefined"!==typeof b.yearsLine&&b.yearsLine&&(c=parseInt(b.yearsLine,10),isNaN(c)?this._options.yearsLine=!0:0<c&&(this._options.yearsLine=c));b={};for(c in h.options)a.hasOwnProperty(c)?b[c]=a[c]:this._target.dataset.hasOwnProperty(c)&&(b[c]=this._target.dataset[c]);a.hasOwnProperty("date")?b.date=a.date:b.hasOwnProperty("date")||(0<this._target.value.length&&null!==h.tools.parseDate(this._target.value,!0)?b.date=this._target.value: this._target.dataset.hasOwnProperty("date")&&(b.date=this._target.dataset.date));this._options.autoValueRead="DD/MM/YYYY"===this._options.format||"DD-MM-YYYY"===this._options.format;return b};d.prototype._onFocus=function(){this.jsCalendar.reset();this.show()};d.prototype._onPick=function(a){this.jsCalendar._options.min&&this.jsCalendar._options.min.getTime()>a||this.jsCalendar._options.max&&this.jsCalendar._options.max.getTime()<a||(this._target.value=h.tools.dateToString(a,this._options.format, this.jsCalendar._options.language),this.jsCalendar.set(a),this.hide())};d.prototype.show=function(){this._visible||(this.position(),this._wrapper.style.display="block",this._visible=!0)};d.prototype.hide=function(){this._wrapper.style.display="none";this._visible=!1};d.prototype.isVisible=function(){return this._visible};d.prototype.position=function(){var a=this._wrapper.style.display;this._wrapper.style.top="-9999px";this._wrapper.style.left="-9999px";this._wrapper.style.display="block";var b=d.detectElementPositionInfo(this._target), c=b.left,e=b.top;e=e+(this._target.clientHeight||this._target.offsetHeight||this._target.scrollHeight||20)+this._options.offsetTop;c+=this._options.offsetLeft;document.body.offsetWidth<c+this._wrapper.offsetWidth&&(c=document.body.offsetWidth-this._wrapper.offsetWidth-1);this._wrapper.style.zIndex=b.zIndex?b.zIndex:1;this._wrapper.style.display=a;this._wrapper.style.top=e+"px";this._wrapper.style.left=c+"px"};d.prototype.readInput=function(){var a=h.tools.stringToDate(this._target.value,!0);null!== a&&(this.jsCalendar.set(a),this._yearsLineUpdate(a));return a};d.prototype.set=function(a){a=h.tools.parseDate(a);this.jsCalendar.set(a);this._yearsLineUpdate(a);this._target.value=h.tools.dateToString(a,this._options.format,this.jsCalendar._options.language)};d.prototype._creatYearsLine=function(){if(!this._yearsLine){this._yearsLine={};var a=document.createElement("tr");a.className="jsCalendar-years-line";var b=document.createElement("th");b.setAttribute("colspan","7");b.style.width="100%";a.appendChild(b); var c=document.createElement("table");c.style.margin="0 auto 0 auto";c.style.boxShadow="none";b.appendChild(c);var e=document.createElement("tbody");e.style.margin="0";c.appendChild(e);b=document.createElement("tr");e.appendChild(b);e=[];for(var g,f=this,m=!0===this._options.yearsLine?5:this._options.yearsLine,l=0;l<m;l++)g=document.createElement("td"),g.style.display="table-cell",e.push(g),b.appendChild(g),g.addEventListener("click",function(){var k=new Date(f.jsCalendar._date.getTime());k.setFullYear(parseInt(this.dataset.year, 10));f.set(k)},!1);this._yearsLine.wrapper=a;this._yearsLine.table=c;this._yearsLine.years=e;this.jsCalendar._elements.head.insertBefore(a,this.jsCalendar._elements.head.childNodes[0]);this.jsCalendar.onMonthChange(function(){var k=new Date(f.jsCalendar._date.getTime());f._yearsLineUpdate(k)})}};d.prototype._yearsLineUpdate=function(a){if(this._yearsLine){var b=this._yearsLine.years.length;a=a.getFullYear();for(var c=0,e=a-(Math.ceil(b/2)-1);c<b;c++,e++)this._yearsLine.years[c].textContent=e,this._yearsLine.years[c].dataset.year= e,this._yearsLine.years[c].className=e===a?"jsCalendar-current":""}};d.detectElementPositionInfo=function(a){var b=0,c=0,e=!1,g=!1,f;for(f=window.getComputedStyle(a);a;){b+=a.offsetLeft-a.scrollLeft;c+=a.offsetTop-a.scrollTop;if("fixed"==f.position){e="fixed";c+=window.scrollY;b+=window.scrollX;break}if("absolute"==f.position||"fixed"==f.position)g=parseInt(f.zIndex,10),g=isNaN(g)?0:g;if(a=a.offsetParent)f=window.getComputedStyle(a),b+=parseInt(f.borderLeftWidth,10),c+=+parseInt(f.borderTopWidth, 10)}for(;a;){f=window.getComputedStyle(a);if("fixed"==f.position||"absolute"==f.position)g=parseInt(f.zIndex,10),g=isNaN(g)?0:g;a=a.offsetParent}return{top:c,left:b,zIndex:g,position:e}};d.autoFind=function(){for(var a=document.getElementsByTagName("input"),b=[],c=0;c<a.length;c++)"text"===a[c].type&&a[c].dataset.hasOwnProperty("datepicker")&&"true"!==a[c].getAttribute("jsCalendar-datepicker-loaded")&&b.push(a[c]);for(c=0;c<b.length;c++)b[c].setAttribute("jsCalendar-datepicker-loaded","true"),new d(b[c])}; h.datepicker=function(){var a=new d;a._construct(arguments);return a};window.addEventListener("load",function(){d.autoFind()},!1);h.ext("DatePicker",d)})(window.jsCalendar);