Skip to content
contact.js 2.36 KiB
Newer Older
Bengfort's avatar
Bengfort committed
(function() {
    var setVisibility = function() {
Bengfort's avatar
Bengfort committed
        var status = document.querySelector('[name="status"]');
        var followupInput = document.querySelector('[name="followup_date"]');
        var followup = followupInput.closest('.form-row');
Bengfort's avatar
Bengfort committed
        if (status.value === '4') {
            followup.hidden = false;
            followupInput.required = true;
Bengfort's avatar
Bengfort committed
            followup.hidden = true;
            followupInput.required = false;
            followupInput.value = '';
Bengfort's avatar
Bengfort committed
    $$.on(document, 'change', '[name="status"]', setVisibility);
Bengfort's avatar
Bengfort committed
    var invited = document.querySelector('[name="status"] [value="3"]');
    var alert = document.querySelector('[data-js="blocking_invitation_alert"]');
    if (!invited.disabled) {
        var setInvitedDisabled = function() {
Bengfort's avatar
Bengfort committed
            var checked = document.querySelector('[name="exclusion_criteria_checked"]');
            var needsCheck = checked && !checked.checked;
            invited.disabled = needsCheck;
Bengfort's avatar
Bengfort committed
            if (alert) {
                alert.hidden = !needsCheck;
            }
Bengfort's avatar
Bengfort committed
        $$.on(document, 'change', '[name="exclusion_criteria_checked"]', setInvitedDisabled);
        setInvitedDisabled();
    }
Bengfort's avatar
Bengfort committed
    $$.on(document, 'change', '[name="exclusion_criteria_checked"]', function() {
Bengfort's avatar
Bengfort committed
        document.querySelector('#criteria-tab .fa-exclamation-triangle').hidden = this.checked;
Bengfort's avatar
Bengfort committed
    if (document.querySelector('#appointments .is-invalid')) {
        $('#appointments-tab').tab('show');
    }

    var getDate = function(selector) {
Bengfort's avatar
Bengfort committed
        var el = document.querySelector(selector);
        return (el && el.dateTime) ? new Date(el.dateTime) : null;
Bengfort's avatar
Bengfort committed
    $$.on(document, 'submit', 'form', function(event) {
        var start = getDate('#sessions-start');
        var end = getDate('#sessions-end');
        var warn = false;
Bengfort's avatar
Bengfort committed
        document.querySelectorAll('[name^="appointment"][type="date"]').forEach(function(el) {
            if (el.value) {
                var date = new Date(el.value)
                if ((start && date < start) || (end && date > end)) {
                    warn = true;
                }
            }
        });
        if (warn && !confirm(django.gettext('Some of the appointments are outside of the planned time period. Do you want to save it anyway?'))) {
            event.preventDefault();
        }
    });
Bengfort's avatar
Bengfort committed
})();