Newer
Older
require('jquery-ui/datepicker');
import provide from '../util/provide';
let nm = provide('jQueryPlugin');
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
class DayRange {
/**
* constructor for the date range
* @param {number} dayRange number of days
* @param {jQuery} jQueryRef reference to the jquery element
*/
constructor(dayRange, jQueryRef) {
this._workingDayRange = dayRange - 1;
let pickerHtml = '<label for="start-date" style="width: 78px; display: inline-block; margin:5px;">Start Date</label>' +
'<input type="text" readonly id="start-date" class="date-pick" style="width: 90px;">' +
'<br><label for="end-date" style="width: 78px; display: inline-block; margin:5px;">End Date</label>' +
'<input type="text" readonly id="end-date" class="date-pick" style="width: 90px;">';
jQueryRef.append(pickerHtml);
this._$startDate = $('#start-date');
this._$endDate = $('#end-date');
this._$startDate.datepicker();
this._$endDate.datepicker();
this._startDate = null;
this._endDate = null;
let dte1 = new Date();
dte1.setHours(0, 0, 0, 0);
let dte2 = new Date(dte1.getTime());
dte2.setDate(dte2.getDate() + dayRange);
dte2.setHours(23, 59, 59, 0);
this._maxDateRange = dte2 - dte1;
let _this = this;
//add event listeners
this._$startDate.change(function () {
_this.startDate = this.value;
});
this._$endDate.change(function () {
_this.endDate = this.value;
});
// initialize
this.endDate = (new Date()).getTime();
}
get startDate() {
return this._startDate;
}
set startDate(val) {
this._startDate = new Date(val);
this._startDate.setHours(0, 0, 0, 0);
this._$startDate.val(this._startDate.toLocaleDateString());
if (this.endDate == null || this._endDate - this._startDate > this._maxDateRange || this._endDate.getTime() - this._startDate.getTime() < 24 * 60 * 60 * 1000) {
let tmpDate = new Date(this._startDate.getTime());
tmpDate.setDate(tmpDate.getDate() + this._workingDayRange);
this.endDate = tmpDate.getTime();
}
}
get endDate() {
return this._endDate;
}
set endDate(val) {
this._endDate = new Date(val);
this._endDate.setHours(23, 59, 59, 0);
this._$endDate.val(this._endDate.toLocaleDateString());
if (this._startDate == null || this._endDate - this.startDate > this._maxDateRange || this._endDate.getTime() - this._startDate.getTime() < 24 * 60 * 60 * 1000) {
let tmpDate = new Date(this._endDate.getTime());
tmpDate.setDate(tmpDate.getDate() - this._workingDayRange);
this.startDate = tmpDate.getTime();
}
}
}
nm.DayRange = DayRange;
/**
* Adds day range control
* @param {number} dayRange the number of days
* @returns {DayRange} the day range object
*/
return new DayRange(dayRange, this);
};
export default undefined;