Passed
Push — master ( 1624ba...223f84 )
by Andreas
15:25
created

subform.js ➔ init_subform   C

Complexity

Conditions 11

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 31
dl 0
loc 50
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like subform.js ➔ init_subform often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
function init_subform(id, sortable) {
2
    var container = $('#' + id),
3
        delete_button = $('<a class="button remove-item">-</a>'),
4
        add_button = $('<a class="button add-item">+</a>')
5
            .on('click', function(e) {
6
                e.preventDefault();
7
                add_form(container, add_button, delete_button, sortable);
8
            }),
9
        index = 0;
10
11
    container.on('click', 'a.remove-item', function(e) {
12
        e.preventDefault();
13
        $(this).parent().remove();
14
        if (   container.data('max-count') > 0
15
            && container.data('max-count') >= container.find('fieldset').length
16
            && container.find('.add-item').length === 0) {
17
            container.append(add_button);
18
        }
19
    });
20
21
    container.children().each(function() {
22
        $(this).prepend(delete_button.clone());
23
        index++;
24
    });
25
26
    container.data('index', index);
27
    if (   container.data('max-count') === 0
28
        || container.data('max-count') > index) {
29
        container.append(add_button);
30
    }
31
32
    if (sortable === true) {
33
        container
34
            .sortable({items: '> :not(a.add-item)'})
35
            .on('sortupdate', function() {
36
                $($(this).find('> .ui-sortable-handle').get().reverse()).each(function(index, element) {
37
                    $('#' + element.id + '_score').val(index);
38
                });
39
            });
40
    }
41
42
    add_button.on('click', function() {
43
        // If there is exactly one file selector, we're probably in some sort of attachment list,
44
        // so let's assume the user wants to add a file
45
        // (at some point this should probably be made configurable)
46
        if ($(this).prev().find('input[type="file"]').length === 1) {
47
            $(this).prev().find('input[type="file"]').click();
48
        }
49
    });
50
}
51
52
function add_form(container, add_button, delete_button, sortable) {
53
    var prototype = container.data('prototype'),
54
        index = container.data('index'),
55
        new_form = prototype.replace(/__name__/g, index);
56
    container.data('index', index + 1);
57
    $(new_form)
58
        .prepend(delete_button.clone())
59
        .insertBefore(add_button);
60
61
    if (   container.data('max-count') > 0
62
        && container.data('max-count') >= container.find('> :not(.button.add-item)').length) {
63
        add_button.detach();
64
    }
65
    if (sortable === true) {
66
        container.sortable('refresh');
67
        container.trigger('sortupdate');
68
    }
69
}
70