Completed
Push — master ( 6777bc...42c837 )
by Andreas
21:48
created

static/midcom.datamanager/datepicker.js   A

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 49
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 49
rs 10
wmc 7
mnd 4
bc 4
fnc 3
bpm 1.3333
cpm 2.3333
noi 0
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
    }).on('change', function() {
14
        if ($(this).val() == '') {
15
            $(options.alt_id).val('');
16
        }
17
    });
18
    if ($(options.alt_id).val() && $(options.alt_id).val() !== '0000-00-00') {
19
        $(options.id).datepicker('setDate', new Date($(options.alt_id).val()));
20
    }
21
22
    if (options.hasOwnProperty('later_than')) {
23
        var pickers = $(options.id + ', ' + options.later_than);
24
        pickers.datepicker('option', 'beforeShow', function (input) {
25
            var default_date = $(input).val(),
26
                other_option, option, other_picker,
27
                instance = $(this).data("datepicker"),
28
                date = $.datepicker.parseDate(
29
                    instance.settings.dateFormat ||
30
                        $.datepicker._defaults.dateFormat,
31
                    default_date, instance.settings),
32
                config = {defaultDate: default_date};
33
34
            if ('#' + this.id == options.later_than) {
35
                other_option = "minDate";
36
                option = "maxDate";
37
                other_picker = $(options.id);
38
            } else {
39
                other_option = "maxDate";
40
                option = "minDate";
41
                other_picker = $(options.later_than);
42
            }
43
44
            config[option] = other_picker.val();
45
            other_picker.datepicker("option", other_option, date);
46
            return config;
47
        });
48
    }
49
}
50