Comparing version 1.0.2 to 1.1.0
@@ -40,4 +40,6 @@ ;(function(global) { | ||
function calendarClick(e) { | ||
var time = new Date(this.year, this.month).getTime(); | ||
switch (e.target.getAttribute('data-target')) { | ||
case 'month-prev': | ||
if (this.config.minDate && time <= this.config.minDate) return; | ||
this.month--; | ||
@@ -51,2 +53,3 @@ if (this.month < 0) { | ||
case 'month-next': | ||
if (this.config.maxDate && time >= this.config.maxDate) return; | ||
this.month++; | ||
@@ -306,2 +309,3 @@ if (this.month > 11) { | ||
return function(el, cb, options) { | ||
var datepickr = {}; | ||
this.element = el; | ||
@@ -314,2 +318,4 @@ this.callback = cb; | ||
startMonth: date.current.month.integer(), | ||
minDate: null, | ||
maxDate: null, | ||
halfDay: false, | ||
@@ -344,5 +350,16 @@ omitPast: false, | ||
datepickr.options = function(options) { | ||
if (options) { | ||
for (var key in options) { | ||
if (this.config.hasOwnProperty(key)) { | ||
this.config[key] = options[key]; | ||
} | ||
} | ||
} | ||
}.bind(this); | ||
this.year = this.config.startYear; | ||
this.month = this.config.startMonth; | ||
buildCalendar.call(this); | ||
return datepickr; | ||
}; | ||
@@ -349,0 +366,0 @@ })(); |
@@ -1,1 +0,1 @@ | ||
(function(global){var Datepickr=function(){var currentDate=new Date;var daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];var buildCache=[];var date={current:{year:function(){return currentDate.getFullYear()},month:{integer:function(){return currentDate.getMonth()},string:function(months){var date=currentDate.getMonth();return monthToStr(date,months)}},day:function(){return currentDate.getDate()}},month:{string:function(month,months){var date=month;return monthToStr(date,months)},numDays:function(month,year){return month===1&&!(year&3)&&(year%100||!(year%400))?29:daysInMonth[month]}}};function calendarClick(e){switch(e.target.getAttribute("data-target")){case"month-prev":this.month--;if(this.month<0){this.year--;this.month=11}rebuildCalendar.call(this);break;case"month-next":this.month++;if(this.month>11){this.year++;this.month=0}rebuildCalendar.call(this);break;case"day":var today=(new Date).getTime();var d=new Date(this.year,this.month,e.target.textContent).getTime();var c=e.target.classList;if(this.config.halfDay){if(c.contains("halfday")){c.remove("halfday");this.config.activeDays=this.config.activeDays.map(function(date){if(date[0]===d)date[1]=1;return date})}else if(c.contains("active")){c.remove("active","halfday");this.config.activeDays=this.config.activeDays.filter(function(date){return date[0]!==d})}else{c.add("active","halfday");this.config.activeDays.push([d,.5])}}else{if(c.contains("active")){c.remove("active","halfday");this.config.activeDays=this.config.activeDays.filter(function(date){return date[0]!==d})}else{c.add("active");this.config.activeDays.push([d,1])}}this.config.activeDays.sort(function(a,b){return a[0]>b[0]});this.callback(this.config.activeDays);break}}function buildNode(nodeName,attributes,content){if(!(nodeName in buildCache)){buildCache[nodeName]=document.createElement(nodeName)}var element=buildCache[nodeName].cloneNode(false);if(attributes){for(var attribute in attributes){element.setAttribute(attribute,attributes[attribute])}}if(content){if(typeof content==="object"){element.appendChild(content)}else{element.textContent=content}}return element}function monthToStr(date,months){return months[date]}function roundDate(d){return new Date(d.getFullYear(),d.getMonth(),d.getDate())}function isToday(year,month,day){return day===date.current.day()&&month===date.current.month.integer()&&year===date.current.year()}function isPast(year,month,day){return new Date(year,month,day).getTime()<(new Date).getTime()}function isWeekend(year,month,day){var d=new Date(year,month,day).getDay();return d===0||d===6}function isOmitted(year,month,day){var d=new Date(year,month,day).getTime(),is;if(this.config.omitDays.length){this.config.omitDays.forEach(function(omitted){if(omitted===d)is=true})}return is}function buildWeekdays(weekdays){var weekdayHtml=document.createDocumentFragment();weekdays.forEach(function(weekday){weekdayHtml.appendChild(buildNode("th",{},weekday))});return weekdayHtml}function rebuildCalendar(){while(this.calendarBody.hasChildNodes()){this.calendarBody.removeChild(this.calendarBody.lastChild)}var firstOfMonth=new Date(this.year,this.month,1).getDay(),numDays=date.month.numDays(this.month,this.year);this.currentMonth.textContent=date.month.string(this.month,this.config.months)+" "+this.year;this.calendarBody.appendChild(buildDays.call(this,firstOfMonth,numDays,this.month,this.year))}function buildCurrentMonth(config,month,year,months){return buildNode("strong",{"class":"small"},date.month.string(month,months)+" "+year)}function buildMonths(config,month,year){var months=buildNode("div",{"class":"months"});var prevMonth=buildNode("a",{"class":"icon next button short quiet","data-target":"month-next",href:"#"});var nextMonth=buildNode("a",{"class":"icon prev button short quiet","data-target":"month-prev",href:"#"});months.appendChild(prevMonth);months.appendChild(nextMonth);return months}function buildDays(firstOfMonth,numDays,month,year){var calendarBody=document.createDocumentFragment(),row=buildNode("tr"),dayCount=0,klass,omit,i;for(i=1;i<=firstOfMonth;i++){row.appendChild(buildNode("td"));dayCount++}for(i=1;i<=numDays;i++){omit=false;if(dayCount===7){calendarBody.appendChild(row);row=buildNode("tr");dayCount=0}if(isToday(year,month,i)){if(this.config.omitWeekends&&isWeekend(year,month,i)){klass="today quiet";omit=true}else{klass="today"}}else if(this.config.omitPast&&isPast(year,month,i)||this.config.omitWeekends&&isWeekend(year,month,i)||this.config.omitDays&&this.config.omitDays.length&&isOmitted.call(this,year,month,i)){klass="fill-light quiet";omit=true}else{klass="fill-light"}if(this.config.activeDays.length){this.config.activeDays.forEach(function(d){if(roundDate(new Date(d[0])).getTime()===new Date(year,month,i).getTime()){klass+=d[1]===1?" active":" halfday active"}})}row.appendChild(buildNode("td",{},buildNode("a",{"class":klass,"data-target":!omit?"day":false,href:"#"},i)));dayCount++}for(i=1;i<=7-dayCount;i++){row.appendChild(buildNode("td"))}calendarBody.appendChild(row);return calendarBody}function buildCalendar(){var firstOfMonth=new Date(this.config.startYear,this.config.startMonth,1).getDay();var numDays=date.month.numDays(this.month,this.year);var calendarDiv=buildNode("div",{"class":"date-pickr"});this.currentMonth=buildCurrentMonth(this.config,this.month,this.year,this.config.months);var months=buildMonths(this.config,this.month,this.year);months.appendChild(this.currentMonth);var calendar=buildNode("table",{"class":"small"},buildNode("thead",{},buildNode("tr",{"class":"weekdays"},buildWeekdays(this.config.weekdays))));this.calendarBody=buildNode("tbody");this.calendarBody.appendChild(buildDays.call(this,firstOfMonth,numDays,this.month,this.year));calendar.appendChild(this.calendarBody);calendarDiv.appendChild(months);calendarDiv.appendChild(calendar);this.element.appendChild(calendarDiv);calendarDiv.addEventListener("click",function(e){e.preventDefault();calendarClick.call(this,e)}.bind(this));return calendarDiv}return function(el,cb,options){this.element=el;this.callback=cb;this.config={weekdays:["Sun","Mon","Tue","Wed","Thur","Fri","Sat"],months:["January","February","March","April","May","June","July","August","September","October","November","December"],startYear:date.current.year(),startMonth:date.current.month.integer(),halfDay:false,omitPast:false,omitWeekends:false,omitDays:[],activeDays:[]};if(options){for(var key in options){if(this.config.hasOwnProperty(key)){this.config[key]=options[key]}}}if(this.config.activeDays.length){this.config.activeDays=this.config.activeDays.map(function(d){return[roundDate(new Date(d[0])).getTime(),d[1]]})}if(this.config.omitDays.length){this.config.omitDays=this.config.omitDays.map(function(d){return roundDate(new Date(d)).getTime()})}this.year=this.config.startYear;this.month=this.config.startMonth;buildCalendar.call(this)}}();global.Datepickr=Datepickr;if(typeof module!=="undefined"&&module.exports)module.exports=Datepickr})(this); | ||
(function(global){var Datepickr=function(){var currentDate=new Date;var daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];var buildCache=[];var date={current:{year:function(){return currentDate.getFullYear()},month:{integer:function(){return currentDate.getMonth()},string:function(months){var date=currentDate.getMonth();return monthToStr(date,months)}},day:function(){return currentDate.getDate()}},month:{string:function(month,months){var date=month;return monthToStr(date,months)},numDays:function(month,year){return month===1&&!(year&3)&&(year%100||!(year%400))?29:daysInMonth[month]}}};function calendarClick(e){var time=new Date(this.year,this.month).getTime();switch(e.target.getAttribute("data-target")){case"month-prev":if(this.config.minDate&&time<=this.config.minDate)return;this.month--;if(this.month<0){this.year--;this.month=11}rebuildCalendar.call(this);break;case"month-next":if(this.config.maxDate&&time>=this.config.maxDate)return;this.month++;if(this.month>11){this.year++;this.month=0}rebuildCalendar.call(this);break;case"day":var today=(new Date).getTime();var d=new Date(this.year,this.month,e.target.textContent).getTime();var c=e.target.classList;if(this.config.halfDay){if(c.contains("halfday")){c.remove("halfday");this.config.activeDays=this.config.activeDays.map(function(date){if(date[0]===d)date[1]=1;return date})}else if(c.contains("active")){c.remove("active","halfday");this.config.activeDays=this.config.activeDays.filter(function(date){return date[0]!==d})}else{c.add("active","halfday");this.config.activeDays.push([d,.5])}}else{if(c.contains("active")){c.remove("active","halfday");this.config.activeDays=this.config.activeDays.filter(function(date){return date[0]!==d})}else{c.add("active");this.config.activeDays.push([d,1])}}this.config.activeDays.sort(function(a,b){return a[0]>b[0]});this.callback(this.config.activeDays);break}}function buildNode(nodeName,attributes,content){if(!(nodeName in buildCache)){buildCache[nodeName]=document.createElement(nodeName)}var element=buildCache[nodeName].cloneNode(false);if(attributes){for(var attribute in attributes){element.setAttribute(attribute,attributes[attribute])}}if(content){if(typeof content==="object"){element.appendChild(content)}else{element.textContent=content}}return element}function monthToStr(date,months){return months[date]}function roundDate(d){return new Date(d.getFullYear(),d.getMonth(),d.getDate())}function isToday(year,month,day){return day===date.current.day()&&month===date.current.month.integer()&&year===date.current.year()}function isPast(year,month,day){return new Date(year,month,day).getTime()<(new Date).getTime()}function isWeekend(year,month,day){var d=new Date(year,month,day).getDay();return d===0||d===6}function isOmitted(year,month,day){var d=new Date(year,month,day).getTime(),is;if(this.config.omitDays.length){this.config.omitDays.forEach(function(omitted){if(omitted===d)is=true})}return is}function buildWeekdays(weekdays){var weekdayHtml=document.createDocumentFragment();weekdays.forEach(function(weekday){weekdayHtml.appendChild(buildNode("th",{},weekday))});return weekdayHtml}function rebuildCalendar(){while(this.calendarBody.hasChildNodes()){this.calendarBody.removeChild(this.calendarBody.lastChild)}var firstOfMonth=new Date(this.year,this.month,1).getDay(),numDays=date.month.numDays(this.month,this.year);this.currentMonth.textContent=date.month.string(this.month,this.config.months)+" "+this.year;this.calendarBody.appendChild(buildDays.call(this,firstOfMonth,numDays,this.month,this.year))}function buildCurrentMonth(config,month,year,months){return buildNode("strong",{"class":"small"},date.month.string(month,months)+" "+year)}function buildMonths(config,month,year){var months=buildNode("div",{"class":"months"});var prevMonth=buildNode("a",{"class":"icon next button short quiet","data-target":"month-next",href:"#"});var nextMonth=buildNode("a",{"class":"icon prev button short quiet","data-target":"month-prev",href:"#"});months.appendChild(prevMonth);months.appendChild(nextMonth);return months}function buildDays(firstOfMonth,numDays,month,year){var calendarBody=document.createDocumentFragment(),row=buildNode("tr"),dayCount=0,klass,omit,i;for(i=1;i<=firstOfMonth;i++){row.appendChild(buildNode("td"));dayCount++}for(i=1;i<=numDays;i++){omit=false;if(dayCount===7){calendarBody.appendChild(row);row=buildNode("tr");dayCount=0}if(isToday(year,month,i)){if(this.config.omitWeekends&&isWeekend(year,month,i)){klass="today quiet";omit=true}else{klass="today"}}else if(this.config.omitPast&&isPast(year,month,i)||this.config.omitWeekends&&isWeekend(year,month,i)||this.config.omitDays&&this.config.omitDays.length&&isOmitted.call(this,year,month,i)){klass="fill-light quiet";omit=true}else{klass="fill-light"}if(this.config.activeDays.length){this.config.activeDays.forEach(function(d){if(roundDate(new Date(d[0])).getTime()===new Date(year,month,i).getTime()){klass+=d[1]===1?" active":" halfday active"}})}row.appendChild(buildNode("td",{},buildNode("a",{"class":klass,"data-target":!omit?"day":false,href:"#"},i)));dayCount++}for(i=1;i<=7-dayCount;i++){row.appendChild(buildNode("td"))}calendarBody.appendChild(row);return calendarBody}function buildCalendar(){var firstOfMonth=new Date(this.config.startYear,this.config.startMonth,1).getDay();var numDays=date.month.numDays(this.month,this.year);var calendarDiv=buildNode("div",{"class":"date-pickr"});this.currentMonth=buildCurrentMonth(this.config,this.month,this.year,this.config.months);var months=buildMonths(this.config,this.month,this.year);months.appendChild(this.currentMonth);var calendar=buildNode("table",{"class":"small"},buildNode("thead",{},buildNode("tr",{"class":"weekdays"},buildWeekdays(this.config.weekdays))));this.calendarBody=buildNode("tbody");this.calendarBody.appendChild(buildDays.call(this,firstOfMonth,numDays,this.month,this.year));calendar.appendChild(this.calendarBody);calendarDiv.appendChild(months);calendarDiv.appendChild(calendar);this.element.appendChild(calendarDiv);calendarDiv.addEventListener("click",function(e){e.preventDefault();calendarClick.call(this,e)}.bind(this));return calendarDiv}return function(el,cb,options){var datepickr={};this.element=el;this.callback=cb;this.config={weekdays:["Sun","Mon","Tue","Wed","Thur","Fri","Sat"],months:["January","February","March","April","May","June","July","August","September","October","November","December"],startYear:date.current.year(),startMonth:date.current.month.integer(),minDate:null,maxDate:null,halfDay:false,omitPast:false,omitWeekends:false,omitDays:[],activeDays:[]};if(options){for(var key in options){if(this.config.hasOwnProperty(key)){this.config[key]=options[key]}}}if(this.config.activeDays.length){this.config.activeDays=this.config.activeDays.map(function(d){return[roundDate(new Date(d[0])).getTime(),d[1]]})}if(this.config.omitDays.length){this.config.omitDays=this.config.omitDays.map(function(d){return roundDate(new Date(d)).getTime()})}datepickr.options=function(options){if(options){for(var key in options){if(this.config.hasOwnProperty(key)){this.config[key]=options[key]}}}}.bind(this);this.year=this.config.startYear;this.month=this.config.startMonth;buildCalendar.call(this);return datepickr}}();global.Datepickr=Datepickr;if(typeof module!=="undefined"&&module.exports)module.exports=Datepickr})(this); |
17
index.js
@@ -40,4 +40,6 @@ ;(function(global) { | ||
function calendarClick(e) { | ||
var time = new Date(this.year, this.month).getTime(); | ||
switch (e.target.getAttribute('data-target')) { | ||
case 'month-prev': | ||
if (this.config.minDate && time <= this.config.minDate) return; | ||
this.month--; | ||
@@ -51,2 +53,3 @@ if (this.month < 0) { | ||
case 'month-next': | ||
if (this.config.maxDate && time >= this.config.maxDate) return; | ||
this.month++; | ||
@@ -306,2 +309,3 @@ if (this.month > 11) { | ||
return function(el, cb, options) { | ||
var datepickr = {}; | ||
this.element = el; | ||
@@ -314,2 +318,4 @@ this.callback = cb; | ||
startMonth: date.current.month.integer(), | ||
minDate: null, | ||
maxDate: null, | ||
halfDay: false, | ||
@@ -344,5 +350,16 @@ omitPast: false, | ||
datepickr.options = function(options) { | ||
if (options) { | ||
for (var key in options) { | ||
if (this.config.hasOwnProperty(key)) { | ||
this.config[key] = options[key]; | ||
} | ||
} | ||
} | ||
}.bind(this); | ||
this.year = this.config.startYear; | ||
this.month = this.config.startMonth; | ||
buildCalendar.call(this); | ||
return datepickr; | ||
}; | ||
@@ -349,0 +366,0 @@ })(); |
{ | ||
"name": "datepickr", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A do it yourself Date picker.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,5 +0,5 @@ | ||
Datepickr - The do it yourself date picker. | ||
Datepickr | ||
--- | ||
Datepickr is just a date picker. It makes as no assumptions how it's shown on | ||
The do it yourself date picker. It makes as no assumptions how it's shown on | ||
the page. [__See demo__](http://tristen.ca/datepickr/demo/). | ||
@@ -27,3 +27,3 @@ | ||
### `new Datepickr(element, callback, options);` | ||
#### `new Datepickr(element, callback, options);` | ||
@@ -50,2 +50,4 @@ The callback returns an array of arrayed dates whenever a date is selected. | ||
| startMonth | Current month | Start the date picker on a particular month | | ||
| minDate | null | a timestamp representing a maximum date the pager should page | | ||
| maxDate | null | a timestamp representing a minimum date the pager should page | | ||
| halfDay | false | Enables halfday selection of dates | | ||
@@ -59,5 +61,16 @@ | omitPast | false | Prevent selecting days in the past | | ||
### Use with node or browserify via `npm install datepickr`. | ||
### Adding/Removing options after initialization. | ||
``` js | ||
var pickr = new Datepickr(el); | ||
pickr.options({ | ||
halfDay: true | ||
}); | ||
``` | ||
### Use with node or browserify. | ||
``` js | ||
// npm install datepickr | ||
var datepickr = require('datepickr') | ||
@@ -64,0 +77,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111624
14
2232
84