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.startViewMode = this.viewMode;
79 this.weekStart = options.weekStart||this.element.data('date-weekstart')||0;
80 this.weekEnd = this.weekStart === 0 ? 6 : this.weekStart - 1;
81 this.onRender = options.onRender;
88 Datepicker.prototype = {
89 constructor: Datepicker,
93 this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
95 $(window).on('resize', $.proxy(this.place, this));
103 $(document).on('mousedown', function(ev){
104 if ($(ev.target).closest('.datepicker').length == 0) {
108 this.element.trigger({
116 $(window).off('resize', this.place);
117 this.viewMode = this.startViewMode;
120 $(document).off('mousedown', this.hide);
123 this.element.trigger({
130 var formated = DPGlobal.formatDate(this.date, this.format);
133 this.element.find('input').prop('value', formated);
135 this.element.data('date', formated);
137 this.element.prop('value', formated);
141 setValue: function(newDate) {
142 if (typeof newDate === 'string') {
143 this.date = DPGlobal.parseDate(newDate, this.format);
145 this.date = new Date(newDate);
148 this.viewDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
153 var offset = this.component ? this.component.offset() : this.element.offset();
155 top: offset.top + this.height,
160 update: function(newDate){
161 this.date = DPGlobal.parseDate(
162 typeof newDate === 'string' ? newDate : (this.isInput ? this.element.prop('value') : this.element.data('date')),
165 this.viewDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
170 var dowCnt = this.weekStart;
172 while (dowCnt < this.weekStart + 7) {
173 html += '<th class="dow">'+DPGlobal.dates.daysMin[(dowCnt++)%7]+'</th>';
176 this.picker.find('.datepicker-days thead').append(html);
179 fillMonths: function(){
183 html += '<span class="month">'+DPGlobal.dates.monthsShort[i++]+'</span>';
185 this.picker.find('.datepicker-months td').append(html);
189 var d = new Date(this.viewDate),
190 year = d.getFullYear(),
191 month = d.getMonth(),
192 currentDate = this.date.valueOf();
193 this.picker.find('.datepicker-days th:eq(1)')
194 .text(DPGlobal.dates.months[month]+' '+year);
195 var prevMonth = new Date(year, month-1, 28,0,0,0,0),
196 day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
197 prevMonth.setDate(day);
198 prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7);
199 var nextMonth = new Date(prevMonth);
200 nextMonth.setDate(nextMonth.getDate() + 42);
201 nextMonth = nextMonth.valueOf();
206 while(prevMonth.valueOf() < nextMonth) {
207 if (prevMonth.getDay() === this.weekStart) {
210 clsName = this.onRender(prevMonth);
211 prevY = prevMonth.getFullYear();
212 prevM = prevMonth.getMonth();
213 if ((prevM < month && prevY === year) || prevY < year) {
215 } else if ((prevM > month && prevY === year) || prevY > year) {
218 if (prevMonth.valueOf() === currentDate) {
219 clsName += ' active';
221 html.push('<td class="day '+clsName+'">'+prevMonth.getDate() + '</td>');
222 if (prevMonth.getDay() === this.weekEnd) {
225 prevMonth.setDate(prevMonth.getDate()+1);
227 this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
228 var currentYear = this.date.getFullYear();
230 var months = this.picker.find('.datepicker-months')
234 .find('span').removeClass('active');
235 if (currentYear === year) {
236 months.eq(this.date.getMonth()).addClass('active');
240 year = parseInt(year/10, 10) * 10;
241 var yearCont = this.picker.find('.datepicker-years')
243 .text(year + '-' + (year + 9))
247 for (var i = -1; i < 11; i++) {
248 html += '<span class="year'+(i === -1 || i === 10 ? ' old' : '')+(currentYear === year ? ' active' : '')+'">'+year+'</span>';
257 var target = $(e.target).closest('span, td, th');
258 if (target.length === 1) {
259 switch(target[0].nodeName.toLowerCase()) {
261 switch(target[0].className) {
267 this.viewDate['set'+DPGlobal.modes[this.viewMode].navFnc].call(
269 this.viewDate['get'+DPGlobal.modes[this.viewMode].navFnc].call(this.viewDate) +
270 DPGlobal.modes[this.viewMode].navStep * (target[0].className === 'prev' ? -1 : 1)
278 if (target.is('.month')) {
279 var month = target.parent().find('span').index(target);
280 this.viewDate.setMonth(month);
282 var year = parseInt(target.text(), 10)||0;
283 this.viewDate.setFullYear(year);
285 if (this.viewMode !== 0) {
286 this.date = new Date(this.viewDate);
287 this.element.trigger({
290 viewMode: DPGlobal.modes[this.viewMode].clsName
298 if (target.is('.day') && !target.is('.disabled')){
299 var day = parseInt(target.text(), 10)||1;
300 var month = this.viewDate.getMonth();
301 if (target.is('.old')) {
303 } else if (target.is('.new')) {
306 var year = this.viewDate.getFullYear();
307 this.date = new Date(year, month, day,0,0,0,0);
308 this.viewDate = new Date(year, month, Math.min(28, day),0,0,0,0);
311 this.element.trigger({
314 viewMode: DPGlobal.modes[this.viewMode].clsName
322 mousedown: function(e){
327 showMode: function(dir) {
329 this.viewMode = Math.max(this.minViewMode, Math.min(2, this.viewMode + dir));
331 this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
335 $.fn.datepicker = function ( option, val ) {
336 return this.each(function () {
338 data = $this.data('datepicker'),
339 options = typeof option === 'object' && option;
341 $this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.datepicker.defaults,options))));
343 if (typeof option === 'string') data[option](val);
347 $.fn.datepicker.defaults = {
348 onRender: function(date) {
352 $.fn.datepicker.Constructor = Datepicker;
372 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
373 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
374 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
375 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
376 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
378 isLeapYear: function (year) {
379 return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
381 getDaysInMonth: function (year, month) {
382 return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
384 parseFormat: function(format){
385 var separator = format.match(/[.\/\-\s].*?/),
386 parts = format.split(/\W+/);
387 if (!separator || !parts || parts.length === 0){
388 throw new Error("Invalid date format.");
390 return {separator: separator, parts: parts};
392 parseDate: function(date, format) {
393 var parts = date.split(format.separator),
399 date.setMilliseconds(0);
400 if (parts.length === format.parts.length) {
401 var year = date.getFullYear(), day = date.getDate(), month = date.getMonth();
402 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
403 val = parseInt(parts[i], 10)||1;
404 switch(format.parts[i]) {
413 date.setMonth(val - 1);
417 date.setFullYear(2000 + val);
421 date.setFullYear(val);
425 date = new Date(year, month, day, 0 ,0 ,0);
429 formatDate: function(date, format){
432 m: date.getMonth() + 1,
433 yy: date.getFullYear().toString().substring(2),
434 yyyy: date.getFullYear()
436 val.dd = (val.d < 10 ? '0' : '') + val.d;
437 val.mm = (val.m < 10 ? '0' : '') + val.m;
439 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
440 date.push(val[format.parts[i]]);
442 return date.join(format.separator);
444 headTemplate: '<thead>'+
446 '<th class="prev">‹</th>'+
447 '<th colspan="5" class="switch"></th>'+
448 '<th class="next">›</th>'+
451 contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>'
453 DPGlobal.template = '<div class="datepicker dropdown-menu">'+
454 '<div class="datepicker-days">'+
455 '<table class=" table-condensed">'+
456 DPGlobal.headTemplate+
460 '<div class="datepicker-months">'+
461 '<table class="table-condensed">'+
462 DPGlobal.headTemplate+
463 DPGlobal.contTemplate+
466 '<div class="datepicker-years">'+
467 '<table class="table-condensed">'+
468 DPGlobal.headTemplate+
469 DPGlobal.contTemplate+