Test Setup Failed
Push — master ( 95bd70...48e609 )
by Xu
35:35
created

src/admin/views/user-item/_script.js   A

Complexity

Total Complexity 11
Complexity/F 1.38

Size

Lines of Code 55
Function Count 8

Duplication

Duplicated Lines 55
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 55
loc 55
rs 10
wmc 11
mnd 1
bc 11
fnc 8
bpm 1.375
cpm 1.375
noi 2

4 Functions

Rating   Name   Duplication   Size   Complexity  
A _script.js ➔ updateItems 6 6 1
A _script.js ➔ search 22 22 1
A $(ꞌ.search[data-target]ꞌ).keyup 3 3 1
A $(ꞌ.btn-assignꞌ).click 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
$('i.glyphicon-refresh-animate').hide();
2
function updateItems(r) {
3
    _opts.items.avaliable = r.avaliable;
0 ignored issues
show
Bug introduced by
The variable _opts seems to be never declared. If this is a global, consider adding a /** global: _opts */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
    _opts.items.assigned = r.assigned;
5
    search('avaliable');
6
    search('assigned');
7
}
8
9
$('.btn-assign').click(function (e) {
10
    e.preventDefault();
11
    var $this = $(this);
12
    var target = $this.data('target');
13
    var items = $('select.list[data-target="' + target + '"]').val();
14
15
    if (items.length) {
16
        $this.children('i.glyphicon-refresh-animate').show();
17
        $.post($this.attr('href'), {items: items}, function (r) {
18
            updateItems(r);
19
        }).always(function () {
20
            $this.children('i.glyphicon-refresh-animate').hide();
21
        });
22
    }
23
    return false;
24
});
25
26
$('.search[data-target]').keyup(function () {
27
    search($(this).data('target'));
28
});
29
30
function search(target) {
31
    var $list = $('select.list[data-target="' + target + '"]');
32
    $list.html('');
33
    var q = $('.search[data-target="' + target + '"]').val();
34
35
    var groups = {
36
        role: [$('<optgroup label="Roles">'), false],
37
        permission: [$('<optgroup label="Permission">'), false],
38
        route: [$('<optgroup label="Routes">'), false],
39
    };
40
    $.each(_opts.items[target], function (name, group) {
0 ignored issues
show
Bug introduced by
The variable _opts seems to be never declared. If this is a global, consider adding a /** global: _opts */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
        if (name.indexOf(q) >= 0) {
42
            $('<option>').text(name).val(name).appendTo(groups[group][0]);
43
            groups[group][1] = true;
44
        }
45
    });
46
    $.each(groups, function () {
47
        if (this[1]) {
48
            $list.append(this[0]);
49
        }
50
    });
51
}
52
53
// initial
54
search('avaliable');
55
search('assigned');
56