Skip to content

Commit 3c84820

Browse files
committed
Add 'linked' option for todayBtn
1 parent c921f83 commit 3c84820

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ The view that the datepicker should show when it is opened. Accepts values of 0
142142

143143
### todayBtn
144144

145-
Boolean. Default: false
145+
Boolean, "linked". Default: false
146146

147-
If true, display a button at the bottom of the datepicker to select the current date.
147+
If true or "linked", displays a "Today" button at the bottom of the datepicker to select the current date. If true, the "Today" button will only move the current date into view; if "linked", the current date will also be selected.
148148

149149
### keyboardNavigation
150150

js/bootstrap-datepicker.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
.text(dates[this.language].months[month]+' '+year);
241241
this.picker.find('tfoot th.today')
242242
.text(dates[this.language].today)
243-
.toggle(this.todayBtn);
243+
.toggle(!!this.todayBtn);
244244
this.updateNavArrows();
245245
this.fillMonths();
246246
var prevMonth = UTCDate(year, month-1, 28,0,0,0,0),
@@ -376,7 +376,8 @@
376376
date.setUTCMilliseconds(0);
377377

378378
this.showMode(-2);
379-
this._setDate(date);
379+
var which = this.todayBtn == 'linked' ? null : 'view';
380+
this._setDate(date, which);
380381
break;
381382
}
382383
break;
@@ -429,9 +430,11 @@
429430
}
430431
},
431432

432-
_setDate: function( date ){
433-
this.date = date;
434-
this.viewDate = date;
433+
_setDate: function(date, which){
434+
if (!which || which == 'date')
435+
this.date = date;
436+
if (!which || which == 'view')
437+
this.viewDate = date;
435438
this.fill();
436439
this.setValue();
437440
this.element.trigger({

tests/suites/options.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ test('Today Button: data-api', function(){
163163
ok(picker.find('.datepicker-days tfoot .today').is(':visible'), 'Today button visible');
164164
});
165165

166-
test('Today Button: selects today\'s date', function(){
166+
test('Today Button: moves to today\'s date', function(){
167167
var input = $('<input />')
168168
.appendTo('#qunit-fixture')
169169
.val('2012-03-05')
@@ -183,7 +183,33 @@ test('Today Button: selects today\'s date', function(){
183183
target.click();
184184

185185
var d = new Date(),
186-
today = UTCDate(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate())
186+
today = UTCDate(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
187+
datesEqual(dp.viewDate, today);
188+
datesEqual(dp.date, UTCDate(2012, 2, 5));
189+
});
190+
191+
test('Today Button: "linked" selects today\'s date', function(){
192+
var input = $('<input />')
193+
.appendTo('#qunit-fixture')
194+
.val('2012-03-05')
195+
.datepicker({
196+
format: 'yyyy-mm-dd',
197+
todayBtn: "linked"
198+
}),
199+
dp = input.data('datepicker'),
200+
picker = dp.picker,
201+
target;
202+
203+
input.focus();
204+
ok(picker.find('.datepicker-days').is(':visible'), 'Days view visible');
205+
ok(picker.find('.datepicker-days tfoot .today').is(':visible'), 'Today button visible');
206+
207+
target = picker.find('.datepicker-days tfoot .today');
208+
target.click();
209+
210+
var d = new Date(),
211+
today = UTCDate(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
187212
datesEqual(dp.viewDate, today);
188213
datesEqual(dp.date, today);
189214
});
215+

0 commit comments

Comments
 (0)