1 /* =========================================================
2 * bootstrap-datepicker.js
3 * http://www.eyecon.ro/bootstrap-datepicker
4 * =========================================================
5 * Copyright 2012 Stefan Petre
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================================================= */
24 var Datepicker = function(element, options){
25 this.element = $(element);
26 this.format = DPGlobal.parseFormat(options.format||this.element.data('date-format')||'mm/dd/yyyy');
27 this.picker = $(DPGlobal.template)
30 click: $.proxy(this.click, this)//,
31 //mousedown: $.proxy(this.mousedown, this)
33 this.isInput = this.element.is('input');
34 this.component = this.element.is('.date') ? this.element.find('.add-on') : false;
38 focus: $.proxy(this.show, this),
39 //blur: $.proxy(this.hide, this),
40 keyup: $.proxy(this.update, this)
44 this.component.on('click', $.proxy(this.show, this));
46 this.element.on('click', $.proxy(this.show, this));
50 this.minViewMode = options.minViewMode||this.element.data('date-minviewmode')||0;
51 if (typeof this.minViewMode === 'string') {
52 switch (this.minViewMode) {
64 this.viewMode = options.viewMode||this.element.data('date-viewmode')||0;
65 if (typeof this.viewMode === 'string') {
66 switch (this.viewMode) {
78 this.color = options.color||'azure';
79 this.startViewMode = this.viewMode;
80 this.weekStart = options.weekStart||this.element.data('date-weekstart')||0;
81 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
82 this.onRender = options.onRender;
92 Datepicker.prototype = {
93 constructor: Datepicker,
96 var datepicker = this.picker;
99 this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
101 $(window).on('resize', $.proxy(this.place, this));
109 $(document).on('mousedown', function(ev){
110 if ($(ev.target).closest('.datepicker').length == 0) {
114 this.element.trigger({
119 setTimeout(function(){
120 datepicker.addClass('open');
125 var datepicker = this.picker;
126 datepicker.removeClass('open');
128 setTimeout(function(){
132 $(window).off('resize', this.place);
133 this.viewMode = this.startViewMode;
136 $(document).off('mousedown', this.hide);
139 this.element.trigger({
147 var formated = DPGlobal.formatDate(this.date, this.format);
150 this.element.find('input').prop('value', formated);
152 this.element.data('date', formated);
154 this.element.prop('value', formated);
158 setValue: function(newDate) {
159 if (typeof newDate === 'string') {
160 this.date = DPGlobal.parseDate(newDate, this.format);
162 this.date = new Date(newDate);
165 this.viewDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
170 var offset = this.component ? this.component.offset() : this.element.offset();
172 top: offset.top + this.height,
177 update: function(newDate){
178 this.date = DPGlobal.parseDate(
179 typeof newDate === 'string' ? newDate : (this.isInput ? this.element.prop('value') : this.element.data('date')),
182 this.viewDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
187 var dowCnt = this.weekStart;
189 while (dowCnt < this.weekStart + 7) {
190 html += '<th class="dow">'+DPGlobal.dates.daysMin[(dowCnt++)%7]+'</th>';
193 this.picker.find('.datepicker-days thead').append(html);
196 fillMonths: function(){
200 html += '<span class="month">'+DPGlobal.dates.monthsShort[i++]+'</span>';
202 this.picker.find('.datepicker-months td').append(html);
206 var d = new Date(this.viewDate),
207 year = d.getFullYear(),
208 month = d.getMonth(),
209 currentDate = this.date.valueOf();
210 this.picker.find('.datepicker-days th:eq(1)')
211 .text(DPGlobal.dates.months[month]+' '+year);
212 var prevMonth = new Date(year, month-1, 28,0,0,0,0),
213 day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
214 prevMonth.setDate(day);
215 prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7);
216 var nextMonth = new Date(prevMonth);
217 nextMonth.setDate(nextMonth.getDate() + 42);
218 nextMonth = nextMonth.valueOf();
223 while(prevMonth.valueOf() < nextMonth) {
224 if (prevMonth.getDay() === this.weekStart) {
227 clsName = this.onRender(prevMonth);
228 prevY = prevMonth.getFullYear();
229 prevM = prevMonth.getMonth();
230 if ((prevM < month && prevY === year) || prevY < year) {
232 } else if ((prevM > month && prevY === year) || prevY > year) {
235 if (prevMonth.valueOf() === currentDate) {
236 clsName += ' active ' + this.color;
238 html.push('<td class="day '+clsName+'"><p>'+prevMonth.getDate() + '</p></td>');
239 if (prevMonth.getDay() === this.weekEnd) {
242 prevMonth.setDate(prevMonth.getDate()+1);
244 this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
245 var currentYear = this.date.getFullYear();
247 var months = this.picker.find('.datepicker-months')
251 .find('span').removeClass('active');
252 if (currentYear === year) {
253 months.eq(this.date.getMonth()).addClass('active').addClass(this.color);
257 year = parseInt(year/10, 10) * 10;
258 var yearCont = this.picker.find('.datepicker-years')
260 .text(year + '-' + (year + 9))
264 for (var i = -1; i < 11; i++) {
265 html += '<span class="year'+(i === -1 || i === 10 ? ' old' : '')+(currentYear === year ? ' active ' : '')+ this.color + '">'+year+'</span>';
274 var target = $(e.target).closest('span, td, th');
275 if (target.length === 1) {
276 switch(target[0].nodeName.toLowerCase()) {
278 switch(target[0].className) {
279 case 'switch-datepicker':
284 this.viewDate['set'+DPGlobal.modes[this.viewMode].navFnc].call(
286 this.viewDate['get'+DPGlobal.modes[this.viewMode].navFnc].call(this.viewDate) +
287 DPGlobal.modes[this.viewMode].navStep * (target[0].className === 'prev' ? -1 : 1)
295 if (target.is('.month')) {
296 var month = target.parent().find('span').index(target);
297 this.viewDate.setMonth(month);
299 var year = parseInt(target.text(), 10)||0;
300 this.viewDate.setFullYear(year);
302 if (this.viewMode !== 0) {
303 this.date = new Date(this.viewDate);
304 this.element.trigger({
307 viewMode: DPGlobal.modes[this.viewMode].clsName
315 if (target.is('.day') && !target.is('.disabled')){
316 var day = parseInt(target.text(), 10)||1;
317 var month = this.viewDate.getMonth();
318 if (target.is('.old')) {
320 } else if (target.is('.new')) {
323 var year = this.viewDate.getFullYear();
324 this.date = new Date(year, month, day,0,0,0,0);
325 this.viewDate = new Date(year, month, Math.min(28, day),0,0,0,0);
328 this.element.trigger({
331 viewMode: DPGlobal.modes[this.viewMode].clsName
339 mousedown: function(e){
344 showMode: function(dir) {
346 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
348 this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
352 $.fn.datepicker = function ( option, val ) {
353 return this.each(function () {
355 data = $this.data('datepicker'),
356 options = typeof option === 'object' && option;
358 $this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.datepicker.defaults,options))));
360 if (typeof option === 'string') data[option](val);
364 $.fn.datepicker.defaults = {
365 onRender: function(date) {
369 $.fn.datepicker.Constructor = Datepicker;
389 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
390 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
391 daysMin: ["S", "M", "T", "W", "T", "F", "S", "S"],
392 months: ["JAN.", "FEB.", "MAR.", "APR.", "MAY", "JUN.", "JUL.", "AUG.", "SEPT.", "OCT.", "NOV.", "DEC."],
393 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
395 isLeapYear: function (year) {
396 return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
398 getDaysInMonth: function (year, month) {
399 return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
401 parseFormat: function(format){
402 var separator = format.match(/[.\/\-\s].*?/),
403 parts = format.split(/\W+/);
404 if (!separator || !parts || parts.length === 0){
405 throw new Error("Invalid date format.");
407 return {separator: separator, parts: parts};
409 parseDate: function(date, format) {
410 var parts = date.split(format.separator),
416 date.setMilliseconds(0);
417 if (parts.length === format.parts.length) {
418 var year = date.getFullYear(), day = date.getDate(), month = date.getMonth();
419 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
420 val = parseInt(parts[i], 10)||1;
421 switch(format.parts[i]) {
430 date.setMonth(val - 1);
434 date.setFullYear(2000 + val);
438 date.setFullYear(val);
442 date = new Date(year, month, day, 0 ,0 ,0);
446 formatDate: function(date, format){
449 m: date.getMonth() + 1,
450 yy: date.getFullYear().toString().substring(2),
451 yyyy: date.getFullYear()
453 val.dd = (val.d < 10 ? '0' : '') + val.d;
454 val.mm = (val.m < 10 ? '0' : '') + val.m;
456 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
457 date.push(val[format.parts[i]]);
459 return date.join(format.separator);
461 headTemplate: '<thead>'+
463 '<th class="prev"><p>‹</p></th>'+
464 '<th colspan="5" class="switch-datepicker"></th>'+
465 '<th class="next"><p>›</p></th>'+
468 contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>'
471 DPGlobal.template = '<div class="datepicker dropdown-menu">'+
472 '<div class="datepicker-days">'+
473 '<table class=" table-condensed">'+
474 DPGlobal.headTemplate+
478 '<div class="datepicker-months">'+
479 '<table class="table-condensed">'+
480 DPGlobal.headTemplate+
481 DPGlobal.contTemplate+
484 '<div class="datepicker-years">'+
485 '<table class="table-condensed">'+
486 DPGlobal.headTemplate+
487 DPGlobal.contTemplate+