forked from vaadin/vaadin-date-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaadin-date-picker-helper.html
94 lines (83 loc) · 3.06 KB
/
vaadin-date-picker-helper.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<script>
window.vaadin = window.vaadin || {};
vaadin.elements = vaadin.elements || {};
vaadin.elements.datepicker = vaadin.elements.datepicker || {};
vaadin.elements.datepicker.DatePickerHelper = {
/**
* Get ISO 8601 week number for the given date.
*
* @param {Date} Date object
* @return {Number} Week number
*/
_getISOWeekNumber: function(date) {
// Ported from Vaadin Framework method com.vaadin.client.DateTimeService.getISOWeekNumber(date)
var dayOfWeek = date.getDay(); // 0 == sunday
// ISO 8601 use weeks that start on monday so we use
// mon=1,tue=2,...sun=7;
if (dayOfWeek === 0) {
dayOfWeek = 7;
}
// Find nearest thursday (defines the week in ISO 8601). The week number
// for the nearest thursday is the same as for the target date.
var nearestThursdayDiff = 4 - dayOfWeek; // 4 is thursday
var nearestThursday = new Date(date.getTime() + nearestThursdayDiff * 24 * 3600 * 1000);
var firstOfJanuary = new Date(0, 0);
firstOfJanuary.setFullYear(nearestThursday.getFullYear());
var timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime();
// Rounding the result, as the division doesn't result in an integer
// when the given date is inside daylight saving time period.
var daysSinceFirstOfJanuary = Math.round(timeDiff / (24 * 3600 * 1000));
return Math.floor((daysSinceFirstOfJanuary) / 7 + 1);
},
/**
* Check if two dates are equal.
*
* @param {Date} date1
* @param {Date} date2
* @return {Boolean} True if the given date objects refer to the same date
*/
_dateEquals: function(date1, date2) {
if (date1 && date2) {
return date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate();
} else {
return false;
}
},
/**
* Check if the given date is in the range of allowed dates.
*
* @param {Date} date The date to check
* @param {Date} min Range start
* @param {Date} max Range end
* @return {Boolean} True if the date is in the range
*/
_dateAllowed: function(date, min, max) {
return (!min || date >= min) && (!max || date <= max);
},
/**
* Get closest date from array of dates.
*
* @param {Date} date The date to compare dates with
* @param {Array} dates Array of date objects
* @return {Date} Closest date
*/
_getClosestDate: function(date, dates) {
return dates.filter(function(date) {
return date !== undefined;
})
.reduce(function(closestDate, candidate) {
if (!candidate) {
return closestDate;
}
if (!closestDate) {
return candidate;
}
var candidateDiff = Math.abs(date.getTime() - candidate.getTime());
var closestDateDiff = Math.abs(closestDate.getTime() - date.getTime());
return candidateDiff < closestDateDiff ? candidate : closestDate;
});
}
};
</script>