Skip to content

Commit

Permalink
Merging latest master into pickYear/pickMonth branch to develop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eternicode committed Jul 9, 2012
2 parents 943e3aa + 80d988f commit 87e98c6
Show file tree
Hide file tree
Showing 37 changed files with 3,923 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrumented/
tests/coverage.html
2 changes: 2 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrumented/
tests/coverage.html
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Home

http://www.eyecon.ro/bootstrap-datepicker/
This is a fork of Stefan Petre's [original code](http://www.eyecon.ro/bootstrap-datepicker/);
thanks go to him for getting this thing started!

Please note that this fork is not used on Stefan's page at this time, nor is it maintained or
contributed to by him (yet?)

# Example

Expand Down Expand Up @@ -104,7 +108,7 @@ All options that take a "Date" can handle a `Date` object; a String formatted ac

String. Default: 'mm/dd/yyyy'

The date format, combination of d, dd, m, mm, M, MM, yy, yyy.
The date format, combination of d, dd, m, mm, M, MM, yy, yyyy.

### weekStart

Expand Down
211 changes: 141 additions & 70 deletions js/bootstrap-datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
});
} else {
if (this.component){
// For components that are not readonly, allow keyboard nav
this.element.find('input').on({
focus: $.proxy(this.show, this),
blur: $.proxy(this._hide, this),
keyup: $.proxy(this.update, this),
keydown: $.proxy(this.keydown, this)
});

this.component.on('click', $.proxy(this.show, this));
var element = this.element.find('input');
element.on({
Expand All @@ -64,7 +72,7 @@
this.autoclose = this.element.data('date-autoclose');
}

switch(options.startView){
switch(options.startView || this.element.data('date-start-view')){
case 2:
case 'decade':
this.viewMode = this.startViewMode = 2;
Expand Down Expand Up @@ -155,14 +163,14 @@
},

setValue: function() {
var formated = DPGlobal.formatDate(this.date, this.format, this.language);
var formatted = DPGlobal.formatDate(this.date, this.format, this.language);
if (!this.isInput) {
if (this.component){
this.element.find('input').prop('value', formated);
this.element.find('input').prop('value', formatted);
}
this.element.data('date', formated);
this.element.data('date', formatted);
} else {
this.element.prop('value', formated);
this.element.prop('value', formatted);
}
},

Expand All @@ -185,16 +193,20 @@
},

place: function(){
var zIndex = parseInt(this.element.parents().filter(function() {
return $(this).css('z-index') != 'auto';
}).first().css('z-index'))+10;
var offset = this.component ? this.component.offset() : this.element.offset();
this.picker.css({
top: offset.top + this.height,
left: offset.left
left: offset.left,
zIndex: zIndex
});
},

update: function(){
this.date = DPGlobal.parseDate(
this.isInput ? this.element.prop('value') : this.element.data('date'),
this.isInput ? this.element.prop('value') : this.element.data('date') || this.element.find('input').prop('value'),
this.format, this.language
);
if (this.date < this.startDate) {
Expand Down Expand Up @@ -240,13 +252,14 @@
this.updateNavArrows();
this.fillMonths();
var prevMonth = new Date(year, month-1, 28,0,0,0,0),
day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth()),
prevDate, dstDay = 0, date;
prevMonth.setDate(day);
prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7);
var nextMonth = new Date(prevMonth);
nextMonth.setDate(nextMonth.getDate() + 42);
nextMonth = nextMonth.valueOf();
html = [];
var html = [];
var clsName;
while(prevMonth.valueOf() < nextMonth) {
if (prevMonth.getDay() == this.weekStart) {
Expand All @@ -264,11 +277,42 @@
if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate) {
clsName += ' disabled';
}
html.push('<td class="day'+clsName+'">'+prevMonth.getDate() + '</td>');
date = prevMonth.getDate();
if (dstDay == -1) date++;
html.push('<td class="day'+clsName+'">'+date+ '</td>');
if (prevMonth.getDay() == this.weekEnd) {
html.push('</tr>');
}
prevDate = prevMonth.getDate();
prevMonth.setDate(prevMonth.getDate()+1);
if (prevMonth.getHours() != 0) {
// Fix for DST bug: if we are no longer at start of day, a DST jump probably happened
// We either fell back (eg, Jan 1 00:00 -> Jan 1 23:00)
// or jumped forward (eg, Jan 1 00:00 -> Jan 2 01:00)
// Unfortunately, I can think of no way to test this in the unit tests, as it depends
// on the TZ of the client system.
if (!dstDay) {
// We are not currently handling a dst day (next round will deal with it)
if (prevMonth.getDate() == prevDate)
// We must compensate for fall-back
dstDay = -1;
else
// We must compensate for a jump-ahead
dstDay = +1;
}
else {
// The last round was our dst day (hours are still non-zero)
if (dstDay == -1)
// For a fall-back, fast-forward to next midnight
prevMonth.setHours(24);
else
// For a jump-ahead, just reset to 0
prevMonth.setHours(0);
// Reset minutes, as some TZs may be off by portions of an hour
prevMonth.setMinutes(0);
dstDay = 0;
}
}
}
this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
var currentYear = this.date.getFullYear();
Expand Down Expand Up @@ -368,6 +412,7 @@
break;
case 'span':
if (!target.is('.disabled')) {
this.viewDate.setDate(1);
if (target.is('.month')) {
var month = target.parent().find('span').index(target);
this.viewDate.setDate(1);
Expand Down Expand Up @@ -485,14 +530,19 @@
return this.moveMonth(date, dir*12);
},

dateWithinRange: function(date){
return date >= this.startDate && date <= this.endDate;
},

keydown: function(e){
if (this.picker.is(':not(:visible)')){
if (e.keyCode == 27) // allow escape to hide and re-show picker
this.show();
return;
}
var dateChanged = false,
dir, day, month;
dir, day, month,
newDate, newViewDate;
switch(e.keyCode){
case 27: // escape
this.hide();
Expand All @@ -502,37 +552,49 @@
case 39: // right
dir = e.keyCode == 37 ? -1 : 1;
if (e.ctrlKey){
this.date = this.moveYear(this.date, dir);
this.viewDate = this.moveYear(this.viewDate, dir);
newDate = this.moveYear(this.date, dir);
newViewDate = this.moveYear(this.viewDate, dir);
} else if (e.shiftKey){
this.date = this.moveMonth(this.date, dir);
this.viewDate = this.moveMonth(this.viewDate, dir);
newDate = this.moveMonth(this.date, dir);
newViewDate = this.moveMonth(this.viewDate, dir);
} else {
this.date.setDate(this.date.getDate() + dir);
this.viewDate.setDate(this.viewDate.getDate() + dir);
newDate = new Date(this.date);
newDate.setDate(this.date.getDate() + dir);
newViewDate = new Date(this.viewDate);
newViewDate.setDate(this.viewDate.getDate() + dir);
}
if (this.dateWithinRange(newDate)){
this.date = newDate;
this.viewDate = newViewDate;
this.setValue();
this.update();
e.preventDefault();
dateChanged = true;
}
this.setValue();
this.update();
e.preventDefault();
dateChanged = true;
break;
case 38: // up
case 40: // down
dir = e.keyCode == 38 ? -1 : 1;
if (e.ctrlKey){
this.date = this.moveYear(this.date, dir);
this.viewDate = this.moveYear(this.viewDate, dir);
newDate = this.moveYear(this.date, dir);
newViewDate = this.moveYear(this.viewDate, dir);
} else if (e.shiftKey){
this.date = this.moveMonth(this.date, dir);
this.viewDate = this.moveMonth(this.viewDate, dir);
newDate = this.moveMonth(this.date, dir);
newViewDate = this.moveMonth(this.viewDate, dir);
} else {
this.date.setDate(this.date.getDate() + dir * 7);
this.viewDate.setDate(this.viewDate.getDate() + dir * 7);
newDate = new Date(this.date);
newDate.setDate(this.date.getDate() + dir * 7);
newViewDate = new Date(this.viewDate);
newViewDate.setDate(this.viewDate.getDate() + dir * 7);
}
if (this.dateWithinRange(newDate)){
this.date = newDate;
this.viewDate = newViewDate;
this.setValue();
this.update();
e.preventDefault();
dateChanged = true;
}
this.setValue();
this.update();
e.preventDefault();
dateChanged = true;
break;
case 13: // enter
this.hide();
Expand Down Expand Up @@ -654,49 +716,58 @@
}
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
}
var parts = date ? date.match(this.nonpunctuation) : [],
var parts = date && date.match(this.nonpunctuation) || [],
date = new Date(),
val, filtered;
parsed = {},
setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
setters_map = {
yyyy: function(d,v){ return d.setFullYear(v); },
yy: function(d,v){ return d.setFullYear(2000+v); },
m: function(d,v){
v -= 1;
while (v<0) v += 12;
v %= 12;
d.setMonth(v);
while (d.getMonth() != v)
d.setDate(d.getDate()-1);
return d;
},
d: function(d,v){ return d.setDate(v); }
},
val, filtered, part;
setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
setters_map['dd'] = setters_map['d'];
date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
if (parts.length == format.parts.length) {
for (var i=0, cnt = format.parts.length; i < cnt; i++) {
val = parseInt(parts[i], 10)||1;
switch(format.parts[i]) {
case 'MM':
filtered = $(dates[language].months).filter(function(){
var m = this.slice(0, parts[i].length),
p = parts[i].slice(0, m.length);
return m == p;
});
val = $.inArray(filtered[0], dates[language].months) + 1;
break;
case 'M':
filtered = $(dates[language].monthsShort).filter(function(){
var m = this.slice(0, parts[i].length),
p = parts[i].slice(0, m.length);
return m == p;
});
val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
break;
}
switch(format.parts[i]) {
case 'dd':
case 'd':
date.setDate(val);
break;
case 'mm':
case 'm':
case 'MM':
case 'M':
date.setMonth(val - 1);
break;
case 'yy':
date.setFullYear(2000 + val);
break;
case 'yyyy':
date.setFullYear(val);
break;
val = parseInt(parts[i], 10);
part = format.parts[i];
if (isNaN(val)) {
switch(part) {
case 'MM':
filtered = $(dates[language].months).filter(function(){
var m = this.slice(0, parts[i].length),
p = parts[i].slice(0, m.length);
return m == p;
});
val = $.inArray(filtered[0], dates[language].months) + 1;
break;
case 'M':
filtered = $(dates[language].monthsShort).filter(function(){
var m = this.slice(0, parts[i].length),
p = parts[i].slice(0, m.length);
return m == p;
});
val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
break;
}
}
parsed[part] = val;
}
for (var i=0, s; i<setters_order.length; i++){
s = setters_order[i];
if (s in parsed)
setters_map[s](date, parsed[s])
}
}
return date;
Expand Down Expand Up @@ -751,4 +822,4 @@
'</div>'+
'</div>';

}( window.jQuery )
}( window.jQuery );
13 changes: 13 additions & 0 deletions js/locales/bootstrap-datepicker.cs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Czech translation for bootstrap-datepicker
* Matěj Koubík <[email protected]>
*/
;(function($){
$.fn.datepicker.dates['cs'] = {
days: ["Neděle", "Pondělí", "Úterý", "Středa", "Čtvrtek", "Pátek", "Sobota", "Neděle"],
daysShort: ["Ne", "Po", "Út", "St", "Čt", "Pá", "So", "Ne"],
daysMin: ["N", "P", "Ú", "St", "Č", "P", "So", "N"],
months: ["Leden", "Únor", "Březen", "Duben", "Květen", "Červen", "Červenec", "Srpen", "Září", "Říjen", "Listopad", "Prosinec"],
monthsShort: ["Led", "Úno", "Bře", "Dub", "Kvě", "Čer", "Čnc", "Srp", "Zář", "Říj", "Lis", "Pro"]
};
}(jQuery));
13 changes: 13 additions & 0 deletions js/locales/bootstrap-datepicker.fi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Finnish translation for bootstrap-datepicker
* Jaakko Salonen <https://github.com/jsalonen>
*/
;(function($){
$.fn.datepicker.dates['fi'] = {
days: ["sunnuntai", "maanantai", "tiistai", "keskiviikko", "torstai", "perjantai", "lauantai", "sunnuntai"],
daysShort: ["sun", "maa", "tii", "kes", "tor", "per", "lau", "sun"],
daysMin: ["su", "ma", "ti", "ke", "to", "pe", "la", "su"],
months: ["tammikuu", "helmikuu", "maaliskuu", "huhtikuu", "toukokuu", "kesäkuu", "heinäkuu", "elokuu", "syyskuu", "lokakuu", "marraskuu", "joulukuu"],
monthsShort: ["tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mar", "jou"]
};
}(jQuery));
13 changes: 13 additions & 0 deletions js/locales/bootstrap-datepicker.id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Bahasa translation for bootstrap-datepicker
* Azwar Akbar <[email protected]>
*/
;(function($){
$.fn.datepicker.dates['id'] = {
days: ["Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu", "Minggu"],
daysShort: ["Mgu", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab", "Mgu"],
daysMin: ["Mg", "Sn", "Sl", "Ra", "Ka", "Ju", "Sa", "Mg"],
months: ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"],
monthsShort: ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Ags", "Sep", "Okt", "Nov", "Des"]
};
}(jQuery));
Loading

0 comments on commit 87e98c6

Please sign in to comment.