jquery-ui/ui/date.js

198 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-12-02 17:54:47 +00:00
/*!
* jQuery UI Date @VERSION
* http://jqueryui.com
*
2015-12-02 17:54:47 +00:00
* Copyright jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*/
2015-12-02 17:54:47 +00:00
//>>label: Date
//>>group: Core
//>>description: Calendar math built on Globalize.
//>>docs: http://api.jqueryui.com/date/
2015-07-15 13:40:38 +00:00
( function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( [
"jquery", "./version" ], factory );
2015-07-15 13:40:38 +00:00
} else {
// Browser globals
factory( jQuery );
2015-07-15 13:40:38 +00:00
}
}( function( $ ) {
$.ui.date = function( date, attributes ) {
if ( !( this instanceof $.ui.date ) ) {
return new $.ui.date( date, attributes );
}
this.setAttributes( attributes );
2013-11-30 12:22:17 +00:00
if ( typeof date === "string" && date.length ) {
this.dateObject = attributes.parse( date );
} else if ( $.type( date ) === "date" ) {
this.dateObject = date;
}
this.dateObject = this.dateObject || new Date();
};
2013-11-30 12:22:17 +00:00
$.extend( $.ui.date.prototype, {
setAttributes: function( attributes ) {
this.attributes = attributes;
this.firstDay = this.attributes.firstDay;
},
setTimestamp: function( time ) {
this.dateObject.setTime( time );
return this;
},
setDay: function( day ) {
this.dateObject.setDate( day );
return this;
},
setFullDate: function( year, month, day ) {
this.dateObject = new Date( year, month, day );
return this;
},
adjust: function( period, offset ) {
var date = this.dateObject,
day = period === "D" ? date.getDate() + offset : date.getDate(),
month = period === "M" ? date.getMonth() + offset : date.getMonth(),
year = period === "Y" ? date.getFullYear() + offset : date.getFullYear();
// If not day, update the day to the new month and year
if ( period !== "D" ) {
day = Math.max( 1, Math.min( day, this.daysInMonth( year, month ) ) );
}
this.dateObject = new Date( year, month, day, date.getHours(),
date.getMinutes(), date.getSeconds() );
return this;
},
daysInMonth: function( year, month ) {
var date = this.dateObject;
year = year || date.getFullYear();
month = month || date.getMonth();
return 32 - new Date( year, month, 32 ).getDate();
},
monthName: function() {
return this.attributes.formatMonth( this.dateObject );
},
day: function() {
return this.dateObject.getDate();
},
month: function() {
return this.dateObject.getMonth();
},
year: function() {
return this.dateObject.getFullYear();
},
weekdays: function() {
2015-09-27 16:57:46 +00:00
var date, dow,
firstDay = this.firstDay,
result = [];
// date = firstDay
date = new Date( this.dateObject.getTime() );
date.setDate( date.getDate() + firstDay - 1 - date.getDay() );
2015-09-27 16:57:46 +00:00
for ( dow = 0; dow < 7; dow++ ) {
date.setTime( date.getTime() + 86400000 );
2015-09-27 16:57:46 +00:00
result.push( {
shortname: this.attributes.formatWeekdayShort( date ),
fullname: this.attributes.formatWeekdayFull( date )
2015-09-27 16:57:46 +00:00
} );
}
return result;
},
days: function() {
2015-09-27 16:57:46 +00:00
var row, week, dayx, day,
result = [],
today = new $.ui.date( new Date(), this.attributes ),
date = this.dateObject,
firstDayOfMonth = new Date( this.year(), date.getMonth(), 1 ).getDay(),
leadDays = ( firstDayOfMonth - this.firstDay + 7 ) % 7,
rows = Math.ceil( ( leadDays + this.daysInMonth() ) / 7 ),
printDate = new Date( this.year(), date.getMonth(), 1 - leadDays );
2015-09-27 16:57:46 +00:00
for ( row = 0; row < rows; row++ ) {
week = result[ result.length ] = {
number: this.attributes.formatWeekOfYear( printDate ),
days: []
};
2015-09-27 16:57:46 +00:00
for ( dayx = 0; dayx < 7; dayx++ ) {
day = week.days[ week.days.length ] = {
lead: printDate.getMonth() !== date.getMonth(),
date: printDate.getDate(),
month: printDate.getMonth(),
year: printDate.getFullYear(),
timestamp: printDate.getTime(),
today: today.equal( printDate )
};
day.render = day.selectable = !day.lead;
if ( this.eachDay ) {
this.eachDay( day );
}
printDate.setDate( printDate.getDate() + 1 );
}
}
return result;
},
// specialized for multi-month template, could be used in general
months: function( add ) {
2015-09-27 16:57:46 +00:00
var clone, i,
result = [ this ];
2015-09-27 16:57:46 +00:00
for ( i = 0; i < add; i++ ) {
clone = this.clone();
clone.adjust( "M", i + 1 );
result.push( clone );
}
result[ 0 ].first = true;
result[ result.length - 1 ].last = true;
return result;
},
clone: function() {
var date = this.dateObject;
return new $.ui.date( new Date( date.getTime() ), this.attributes );
},
equal: function( other ) {
var format = function( date ) {
return "" + date.getFullYear() + date.getMonth() + date.getDate();
};
return format( this.dateObject ) === format( other );
},
timestamp: function() {
return this.dateObject.getTime();
},
date: function() {
return this.dateObject;
}
2015-09-27 16:57:46 +00:00
} );
return $.ui.date;
2015-07-15 13:40:38 +00:00
} ) );