@materializecss/materialize 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/Gruntfile.js +38 -24
  2. package/LICENSE +21 -21
  3. package/README.md +91 -97
  4. package/dist/css/materialize.css +8608 -8631
  5. package/dist/css/materialize.min.css +12 -12
  6. package/dist/js/materialize.js +12816 -12669
  7. package/dist/js/materialize.min.js +6 -6
  8. package/extras/noUiSlider/nouislider.css +404 -406
  9. package/extras/noUiSlider/nouislider.js +2147 -2147
  10. package/extras/noUiSlider/nouislider.min.js +0 -0
  11. package/js/anime.min.js +34 -34
  12. package/js/autocomplete.js +479 -479
  13. package/js/buttons.js +354 -354
  14. package/js/cards.js +40 -40
  15. package/js/carousel.js +732 -732
  16. package/js/cash.js +960 -960
  17. package/js/characterCounter.js +136 -136
  18. package/js/chips.js +486 -486
  19. package/js/collapsible.js +275 -275
  20. package/js/component.js +44 -44
  21. package/js/datepicker.js +983 -983
  22. package/js/dropdown.js +669 -669
  23. package/js/forms.js +285 -275
  24. package/js/global.js +428 -424
  25. package/js/materialbox.js +453 -453
  26. package/js/modal.js +382 -382
  27. package/js/parallax.js +138 -138
  28. package/js/pushpin.js +148 -148
  29. package/js/range.js +263 -263
  30. package/js/scrollspy.js +295 -295
  31. package/js/select.js +391 -310
  32. package/js/sidenav.js +583 -583
  33. package/js/slider.js +359 -359
  34. package/js/tabs.js +402 -402
  35. package/js/tapTarget.js +315 -315
  36. package/js/timepicker.js +712 -648
  37. package/js/toasts.js +325 -322
  38. package/js/tooltip.js +320 -320
  39. package/js/waves.js +614 -614
  40. package/package.json +87 -82
  41. package/sass/_style.scss +929 -929
  42. package/sass/components/_badges.scss +55 -55
  43. package/sass/components/_buttons.scss +322 -322
  44. package/sass/components/_cards.scss +195 -195
  45. package/sass/components/_carousel.scss +90 -90
  46. package/sass/components/_chips.scss +96 -96
  47. package/sass/components/_collapsible.scss +91 -91
  48. package/sass/components/_collection.scss +106 -106
  49. package/sass/components/_color-classes.scss +32 -32
  50. package/sass/components/_color-variables.scss +370 -370
  51. package/sass/components/_datepicker.scss +191 -191
  52. package/sass/components/_dropdown.scss +84 -84
  53. package/sass/components/_global.scss +646 -642
  54. package/sass/components/_grid.scss +158 -158
  55. package/sass/components/_icons-material-design.scss +5 -5
  56. package/sass/components/_materialbox.scss +42 -42
  57. package/sass/components/_modal.scss +97 -97
  58. package/sass/components/_navbar.scss +208 -208
  59. package/sass/components/_normalize.scss +447 -447
  60. package/sass/components/_preloader.scss +334 -334
  61. package/sass/components/_pulse.scss +34 -34
  62. package/sass/components/_sidenav.scss +214 -214
  63. package/sass/components/_slider.scss +91 -91
  64. package/sass/components/_table_of_contents.scss +33 -33
  65. package/sass/components/_tabs.scss +99 -99
  66. package/sass/components/_tapTarget.scss +103 -103
  67. package/sass/components/_timepicker.scss +199 -183
  68. package/sass/components/_toast.scss +58 -58
  69. package/sass/components/_tooltip.scss +32 -32
  70. package/sass/components/_transitions.scss +12 -12
  71. package/sass/components/_typography.scss +62 -62
  72. package/sass/components/_variables.scss +352 -352
  73. package/sass/components/_waves.scss +187 -187
  74. package/sass/components/forms/_checkboxes.scss +200 -200
  75. package/sass/components/forms/_file-input.scss +44 -44
  76. package/sass/components/forms/_forms.scss +22 -22
  77. package/sass/components/forms/_input-fields.scss +388 -379
  78. package/sass/components/forms/_radio-buttons.scss +115 -115
  79. package/sass/components/forms/_range.scss +161 -161
  80. package/sass/components/forms/_select.scss +199 -199
  81. package/sass/components/forms/_switches.scss +91 -91
  82. package/sass/ghpages-materialize.scss +7 -7
  83. package/sass/materialize.scss +42 -42
