This repository has been archived by the owner on Aug 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
multipleDatePicker.js
553 lines (503 loc) · 25.1 KB
/
multipleDatePicker.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
@author : Maelig GOHIN For ARCA-Computing - www.arca-computing.fr
@version: 2.1.6
@description: MultipleDatePicker is an Angular directive to show a simple calendar allowing user to select multiple dates.
Css style can be changed by editing less or css stylesheet.
See scope declaration below for options you can pass through html directive.
Feel free to edit and share this piece of code, our idea is to keep it simple ;)
Demo page : http://arca-computing.github.io/MultipleDatePicker/
*/
(function (angular) {
'use strict';
var multipleDatePicker = function () {
return {
restrict: 'AE',
scope: {
/*
* Type : Array of moment dates
* Array will mutate when user select/unselect a date
*/
ngModel: '=?',
/*
* Type: array of objects (see doc)
* Days to highlights
* */
highlightDays: '=?',
/*
* Type : function
* Will be called to manage (un)selection of a date
*/
dayClick: '=?',
/*
* Type : function
* Will be called to manage hover of a date
*/
dayHover: '=?',
/*
* Type: function
* The function to be called when a user right-clicks on a date
*/
rightClick: '=?',
/*
* Type: moment date
* Month to be displayed
* Default is current month
*/
month: '=?',
/*
* Type: function(newMonth, oldMonth)
* Will be called when month changed
* Param newMonth/oldMonth will be the first day of month at midnight
* */
monthChanged: '=?',
/*
* Type: function(event, month)
* Will be called when trying to change month
* Param month will be the first day of month at midnight
* */
monthClick: '=?',
/*
* Type: array of integers
* Recurrent week days not selectables
* /!\ Sunday = 0, Monday = 1 ... Saturday = 6
* */
weekDaysOff: '=?',
/*
* Type: boolean
* Set all days off
* */
allDaysOff: '=?',
/*
* Type: array of moment dates
* Set days allowed (only thos dates will be selectable)
* */
daysAllowed: '=?',
/*
* Type: boolean
* Sunday be the first day of week, default will be Monday
* */
sundayFirstDay: '=?',
/*
* Type: boolean
* if true can't navigate
* */
disableNavigation: '=?',
/*
* Type: boolean
* if true can't go back in months before today's month
* */
disallowBackPastMonths: '=?',
/*
* Type: boolean
* if true can't go in futur months after today's month
* */
disallowGoFuturMonths: '=?',
/*
* Type: boolean
* if true empty boxes will be filled with days of previous/next month
* */
showDaysOfSurroundingMonths: '=?',
/*
* Type: string
* CSS classes to apply to days of next/previous months
* */
cssDaysOfSurroundingMonths: '=?',
/*
* Type: boolean
* if true events on empty boxes (or next/previous month) will be fired
* */
fireEventsForDaysOfSurroundingMonths: '=?',
/*
* Type: any type moment can parse
* If filled will disable all days before this one (not included)
* */
disableDaysBefore: '=?',
/*
* Type: any type moment can parse
* If filled will disable all days after this one (not included)
* */
disableDaysAfter: '=?',
/*
* Type: int
* Number of years from scope.month to show in past in select
* note : will change year into a select
*/
changeYearPast: '=?',
/*
* Type: int
* Number of years from scope.month to show in future in select
* note : will change year into a select
*/
changeYearFuture: '=?',
/*
* Type: bool
* Allow months to be selected with a dropdown box
* note : will change month into a select
*/
enableSelectMonth: '=?',
/*
* Type: function
* The moment object with functions. Optional if you dont want it on the global scope
*/
moment: '=?',
},
template: '<div class="multiple-date-picker">' +
'<div class="picker-top-row">' +
'<div class="text-center picker-navigate picker-navigate-left-arrow" ng-class="{\'disabled\':disableBackButton}" ng-click="changeMonth($event, disableBackButton, -1)"><</div>' +
'<div class="text-center picker-month">' +
'<span ng-if="!enableSelectMonth">{{monthToDisplay}}</span>' +
'<select ng-if="enableSelectMonth" ng-model="select.month" ng-change="changeMonthBySelect(select.month)" ng-options="m for m in monthsForSelect"></select> ' +
'<span ng-if="yearsForSelect.length < 2">{{yearToDisplay}}</span>' +
'<select ng-if="yearsForSelect.length > 1" ng-model="select.year" ng-change="changeYear(select.year)" ng-options="y for y in yearsForSelect"></select>' +
'</div>' +
'<div class="text-center picker-navigate picker-navigate-right-arrow" ng-class="{\'disabled\':disableNextButton}" ng-click="changeMonth($event, disableNextButton, 1)">></div>' +
'</div>' +
'<div class="picker-days-week-row">' +
'<div class="text-center" ng-repeat="day in daysOfWeek">{{day}}</div>' +
'</div>' +
'<div class="picker-days-row">' +
'<div class="text-center picker-day {{getDayClasses(day)}}" title="{{day.title}}" ng-repeat="day in days" ng-click="toggleDay($event, day)" ng-mouseover="hoverDay($event, day)" ng-mouseleave="dayHover($event, day)" mdp-right-click="rightClicked($event,day)">{{day ? day.mdp.otherMonth && !showDaysOfSurroundingMonths ? \' \' : day.date.format(\'D\') : \'\'}}</div>' +
'</div>' +
'</div>',
link: function (scope) {
/*jshint ignore:start*/
var moment = window['moment'] || scope.moment;
/*jshint ignore:end*/
scope.ngModel = scope.ngModel || [];
/*utility functions*/
var checkNavigationButtons = function () {
var today = moment(),
previousMonth = moment(scope.month).subtract(1, 'month'),
nextMonth = moment(scope.month).add(1, 'month');
scope.disableBackButton = scope.disableNavigation || (scope.disallowBackPastMonths && today.isAfter(previousMonth, 'month'));
scope.disableNextButton = scope.disableNavigation || (scope.disallowGoFuturMonths && today.isBefore(nextMonth, 'month'));
},
getDaysOfWeek = function () {
/*To display days of week names in moment.lang*/
var momentDaysOfWeek = moment().localeData()._weekdaysMin,
days = [];
for (var i = 1; i < 7; i++) {
days.push(momentDaysOfWeek[i]);
}
if (scope.sundayFirstDay) {
days.splice(0, 0, momentDaysOfWeek[0]);
} else {
days.push(momentDaysOfWeek[0]);
}
return days;
},
getMonthYearToDisplay = function () {
var month = scope.month.format('MMMM');
return month.charAt(0).toUpperCase() + month.slice(1);
},
getYearsForSelect = function () {
var now = moment(),
changeYearPast = Math.max(0, parseInt(scope.changeYearPast, 10) || 0),
changeYearFuture = Math.max(0, parseInt(scope.changeYearFuture, 10) || 0),
min = moment(scope.month).subtract(changeYearPast, 'year'),
max = moment(scope.month).add(changeYearFuture, 'year'),
result = [];
max.add(1, 'year');
for (var m = moment(min); max.isAfter(m, 'YEAR'); m.add(1, 'year')) {
if ((!scope.disallowBackPastMonths || (m.isAfter(now, 'year') || m.isSame(now, 'year'))) && (!scope.disallowGoFuturMonths || (m.isBefore(now, 'year') || m.isSame(now, 'year')))) {
result.push(m.format('YYYY'));
}
}
return result;
};
/*scope functions*/
var watches = [];
watches.push(
scope.$watch('ngModel', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('month', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('highlightDays', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('weekDaysOff', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('allDaysOff', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('daysAllowed', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('disableDaysBefore', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch('disableDaysAfter', function () {
scope.generate();
}, true)
);
watches.push(
scope.$watch(function () {
return moment.locale();
}, function (newLocale) {
//must change month locale too to change month displayed
scope.month.locale(newLocale);
scope.daysOfWeek = getDaysOfWeek();
scope.monthToDisplay = getMonthYearToDisplay();
}, true)
);
scope.$on('destroy', function () {
for (var i = 0; i < watches.length; i++) {
watches[i]();
}
});
//default values
scope.month = scope.month || moment().startOf('day');
scope.days = [];
scope.weekDaysOff = scope.weekDaysOff || [];
scope.daysOff = scope.daysOff || [];
scope.disableBackButton = false;
scope.disableNextButton = false;
scope.daysOfWeek = getDaysOfWeek();
scope.cssDaysOfSurroundingMonths = scope.cssDaysOfSurroundingMonths || 'picker-empty';
scope.yearsForSelect = [];
scope.monthsForSelect = [];
scope.select = {};
/**
* Called when user clicks a date
* @param event event the click event
* @param day "complex" mdp object with all properties
*/
scope.toggleDay = function (event, day) {
event.preventDefault();
if (day.mdp.otherMonth && !scope.fireEventsForDaysOfSurroundingMonths) {
return;
}
var prevented = false;
event.preventDefault = function () {
prevented = true;
};
if (typeof scope.dayClick == 'function') {
scope.dayClick(event, day);
}
if (day.selectable && !prevented) {
day.mdp.selected = !day.mdp.selected;
if (day.mdp.selected) {
scope.ngModel.push(day.date);
} else {
var idx = -1;
for (var i = 0; i < scope.ngModel.length; ++i) {
if (moment.isMoment(scope.ngModel[i])) {
if (scope.ngModel[i].isSame(day.date, 'day')) {
idx = i;
break;
}
} else {
if (scope.ngModel[i].date.isSame(day.date, 'day')) {
idx = i;
break;
}
}
}
if (idx !== -1) scope.ngModel.splice(idx, 1);
}
}
};
/**
* Hover day
* @param event hover event
* @param day "complex" mdp object with all properties
*/
scope.hoverDay = function (event, day) {
event.preventDefault();
var prevented = false;
event.preventDefault = function () {
prevented = true;
};
if (typeof scope.dayHover == 'function') {
scope.dayHover(event, day);
}
if (!prevented) {
day.mdp.hover = event.type === 'mouseover';
}
};
/**
* Right clicked on day
* @param event Click event
* @param day Day clicked
*/
scope.rightClicked = function (event, day) {
if (typeof scope.rightClick === 'function') {
event.preventDefault();
scope.rightClick(event, day);
}
};
scope.getDayClasses = function (day) {
var css = '';
if (day.css && (!day.mdp.otherMonth || scope.showDaysOfSurroundingMonths)) {
css += ' ' + day.css;
}
if (scope.cssDaysOfSurroundingMonths && day.mdp.otherMonth) {
css += ' ' + scope.cssDaysOfSurroundingMonths;
}
if (day.mdp.selected) {
css += ' picker-selected';
}
if (!day.selectable) {
css += ' picker-off';
}
if (day.mdp.today) {
css += ' today';
}
if (day.mdp.past) {
css += ' past';
}
if (day.mdp.future) {
css += ' future';
}
if (day.mdp.otherMonth) {
css += ' picker-other-month';
}
return css;
};
function updateMonth(monthTo) {
var oldMonth = moment(scope.month);
scope.month = monthTo;
scope.select.month = monthTo.format('MMMM');
if (typeof scope.monthChanged == 'function') {
scope.monthChanged(scope.month, oldMonth);
}
}
/*Navigate to another month*/
scope.changeMonth = function (event, disable, add) {
if (disable) {
return;
}
event.preventDefault();
var prevented = false;
event.preventDefault = function () {
prevented = true;
};
var monthTo = moment(scope.month).add(add, 'month');
if (typeof scope.monthClick == 'function') {
scope.monthClick(event, monthTo);
}
if (!prevented) {
updateMonth(monthTo);
}
};
/*Change month*/
scope.changeMonthBySelect = function (monthName) {
updateMonth(moment(scope.month).month(monthName));
};
/*Change year*/
scope.changeYear = function (year) {
scope.month = scope.month.year(parseInt(year, 10));
};
/*Check if the date is off : unselectable*/
scope.isDayOff = function (day) {
return scope.allDaysOff ||
(!!scope.disableDaysBefore && moment(day.date).isBefore(scope.disableDaysBefore, 'day')) ||
(!!scope.disableDaysAfter && moment(day.date).isAfter(scope.disableDaysAfter, 'day')) ||
(angular.isArray(scope.weekDaysOff) && scope.weekDaysOff.some(function (dayOff) {
return day.date.day() === dayOff;
})) ||
(angular.isArray(scope.daysOff) && scope.daysOff.some(function (dayOff) {
return day.date.isSame(dayOff, 'day');
})) ||
(angular.isArray(scope.daysAllowed) && !scope.daysAllowed.some(function (dayAllowed) {
return day.date.isSame(dayAllowed, 'day');
})) ||
(angular.isArray(scope.highlightDays) && scope.highlightDays.some(function (highlightDay) {
return day.date.isSame(highlightDay.date, 'day') && !highlightDay.selectable;
}));
};
/*Check if the date is selected*/
scope.isSelected = function (day) {
return scope.ngModel.some(function (d) {
return day.date.isSame(d, 'day');
});
};
/*Generate the calendar*/
scope.generate = function () {
scope.monthsForSelect = moment.months();
scope.yearsForSelect = getYearsForSelect();
scope.monthToDisplay = getMonthYearToDisplay();
scope.yearToDisplay = scope.month.format('YYYY');
scope.select = {
year: scope.month.year().toString(),
month: scope.month.format('MMMM')
};
var previousDay = moment(scope.month).date(0).day(scope.sundayFirstDay ? 0 : 1).subtract(1, 'day');
if (moment(scope.month).date(0).diff(previousDay, 'day') > 6) {
previousDay = previousDay.add(1, 'week');
}
var firstDayOfMonth = moment(scope.month).date(1),
days = [],
now = moment(),
lastDay = moment(firstDayOfMonth).endOf('month'),
createDate = function () {
var day = {
date: moment(previousDay.add(1, 'day')),
mdp: {
selected: false
}
};
if (angular.isArray(scope.highlightDays)) {
var hlDay = scope.highlightDays.filter(function (d) {
return day.date.isSame(d.date, 'day');
});
day.css = hlDay.length > 0 ? hlDay[0].css : '';
day.title = hlDay.length > 0 ? hlDay[0].title : '';
}
day.selectable = !scope.isDayOff(day);
day.mdp.selected = scope.isSelected(day);
day.mdp.today = day.date.isSame(now, 'day');
day.mdp.past = day.date.isBefore(now, 'day');
day.mdp.future = day.date.isAfter(now, 'day');
if (!day.date.isSame(scope.month, 'month')) {
day.mdp.otherMonth = true;
}
return day;
},
maxDays = lastDay.diff(previousDay, 'days'),
lastDayOfWeek = scope.sundayFirstDay ? 6 : 0;
if (lastDay.day() !== lastDayOfWeek) {
maxDays += (scope.sundayFirstDay ? 6 : 7) - lastDay.day();
}
for (var j = 0; j < maxDays; j++) {
days.push(createDate());
}
scope.days = days;
checkNavigationButtons();
};
scope.generate();
}
};
}
;
angular.module('multipleDatePicker', [])
.directive('multipleDatePicker', multipleDatePicker)
.directive('mdpRightClick', ['$parse', function ($parse) {
return function (scope, element, attrs) {
var fn = $parse(attrs.mdpRightClick);
element.bind('contextmenu', function (event) {
scope.$apply(function () {
fn(scope, {$event: event});
});
});
};
}]);
})
(window.angular);