Passed
Push — master ( 2b96c9...750a3a )
by Alexander
02:50
created

tcms/static/js/tags.js (6 issues)

Severity
1
/*
2
    Applies tag to the chosen model
3
4
    @model - string - model name which accepts tags. There must
5
        be a 'MM.add_tag' RPC function for this to work!
6
    @object_id - int - PK of the object that will be tagged
7
    @tag_input - jQuery object - usually an <input> element which
8
        provides the value used for tagging
9
    @to_table - DataTable object - the table which displays the results
10
*/
11
function addTag(model, object_id, tag_input, to_table) {
12
    var tag_name = tag_input.value;
13
14
    if (tag_name.length > 0) {
15
        jsonRPC(model + '.add_tag', [object_id, tag_name], function(data) {
0 ignored issues
show
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
16
            to_table.row.add({name: tag_name}).draw();
17
            $(tag_input).val('');
18
        });
19
    }
20
}
21
22
23
/*
24
    Displays the tags table inside a card and binds all buttons
25
    and actions for it.
26
27
    @model - string - model name which accepts tags. There must
28
        be a 'MM.add_tag' RPC function for this to work!
29
    @object_id - int - PK of the object that will be tagged
30
    @display_filter - dict - passed directly to `Tag.filter` to display
31
        tags for @object_id
32
    @perm_remove - bool - if we have permission to remove tags
33
34
*/
35
function tagsCard(model, object_id, display_filter, perm_remove) {
36
    // load the tags table
37
    var tags_table = $('#tags').DataTable({
38
        ajax: function(data, callback, settings) {
0 ignored issues
show
The parameter settings is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
39
            dataTableJsonRPC('Tag.filter', display_filter, callback, function(data, callback) {
40
                // b/c tags are now annotated with case, run, plan IDs there are duplicate names.
41
                // Filter them out by only looking at Tag.id uniqueness!
42
                data = arrayToDict(data)
43
                callback({'data': Object.values(data)})
44
            })
45
        },
46
        columns: [
47
            { data: "name" },
48
            {
49
                data: null,
50
                sortable: false,
51
                render: function (data, type, full, meta) {
0 ignored issues
show
The parameter meta is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter full is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter type is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
52
                    if (perm_remove) {
53
                        return '<a href="#tags" class="remove-tag" data-name="' + data.name  + '"><span class="pficon-error-circle-o hidden-print"></span></a>';
54
                    }
55
                    return '';
56
                }
57
            },
58
        ],
59
        dom: "t",
60
        language: {
61
            loadingRecords: '<div class="spinner spinner-lg"></div>',
62
            processing: '<div class="spinner spinner-lg"></div>',
63
            zeroRecords: "No records found"
64
        },
65
        order: [[ 0, 'asc' ]],
66
    });
67
68
69
    // remove tags button
70
    tags_table.on('draw', function() {
71
        $('.remove-tag').click(function() {
72
            var tr = $(this).parents('tr');
73
74
            jsonRPC(model + '.remove_tag', [object_id, $(this).data('name')], function(data) {
0 ignored issues
show
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
75
                tags_table.row($(tr)).remove().draw();
76
            });
77
        });
78
    });
79
80
    // add tag button and Enter key
81
    $('#add-tag').click(function() {
82
        addTag(model, object_id, $('#id_tags')[0], tags_table);
83
    });
84
85
    $('#id_tags').keyup(function(event) {
86
        if (event.keyCode === 13) {
87
            addTag(model, object_id, $('#id_tags')[0], tags_table);
88
        };
89
    });
90
91
    // tag autocomplete
92
    $('#id_tags.typeahead').typeahead({
93
        minLength: 3,
94
        highlight: true
95
        }, {
96
        name: 'tags-autocomplete',
97
        // will display up to X results even if more were returned
98
        limit: 100,
99
        async: true,
100
        display: function(element) {
101
            return element.name;
102
        },
103
        source: function(query, processSync, processAsync) {
104
            jsonRPC('Tag.filter', {name__icontains: query}, function(data) {
105
                // b/c tags are now annotated with case, run, plan IDs there are duplicate names.
106
                // Filter them out by only looking at Tag.id uniqueness!
107
                data = arrayToDict(data)
108
                return processAsync(Object.values(data));
109
            });
110
        }
111
    });
112
}
113