-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-weekpicker.js
67 lines (59 loc) · 2.24 KB
/
jquery-weekpicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(function($, undefined) {
$.widget('lugolabs.weekpicker', {
_weekOptions: {
showOtherMonths: true,
selectOtherMonths: true
},
_create: function() {
var self = this;
this._dateFormat = this.options.dateFormat || $.datepicker._defaults.dateFormat;
var date = this._initialDate();
this._setWeek(date);
var onSelect = this.options.onSelect;
this._picker = $(this.element).datepicker($.extend(this.options, this._weekOptions, {
onSelect: function(dateText, inst) {
self._select(dateText, inst, onSelect);
},
beforeShowDay: function(date) {
return self._showDay(date);
},
onChangeMonthYear: function(year, month, inst) {
self._selectCurrentWeek();
}
}));
$(document)
.on('mousemove', '.ui-datepicker-calendar tr', function() { $(this).find('td a').addClass('ui-state-hover'); })
.on('mouseleave', '.ui-datepicker-calendar tr', function() { $(this).find('td a').removeClass('ui-state-hover'); });
this._picker.datepicker('setDate', date);
},
_initialDate: function() {
if (this.options.currentText) {
return $.datepicker.parseDate(this._dateFormat, this.options.currentText);
} else {
return new Date;
}
},
_select: function(dateText, inst, onSelect) {
this._setWeek(this._picker.datepicker('getDate'));
var startDateText = $.datepicker.formatDate(this._dateFormat, this._startDate, inst.settings);
this._picker.val(startDateText);
if (onSelect) onSelect(dateText, startDateText, this._startDate, this._endDate, inst);
},
_showDay: function(date) {
var cssClass = date >= this._startDate && date <= this._endDate ? 'ui-datepicker-current-day' : '';
return [true, cssClass];
},
_setWeek: function(date) {
var year = date.getFullYear(),
month = date.getMonth(),
day = date.getDate() - date.getDay();
this._startDate = new Date(year, month, day);
this._endDate = new Date(year, month, day + 6);
},
_selectCurrentWeek: function() {
$('.ui-datepicker-calendar')
.find('.ui-datepicker-current-day a')
.addClass('ui-state-active');
}
});
})(jQuery);