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);
148 console.log(formated);
151 this.element.find('input').prop('value', formated);
153 this.element.data('date', formated);
155 this.element.prop('value', formated);
159 setValue: function(newDate) {
160 if (typeof newDate === 'string') {
161 this.date = DPGlobal.parseDate(newDate, this.format);
163 this.date = new Date(newDate);
166 this.viewDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
171 var offset = this.component ? this.component.offset() : this.element.offset();
173 top: offset.top + this.height,
178 update: function(newDate){
179 this.date = DPGlobal.parseDate(
180 typeof newDate === 'string' ? newDate : (this.isInput ? this.element.prop('value') : this.element.data('date')),
183 this.viewDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
188 var dowCnt = this.weekStart;
190 while (dowCnt < this.weekStart + 7) {
191 html += '<th class="dow">'+DPGlobal.dates.daysMin[(dowCnt++)%7]+'</th>';
194 this.picker.find('.datepicker-days thead').append(html);
197 fillMonths: function(){
201 html += '<span class="month">'+DPGlobal.dates.monthsShort[i++]+'</span>';
203 this.picker.find('.datepicker-months td').append(html);
207 var d = new Date(this.viewDate),
208 year = d.getFullYear(),
209 month = d.getMonth(),
210 currentDate = this.date.valueOf();
211 this.picker.find('.datepicker-days th:eq(1)')
212 .text(DPGlobal.dates.months[month]+' '+year);
213 var prevMonth = new Date(year, month-1, 28,0,0,0,0),
214 day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
215 prevMonth.setDate(day);
216 prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7);
217 var nextMonth = new Date(prevMonth);
218 nextMonth.setDate(nextMonth.getDate() + 42);
219 nextMonth = nextMonth.valueOf();
224 while(prevMonth.valueOf() < nextMonth) {
225 if (prevMonth.getDay() === this.weekStart) {
228 clsName = this.onRender(prevMonth);
229 prevY = prevMonth.getFullYear();
230 prevM = prevMonth.getMonth();
231 if ((prevM < month && prevY === year) || prevY < year) {
233 } else if ((prevM > month && prevY === year) || prevY > year) {
236 if (prevMonth.valueOf() === currentDate) {
237 clsName += ' active ' + this.color;
239 html.push('<td class="day '+clsName+'"><p>'+prevMonth.getDate() + '</p></td>');
240 if (prevMonth.getDay() === this.weekEnd) {
243 prevMonth.setDate(prevMonth.getDate()+1);
245 this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
246 var currentYear = this.date.getFullYear();
248 var months = this.picker.find('.datepicker-months')
252 .find('span').removeClass('active');
253 if (currentYear === year) {
254 months.eq(this.date.getMonth()).addClass('active').addClass(this.color);
258 year = parseInt(year/10, 10) * 10;
259 var yearCont = this.picker.find('.datepicker-years')
261 .text(year + '-' + (year + 9))
265 for (var i = -1; i < 11; i++) {
266 html += '<span class="year'+(i === -1 || i === 10 ? ' old' : '')+(currentYear === year ? ' active ' : '')+ this.color + '">'+year+'</span>';
275 var target = $(e.target).closest('span, td, th');
276 if (target.length === 1) {
277 switch(target[0].nodeName.toLowerCase()) {
279 switch(target[0].className) {
280 case 'switch-datepicker':
285 this.viewDate['set'+DPGlobal.modes[this.viewMode].navFnc].call(
287 this.viewDate['get'+DPGlobal.modes[this.viewMode].navFnc].call(this.viewDate) +
288 DPGlobal.modes[this.viewMode].navStep * (target[0].className === 'prev' ? -1 : 1)
296 if (target.is('.month')) {
297 var month = target.parent().find('span').index(target);
298 this.viewDate.setMonth(month);
300 var year = parseInt(target.text(), 10)||0;
301 this.viewDate.setFullYear(year);
303 if (this.viewMode !== 0) {
304 this.date = new Date(this.viewDate);
305 this.element.trigger({
308 viewMode: DPGlobal.modes[this.viewMode].clsName
316 if (target.is('.day') && !target.is('.disabled')){
317 var day = parseInt(target.text(), 10)||1;
318 var month = this.viewDate.getMonth();
319 if (target.is('.old')) {
321 } else if (target.is('.new')) {
324 var year = this.viewDate.getFullYear();
325 this.date = new Date(year, month, day,0,0,0,0);
326 this.viewDate = new Date(year, month, Math.min(28, day),0,0,0,0);
329 this.element.trigger({
332 viewMode: DPGlobal.modes[this.viewMode].clsName
340 mousedown: function(e){
345 showMode: function(dir) {
347 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
349 this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
353 $.fn.datepicker = function ( option, val ) {
354 return this.each(function () {
356 data = $this.data('datepicker'),
357 options = typeof option === 'object' && option;
359 $this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.datepicker.defaults,options))));
361 if (typeof option === 'string') data[option](val);
365 $.fn.datepicker.defaults = {
366 onRender: function(date) {
370 $.fn.datepicker.Constructor = Datepicker;
390 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
391 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
392 daysMin: ["S", "M", "T", "W", "T", "F", "S", "S"],
393 months: ["JAN.", "FEB.", "MAR.", "APR.", "MAY", "JUN.", "JUL.", "AUG.", "SEPT.", "OCT.", "NOV.", "DEC."],
394 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
396 isLeapYear: function (year) {
397 return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
399 getDaysInMonth: function (year, month) {
400 return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
402 parseFormat: function(format){
403 var separator = format.match(/[.\/\-\s].*?/),
404 parts = format.split(/\W+/);
405 if (!separator || !parts || parts.length === 0){
406 throw new Error("Invalid date format.");
408 return {separator: separator, parts: parts};
410 parseDate: function(date, format) {
411 var parts = date.split(format.separator),
417 date.setMilliseconds(0);
418 if (parts.length === format.parts.length) {
419 var year = date.getFullYear(), day = date.getDate(), month = date.getMonth();
420 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
421 val = parseInt(parts[i], 10)||1;
422 switch(format.parts[i]) {
431 date.setMonth(val - 1);
435 date.setFullYear(2000 + val);
439 date.setFullYear(val);
443 date = new Date(year, month, day, 0 ,0 ,0);
447 formatDate: function(date, format){
450 m: date.getMonth() + 1,
451 yy: date.getFullYear().toString().substring(2),
452 yyyy: date.getFullYear()
454 val.dd = (val.d < 10 ? '0' : '') + val.d;
455 val.mm = (val.m < 10 ? '0' : '') + val.m;
457 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
458 date.push(val[format.parts[i]]);
460 return date.join(format.separator);
462 headTemplate: '<thead>'+
464 '<th class="prev"><p>‹</p></th>'+
465 '<th colspan="5" class="switch-datepicker"></th>'+
466 '<th class="next"><p>›</p></th>'+
469 contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>'
472 DPGlobal.template = '<div class="datepicker dropdown-menu">'+
473 '<div class="datepicker-days">'+
474 '<table class=" table-condensed">'+
475 DPGlobal.headTemplate+
479 '<div class="datepicker-months">'+
480 '<table class="table-condensed">'+
481 DPGlobal.headTemplate+
482 DPGlobal.contTemplate+
485 '<div class="datepicker-years">'+
486 '<table class="table-condensed">'+
487 DPGlobal.headTemplate+
488 DPGlobal.contTemplate+