-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,77 @@ | ||
const $ = require('jquery') | ||
const datePickerController = require('datepicker') | ||
const django = require('django') | ||
import django from 'django' | ||
import flatpickr from 'flatpickr' | ||
|
||
$(function () { | ||
const $inputs = $('.datepicker') | ||
function link (f0, f1) { | ||
const date = f0.selectedDates[0] | ||
if (date) { | ||
// if p0 has a date set that date as minDate for p1 | ||
f1.set('minDate', f0.formatDate(date, f0.config.dateFormat)) | ||
} | ||
f0.config.onChange.push((selectedDates, dateStr) => { | ||
// if date of p0 is changed adapt minDate for p1 | ||
f1.set('minDate', dateStr) | ||
}) | ||
} | ||
|
||
function linkDatePickers (flatpickrs) { | ||
// normal flatpickers | ||
const idStart = 'id_start_date_date' | ||
const idEnd = 'id_end_date_date' | ||
// (multi-)phase flatpickrs | ||
const phaseIds = [ | ||
'id_phase_set-0-start_date_date', | ||
'id_phase_set-0-end_date_date', | ||
'id_phase_set-1-start_date_date', | ||
'id_phase_set-1-end_date_date', | ||
'id_phase_set-2-start_date_date', | ||
'id_phase_set-2-end_date_date' | ||
] | ||
|
||
// link non-phase datepickers if exist | ||
const fStart = flatpickrs.get(idStart) | ||
const fEnd = flatpickrs.get(idEnd) | ||
if (fStart && fEnd) { | ||
link(fStart, fEnd) | ||
} | ||
|
||
// link phase datepickers if exist | ||
for (let i = 0; i < phaseIds.length - 1; i++) { | ||
if (flatpickrs.length <= i) { | ||
return | ||
} | ||
const p0 = flatpickrs.get(phaseIds[i]) | ||
const p1 = flatpickrs.get(phaseIds[i + 1]) | ||
if (p0 && p1) { | ||
link(p0, p1) | ||
} | ||
} | ||
} | ||
|
||
$inputs.addClass('form-control') | ||
function initDatePicker () { | ||
const datepickers = document.querySelectorAll('.datepicker') | ||
const format = django.get_format('DATE_INPUT_FORMATS')[0].replaceAll('%', '') | ||
const flatpickrs = new Map() | ||
datepickers.forEach((e) => { | ||
e.classList.add('form-control') | ||
const f = flatpickr(e, { dateFormat: format }) | ||
flatpickrs.set(e.id, f) | ||
}) | ||
|
||
linkDatePickers(flatpickrs) | ||
|
||
$inputs.each(function (i, e) { | ||
const initObject = { formElements: {} } | ||
initObject.formElements[e.id] = django.get_format('DATE_INPUT_FORMATS')[0] | ||
datePickerController.createDatePicker(initObject) | ||
const timepickers = document.querySelectorAll('.timepicker') | ||
|
||
timepickers.forEach((e) => { | ||
const f = flatpickr(e, { | ||
defaultHour: e.id.endsWith('start_date_time') ? '00' : '23', | ||
defaultMinute: e.id.endsWith('start_date_time') ? '00' : '59', | ||
dateFormat: 'H:i', | ||
enableTime: true, | ||
noCalendar: true, | ||
time_24hr: true | ||
}) | ||
flatpickrs.set(e.id, f) | ||
}) | ||
}) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', initDatePicker, false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### Changed | ||
|
||
- replaced the old datepicker with flatpickr | ||
- make flatpickr instances aware of each other (e.g. for start and end phase of | ||
a module, you can't choose an end date which is earlier than the start date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters