implement calendarWeeks option

it prepends a .cw cell to each row and displays the current calendar week before the first day of the week
defaults weekStart to monday
specs and basic styling included
This commit is contained in:
Christoph 2013-01-07 11:54:41 +01:00
parent 50a1396e5a
commit 0bb04761fd
4 changed files with 81 additions and 2 deletions

View File

@ -108,7 +108,8 @@
this.todayBtn = (options.todayBtn||this.element.data('date-today-btn')||false);
this.todayHighlight = (options.todayHighlight||this.element.data('date-today-highlight')||false);
this.weekStart = ((options.weekStart||this.element.data('date-weekstart')||dates[this.language].weekStart||0) % 7);
this.calendarWeeks = !!options.calendarWeeks;
this.weekStart = ((options.weekStart||this.element.data('date-weekstart')||this.calendarWeeks||dates[this.language].weekStart||0) % 7);
this.weekEnd = ((this.weekStart + 6) % 7);
this.startDate = -Infinity;
this.endDate = Infinity;
@ -339,6 +340,11 @@
fillDow: function(){
var dowCnt = this.weekStart,
html = '<tr>';
if(this.calendarWeeks){
var cell = '<th class="cw">&nbsp;</th>';
html += cell;
this.picker.find('.datepicker-days thead tr:first-child').prepend(cell);
}
while (dowCnt < this.weekStart + 7) {
html += '<th class="dow">'+dates[this.language].daysMin[(dowCnt++)%7]+'</th>';
}
@ -365,7 +371,7 @@
endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
currentDate = this.date && this.date.valueOf(),
today = new Date();
this.picker.find('.datepicker-days thead th:eq(1)')
this.picker.find('.datepicker-days thead th.switch')
.text(dates[this.language].months[month]+' '+year);
this.picker.find('tfoot th.today')
.text(dates[this.language].today)
@ -384,6 +390,13 @@
while(prevMonth.valueOf() < nextMonth) {
if (prevMonth.getUTCDay() == this.weekStart) {
html.push('<tr>');
if(this.calendarWeeks){
// from https://github.com/timrwood/moment/blob/master/moment.js#L128
var a = new Date(prevMonth.getYear(), prevMonth.getMonth(), prevMonth.getDate() - prevMonth.getDay() + 5),
b = new Date(a.getFullYear(), 0, 4),
calWeek = ~~((a - b) / 864e5 / 7 + 1.5);
html.push('<td class="cw">'+ calWeek +'</td>')
}
}
clsName = '';
if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {

View File

@ -154,6 +154,19 @@
/*.dow {
border-top: 1px solid #ddd !important;
}*/
// Basic styling for calendar-week cells
.cw {
font-size: 10px;
width: 12px;
padding: 0 2px 0 5px;
}
thead tr:first-child th.cw {
cursor: default;
}
thead tr:first-child th.cw {
background-color: transparent;
}
}
.input-append,
.input-prepend {

View File

@ -0,0 +1,52 @@
module('Calendar Weeks', {
setup: function(){
this.input = $('<input type="text">')
.appendTo('#qunit-fixture')
.val('2013-01-14')
.datepicker({
format: 'yyyy-mm-dd',
calendarWeeks: true
})
.focus(); // Activate for visibility checks
this.dp = this.input.data('datepicker')
this.picker = this.dp.picker;
},
teardown: function(){
this.picker.remove();
}
});
test('weekStart defaults to monday', function(){
equal(this.dp.weekStart, 1, 'Week start defaults to monday');
});
test('adds cw header column', function(){
var target = this.picker.find('.datepicker-days thead th:first-child');
ok(target.hasClass('cw'), 'First column heading is from cw column');
});
test('adds calendar week cells to each day row', function(){
var target = this.picker.find('.datepicker-days tbody tr');
expect(target.length);
target.each(function(i){
var t = $(this).children().first();
ok(t.hasClass('cw'), "First column is cw column");
});
});
test('displays correct calendar week', function(){
var target = this.picker.find('.datepicker-days tbody tr');
expect(target.length);
target.each(function(i){
var t = $(this).children().first();
equal(t.text(), i+1, "Displays correct calendar weeks");
});
});
test('it prepends column to switcher thead row', function(){
var target = this.picker.find('.datepicker-days thead tr:first-child');
equal(target.children().length, 4, 'first row has 4 columns');
ok(target.children().first().hasClass('cw'), 'cw column is prepended');
});

View File

@ -34,6 +34,7 @@
<script src="suites/events.js"></script>
<script src="suites/options.js"></script>
<script src="suites/inline.js"></script>
<script src="suites/calendar-weeks.js"></script>
</head>
<body>
<h1 id="qunit-header">bootstrap-datepicker</h1>