-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathang.datepicker.component.js
290 lines (290 loc) · 11.4 KB
/
ang.datepicker.component.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { animate, Component, ElementRef, EventEmitter, Input, keyframes, Output, Renderer, style, transition, trigger } from '@angular/core';
import { Calendar } from './ang.calendar';
import * as moment from 'moment';
var DatePickerComponent = /** @class */ (function () {
function DatePickerComponent(renderer, elementRef) {
var _this = this;
this.renderer = renderer;
this.elementRef = elementRef;
this.DEFAULT_FORMAT = 'YYYY-MM-DD';
this.dateChange = new EventEmitter();
this.weekStart = 0;
this.dayNames = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
this.onSelect = new EventEmitter();
this.dateFormat = this.DEFAULT_FORMAT;
this.colors = {
'black': '#333333',
'blue': '#1285bf',
'lightBlue': '#daedf7',
'lightGrey': '#f1f1f1',
'white': '#ffffff'
};
this.accentColor = this.colors['blue'];
this.altInputStyle = false;
this.updateDayNames();
this.months = [
'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', ' December'
];
this.clickListener = renderer.listenGlobal('document', 'click', function (event) { return _this.handleGlobalClick(event); });
}
Object.defineProperty(DatePickerComponent.prototype, "date", {
get: function () { return this.dateVal; },
set: function (val) {
this.dateVal = val;
this.dateChange.emit(val);
this.setCurrentYear(val.getFullYear());
},
enumerable: true,
configurable: true
});
;
DatePickerComponent.prototype.ngOnInit = function () {
this.updateDayNames();
this.syncVisualsWithDate();
};
DatePickerComponent.prototype.ngOnChanges = function (changes) {
if ((changes['date'] || changes['dateFormat'])) {
this.syncVisualsWithDate();
}
if (changes['firstDayOfTheWeek'] || changes['dayNames']) {
this.updateDayNames();
}
};
DatePickerComponent.prototype.ngOnDestroy = function () {
this.clickListener();
};
DatePickerComponent.prototype.closeCalendar = function () {
this.syncVisualsWithDate();
};
DatePickerComponent.prototype.setCurrentValues = function (date) {
this.currentMonthNumber = date.getMonth();
this.currentMonth = this.months[this.currentMonthNumber];
this.currentYear = date.getFullYear();
var calendarArray = this.calendar.monthDays(this.currentYear, this.currentMonthNumber);
this.calendarDays = [].concat.apply([], calendarArray);
this.calendarDays = this.filterInvalidDays(this.calendarDays);
};
DatePickerComponent.prototype.updateDayNames = function () {
this.dayNamesOrdered = this.dayNames.slice(); // Copy DayNames with default value (weekStart = 0)
if (this.weekStart < 0 || this.weekStart >= this.dayNamesOrdered.length) {
// Out of range
throw Error("The weekStart is not in range between " + 0 + " and " + (this.dayNamesOrdered.length - 1));
}
else {
this.calendar = new Calendar(this.weekStart);
this.dayNamesOrdered = this.dayNamesOrdered.slice(this.weekStart, this.dayNamesOrdered.length)
.concat(this.dayNamesOrdered.slice(0, this.weekStart)); // Append beginning to end
}
};
DatePickerComponent.prototype.syncVisualsWithDate = function () {
if (this.date) {
this.setInputText(this.date);
this.setCurrentValues(this.date);
}
else {
this.inputText = '';
this.setCurrentValues(new Date());
}
};
DatePickerComponent.prototype.setCurrentMonth = function (monthNumber) {
this.currentMonth = this.months[monthNumber];
var calendarArray = this.calendar.monthDays(this.currentYear, this.currentMonthNumber);
this.calendarDays = [].concat.apply([], calendarArray);
this.calendarDays = this.filterInvalidDays(this.calendarDays);
};
DatePickerComponent.prototype.setCurrentYear = function (year) {
this.currentYear = year;
};
DatePickerComponent.prototype.setInputText = function (date) {
var inputText = "";
var dateFormat = this.dateFormat;
if (dateFormat === undefined || dateFormat === null) {
inputText = moment(date).format(this.DEFAULT_FORMAT);
}
else if (typeof dateFormat === 'string') {
inputText = moment(date).format(dateFormat);
}
else if (typeof dateFormat === 'function') {
inputText = dateFormat(date);
}
this.inputText = inputText;
};
DatePickerComponent.prototype.onArrowClick = function (direction) {
var currentMonth = this.currentMonthNumber;
var newYear = this.currentYear;
var newMonth;
// sets the newMonth
// changes newYear is necessary
if (direction === 'left') {
if (currentMonth === 0) {
newYear = this.currentYear - 1;
newMonth = 11;
}
else {
newMonth = currentMonth - 1;
}
}
else if (direction === 'right') {
if (currentMonth === 11) {
newYear = this.currentYear + 1;
newMonth = 0;
}
else {
newMonth = currentMonth + 1;
}
}
// check if new date would be within range
var newDate = new Date(newYear, newMonth);
var newDateValid;
if (direction === 'left') {
newDateValid = !this.rangeStart || newDate.getTime() >= this.rangeStart.getTime();
}
else if (direction === 'right') {
newDateValid = !this.rangeEnd || newDate.getTime() <= this.rangeEnd.getTime();
}
if (newDateValid) {
this.setCurrentYear(newYear);
this.currentMonthNumber = newMonth;
this.setCurrentMonth(newMonth);
this.triggerAnimation(direction);
}
};
DatePickerComponent.prototype.filterInvalidDays = function (calendarDays) {
var newCalendarDays = [];
calendarDays.forEach(function (day) {
if (day === 0) {
newCalendarDays.push(0);
}
else {
newCalendarDays.push(day);
}
});
return newCalendarDays;
};
DatePickerComponent.prototype.onSelectDay = function (day) {
this.date = day;
this.syncVisualsWithDate();
this.onSelect.emit({ date: day, dateText: this.inputText });
};
DatePickerComponent.prototype.handleGlobalClick = function (event) {
var withinElement = this.elementRef.nativeElement.contains(event.target);
if (!this.elementRef.nativeElement.contains(event.target)) {
this.closeCalendar();
}
};
DatePickerComponent.prototype.getDayBackgroundColor = function (day) {
var color = this.colors['white'];
if (this.isChosenDay(day)) {
color = this.accentColor;
}
else if (this.isCurrentDay(day)) {
color = this.colors['lightGrey'];
}
else if (this.isBetweenSelectedDateRange(day)) {
color = this.colors['lightBlue'];
}
return color;
};
DatePickerComponent.prototype.getDayFontColor = function (day) {
var color = this.colors['black'];
if (this.isChosenDay(day)) {
color = this.colors['white'];
}
return color;
};
DatePickerComponent.prototype.isChosenDay = function (day) {
if (day) {
return this.date ? day.toDateString() === this.date.toDateString() : false;
}
else {
return false;
}
};
DatePickerComponent.prototype.isCurrentDay = function (day) {
if (day) {
return day.toDateString() === new Date().toDateString();
}
else {
return false;
}
};
DatePickerComponent.prototype.isBetweenSelectedDateRange = function (day) {
if (!day) {
return false;
}
if (this.startDate instanceof Date && this.startDate != undefined && (this.startDate.getTime() > day.getTime())) {
return false;
}
if (this.endDate instanceof Date && this.endDate != undefined && (this.endDate.getTime() < day.getTime())) {
return false;
}
if ((this.startDate instanceof Date) && (this.endDate instanceof Date) &&
(this.startDate == undefined && this.endDate == undefined)) {
return false;
}
return true;
};
DatePickerComponent.prototype.isHoveredDay = function (day) {
return this.hoveredDay ? this.hoveredDay === day && !this.isChosenDay(day) : false;
};
DatePickerComponent.prototype.triggerAnimation = function (direction) {
var _this = this;
this.animate = direction;
setTimeout(function () { return _this.animate = 'reset'; }, 185);
};
DatePickerComponent.decorators = [
{ type: Component, args: [{
selector: 'ang-datepicker',
animations: [
trigger('calendarAnimation', [
transition('* => left', [
animate(180, keyframes([
style({ transform: 'translateX(105%)', offset: 0.5 }),
style({ transform: 'translateX(-130%)', offset: 0.51 }),
style({ transform: 'translateX(0)', offset: 1 })
]))
]),
transition('* => right', [
animate(180, keyframes([
style({ transform: 'translateX(-105%)', offset: 0.5 }),
style({ transform: 'translateX(130%)', offset: 0.51 }),
style({ transform: 'translateX(0)', offset: 1 })
]))
])
])
],
templateUrl: './ang.datepicker.component.html',
styleUrls: ['./ang.datepicker.component.css']
},] },
];
/** @nocollapse */
DatePickerComponent.ctorParameters = function () { return [
{ type: Renderer, },
{ type: ElementRef, },
]; };
DatePickerComponent.propDecorators = {
"dateChange": [{ type: Output },],
"date": [{ type: Input },],
"startDate": [{ type: Input },],
"endDate": [{ type: Input },],
"disabled": [{ type: Input },],
"accentColor": [{ type: Input },],
"altInputStyle": [{ type: Input },],
"dateFormat": [{ type: Input },],
"fontFamily": [{ type: Input },],
"rangeStart": [{ type: Input },],
"rangeEnd": [{ type: Input },],
"inputText": [{ type: Input },],
"weekStart": [{ type: Input },],
"calendarDays": [{ type: Input },],
"currentMonth": [{ type: Input },],
"dayNames": [{ type: Input },],
"hoveredDay": [{ type: Input },],
"months": [{ type: Input },],
"onSelect": [{ type: Output },],
};
return DatePickerComponent;
}());
export { DatePickerComponent };
//# sourceMappingURL=ang.datepicker.component.js.map