Code Duplication    Length = 53-55 lines in 2 locations

src/admin/views/item/_script.js 1 location

@@ 1-55 (lines=55) @@
1
$('i.glyphicon-refresh-animate').hide();
2
function updateItems(r) {
3
    _opts.items.avaliable = r.avaliable;
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) {
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

src/admin/views/assignment/_script.js 1 location

@@ 1-53 (lines=53) @@
1
$('i.glyphicon-refresh-animate').hide();
2
function updateItems(r) {
3
    _opts.items.avaliable = r.avaliable;
4
    _opts.items.assigned = r.assigned;
5
    search('avaliable');
6
    search('assigned');
7
}
8
9
$('.btn-assign').click(function () {
10
    var $this = $(this);
11
    var target = $this.data('target');
12
    var items = $('select.list[data-target="' + target + '"]').val();
13
14
    if (items.length) {
15
        $this.children('i.glyphicon-refresh-animate').show();
16
        $.post($this.attr('href'), {items: items}, function (r) {
17
            updateItems(r);
18
        }).always(function () {
19
            $this.children('i.glyphicon-refresh-animate').hide();
20
        });
21
    }
22
    return false;
23
});
24
25
$('.search[data-target]').keyup(function () {
26
    search($(this).data('target'));
27
});
28
29
function search(target) {
30
    var $list = $('select.list[data-target="' + target + '"]');
31
    $list.html('');
32
    var q = $('.search[data-target="' + target + '"]').val();
33
34
    var groups = {
35
        role: [$('<optgroup label="Roles">'), false],
36
        permission: [$('<optgroup label="Permission">'), false],
37
    };
38
    $.each(_opts.items[target], function (name, group) {
39
        if (name.indexOf(q) >= 0) {
40
            $('<option>').text(name).val(name).appendTo(groups[group][0]);
41
            groups[group][1] = true;
42
        }
43
    });
44
    $.each(groups, function () {
45
        if (this[1]) {
46
            $list.append(this[0]);
47
        }
48
    });
49
}
50
51
// initial
52
search('avaliable');
53
search('assigned');
54