static/midcom.datamanager/datepicker.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 2.2

Size

Lines of Code 71
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 11
mnd 6
bc 6
fnc 5
bpm 1.2
cpm 2.2
noi 1
1
function init_datepicker(options)
2
{
3
    $(options.id).datepicker({
4
        maxDate: new Date(options.max_date),
5
        minDate: new Date(options.min_date),
6
        dateFormat: $.datepicker.regional[Object.keys($.datepicker.regional)[Object.keys($.datepicker.regional).length - 1]].dateFormat || $.datepicker.ISO_8601,
7
        altField: options.alt_id,
8
        altFormat: $.datepicker.ISO_8601,
9
        prevText: '',
10
        nextText: '',
11
        showOn: options.showOn,
12
        buttonText: '',
13
        onClose: function() {
14
            $(options.id).next().focus();
15
        }
16
    }).on('change', function() {
17
        if ($(this).val() == '') {
18
            $(options.alt_id).val('');
19
        }
20
    });
21
    if ($(options.alt_id).val() && $(options.alt_id).val() !== '0000-00-00') {
22
        $(options.id).datepicker('setDate', new Date($(options.alt_id).val()));
23
    }
24
25
    if (options.hasOwnProperty('later_than')) {
26
        let pickers = $(options.id + ', ' + options.later_than),
27
            start_max = $(options.later_than).datepicker('option', 'maxDate'),
28
            end_min = $(options.id).datepicker('option', 'minDate');
29
30
        function parse_date(node)
0 ignored issues
show
Bug introduced by
The function parse_date is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var parse_date = function() { /* ... */ }; instead.
Loading history...
31
        {
32
            const instance = $(node).data("datepicker"),
33
                value = $(node).val();
34
35
            return $.datepicker.parseDate(
36
                instance.settings.dateFormat || $.datepicker._defaults.dateFormat,
37
                value, instance.settings
38
            );
39
        }
40
41
        pickers.datepicker('option', 'beforeShow', function (input) {
42
            var default_date = $(input).val(),
43
                other_option, option, other_picker,
44
                date = parse_date(this),
45
                other_date,
46
                config = {defaultDate: default_date};
47
48
            if ('#' + this.id == options.later_than) {
49
                other_option = "minDate";
50
                option = "maxDate";
51
                other_picker = $(options.id);
52
                other_date = parse_date(other_picker);
53
                if (!other_date || other_date > start_max) {
54
                    other_date = start_max
55
                }
56
            } else {
57
                other_option = "maxDate";
58
                option = "minDate";
59
                other_picker = $(options.later_than);
60
                other_date = parse_date(other_picker);
61
                if (!other_date || other_date < end_min) {
62
                    other_date = end_min
63
                }
64
            }
65
66
            config[option] = other_date;
67
            other_picker.datepicker("option", other_option, date);
68
            return config;
69
        });
70
    }
71
}
72