package/js/datepicker.js CHANGED
@@ -1,983 +1,983 @@
1
- (function($) {
2
- 'use strict';
3
-
4
- let _defaults = {
5
- // Close when date is selected
6
- autoClose: false,
7
-
8
- // the default output format for the input field value
9
- format: 'mmm dd, yyyy',
10
-
11
- // Used to create date object from current input string
12
- parse: null,
13
-
14
- // The initial date to view when first opened
15
- defaultDate: null,
16
-
17
- // Make the `defaultDate` the initial selected value
18
- setDefaultDate: false,
19
-
20
- disableWeekends: false,
21
-
22
- disableDayFn: null,
23
-
24
- // First day of week (0: Sunday, 1: Monday etc)
25
- firstDay: 0,
26
-
27
- // The earliest date that can be selected
28
- minDate: null,
29
- // Thelatest date that can be selected
30
- maxDate: null,
31
-
32
- // Number of years either side, or array of upper/lower range
33
- yearRange: 10,
34
-
35
- // used internally (don't config outside)
36
- minYear: 0,
37
- maxYear: 9999,
38
- minMonth: undefined,
39
- maxMonth: undefined,
40
-
41
- startRange: null,
42
- endRange: null,
43
-
44
- isRTL: false,
45
-
46
- // Render the month after year in the calendar title
47
- showMonthAfterYear: false,
48
-
49
- // Render days of the calendar grid that fall in the next or previous month
50
- showDaysInNextAndPreviousMonths: false,
51
-
52
- // Specify a DOM element to render the calendar in
53
- container: null,
54
-
55
- // Show clear button
56
- showClearBtn: false,
57
-
58
- // internationalization
59
- i18n: {
60
- cancel: 'Cancel',
61
- clear: 'Clear',
62
- done: 'Ok',
63
- previousMonth: '‹',
64
- nextMonth: '›',
65
- months: [
66
- 'January',
67
- 'February',
68
- 'March',
69
- 'April',
70
- 'May',
71
- 'June',
72
- 'July',
73
- 'August',
74
- 'September',
75
- 'October',
76
- 'November',
77
- 'December'
78
- ],
79
- monthsShort: [
80
- 'Jan',
81
- 'Feb',
82
- 'Mar',
83
- 'Apr',
84
- 'May',
85
- 'Jun',
86
- 'Jul',
87
- 'Aug',
88
- 'Sep',
89
- 'Oct',
90
- 'Nov',
91
- 'Dec'
92
- ],
93
- weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
94
- weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
95
- weekdaysAbbrev: ['S', 'M', 'T', 'W', 'T', 'F', 'S']
96
- },
97
-
98
- // events array
99
- events: [],
100
-
101
- // callback function
102
- onSelect: null,
103
- onOpen: null,
104
- onClose: null,
105
- onDraw: null
106
- };
107
-
108
- /**
109
- * @class
110
- *
111
- */
112
- class Datepicker extends Component {
113
- /**
114
- * Construct Datepicker instance and set up overlay
115
- * @constructor
116
- * @param {Element} el
117
- * @param {Object} options
118
- */
119
- constructor(el, options) {
120
- super(Datepicker, el, options);
121
-
122
- this.el.M_Datepicker = this;
123
-
124
- this.options = $.extend({}, Datepicker.defaults, options);
125
-
126
- // make sure i18n defaults are not lost when only few i18n option properties are passed
127
- if (!!options && options.hasOwnProperty('i18n') && typeof options.i18n === 'object') {
128
- this.options.i18n = $.extend({}, Datepicker.defaults.i18n, options.i18n);
129
- }
130
-
131
- // Remove time component from minDate and maxDate options
132
- if (this.options.minDate) this.options.minDate.setHours(0, 0, 0, 0);
133
- if (this.options.maxDate) this.options.maxDate.setHours(0, 0, 0, 0);
134
-
135
- this.id = M.guid();
136
-
137
- this._setupVariables();
138
- this._insertHTMLIntoDOM();
139
- this._setupModal();
140
-
141
- this._setupEventHandlers();
142
-
143
- if (!this.options.defaultDate) {
144
- this.options.defaultDate = new Date(Date.parse(this.el.value));
145
- }
146
-
147
- let defDate = this.options.defaultDate;
148
- if (Datepicker._isDate(defDate)) {
149
- if (this.options.setDefaultDate) {
150
- this.setDate(defDate, true);
151
- this.setInputValue();
152
- } else {
153
- this.gotoDate(defDate);
154
- }
155
- } else {
156
- this.gotoDate(new Date());
157
- }
158
-
159
- /**
160
- * Describes open/close state of datepicker
161
- * @type {Boolean}
162
- */
163
- this.isOpen = false;
164
- }
165
-
166
- static get defaults() {
167
- return _defaults;
168
- }
169
-
170
- static init(els, options) {
171
- return super.init(this, els, options);
172
- }
173
-
174
- static _isDate(obj) {
175
- return /Date/.test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime());
176
- }
177
-
178
- static _isWeekend(date) {
179
- let day = date.getDay();
180
- return day === 0 || day === 6;
181
- }
182
-
183
- static _setToStartOfDay(date) {
184
- if (Datepicker._isDate(date)) date.setHours(0, 0, 0, 0);
185
- }
186
-
187
- static _getDaysInMonth(year, month) {
188
- return [31, Datepicker._isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][
189
- month
190
- ];
191
- }
192
-
193
- static _isLeapYear(year) {
194
- // solution by Matti Virkkunen: http://stackoverflow.com/a/4881951
195
- return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
196
- }
197
-
198
- static _compareDates(a, b) {
199
- // weak date comparison (use setToStartOfDay(date) to ensure correct result)
200
- return a.getTime() === b.getTime();
201
- }
202
-
203
- static _setToStartOfDay(date) {
204
- if (Datepicker._isDate(date)) date.setHours(0, 0, 0, 0);
205
- }
206
-
207
- /**
208
- * Get Instance
209
- */
210
- static getInstance(el) {
211
- let domElem = !!el.jquery ? el[0] : el;
212
- return domElem.M_Datepicker;
213
- }
214
-
215
- /**
216
- * Teardown component
217
- */
218
- destroy() {
219
- this._removeEventHandlers();
220
- this.modal.destroy();
221
- $(this.modalEl).remove();
222
- this.destroySelects();
223
- this.el.M_Datepicker = undefined;
224
- }
225
-
226
- destroySelects() {
227
- let oldYearSelect = this.calendarEl.querySelector('.orig-select-year');
228
- if (oldYearSelect) {
229
- M.FormSelect.getInstance(oldYearSelect).destroy();
230
- }
231
- let oldMonthSelect = this.calendarEl.querySelector('.orig-select-month');
232
- if (oldMonthSelect) {
233
- M.FormSelect.getInstance(oldMonthSelect).destroy();
234
- }
235
- }
236
-
237
- _insertHTMLIntoDOM() {
238
- if (this.options.showClearBtn) {
239
- $(this.clearBtn).css({ visibility: '' });
240
- this.clearBtn.innerHTML = this.options.i18n.clear;
241
- }
242
-
243
- this.doneBtn.innerHTML = this.options.i18n.done;
244
- this.cancelBtn.innerHTML = this.options.i18n.cancel;
245
-
246
- if (this.options.container) {
247
- const optEl = this.options.container;
248
- this.options.container =
249
- optEl instanceof HTMLElement ? optEl : document.querySelector(optEl);
250
- this.$modalEl.appendTo(this.options.container);
251
- } else {
252
- this.$modalEl.insertBefore(this.el);
253
- }
254
- }
255
-
256
- _setupModal() {
257
- this.modalEl.id = 'modal-' + this.id;
258
- this.modal = M.Modal.init(this.modalEl, {
259
- onCloseEnd: () => {
260
- this.isOpen = false;
261
- }
262
- });
263
- }
264
-
265
- toString(format) {
266
- format = format || this.options.format;
267
- if (typeof format === 'function') {
268
- return format(this.date);
269
- }
270
-
271
- if (!Datepicker._isDate(this.date)) {
272
- return '';
273
- }
274
-
275
- let formatArray = format.split(/(d{1,4}|m{1,4}|y{4}|yy|!.)/g);
276
- let formattedDate = formatArray
277
- .map((label) => {
278
- if (this.formats[label]) {
279
- return this.formats[label]();
280
- }
281
-
282
- return label;
283
- })
284
- .join('');
285
- return formattedDate;
286
- }
287
-
288
- setDate(date, preventOnSelect) {
289
- if (!date) {
290
- this.date = null;
291
- this._renderDateDisplay();
292
- return this.draw();
293
- }
294
- if (typeof date === 'string') {
295
- date = new Date(Date.parse(date));
296
- }
297
- if (!Datepicker._isDate(date)) {
298
- return;
299
- }
300
-
301
- let min = this.options.minDate,
302
- max = this.options.maxDate;
303
-
304
- if (Datepicker._isDate(min) && date < min) {
305
- date = min;
306
- } else if (Datepicker._isDate(max) && date > max) {
307
- date = max;
308
- }
309
-
310
- this.date = new Date(date.getTime());
311
-
312
- this._renderDateDisplay();
313
-
314
- Datepicker._setToStartOfDay(this.date);
315
- this.gotoDate(this.date);
316
-
317
- if (!preventOnSelect && typeof this.options.onSelect === 'function') {
318
- this.options.onSelect.call(this, this.date);
319
- }
320
- }
321
-
322
- setInputValue() {
323
- this.el.value = this.toString();
324
- this.$el.trigger('change', { firedBy: this });
325
- }
326
-
327
- _renderDateDisplay() {
328
- let displayDate = Datepicker._isDate(this.date) ? this.date : new Date();
329
- let i18n = this.options.i18n;
330
- let day = i18n.weekdaysShort[displayDate.getDay()];
331
- let month = i18n.monthsShort[displayDate.getMonth()];
332
- let date = displayDate.getDate();
333
- this.yearTextEl.innerHTML = displayDate.getFullYear();
334
- this.dateTextEl.innerHTML = `${day}, ${month} ${date}`;
335
- }
336
-
337
- /**
338
- * change view to a specific date
339
- */
340
- gotoDate(date) {
341
- let newCalendar = true;
342
-
343
- if (!Datepicker._isDate(date)) {
344
- return;
345
- }
346
-
347
- if (this.calendars) {
348
- let firstVisibleDate = new Date(this.calendars[0].year, this.calendars[0].month, 1),
349
- lastVisibleDate = new Date(
350
- this.calendars[this.calendars.length - 1].year,
351
- this.calendars[this.calendars.length - 1].month,
352
- 1
353
- ),
354
- visibleDate = date.getTime();
355
- // get the end of the month
356
- lastVisibleDate.setMonth(lastVisibleDate.getMonth() + 1);
357
- lastVisibleDate.setDate(lastVisibleDate.getDate() - 1);
358
- newCalendar =
359
- visibleDate < firstVisibleDate.getTime() || lastVisibleDate.getTime() < visibleDate;
360
- }
361
-
362
- if (newCalendar) {
363
- this.calendars = [
364
- {
365
- month: date.getMonth(),
366
- year: date.getFullYear()
367
- }
368
- ];
369
- }
370
-
371
- this.adjustCalendars();
372
- }
373
-
374
- adjustCalendars() {
375
- this.calendars[0] = this.adjustCalendar(this.calendars[0]);
376
- this.draw();
377
- }
378
-
379
- adjustCalendar(calendar) {
380
- if (calendar.month < 0) {
381
- calendar.year -= Math.ceil(Math.abs(calendar.month) / 12);
382
- calendar.month += 12;
383
- }
384
- if (calendar.month > 11) {
385
- calendar.year += Math.floor(Math.abs(calendar.month) / 12);
386
- calendar.month -= 12;
387
- }
388
- return calendar;
389
- }
390
-
391
- nextMonth() {
392
- this.calendars[0].month++;
393
- this.adjustCalendars();
394
- }
395
-
396
- prevMonth() {
397
- this.calendars[0].month--;
398
- this.adjustCalendars();
399
- }
400
-
401
- render(year, month, randId) {
402
- let opts = this.options,
403
- now = new Date(),
404
- days = Datepicker._getDaysInMonth(year, month),
405
- before = new Date(year, month, 1).getDay(),
406
- data = [],
407
- row = [];
408
- Datepicker._setToStartOfDay(now);
409
- if (opts.firstDay > 0) {
410
- before -= opts.firstDay;
411
- if (before < 0) {
412
- before += 7;
413
- }
414
- }
415
- let previousMonth = month === 0 ? 11 : month - 1,
416
- nextMonth = month === 11 ? 0 : month + 1,
417
- yearOfPreviousMonth = month === 0 ? year - 1 : year,
418
- yearOfNextMonth = month === 11 ? year + 1 : year,
419
- daysInPreviousMonth = Datepicker._getDaysInMonth(yearOfPreviousMonth, previousMonth);
420
- let cells = days + before,
421
- after = cells;
422
- while (after > 7) {
423
- after -= 7;
424
- }
425
- cells += 7 - after;
426
- let isWeekSelected = false;
427
- for (let i = 0, r = 0; i < cells; i++) {
428
- let day = new Date(year, month, 1 + (i - before)),
429
- isSelected = Datepicker._isDate(this.date)
430
- ? Datepicker._compareDates(day, this.date)
431
- : false,
432
- isToday = Datepicker._compareDates(day, now),
433
- hasEvent = opts.events.indexOf(day.toDateString()) !== -1 ? true : false,
434
- isEmpty = i < before || i >= days + before,
435
- dayNumber = 1 + (i - before),
436
- monthNumber = month,
437
- yearNumber = year,
438
- isStartRange = opts.startRange && Datepicker._compareDates(opts.startRange, day),
439
- isEndRange = opts.endRange && Datepicker._compareDates(opts.endRange, day),
440
- isInRange =
441
- opts.startRange && opts.endRange && opts.startRange < day && day < opts.endRange,
442
- isDisabled =
443
- (opts.minDate && day < opts.minDate) ||
444
- (opts.maxDate && day > opts.maxDate) ||
445
- (opts.disableWeekends && Datepicker._isWeekend(day)) ||
446
- (opts.disableDayFn && opts.disableDayFn(day));
447
-
448
- if (isEmpty) {
449
- if (i < before) {
450
- dayNumber = daysInPreviousMonth + dayNumber;
451
- monthNumber = previousMonth;
452
- yearNumber = yearOfPreviousMonth;
453
- } else {
454
- dayNumber = dayNumber - days;
455
- monthNumber = nextMonth;
456
- yearNumber = yearOfNextMonth;
457
- }
458
- }
459
-
460
- let dayConfig = {
461
- day: dayNumber,
462
- month: monthNumber,
463
- year: yearNumber,
464
- hasEvent: hasEvent,
465
- isSelected: isSelected,
466
- isToday: isToday,
467
- isDisabled: isDisabled,
468
- isEmpty: isEmpty,
469
- isStartRange: isStartRange,
470
- isEndRange: isEndRange,
471
- isInRange: isInRange,
472
- showDaysInNextAndPreviousMonths: opts.showDaysInNextAndPreviousMonths
473
- };
474
-
475
- row.push(this.renderDay(dayConfig));
476
-
477
- if (++r === 7) {
478
- data.push(this.renderRow(row, opts.isRTL, isWeekSelected));
479
- row = [];
480
- r = 0;
481
- isWeekSelected = false;
482
- }
483
- }
484
- return this.renderTable(opts, data, randId);
485
- }
486
-
487
- renderDay(opts) {
488
- let arr = [];
489
- let ariaSelected = 'false';
490
- if (opts.isEmpty) {
491
- if (opts.showDaysInNextAndPreviousMonths) {
492
- arr.push('is-outside-current-month');
493
- arr.push('is-selection-disabled');
494
- } else {
495
- return '<td class="is-empty"></td>';
496
- }
497
- }
498
- if (opts.isDisabled) {
499
- arr.push('is-disabled');
500
- }
501
-
502
- if (opts.isToday) {
503
- arr.push('is-today');
504
- }
505
- if (opts.isSelected) {
506
- arr.push('is-selected');
507
- ariaSelected = 'true';
508
- }
509
- if (opts.hasEvent) {
510
- arr.push('has-event');
511
- }
512
- if (opts.isInRange) {
513
- arr.push('is-inrange');
514
- }
515
- if (opts.isStartRange) {
516
- arr.push('is-startrange');
517
- }
518
- if (opts.isEndRange) {
519
- arr.push('is-endrange');
520
- }
521
- return (
522
- `<td data-day="${opts.day}" class="${arr.join(' ')}" aria-selected="${ariaSelected}">` +
523
- `<button class="datepicker-day-button" type="button" data-year="${opts.year}" data-month="${opts.month}" data-day="${opts.day}">${opts.day}</button>` +
524
- '</td>'
525
- );
526
- }
527
-
528
- renderRow(days, isRTL, isRowSelected) {
529
- return (
530
- '<tr class="datepicker-row' +
531
- (isRowSelected ? ' is-selected' : '') +
532
- '">' +
533
- (isRTL ? days.reverse() : days).join('') +
534
- '</tr>'
535
- );
536
- }
537
-
538
- renderTable(opts, data, randId) {
539
- return (
540
- '<div class="datepicker-table-wrapper"><table cellpadding="0" cellspacing="0" class="datepicker-table" role="grid" aria-labelledby="' +
541
- randId +
542
- '">' +
543
- this.renderHead(opts) +
544
- this.renderBody(data) +
545
- '</table></div>'
546
- );
547
- }
548
-
549
- renderHead(opts) {
550
- let i,
551
- arr = [];
552
- for (i = 0; i < 7; i++) {
553
- arr.push(
554
- `<th scope="col"><abbr title="${this.renderDayName(opts, i)}">${this.renderDayName(
555
- opts,
556
- i,
557
- true
558
- )}</abbr></th>`
559
- );
560
- }
561
- return '<thead><tr>' + (opts.isRTL ? arr.reverse() : arr).join('') + '</tr></thead>';
562
- }
563
-
564
- renderBody(rows) {
565
- return '<tbody>' + rows.join('') + '</tbody>';
566
- }
567
-
568
- renderTitle(instance, c, year, month, refYear, randId) {
569
- let i,
570
- j,
571
- arr,
572
- opts = this.options,
573
- isMinYear = year === opts.minYear,
574
- isMaxYear = year === opts.maxYear,
575
- html =
576
- '<div id="' +
577
- randId +
578
- '" class="datepicker-controls" role="heading" aria-live="assertive">',
579
- monthHtml,
580
- yearHtml,
581
- prev = true,
582
- next = true;
583
-
584
- for (arr = [], i = 0; i < 12; i++) {
585
- arr.push(
586
- '<option value="' +
587
- (year === refYear ? i - c : 12 + i - c) +
588
- '"' +
589
- (i === month ? ' selected="selected"' : '') +
590
- ((isMinYear && i < opts.minMonth) || (isMaxYear && i > opts.maxMonth)
591
- ? 'disabled="disabled"'
592
- : '') +
593
- '>' +
594
- opts.i18n.months[i] +
595
- '</option>'
596
- );
597
- }
598
-
599
- monthHtml =
600
- '<select class="datepicker-select orig-select-month" tabindex="-1">' +
601
- arr.join('') +
602
- '</select>';
603
-
604
- if ($.isArray(opts.yearRange)) {
605
- i = opts.yearRange[0];
606
- j = opts.yearRange[1] + 1;
607
- } else {
608
- i = year - opts.yearRange;
609
- j = 1 + year + opts.yearRange;
610
- }
611
-
612
- for (arr = []; i < j && i <= opts.maxYear; i++) {
613
- if (i >= opts.minYear) {
614
- arr.push(`<option value="${i}" ${i === year ? 'selected="selected"' : ''}>${i}</option>`);
615
- }
616
- }
617
- if (opts.yearRangeReverse) {
618
- arr.reverse();
619
- }
620
-
621
- yearHtml = `<select class="datepicker-select orig-select-year" tabindex="-1">${arr.join(
622
- ''
623
- )}</select>`;
624
-
625
- let leftArrow =
626
- '<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M15.41 16.09l-4.58-4.59 4.58-4.59L14 5.5l-6 6 6 6z"/><path d="M0-.5h24v24H0z" fill="none"/></svg>';
627
- html += `<button class="month-prev${
628
- prev ? '' : ' is-disabled'
629
- }" type="button">${leftArrow}</button>`;
630
-
631
- html += '<div class="selects-container">';
632
- if (opts.showMonthAfterYear) {
633
- html += yearHtml + monthHtml;
634
- } else {
635
- html += monthHtml + yearHtml;
636
- }
637
- html += '</div>';
638
-
639
- if (isMinYear && (month === 0 || opts.minMonth >= month)) {
640
- prev = false;
641
- }
642
-
643
- if (isMaxYear && (month === 11 || opts.maxMonth <= month)) {
644
- next = false;
645
- }
646
-
647
- let rightArrow =
648
- '<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"/><path d="M0-.25h24v24H0z" fill="none"/></svg>';
649
- html += `<button class="month-next${
650
- next ? '' : ' is-disabled'
651
- }" type="button">${rightArrow}</button>`;
652
-
653
- return (html += '</div>');
654
- }
655
-
656
- /**
657
- * refresh the HTML
658
- */
659
- draw(force) {
660
- if (!this.isOpen && !force) {
661
- return;
662
- }
663
- let opts = this.options,
664
- minYear = opts.minYear,
665
- maxYear = opts.maxYear,
666
- minMonth = opts.minMonth,
667
- maxMonth = opts.maxMonth,
668
- html = '',
669
- randId;
670
-
671
- if (this._y <= minYear) {
672
- this._y = minYear;
673
- if (!isNaN(minMonth) && this._m < minMonth) {
674
- this._m = minMonth;
675
- }
676
- }
677
- if (this._y >= maxYear) {
678
- this._y = maxYear;
679
- if (!isNaN(maxMonth) && this._m > maxMonth) {
680
- this._m = maxMonth;
681
- }
682
- }
683
-
684
- randId =
685
- 'datepicker-title-' +
686
- Math.random()
687
- .toString(36)
688
- .replace(/[^a-z]+/g, '')
689
- .substr(0, 2);
690
-
691
- for (let c = 0; c < 1; c++) {
692
- this._renderDateDisplay();
693
- html +=
694
- this.renderTitle(
695
- this,
696
- c,
697
- this.calendars[c].year,
698
- this.calendars[c].month,
699
- this.calendars[0].year,
700
- randId
701
- ) + this.render(this.calendars[c].year, this.calendars[c].month, randId);
702
- }
703
-
704
- this.destroySelects();
705
-
706
- this.calendarEl.innerHTML = html;
707
-
708
- // Init Materialize Select
709
- let yearSelect = this.calendarEl.querySelector('.orig-select-year');
710
- let monthSelect = this.calendarEl.querySelector('.orig-select-month');
711
- M.FormSelect.init(yearSelect, {
712
- classes: 'select-year',
713
- dropdownOptions: { container: document.body, constrainWidth: false }
714
- });
715
- M.FormSelect.init(monthSelect, {
716
- classes: 'select-month',
717
- dropdownOptions: { container: document.body, constrainWidth: false }
718
- });
719
-
720
- // Add change handlers for select
721
- yearSelect.addEventListener('change', this._handleYearChange.bind(this));
722
- monthSelect.addEventListener('change', this._handleMonthChange.bind(this));
723
-
724
- if (typeof this.options.onDraw === 'function') {
725
- this.options.onDraw(this);
726
- }
727
- }
728
-
729
- /**
730
- * Setup Event Handlers
731
- */
732
- _setupEventHandlers() {
733
- this._handleInputKeydownBound = this._handleInputKeydown.bind(this);
734
- this._handleInputClickBound = this._handleInputClick.bind(this);
735
- this._handleInputChangeBound = this._handleInputChange.bind(this);
736
- this._handleCalendarClickBound = this._handleCalendarClick.bind(this);
737
- this._finishSelectionBound = this._finishSelection.bind(this);
738
- this._handleMonthChange = this._handleMonthChange.bind(this);
739
- this._closeBound = this.close.bind(this);
740
-
741
- this.el.addEventListener('click', this._handleInputClickBound);
742
- this.el.addEventListener('keydown', this._handleInputKeydownBound);
743
- this.el.addEventListener('change', this._handleInputChangeBound);
744
- this.calendarEl.addEventListener('click', this._handleCalendarClickBound);
745
- this.doneBtn.addEventListener('click', this._finishSelectionBound);
746
- this.cancelBtn.addEventListener('click', this._closeBound);
747
-
748
- if (this.options.showClearBtn) {
749
- this._handleClearClickBound = this._handleClearClick.bind(this);
750
- this.clearBtn.addEventListener('click', this._handleClearClickBound);
751
- }
752
- }
753
-
754
- _setupVariables() {
755
- this.$modalEl = $(Datepicker._template);
756
- this.modalEl = this.$modalEl[0];
757
-
758
- this.calendarEl = this.modalEl.querySelector('.datepicker-calendar');
759
-
760
- this.yearTextEl = this.modalEl.querySelector('.year-text');
761
- this.dateTextEl = this.modalEl.querySelector('.date-text');
762
- if (this.options.showClearBtn) {
763
- this.clearBtn = this.modalEl.querySelector('.datepicker-clear');
764
- }
765
- this.doneBtn = this.modalEl.querySelector('.datepicker-done');
766
- this.cancelBtn = this.modalEl.querySelector('.datepicker-cancel');
767
-
768
- this.formats = {
769
- d: () => {
770
- return this.date.getDate();
771
- },
772
- dd: () => {
773
- let d = this.date.getDate();
774
- return (d < 10 ? '0' : '') + d;
775
- },
776
- ddd: () => {
777
- return this.options.i18n.weekdaysShort[this.date.getDay()];
778
- },
779
- dddd: () => {
780
- return this.options.i18n.weekdays[this.date.getDay()];
781
- },
782
- m: () => {
783
- return this.date.getMonth() + 1;
784
- },
785
- mm: () => {
786
- let m = this.date.getMonth() + 1;
787
- return (m < 10 ? '0' : '') + m;
788
- },
789
- mmm: () => {
790
- return this.options.i18n.monthsShort[this.date.getMonth()];
791
- },
792
- mmmm: () => {
793
- return this.options.i18n.months[this.date.getMonth()];
794
- },
795
- yy: () => {
796
- return ('' + this.date.getFullYear()).slice(2);
797
- },
798
- yyyy: () => {
799
- return this.date.getFullYear();
800
- }
801
- };
802
- }
803
-
804
- /**
805
- * Remove Event Handlers
806
- */
807
- _removeEventHandlers() {
808
- this.el.removeEventListener('click', this._handleInputClickBound);
809
- this.el.removeEventListener('keydown', this._handleInputKeydownBound);
810
- this.el.removeEventListener('change', this._handleInputChangeBound);
811
- this.calendarEl.removeEventListener('click', this._handleCalendarClickBound);
812
- }
813
-
814
- _handleInputClick() {
815
- this.open();
816
- }
817
-
818
- _handleInputKeydown(e) {
819
- if (e.which === M.keys.ENTER) {
820
- e.preventDefault();
821
- this.open();
822
- }
823
- }
824
-
825
- _handleCalendarClick(e) {
826
- if (!this.isOpen) {
827
- return;
828
- }
829
-
830
- let $target = $(e.target);
831
- if (!$target.hasClass('is-disabled')) {
832
- if (
833
- $target.hasClass('datepicker-day-button') &&
834
- !$target.hasClass('is-empty') &&
835
- !$target.parent().hasClass('is-disabled')
836
- ) {
837
- this.setDate(
838
- new Date(
839
- e.target.getAttribute('data-year'),
840
- e.target.getAttribute('data-month'),
841
- e.target.getAttribute('data-day')
842
- )
843
- );
844
- if (this.options.autoClose) {
845
- this._finishSelection();
846
- }
847
- } else if ($target.closest('.month-prev').length) {
848
- this.prevMonth();
849
- } else if ($target.closest('.month-next').length) {
850
- this.nextMonth();
851
- }
852
- }
853
- }
854
-
855
- _handleClearClick() {
856
- this.date = null;
857
- this.setInputValue();
858
- this.close();
859
- }
860
-
861
- _handleMonthChange(e) {
862
- this.gotoMonth(e.target.value);
863
- }
864
-
865
- _handleYearChange(e) {
866
- this.gotoYear(e.target.value);
867
- }
868
-
869
- /**
870
- * change view to a specific month (zero-index, e.g. 0: January)
871
- */
872
- gotoMonth(month) {
873
- if (!isNaN(month)) {
874
- this.calendars[0].month = parseInt(month, 10);
875
- this.adjustCalendars();
876
- }
877
- }
878
-
879
- /**
880
- * change view to a specific full year (e.g. "2012")
881
- */
882
- gotoYear(year) {
883
- if (!isNaN(year)) {
884
- this.calendars[0].year = parseInt(year, 10);
885
- this.adjustCalendars();
886
- }
887
- }
888
-
889
- _handleInputChange(e) {
890
- let date;
891
-
892
- // Prevent change event from being fired when triggered by the plugin
893
- if (e.firedBy === this) {
894
- return;
895
- }
896
- if (this.options.parse) {
897
- date = this.options.parse(this.el.value, this.options.format);
898
- } else {
899
- date = new Date(Date.parse(this.el.value));
900
- }
901
-
902
- if (Datepicker._isDate(date)) {
903
- this.setDate(date);
904
- }
905
- }
906
-
907
- renderDayName(opts, day, abbr) {
908
- day += opts.firstDay;
909
- while (day >= 7) {
910
- day -= 7;
911
- }
912
- return abbr ? opts.i18n.weekdaysAbbrev[day] : opts.i18n.weekdays[day];
913
- }
914
-
915
- /**
916
- * Set input value to the selected date and close Datepicker
917
- */
918
- _finishSelection() {
919
- this.setInputValue();
920
- this.close();
921
- }
922
-
923
- /**
924
- * Open Datepicker
925
- */
926
- open() {
927
- if (this.isOpen) {
928
- return;
929
- }
930
-
931
- this.isOpen = true;
932
- if (typeof this.options.onOpen === 'function') {
933
- this.options.onOpen.call(this);
934
- }
935
- this.draw();
936
- this.modal.open();
937
- return this;
938
- }
939
-
940
- /**
941
- * Close Datepicker
942
- */
943
- close() {
944
- if (!this.isOpen) {
945
- return;
946
- }
947
-
948
- this.isOpen = false;
949
- if (typeof this.options.onClose === 'function') {
950
- this.options.onClose.call(this);
951
- }
952
- this.modal.close();
953
- return this;
954
- }
955
- }
956
-
957
- Datepicker._template = [
958
- '<div class= "modal datepicker-modal">',
959
- '<div class="modal-content datepicker-container">',
960
- '<div class="datepicker-date-display">',
961
- '<span class="year-text"></span>',
962
- '<span class="date-text"></span>',
963
- '</div>',
964
- '<div class="datepicker-calendar-container">',
965
- '<div class="datepicker-calendar"></div>',
966
- '<div class="datepicker-footer">',
967
- '<button class="btn-flat datepicker-clear waves-effect" style="visibility: hidden;" type="button"></button>',
968
- '<div class="confirmation-btns">',
969
- '<button class="btn-flat datepicker-cancel waves-effect" type="button"></button>',
970
- '<button class="btn-flat datepicker-done waves-effect" type="button"></button>',
971
- '</div>',
972
- '</div>',
973
- '</div>',
974
- '</div>',
975
- '</div>'
976
- ].join('');
977
-
978
- M.Datepicker = Datepicker;
979
-
980
- if (M.jQueryLoaded) {
981
- M.initializeJqueryWrapper(Datepicker, 'datepicker', 'M_Datepicker');
982
- }
983
- })(cash);
1
+ (function($) {
2
+ 'use strict';
3
+
4
+ let _defaults = {
5
+ // Close when date is selected
6
+ autoClose: false,
7
+
8
+ // the default output format for the input field value
9
+ format: 'mmm dd, yyyy',
10
+
11
+ // Used to create date object from current input string
12
+ parse: null,
13
+
14
+ // The initial date to view when first opened
15
+ defaultDate: null,
16
+
17
+ // Make the `defaultDate` the initial selected value
18
+ setDefaultDate: false,
19
+
20
+ disableWeekends: false,
21
+
22
+ disableDayFn: null,
23
+
24
+ // First day of week (0: Sunday, 1: Monday etc)
25
+ firstDay: 0,
26
+
27
+ // The earliest date that can be selected
28
+ minDate: null,
29
+ // Thelatest date that can be selected
30
+ maxDate: null,
31
+
32
+ // Number of years either side, or array of upper/lower range
33
+ yearRange: 10,
34
+
35
+ // used internally (don't config outside)
36
+ minYear: 0,
37
+ maxYear: 9999,
38
+ minMonth: undefined,
39
+ maxMonth: undefined,
40
+
41
+ startRange: null,
42
+ endRange: null,
43
+
44
+ isRTL: false,
45
+
46
+ // Render the month after year in the calendar title
47
+ showMonthAfterYear: false,
48
+
49
+ // Render days of the calendar grid that fall in the next or previous month
50
+ showDaysInNextAndPreviousMonths: false,
51
+
52
+ // Specify a DOM element to render the calendar in
53
+ container: null,
54
+
55
+ // Show clear button
56
+ showClearBtn: false,
57
+
58
+ // internationalization
59
+ i18n: {
60
+ cancel: 'Cancel',
61
+ clear: 'Clear',
62
+ done: 'Ok',
63
+ previousMonth: '‹',
64
+ nextMonth: '›',
65
+ months: [
66
+ 'January',
67
+ 'February',
68
+ 'March',
69
+ 'April',
70
+ 'May',
71
+ 'June',
72
+ 'July',
73
+ 'August',
74
+ 'September',
75
+ 'October',
76
+ 'November',
77
+ 'December'
78
+ ],
79
+ monthsShort: [
80
+ 'Jan',
81
+ 'Feb',
82
+ 'Mar',
83
+ 'Apr',
84
+ 'May',
85
+ 'Jun',
86
+ 'Jul',
87
+ 'Aug',
88
+ 'Sep',
89
+ 'Oct',
90
+ 'Nov',
91
+ 'Dec'
92
+ ],
93
+ weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
94
+ weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
95
+ weekdaysAbbrev: ['S', 'M', 'T', 'W', 'T', 'F', 'S']
96
+ },
97
+
98
+ // events array
99
+ events: [],
100
+
101
+ // callback function
102
+ onSelect: null,
103
+ onOpen: null,
104
+ onClose: null,
105
+ onDraw: null
106
+ };
107
+
108
+ /**
109
+ * @class
110
+ *
111
+ */
112
+ class Datepicker extends Component {
113
+ /**
114
+ * Construct Datepicker instance and set up overlay
115
+ * @constructor
116
+ * @param {Element} el
117
+ * @param {Object} options
118
+ */
119
+ constructor(el, options) {
120
+ super(Datepicker, el, options);
121
+
122
+ this.el.M_Datepicker = this;
123
+
124
+ this.options = $.extend({}, Datepicker.defaults, options);
125
+
126
+ // make sure i18n defaults are not lost when only few i18n option properties are passed
127
+ if (!!options && options.hasOwnProperty('i18n') && typeof options.i18n === 'object') {
128
+ this.options.i18n = $.extend({}, Datepicker.defaults.i18n, options.i18n);
129
+ }
130
+
131
+ // Remove time component from minDate and maxDate options
132
+ if (this.options.minDate) this.options.minDate.setHours(0, 0, 0, 0);
133
+ if (this.options.maxDate) this.options.maxDate.setHours(0, 0, 0, 0);
134
+
135
+ this.id = M.guid();
136
+
137
+ this._setupVariables();
138
+ this._insertHTMLIntoDOM();
139
+ this._setupModal();
140
+
141
+ this._setupEventHandlers();
142
+
143
+ if (!this.options.defaultDate) {
144
+ this.options.defaultDate = new Date(Date.parse(this.el.value));
145
+ }
146
+
147
+ let defDate = this.options.defaultDate;
148
+ if (Datepicker._isDate(defDate)) {
149
+ if (this.options.setDefaultDate) {
150
+ this.setDate(defDate, true);
151
+ this.setInputValue();
152
+ } else {
153
+ this.gotoDate(defDate);
154
+ }
155
+ } else {
156
+ this.gotoDate(new Date());
157
+ }
158
+
159
+ /**
160
+ * Describes open/close state of datepicker
161
+ * @type {Boolean}
162
+ */
163
+ this.isOpen = false;
164
+ }
165
+
166
+ static get defaults() {
167
+ return _defaults;
168
+ }
169
+
170
+ static init(els, options) {
171
+ return super.init(this, els, options);
172
+ }
173
+
174
+ static _isDate(obj) {
175
+ return /Date/.test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime());
176
+ }
177
+
178
+ static _isWeekend(date) {
179
+ let day = date.getDay();
180
+ return day === 0 || day === 6;
181
+ }
182
+
183
+ static _setToStartOfDay(date) {
184
+ if (Datepicker._isDate(date)) date.setHours(0, 0, 0, 0);
185
+ }
186
+
187
+ static _getDaysInMonth(year, month) {
188
+ return [31, Datepicker._isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][
189
+ month
190
+ ];
191
+ }
192
+
193
+ static _isLeapYear(year) {
194
+ // solution by Matti Virkkunen: http://stackoverflow.com/a/4881951
195
+ return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
196
+ }
197
+
198
+ static _compareDates(a, b) {
199
+ // weak date comparison (use setToStartOfDay(date) to ensure correct result)
200
+ return a.getTime() === b.getTime();
201
+ }
202
+
203
+ static _setToStartOfDay(date) {
204
+ if (Datepicker._isDate(date)) date.setHours(0, 0, 0, 0);
205
+ }
206
+
207
+ /**
208
+ * Get Instance
209
+ */
210
+ static getInstance(el) {
211
+ let domElem = !!el.jquery ? el[0] : el;
212
+ return domElem.M_Datepicker;
213
+ }
214
+
215
+ /**
216
+ * Teardown component
217
+ */
218
+ destroy() {
219
+ this._removeEventHandlers();
220
+ this.modal.destroy();
221
+ $(this.modalEl).remove();
222
+ this.destroySelects();
223
+ this.el.M_Datepicker = undefined;
224
+ }
225
+
226
+ destroySelects() {
227
+ let oldYearSelect = this.calendarEl.querySelector('.orig-select-year');
228
+ if (oldYearSelect) {
229
+ M.FormSelect.getInstance(oldYearSelect).destroy();
230
+ }
231
+ let oldMonthSelect = this.calendarEl.querySelector('.orig-select-month');
232
+ if (oldMonthSelect) {
233
+ M.FormSelect.getInstance(oldMonthSelect).destroy();
234
+ }
235
+ }
236
+
237
+ _insertHTMLIntoDOM() {
238
+ if (this.options.showClearBtn) {
239
+ $(this.clearBtn).css({ visibility: '' });
240
+ this.clearBtn.innerHTML = this.options.i18n.clear;
241
+ }
242
+
243
+ this.doneBtn.innerHTML = this.options.i18n.done;
244
+ this.cancelBtn.innerHTML = this.options.i18n.cancel;
245
+
246
+ if (this.options.container) {
247
+ const optEl = this.options.container;
248
+ this.options.container =
249
+ optEl instanceof HTMLElement ? optEl : document.querySelector(optEl);
250
+ this.$modalEl.appendTo(this.options.container);
251
+ } else {
252
+ this.$modalEl.insertBefore(this.el);
253
+ }
254
+ }
255
+
256
+ _setupModal() {
257
+ this.modalEl.id = 'modal-' + this.id;
258
+ this.modal = M.Modal.init(this.modalEl, {
259
+ onCloseEnd: () => {
260
+ this.isOpen = false;
261
+ }
262
+ });
263
+ }
264
+
265
+ toString(format) {
266
+ format = format || this.options.format;
267
+ if (typeof format === 'function') {
268
+ return format(this.date);
269
+ }
270
+
271
+ if (!Datepicker._isDate(this.date)) {
272
+ return '';
273
+ }
274
+
275
+ let formatArray = format.split(/(d{1,4}|m{1,4}|y{4}|yy|!.)/g);
276
+ let formattedDate = formatArray
277
+ .map((label) => {
278
+ if (this.formats[label]) {
279
+ return this.formats[label]();
280
+ }
281
+
282
+ return label;
283
+ })
284
+ .join('');
285
+ return formattedDate;
286
+ }
287
+
288
+ setDate(date, preventOnSelect) {
289
+ if (!date) {
290
+ this.date = null;
291
+ this._renderDateDisplay();
292
+ return this.draw();
293
+ }
294
+ if (typeof date === 'string') {
295
+ date = new Date(Date.parse(date));
296
+ }
297
+ if (!Datepicker._isDate(date)) {
298
+ return;
299
+ }
300
+
301
+ let min = this.options.minDate,
302
+ max = this.options.maxDate;
303
+
304
+ if (Datepicker._isDate(min) && date < min) {
305
+ date = min;
306
+ } else if (Datepicker._isDate(max) && date > max) {
307
+ date = max;
308
+ }
309
+
310
+ this.date = new Date(date.getTime());
311
+
312
+ this._renderDateDisplay();
313
+
314
+ Datepicker._setToStartOfDay(this.date);
315
+ this.gotoDate(this.date);
316
+
317
+ if (!preventOnSelect && typeof this.options.onSelect === 'function') {
318
+ this.options.onSelect.call(this, this.date);
319
+ }
320
+ }
321
+
322
+ setInputValue() {
323
+ this.el.value = this.toString();
324
+ this.$el.trigger('change', { firedBy: this });
325
+ }
326
+
327
+ _renderDateDisplay() {
328
+ let displayDate = Datepicker._isDate(this.date) ? this.date : new Date();
329
+ let i18n = this.options.i18n;
330
+ let day = i18n.weekdaysShort[displayDate.getDay()];
331
+ let month = i18n.monthsShort[displayDate.getMonth()];
332
+ let date = displayDate.getDate();
333
+ this.yearTextEl.innerHTML = displayDate.getFullYear();
334
+ this.dateTextEl.innerHTML = `${day}, ${month} ${date}`;
335
+ }
336
+
337
+ /**
338
+ * change view to a specific date
339
+ */
340
+ gotoDate(date) {
341
+ let newCalendar = true;
342
+
343
+ if (!Datepicker._isDate(date)) {
344
+ return;
345
+ }
346
+
347
+ if (this.calendars) {
348
+ let firstVisibleDate = new Date(this.calendars[0].year, this.calendars[0].month, 1),
349
+ lastVisibleDate = new Date(
350
+ this.calendars[this.calendars.length - 1].year,
351
+ this.calendars[this.calendars.length - 1].month,
352
+ 1
353
+ ),
354
+ visibleDate = date.getTime();
355
+ // get the end of the month
356
+ lastVisibleDate.setMonth(lastVisibleDate.getMonth() + 1);
357
+ lastVisibleDate.setDate(lastVisibleDate.getDate() - 1);
358
+ newCalendar =
359
+ visibleDate < firstVisibleDate.getTime() || lastVisibleDate.getTime() < visibleDate;
360
+ }
361
+
362
+ if (newCalendar) {
363
+ this.calendars = [
364
+ {
365
+ month: date.getMonth(),
366
+ year: date.getFullYear()
367
+ }
368
+ ];
369
+ }
370
+
371
+ this.adjustCalendars();
372
+ }
373
+
374
+ adjustCalendars() {
375
+ this.calendars[0] = this.adjustCalendar(this.calendars[0]);
376
+ this.draw();
377
+ }
378
+
379
+ adjustCalendar(calendar) {
380
+ if (calendar.month < 0) {
381
+ calendar.year -= Math.ceil(Math.abs(calendar.month) / 12);
382
+ calendar.month += 12;
383
+ }
384
+ if (calendar.month > 11) {
385
+ calendar.year += Math.floor(Math.abs(calendar.month) / 12);
386
+ calendar.month -= 12;
387
+ }
388
+ return calendar;
389
+ }
390
+
391
+ nextMonth() {
392
+ this.calendars[0].month++;
393
+ this.adjustCalendars();
394
+ }
395
+
396
+ prevMonth() {
397
+ this.calendars[0].month--;
398
+ this.adjustCalendars();
399
+ }
400
+
401
+ render(year, month, randId) {
402
+ let opts = this.options,
403
+ now = new Date(),
404
+ days = Datepicker._getDaysInMonth(year, month),
405
+ before = new Date(year, month, 1).getDay(),
406
+ data = [],
407
+ row = [];
408
+ Datepicker._setToStartOfDay(now);
409
+ if (opts.firstDay > 0) {
410
+ before -= opts.firstDay;
411
+ if (before < 0) {
412
+ before += 7;
413
+ }
414
+ }
415
+ let previousMonth = month === 0 ? 11 : month - 1,
416
+ nextMonth = month === 11 ? 0 : month + 1,
417
+ yearOfPreviousMonth = month === 0 ? year - 1 : year,
418
+ yearOfNextMonth = month === 11 ? year + 1 : year,
419
+ daysInPreviousMonth = Datepicker._getDaysInMonth(yearOfPreviousMonth, previousMonth);
420
+ let cells = days + before,
421
+ after = cells;
422
+ while (after > 7) {
423
+ after -= 7;
424
+ }
425
+ cells += 7 - after;
426
+ let isWeekSelected = false;
427
+ for (let i = 0, r = 0; i < cells; i++) {
428
+ let day = new Date(year, month, 1 + (i - before)),
429
+ isSelected = Datepicker._isDate(this.date)
430
+ ? Datepicker._compareDates(day, this.date)
431
+ : false,
432
+ isToday = Datepicker._compareDates(day, now),
433
+ hasEvent = opts.events.indexOf(day.toDateString()) !== -1 ? true : false,
434
+ isEmpty = i < before || i >= days + before,
435
+ dayNumber = 1 + (i - before),
436
+ monthNumber = month,
437
+ yearNumber = year,
438
+ isStartRange = opts.startRange && Datepicker._compareDates(opts.startRange, day),
439
+ isEndRange = opts.endRange && Datepicker._compareDates(opts.endRange, day),
440
+ isInRange =
441
+ opts.startRange && opts.endRange && opts.startRange < day && day < opts.endRange,
442
+ isDisabled =
443
+ (opts.minDate && day < opts.minDate) ||
444
+ (opts.maxDate && day > opts.maxDate) ||
445
+ (opts.disableWeekends && Datepicker._isWeekend(day)) ||
446
+ (opts.disableDayFn && opts.disableDayFn(day));
447
+
448
+ if (isEmpty) {
449
+ if (i < before) {
450
+ dayNumber = daysInPreviousMonth + dayNumber;
451
+ monthNumber = previousMonth;
452
+ yearNumber = yearOfPreviousMonth;
453
+ } else {
454
+ dayNumber = dayNumber - days;
455
+ monthNumber = nextMonth;
456
+ yearNumber = yearOfNextMonth;
457
+ }
458
+ }
459
+
460
+ let dayConfig = {
461
+ day: dayNumber,
462
+ month: monthNumber,
463
+ year: yearNumber,
464
+ hasEvent: hasEvent,
465
+ isSelected: isSelected,
466
+ isToday: isToday,
467
+ isDisabled: isDisabled,
468
+ isEmpty: isEmpty,
469
+ isStartRange: isStartRange,
470
+ isEndRange: isEndRange,
471
+ isInRange: isInRange,
472
+ showDaysInNextAndPreviousMonths: opts.showDaysInNextAndPreviousMonths
473
+ };
474
+
475
+ row.push(this.renderDay(dayConfig));
476
+
477
+ if (++r === 7) {
478
+ data.push(this.renderRow(row, opts.isRTL, isWeekSelected));
479
+ row = [];
480
+ r = 0;
481
+ isWeekSelected = false;
482
+ }
483
+ }
484
+ return this.renderTable(opts, data, randId);
485
+ }
486
+
487
+ renderDay(opts) {
488
+ let arr = [];
489
+ let ariaSelected = 'false';
490
+ if (opts.isEmpty) {
491
+ if (opts.showDaysInNextAndPreviousMonths) {
492
+ arr.push('is-outside-current-month');
493
+ arr.push('is-selection-disabled');
494
+ } else {
495
+ return '<td class="is-empty"></td>';
496
+ }
497
+ }
498
+ if (opts.isDisabled) {
499
+ arr.push('is-disabled');
500
+ }
501
+
502
+ if (opts.isToday) {
503
+ arr.push('is-today');
504
+ }
505
+ if (opts.isSelected) {
506
+ arr.push('is-selected');
507
+ ariaSelected = 'true';
508
+ }
509
+ if (opts.hasEvent) {
510
+ arr.push('has-event');
511
+ }
512
+ if (opts.isInRange) {
513
+ arr.push('is-inrange');
514
+ }
515
+ if (opts.isStartRange) {
516
+ arr.push('is-startrange');
517
+ }
518
+ if (opts.isEndRange) {
519
+ arr.push('is-endrange');
520
+ }
521
+ return (
522
+ `<td data-day="${opts.day}" class="${arr.join(' ')}" aria-selected="${ariaSelected}">` +
523
+ `<button class="datepicker-day-button" type="button" data-year="${opts.year}" data-month="${opts.month}" data-day="${opts.day}">${opts.day}</button>` +
524
+ '</td>'
525
+ );
526
+ }
527
+
528
+ renderRow(days, isRTL, isRowSelected) {
529
+ return (
530
+ '<tr class="datepicker-row' +
531
+ (isRowSelected ? ' is-selected' : '') +
532
+ '">' +
533
+ (isRTL ? days.reverse() : days).join('') +
534
+ '</tr>'
535
+ );
536
+ }
537
+
538
+ renderTable(opts, data, randId) {
539
+ return (
540
+ '<div class="datepicker-table-wrapper"><table cellpadding="0" cellspacing="0" class="datepicker-table" role="grid" aria-labelledby="' +
541
+ randId +
542
+ '">' +
543
+ this.renderHead(opts) +
544
+ this.renderBody(data) +
545
+ '</table></div>'
546
+ );
547
+ }
548
+
549
+ renderHead(opts) {
550
+ let i,
551
+ arr = [];
552
+ for (i = 0; i < 7; i++) {
553
+ arr.push(
554
+ `<th scope="col"><abbr title="${this.renderDayName(opts, i)}">${this.renderDayName(
555
+ opts,
556
+ i,
557
+ true
558
+ )}</abbr></th>`
559
+ );
560
+ }
561
+ return '<thead><tr>' + (opts.isRTL ? arr.reverse() : arr).join('') + '</tr></thead>';
562
+ }
563
+
564
+ renderBody(rows) {
565
+ return '<tbody>' + rows.join('') + '</tbody>';
566
+ }
567
+
568
+ renderTitle(instance, c, year, month, refYear, randId) {
569
+ let i,
570
+ j,
571
+ arr,
572
+ opts = this.options,
573
+ isMinYear = year === opts.minYear,
574
+ isMaxYear = year === opts.maxYear,
575
+ html =
576
+ '<div id="' +
577
+ randId +
578
+ '" class="datepicker-controls" role="heading" aria-live="assertive">',
579
+ monthHtml,
580
+ yearHtml,
581
+ prev = true,
582
+ next = true;
583
+
584
+ for (arr = [], i = 0; i < 12; i++) {
585
+ arr.push(
586
+ '<option value="' +
587
+ (year === refYear ? i - c : 12 + i - c) +
588
+ '"' +
589
+ (i === month ? ' selected="selected"' : '') +
590
+ ((isMinYear && i < opts.minMonth) || (isMaxYear && i > opts.maxMonth)
591
+ ? 'disabled="disabled"'
592
+ : '') +
593
+ '>' +
594
+ opts.i18n.months[i] +
595
+ '</option>'
596
+ );
597
+ }
598
+
599
+ monthHtml =
600
+ '<select class="datepicker-select orig-select-month" tabindex="-1">' +
601
+ arr.join('') +
602
+ '</select>';
603
+
604
+ if ($.isArray(opts.yearRange)) {
605
+ i = opts.yearRange[0];
606
+ j = opts.yearRange[1] + 1;
607
+ } else {
608
+ i = year - opts.yearRange;
609
+ j = 1 + year + opts.yearRange;
610
+ }
611
+
612
+ for (arr = []; i < j && i <= opts.maxYear; i++) {
613
+ if (i >= opts.minYear) {
614
+ arr.push(`<option value="${i}" ${i === year ? 'selected="selected"' : ''}>${i}</option>`);
615
+ }
616
+ }
617
+ if (opts.yearRangeReverse) {
618
+ arr.reverse();
619
+ }
620
+
621
+ yearHtml = `<select class="datepicker-select orig-select-year" tabindex="-1">${arr.join(
622
+ ''
623
+ )}</select>`;
624
+
625
+ let leftArrow =
626
+ '<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M15.41 16.09l-4.58-4.59 4.58-4.59L14 5.5l-6 6 6 6z"/><path d="M0-.5h24v24H0z" fill="none"/></svg>';
627
+ html += `<button class="month-prev${
628
+ prev ? '' : ' is-disabled'
629
+ }" type="button">${leftArrow}</button>`;
630
+
631
+ html += '<div class="selects-container">';
632
+ if (opts.showMonthAfterYear) {
633
+ html += yearHtml + monthHtml;
634
+ } else {
635
+ html += monthHtml + yearHtml;
636
+ }
637
+ html += '</div>';
638
+
639
+ if (isMinYear && (month === 0 || opts.minMonth >= month)) {
640
+ prev = false;
641
+ }
642
+
643
+ if (isMaxYear && (month === 11 || opts.maxMonth <= month)) {
644
+ next = false;
645
+ }
646
+
647
+ let rightArrow =
648
+ '<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"/><path d="M0-.25h24v24H0z" fill="none"/></svg>';
649
+ html += `<button class="month-next${
650
+ next ? '' : ' is-disabled'
651
+ }" type="button">${rightArrow}</button>`;
652
+
653
+ return (html += '</div>');
654
+ }
655
+
656
+ /**
657
+ * refresh the HTML
658
+ */
659
+ draw(force) {
660
+ if (!this.isOpen && !force) {
661
+ return;
662
+ }
663
+ let opts = this.options,
664
+ minYear = opts.minYear,
665
+ maxYear = opts.maxYear,
666
+ minMonth = opts.minMonth,
667
+ maxMonth = opts.maxMonth,
668
+ html = '',
669
+ randId;
670
+
671
+ if (this._y <= minYear) {
672
+ this._y = minYear;
673
+ if (!isNaN(minMonth) && this._m < minMonth) {
674
+ this._m = minMonth;
675
+ }
676
+ }
677
+ if (this._y >= maxYear) {
678
+ this._y = maxYear;
679
+ if (!isNaN(maxMonth) && this._m > maxMonth) {
680
+ this._m = maxMonth;
681
+ }
682
+ }
683
+
684
+ randId =
685
+ 'datepicker-title-' +
686
+ Math.random()
687
+ .toString(36)
688
+ .replace(/[^a-z]+/g, '')
689
+ .substr(0, 2);
690
+
691
+ for (let c = 0; c < 1; c++) {
692
+ this._renderDateDisplay();
693
+ html +=
694
+ this.renderTitle(
695
+ this,
696
+ c,
697
+ this.calendars[c].year,
698
+ this.calendars[c].month,
699
+ this.calendars[0].year,
700
+ randId
701
+ ) + this.render(this.calendars[c].year, this.calendars[c].month, randId);
702
+ }
703
+
704
+ this.destroySelects();
705
+
706
+ this.calendarEl.innerHTML = html;
707
+
708
+ // Init Materialize Select
709
+ let yearSelect = this.calendarEl.querySelector('.orig-select-year');
710
+ let monthSelect = this.calendarEl.querySelector('.orig-select-month');
711
+ M.FormSelect.init(yearSelect, {
712
+ classes: 'select-year',
713
+ dropdownOptions: { container: document.body, constrainWidth: false }
714
+ });
715
+ M.FormSelect.init(monthSelect, {
716
+ classes: 'select-month',
717
+ dropdownOptions: { container: document.body, constrainWidth: false }
718
+ });
719
+
720
+ // Add change handlers for select
721
+ yearSelect.addEventListener('change', this._handleYearChange.bind(this));
722
+ monthSelect.addEventListener('change', this._handleMonthChange.bind(this));
723
+
724
+ if (typeof this.options.onDraw === 'function') {
725
+ this.options.onDraw(this);
726
+ }
727
+ }
728
+
729
+ /**
730
+ * Setup Event Handlers
731
+ */
732
+ _setupEventHandlers() {
733
+ this._handleInputKeydownBound = this._handleInputKeydown.bind(this);
734
+ this._handleInputClickBound = this._handleInputClick.bind(this);
735
+ this._handleInputChangeBound = this._handleInputChange.bind(this);
736
+ this._handleCalendarClickBound = this._handleCalendarClick.bind(this);
737
+ this._finishSelectionBound = this._finishSelection.bind(this);
738
+ this._handleMonthChange = this._handleMonthChange.bind(this);
739
+ this._closeBound = this.close.bind(this);
740
+
741
+ this.el.addEventListener('click', this._handleInputClickBound);
742
+ this.el.addEventListener('keydown', this._handleInputKeydownBound);
743
+ this.el.addEventListener('change', this._handleInputChangeBound);
744
+ this.calendarEl.addEventListener('click', this._handleCalendarClickBound);
745
+ this.doneBtn.addEventListener('click', this._finishSelectionBound);
746
+ this.cancelBtn.addEventListener('click', this._closeBound);
747
+
748
+ if (this.options.showClearBtn) {
749
+ this._handleClearClickBound = this._handleClearClick.bind(this);
750
+ this.clearBtn.addEventListener('click', this._handleClearClickBound);
751
+ }
752
+ }
753
+
754
+ _setupVariables() {
755
+ this.$modalEl = $(Datepicker._template);
756
+ this.modalEl = this.$modalEl[0];
757
+
758
+ this.calendarEl = this.modalEl.querySelector('.datepicker-calendar');
759
+
760
+ this.yearTextEl = this.modalEl.querySelector('.year-text');
761
+ this.dateTextEl = this.modalEl.querySelector('.date-text');
762
+ if (this.options.showClearBtn) {
763
+ this.clearBtn = this.modalEl.querySelector('.datepicker-clear');
764
+ }
765
+ this.doneBtn = this.modalEl.querySelector('.datepicker-done');
766
+ this.cancelBtn = this.modalEl.querySelector('.datepicker-cancel');
767
+
768
+ this.formats = {
769
+ d: () => {
770
+ return this.date.getDate();
771
+ },
772
+ dd: () => {
773
+ let d = this.date.getDate();
774
+ return (d < 10 ? '0' : '') + d;
775
+ },
776
+ ddd: () => {
777
+ return this.options.i18n.weekdaysShort[this.date.getDay()];
778
+ },
779
+ dddd: () => {
780
+ return this.options.i18n.weekdays[this.date.getDay()];
781
+ },
782
+ m: () => {
783
+ return this.date.getMonth() + 1;
784
+ },
785
+ mm: () => {
786
+ let m = this.date.getMonth() + 1;
787
+ return (m < 10 ? '0' : '') + m;
788
+ },
789
+ mmm: () => {
790
+ return this.options.i18n.monthsShort[this.date.getMonth()];
791
+ },
792
+ mmmm: () => {
793
+ return this.options.i18n.months[this.date.getMonth()];
794
+ },
795
+ yy: () => {
796
+ return ('' + this.date.getFullYear()).slice(2);
797
+ },
798
+ yyyy: () => {
799
+ return this.date.getFullYear();
800
+ }
801
+ };
802
+ }
803
+
804
+ /**
805
+ * Remove Event Handlers
806
+ */
807
+ _removeEventHandlers() {
808
+ this.el.removeEventListener('click', this._handleInputClickBound);
809
+ this.el.removeEventListener('keydown', this._handleInputKeydownBound);
810
+ this.el.removeEventListener('change', this._handleInputChangeBound);
811
+ this.calendarEl.removeEventListener('click', this._handleCalendarClickBound);
812
+ }
813
+
814
+ _handleInputClick() {
815
+ this.open();
816
+ }
817
+
818
+ _handleInputKeydown(e) {
819
+ if (e.which === M.keys.ENTER) {
820
+ e.preventDefault();
821
+ this.open();
822
+ }
823
+ }
824
+
825
+ _handleCalendarClick(e) {
826
+ if (!this.isOpen) {
827
+ return;
828
+ }
829
+
830
+ let $target = $(e.target);
831
+ if (!$target.hasClass('is-disabled')) {
832
+ if (
833
+ $target.hasClass('datepicker-day-button') &&
834
+ !$target.hasClass('is-empty') &&
835
+ !$target.parent().hasClass('is-disabled')
836
+ ) {
837
+ this.setDate(
838
+ new Date(
839
+ e.target.getAttribute('data-year'),
840
+ e.target.getAttribute('data-month'),
841
+ e.target.getAttribute('data-day')
842
+ )
843
+ );
844
+ if (this.options.autoClose) {
845
+ this._finishSelection();
846
+ }
847
+ } else if ($target.closest('.month-prev').length) {
848
+ this.prevMonth();
849
+ } else if ($target.closest('.month-next').length) {
850
+ this.nextMonth();
851
+ }
852
+ }
853
+ }
854
+
855
+ _handleClearClick() {
856
+ this.date = null;
857
+ this.setInputValue();
858
+ this.close();
859
+ }
860
+
861
+ _handleMonthChange(e) {
862
+ this.gotoMonth(e.target.value);
863
+ }
864
+
865
+ _handleYearChange(e) {
866
+ this.gotoYear(e.target.value);
867
+ }
868
+
869
+ /**
870
+ * change view to a specific month (zero-index, e.g. 0: January)
871
+ */
872
+ gotoMonth(month) {
873
+ if (!isNaN(month)) {
874
+ this.calendars[0].month = parseInt(month, 10);
875
+ this.adjustCalendars();
876
+ }
877
+ }
878
+
879
+ /**
880
+ * change view to a specific full year (e.g. "2012")
881
+ */
882
+ gotoYear(year) {
883
+ if (!isNaN(year)) {
884
+ this.calendars[0].year = parseInt(year, 10);
885
+ this.adjustCalendars();
886
+ }
887
+ }
888
+
889
+ _handleInputChange(e) {
890
+ let date;
891
+
892
+ // Prevent change event from being fired when triggered by the plugin
893
+ if (e.firedBy === this) {
894
+ return;
895
+ }
896
+ if (this.options.parse) {
897
+ date = this.options.parse(this.el.value, this.options.format);
898
+ } else {
899
+ date = new Date(Date.parse(this.el.value));
900
+ }
901
+
902
+ if (Datepicker._isDate(date)) {
903
+ this.setDate(date);
904
+ }
905
+ }
906
+
907
+ renderDayName(opts, day, abbr) {
908
+ day += opts.firstDay;
909
+ while (day >= 7) {
910
+ day -= 7;
911
+ }
912
+ return abbr ? opts.i18n.weekdaysAbbrev[day] : opts.i18n.weekdays[day];
913
+ }
914
+
915
+ /**
916
+ * Set input value to the selected date and close Datepicker
917
+ */
918
+ _finishSelection() {
919
+ this.setInputValue();
920
+ this.close();
921
+ }
922
+
923
+ /**
924
+ * Open Datepicker
925
+ */
926
+ open() {
927
+ if (this.isOpen) {
928
+ return;
929
+ }
930
+
931
+ this.isOpen = true;
932
+ if (typeof this.options.onOpen === 'function') {
933
+ this.options.onOpen.call(this);
934
+ }
935
+ this.draw();
936
+ this.modal.open();
937
+ return this;
938
+ }
939
+
940
+ /**
941
+ * Close Datepicker
942
+ */
943
+ close() {
944
+ if (!this.isOpen) {
945
+ return;
946
+ }
947
+
948
+ this.isOpen = false;
949
+ if (typeof this.options.onClose === 'function') {
950
+ this.options.onClose.call(this);
951
+ }
952
+ this.modal.close();
953
+ return this;
954
+ }
955
+ }
956
+
957
+ Datepicker._template = [
958
+ '<div class= "modal datepicker-modal">',
959
+ '<div class="modal-content datepicker-container">',
960
+ '<div class="datepicker-date-display">',
961
+ '<span class="year-text"></span>',
962
+ '<span class="date-text"></span>',
963
+ '</div>',
964
+ '<div class="datepicker-calendar-container">',
965
+ '<div class="datepicker-calendar"></div>',
966
+ '<div class="datepicker-footer">',
967
+ '<button class="btn-flat datepicker-clear waves-effect" style="visibility: hidden;" type="button"></button>',
968
+ '<div class="confirmation-btns">',
969
+ '<button class="btn-flat datepicker-cancel waves-effect" type="button"></button>',
970
+ '<button class="btn-flat datepicker-done waves-effect" type="button"></button>',
971
+ '</div>',
972
+ '</div>',
973
+ '</div>',
974
+ '</div>',
975
+ '</div>'
976
+ ].join('');
977
+
978
+ M.Datepicker = Datepicker;
979
+
980
+ if (M.jQueryLoaded) {
981
+ M.initializeJqueryWrapper(Datepicker, 'datepicker', 'M_Datepicker');
982
+ }
983
+ })(cash);