frDatepicker.js
15.8 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
*
* How it works from the inside?
*
* This calendar is used to display certain dates either by specifying range (min and max)
* or by specifying exact dates that are available.
*
* To simplify calculations you need to provide those dates without the needed timezone offset,
* instead pretend like they are in current timezone. For example, if you need to specify a minimum
* date of "2015-01-08" for a +12 timezone calendar, when providing this date don't provide a
* "2015-01-08T00:00:00+12:00" but instead provide a "2015-01-08 00:00:00" as if the timezone you
* are using is the local one. This simplifies internal calculations while still gives correct
* results.
*
* However, for "Go To Today" functionality to work correctly, you will need to provide a timezone
* you wish your calendar to work with. This timezone is provided via `options.tz`.
*
* Note: at the moment only UTC timezone is supported and is the one used by default.
*
*/
import frDatepicker from '../../../../public/views/ng/common/frDatepicker.html';
angular.module('fr.datepicker', []).directive('frDatepicker', ['$filter', function ($filter) {
return {
restrict: 'A',
scope: true,
template: frDatepicker,
link: function (scope, element, attrs) {
/**
* Initialization
*/
var options = scope.$eval(attrs.options);
// Expose scope.
if (options.ctrl) {
options.ctrl.scope = scope;
}
scope.weekly = options.weekly;
var onChangeHandler = options.onChange || function () {};
var minDate;
var maxDate;
// The day that acts as the day that is chosen when clicking "Go to Today",
// "Current Week", etc. In other words it's the day that is most relevant in respect
// to today.
var currentDay = options.currentDay;
var years = {};
var months = {};
var days = {};
// Set today as perceived by calendar's timezone.
var today = new Date();
today.setHours(0, 0, 0, 0);
// Setup calendar accordingly to whether it's configured by exact dates or range.
if (options.dates) {
setupCalendarByDates();
} else {
setupCalendarByRange();
}
// Choose Monday as current day for weekly calendar.
if (scope.weekly) {
setToMonday(currentDay);
}
// This is the actual date that is selected.
scope.date = options.date || currentDay;
// This is the currently viewed month. Date may or may not be selected within this month.
var viewedMonth = new Date(scope.date);
// Establish how "Go To Today" will look.
if (scope.weekly) {
scope.goToText = 'Current Week';
} else if (options.byDates) {
scope.goToText = 'Current Game Day';
} else {
scope.goToText = 'Go To Today';
}
calculateDates();
/**
* Private Methods
*/
function setupCalendarByDates() {
options.byDates = true;
// Process each provided date.
_(options.dates).each(function (date) {
// Build dictionary of all years, months and dates.
years[(new Date(date.getFullYear(), 0, 1)).getTime()] = true;
months[(new Date(date.getFullYear(), date.getMonth(), 1)).getTime()] = true;
days[date.getTime()] = true;
// Calculate minimum date from provided dates.
if (minDate === undefined || minDate.getTime() > date.getTime()) {
minDate = date;
}
// Calculate maximum date from provided dates.
if (maxDate === undefined || maxDate.getTime() < date.getTime()) {
maxDate = date;
}
// If no current date specified then we need to establish one by ourselves.
if (!options.currentDay) {
if (!currentDay) {
currentDay = date;
} else if (date.getTime() < currentDay.getTime() && today.getTime() <= date.getTime()) {
currentDay = date;
} else if (date.getTime() > currentDay.getTime() && today.getTime() >= currentDay.getTime()) {
currentDay = date;
}
}
});
}
function setupCalendarByRange() {
options.byRange = true;
minDate = options.minDate;
maxDate = options.maxDate;
// If no current date specified then we need to establish one by ourselves.
if (!options.currentDay) {
if (today.getTime() < minDate.getTime()) {
currentDay = new Date(minDate);
} else if (today.getTime() > maxDate.getTime()) {
currentDay = new Date(maxDate);
} else {
currentDay = today;
}
}
}
function calculateYears() {
var year;
var yearDate;
scope.selectedYear = viewedMonth.getFullYear();
// We don't want to overwrite years because it will mess up
// change detection, plus there is no need to.
if (scope.years) {
return;
}
scope.years = [];
for (year = minDate.getFullYear(); year <= maxDate.getFullYear(); year++) {
yearDate = new Date(year, 0, 1);
if (options.byDates && !years[yearDate.getTime()]) {
continue;
}
scope.years.push(year);
}
}
function calculateMonths() {
scope.months = [];
var date;
for (var i = 0; i <= 11; i++) {
date = new Date(viewedMonth.getFullYear(), i, 1);
scope.months.push({
date: date,
enabled: (options.byDates && months[date.getTime()]) || isDateInRange(date),
name: $filter('date')(date, 'MMM'),
selected: viewedMonth.getMonth() === i,
type: 'month',
});
}
}
function calculateWeeks() {
scope.weeks = [];
var week;
var day;
// Calculate date that calendar's 7 by 6 grid will start from.
var startingWeekDay = new Date(viewedMonth.getFullYear(), viewedMonth.getMonth(), 1).getDay();
var daysFromPreviousMonth = ((startingWeekDay === 1) ? 7 : (startingWeekDay === 0) ? 6 : startingWeekDay - 1);
// Iterate over a 7 by 6 calendar grid.
for (var i = 1; i <= 6; i++) {
week = {
type: 'week',
enabled: false,
selected: false,
days: [],
};
for (var j = 1; j <= 7; j++) {
// Set date.
let date = new Date(viewedMonth);
date.setDate(0 - daysFromPreviousMonth + ((i - 1) * 7 + j));
// Get calculated day.
day = calculateDay(date);
// Enable week if at least one day is enabled.
week.enabled = week.enabled || day.enabled;
// Select week if at least one day is selected.
week.selected = week.selected || day.selected;
if (day.date.getDay() === 1) {
week.date = day.date;
}
week.days.push(day);
}
scope.weeks.push(week);
}
}
function calculateDay(date) {
return {
date: date,
enabled: (options.byDates && days[date.getTime()]) || isDateInRange(date),
name: date.getDate(),
otherMonth: viewedMonth.getMonth() !== date.getMonth(),
selected: scope.date.getTime() === date.getTime(),
type: 'day',
};
}
function calculateDates() {
console.time('DatePicker: calculate dates.');
if (scope.calendarOpened) {
calculateYears();
calculateMonths();
calculateWeeks();
}
setSelectedText();
console.timeEnd('DatePicker: calculate dates.');
}
function setYearDate(year) {
var lastAvailableDate;
var date;
for (var i = 0; i < 12; i++) {
date = new Date(year, i, 1);
if (!(options.byDates && months[date.getTime()]) && !isDateInRange(date)) {
continue;
}
if (lastAvailableDate && viewedMonth.getMonth() < lastAvailableDate.getMonth()) {
continue;
}
lastAvailableDate = date;
if (viewedMonth.getMonth() === date.getMonth()) {
break;
}
}
viewedMonth = lastAvailableDate;
}
function selectDate(date) {
scope.date = date;
viewedMonth = new Date(date);
viewedMonth.setDate(1);
calculateDates();
scope.notifyDateChange();
}
function setSelectedText() {
var startDate = scope.date;
var endDate;
var startFormat = 'd';
var endFormat = 'd MMM, yyyy';
if (scope.weekly) {
endDate = new Date(scope.date);
endDate.setDate(endDate.getDate() + 6);
if (startDate.getMonth() !== endDate.getMonth()) {
startFormat = 'd MMM';
}
scope.selected = $filter('date')(startDate, startFormat) + ' - ' + $filter('date')(endDate, endFormat);
} else {
scope.selected = $filter('date')(startDate, endFormat);
}
}
function setToMonday(date) {
var day = date.getDay();
date.setDate(date.getDate() - day + (day == 0 ? -6 : 1));
}
function isDateInRange(date) {
return options.byRange && date.getTime() >= minDate.getTime() && date.getTime() <= maxDate.getTime();
}
scope.notifyDateChange = function () {
var endDate;
if (scope.weekly) {
endDate = new Date(scope.date);
endDate.setDate(endDate.getDate() + 6);
}
onChangeHandler(scope.date, endDate);
};
/**
* Interactions.
*/
scope.clickDate = function (clicked) {
// Don't process invalid clicks.
if (clicked.type === 'week' && !scope.weekly || clicked.type === 'day' && scope.weekly || !clicked.enabled) {
return;
}
// Set viewed month.
viewedMonth = new Date(clicked.date.getFullYear(), clicked.date.getMonth(), 1);
// Set viewed date.
if (clicked.type === 'day' || clicked.type === 'week') {
scope.date = new Date(clicked.date);
scope.notifyDateChange();
}
if (scope.weekly) {
setToMonday(scope.date);
}
calculateDates();
};
scope.clickYear = function (year) {
setYearDate(year);
if (scope.weekly) {
setToMonday(scope.date);
}
calculateDates();
};
scope.clickPrev = function () {
var prevDate = new Date(scope.date);
while (1) {
prevDate.setDate(prevDate.getDate() - 1);
// Stop if we hit minimum date.
if (prevDate.getTime() < minDate.getTime()) {
return;
}
// If previous condition didn't trigger exit then we are in the range.
if (options.byRange) {
break;
}
// Try previous month if this month is not available.
if (!months[(new Date(prevDate.getFullYear(), prevDate.getMonth(), 1)).getTime()]) {
// Set prevDate to 1st of this month so that
// on next iteration it is set to last day of previous month.
prevDate.setDate(1);
continue;
}
// Exit the loop if we found available date.
if (days[prevDate.getTime()]) {
break;
}
}
// Set day to Monday if we have weekly calendar.
if (scope.weekly) {
setToMonday(prevDate);
}
selectDate(prevDate);
};
scope.clickNext = function () {
var nextDate = new Date(scope.date);
if (scope.weekly) {
nextDate.setDate(nextDate.getDate() + 7 - nextDate.getDay());
}
while (1) {
nextDate.setDate(nextDate.getDate() + 1);
// Stop if we hit maximum date.
if (nextDate.getTime() > maxDate.getTime()) {
return;
}
// If previous condition didn't trigger exit then we are in the range.
if (options.byRange) {
break;
}
// Try next month if this month is not available.
if (!months[(new Date(nextDate.getFullYear(), nextDate.getMonth(), 1)).getTime()]) {
// Set nextDate to last day of this month so that
// on next iteration it is set to first day of next month.
nextDate.setMonth(nextDate.getMonth() + 1);
nextDate.setDate(0);
continue;
}
// Exit loop if found available date.
if (days[nextDate.getTime()]) {
break;
}
}
// Set day to Monday if we have weekly calendar.
if (scope.weekly) {
setToMonday(nextDate);
}
selectDate(nextDate);
};
scope.clickToday = function () {
selectDate(currentDay);
};
scope.toggleCalendar = function () {
scope.calendarOpened = !scope.calendarOpened;
calculateDates();
};
},
};
}]);