Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
/**
* DatePickerX
*
* Cool light visual date picker on pure JavaScript
* Browsers support: Chrome 45+, FireFox 40+, Safari 8+, IE10+, iOS Safari 8+, Android Browser 4.4+
*
* @author Alexander Krupko <sanych.zp@gmail.com>
* @author Slava Krasnyansky <krasnyansky.v@gmail.com>
* @copyright 2016 Avrora Team www.avrora.team
* @license MIT
* @tutorial http://datepickerx.avrora.team
* @version 1.0.2
*/
!function(){'use strict';function a(a,b,c,d,e){b=b||[],!Array.isArray(b)&&(b=[b]);for(var f=document.createElement(a),g=b.length;g--;f.classList.add(b[g]));return e&&(f.title=e),f.innerHTML=d||'',c instanceof Element&&c.appendChild(f),f}function b(a){return a=a||new Date,new Date(a.getFullYear(),a.getMonth(),a.getDate())}function c(c){function f(){var a=n.minDate;return a instanceof HTMLInputElement&&(a=a.DatePickerX.getValue(!0),a=null===a?n.minDate.DatePickerX.getMinDate():new Date(a),a.setDate(a.getDate()+1)),b(a)}function g(){var a=n.maxDate;return a instanceof HTMLInputElement&&(a=a.DatePickerX.getValue(!0),a=null===a?n.maxDate.DatePickerX.getMaxDate():new Date(a),a.setDate(a.getDate()-1)),b(a)}function h(){o.container=a('div','date-picker-x');var b=a('div','dpx-title-box',o.container);if(o.prevTitle=a('span','dpx-prev',b,'&#x276e;'),o.title=a('span','dpx-title',b),o.nextTitle=a('span','dpx-next',b,'&#x276f;'),o.content=a('div','dpx-content-box',o.container),c.nextElementSibling?c.parentNode.insertBefore(o.container,c.nextElementSibling):c.parentNode.appendChild(o.container),n.todayButton||n.clearButton){var d=a('div','dpx-btns',o.container);n.todayButton&&(o.today=a('span',['dpx-item','dpx-today'],d,n.todayButtonLabel,n.todayButtonLabel)),n.clearButton&&(o.clear=a('span',['dpx-item','dpx-clear'],d,n.clearButtonLabel,n.clearButtonLabel))}}function i(a,b){var c={d:a.getDate(),dd:a.getDate(),D:a.getDay(),m:a.getMonth()+1,mm:a.getMonth()+1,M:a.getMonth(),MM:a.getMonth(),yy:a.getFullYear().toString().substr(-2),yyyy:a.getFullYear()};return c.dd<10&&(c.dd='0'+c.dd),c.mm<10&&(c.mm='0'+c.mm),c.D=n.weekDayLabels[c.D?c.D-1:6],c.M=n.shortMonthLabels[c.M],c.MM=n.singleMonthLabels[c.MM],b.replace(/(?:[dmM]{1,2}|D|yyyy|yy)/g,function(a){return'undefined'!=typeof c[a]?c[a]:a})}function j(){return o.container.classList.contains('active')}function k(a){c.addEventListener('click',function(a){if(!j()){a.stopPropagation(),r=2,l(),o.container.classList.add('active'),o.container.classList.remove('to-top');var b=o.container.getBoundingClientRect();b.bottom>window.innerHeight&&b.top>o.container.offsetHeight&&o.container.classList.add('to-top'),e&&e!==o.container&&e.classList.remove('active'),e=o.container}}),window.addEventListener('click',function(){e&&e.classList.remove('active')}),o.container.addEventListener('click',function(a){a.stopPropagation(),a.preventDefault()}),o.content.addEventListener('click',function(b){if(2===r)a.setValue(b.target.dpxValue)&&o.container.classList.remove('active');else{var c=f(),d=g();c.setDate(1),d.setDate(1),r||(c.setMonth(0),d.setMonth(0)),b.target.dpxValue>=c.getTime()&&b.target.dpxValue<=d.getTime()&&++r&&l(b.target.dpxValue)}}),o.prevTitle.addEventListener('click',function(){l(this.dpxValue)}),o.nextTitle.addEventListener('click',function(){l(this.dpxValue)}),o.title.addEventListener('click',function(){r&&r--&&l(this.dpxValue)}),o.today&&o.today.addEventListener('click',function(){!this.classList.contains('dpx-disabled')&&a.setValue(b())&&o.container.classList.remove('active')}),o.clear&&o.clear.addEventListener('click',function(){a.setValue(null)&&o.container.classList.remove('active')})}function l(c){o.content.innerHTML='';var d=f(),e=g(),h=b();n.todayButton&&o.today.classList[h>=d&&h<=e?'remove':'add']('dpx-disabled'),r<2&&(d.setDate(1),e.setDate(1),r||(d.setMonth(0),e.setMonth(0))),d=d.getTime(),e=e.getTime(),c=b(new Date(c||q||Date.now())),c.getTime()<d?c=new Date(d):c.getTime()>e&&(c=new Date(e));var i=c.getMonth(),j=c.getFullYear(),k=10*Math.floor(j/10);if(c=new Date(r?j:k,r<2?0:i),o.title.innerHTML=r?(2===r?n.singleMonthLabels[i]+' ':'')+j:k+' - '+(k+9),o.title.dpxValue=c.getTime(),o.title.title=2===r?j:k+' - '+(k+9),o.prevTitle.classList[c.getTime()<=d?'add':'remove']('dpx-disabled'),2===r?c.setMonth(i-1):c.setFullYear(r?j-1:k-10),o.prevTitle.title=r?(2===r?n.singleMonthLabels[c.getMonth()]+' ':'')+c.getFullYear():k-10+' - '+(k-1),o.prevTitle.dpxValue=c.getTime(),2===r?c.setMonth(c.getMonth()+2):c.setFullYear(r?j+1:k+20),o.nextTitle.classList[c.getTime()>e?'add':'remove']('dpx-disabled'),o.nextTitle.title=r?(2===r?n.singleMonthLabels[c.getMonth()]+' ':'')+c.getFullYear():k+10+' - '+(k+19),o.nextTitle.dpxValue=c.getTime(),2===r?c.setMonth(c.getMonth()-1):c.setFullYear(r?j:k),2===r){var l=n.weekDayLabels.length;!n.mondayFirst&&--l&&a('span',['dpx-item','dpx-weekday','dpx-weekend'],o.content,n.weekDayLabels[6]);for(var m=0;m<l;++m){var p=['dpx-item','dpx-weekday'];m>4&&p.push('dpx-weekend'),a('span',p,o.content,n.weekDayLabels[m])}}if(2===r){var s=c.getDay();c.setDate(n.mondayFirst?-(s?s-2:5):-s+1)}else r?c.setMonth(c.getMonth()-2):c.setFullYear(k-3);var t=q;r<2&&(null!==t&&(t=new Date(t),t.setDate(1),!r&&t.setMonth(0),t=t.getTime()),h.setDate(1),!r&&h.setMonth(0)),h=h.getTime(),o.container.setAttribute('data-dpx-type',['year','month','day'][r]);for(var u=['getFullYear','getMonth','getDate'][r],v=['setFullYear','setMonth','setDate'][r],w=2===r?42:16;w--;c[v](c[u]()+1)){var p=['dpx-item'],x=r?n.singleMonthLabels[c.getMonth()]+' ':'';2===r&&(x+=c.getDate()+', '),x+=c.getFullYear(),(r?2===r?c.getMonth()!==i:c.getFullYear()!==j:c.getFullYear()<k||c.getFullYear()>k+9)&&p.push('dpx-out'),2===r&&(6===c.getDay()||0===c.getDay())&&p.push('dpx-weekend'),c.getTime()===h&&p.push('dpx-current'),c.getTime()===q&&p.push('dpx-selected'),(c.getTime()<d||c.getTime()>e)&&p.push('dpx-disabled');var y=r?2===r?c.getDate():n.shortMonthLabels[c.getMonth()]:c.getFullYear(),z=a('span',p,o.content,y,x);z.dpxValue=c.getTime()}}function m(a,b){if('undefined'==typeof n[a])return console.error('DatePickerX, setOption: Option doesn\'t exist.')&&!1;if('minDate'===a||'maxDate'===a){if(!(b instanceof HTMLInputElement)&&(!(b instanceof Date)&&(b=new Date(b)),isNaN(b)))return console.error('DatePickerX, setOption: Invalid date value.')&&!1}else{if(typeof n[a]!=typeof b)return console.error('DatePickerX, setOption: Option has invalid type.')&&!1;if(Array.isArray(n[a])){if(b.length<n[a].length)return console.warn('DatePickerX, setOption: Invalid option length.')&&!1;b=b.slice(0,n[a].length)}}n[a]=b}var n={},o={},p=!1,q=null,r=2;return{init:function(a){if(a=a||{},p)return console.error('DatePickerX, init: Date picker have already inited.')&&!1;p=!0,n={};for(var b in d)n[b]=d[b];if('object'!=typeof a)console.error('DatePickerX, init: Initial options must be an object.');else for(var b in a)m(b,a[b]);return c.parentNode.classList.add('date-picker-x-container'),c.classList.add('date-picker-x-input'),c.readOnly=!0,h(),k(this),!0},remove:function(){return p?(c.parentNode.removeChild(o.container),c.classList.remove('date-picker-x-input'),c.readOnly=p=!1,!0):console.error('DatePickerX, remove: Date picker doesn\'t init yet.')&&!1},setValue:function(a,b){if(!p)return console.error('DatePickerX, remove: Date picker doesn\'t init yet.')&&!1;if(null===a)q=null,c.value='';else{if(!(a instanceof Date)&&(a=new Date(a)),isNaN(a))return console.error('DatePickerX, setValue: Can\'t convert argument to date.')&&!1;if(!b&&(a.getTime()<f().getTime()||a.getTime()>g().getTime()))return console.error('DatePickerX, setValue: Date out of acceptable range.')&&!1;q=a.getTime(),c.value=i(a,n.format)}var d=document.createEvent('Event');return d.initEvent('change',!0,!0),c.dispatchEvent(d),j()&&l(),!0},getValue:function(a){return!p&&console.error('DatePickerX, getValue: Date picker doesn\'t init yet.'),a?q:null===q?'':i(new Date(q),n.format)},getMinDate:function(){var a=n.minDate;return a instanceof HTMLInputElement&&(a=a.DatePickerX.getMinDate()),b(a)},getMaxDate:function(){var a=n.maxDate;return a instanceof HTMLInputElement&&(a=a.DatePickerX.getMaxDate()),b(a)}}}var d={mondayFirst:!0,format:'yyyy/mm/dd',minDate:new Date(0,0),maxDate:new Date(9999,11,31),weekDayLabels:['Mo','Tu','We','Th','Fr','St','Su'],shortMonthLabels:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],singleMonthLabels:['January','February','March','April','May','June','July','August','September','October','November','December'],todayButton:!0,todayButtonLabel:'Today',clearButton:!0,clearButtonLabel:'Clear'},e=null,f=[],g=[];Object.defineProperty(HTMLInputElement.prototype,'DatePickerX',{get:function(){var a=f.indexOf(this);return a===-1&&(a=f.push(this)-1,g.push(new c(this))),g[a]},set:function(){}}),window.DatePickerX={setDefaults:function(a){if('object'!=typeof a)return console.error('DatePickerX, setDefaults: Invalid option type.')&&!1;for(var b in a)typeof a[b]==typeof d[b]&&(Array.isArray(d[b])?a[b].length>=d[b].length&&(d[b]=a[b].slice(0,d[b].length)):d[b]=a[b]);return!0}}}();