Passed
Push — master ( b00894...f36dd6 )
by Andreas
14:26
created

datepicker.js ➔ init_datepicker   C

Complexity

Conditions 8

Size

Total Lines 57
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 57
rs 6.9813
c 0
b 0
f 0
cc 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        pickers.datepicker('option', 'beforeShow', function (input) {
31
            var default_date = $(input).val(),
32
                other_option, option, other_picker, fallback,
33
                instance = $(this).data("datepicker"),
34
                date = $.datepicker.parseDate(
35
                    instance.settings.dateFormat || $.datepicker._defaults.dateFormat,
36
                    default_date, instance.settings
37
                ),
38
                config = {defaultDate: default_date};
39
40
            if ('#' + this.id == options.later_than) {
41
                other_option = "minDate";
42
                option = "maxDate";
43
                other_picker = $(options.id);
44
                fallback = start_max;
45
            } else {
46
                other_option = "maxDate";
47
                option = "minDate";
48
                other_picker = $(options.later_than);
49
                fallback = end_min;
50
            }
51
52
            config[option] = other_picker.val() || fallback;
53
            other_picker.datepicker("option", other_option, date);
54
            return config;
55
        });
56
    }
57
}
58