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
|
|||
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
|
|||
39 | dataTableJsonRPC('Tag.filter', display_filter, callback); |
||
40 | }, |
||
41 | columns: [ |
||
42 | { data: "name" }, |
||
43 | { |
||
44 | data: null, |
||
45 | sortable: false, |
||
46 | render: function (data, type, full, meta) { |
||
0 ignored issues
–
show
|
|||
47 | if (perm_remove) { |
||
48 | return '<a href="#tags" class="remove-tag" data-name="' + data.name + '"><span class="pficon-error-circle-o"></span></a>'; |
||
49 | } |
||
50 | return ''; |
||
51 | } |
||
52 | }, |
||
53 | ], |
||
54 | dom: "t", |
||
55 | language: { |
||
56 | loadingRecords: '<div class="spinner spinner-lg"></div>', |
||
57 | processing: '<div class="spinner spinner-lg"></div>', |
||
58 | zeroRecords: "No records found" |
||
59 | }, |
||
60 | order: [[ 0, 'asc' ]], |
||
61 | }); |
||
62 | |||
63 | |||
64 | // remove tags button |
||
65 | tags_table.on('draw', function() { |
||
66 | $('.remove-tag').click(function() { |
||
67 | var tr = $(this).parents('tr'); |
||
68 | |||
69 | jsonRPC(model + '.remove_tag', [object_id, $(this).data('name')], function(data) { |
||
0 ignored issues
–
show
|
|||
70 | tags_table.row($(tr)).remove().draw(); |
||
71 | }); |
||
72 | }); |
||
73 | }); |
||
74 | |||
75 | // add tag button and Enter key |
||
76 | $('#add-tag').click(function() { |
||
77 | addTag(model, object_id, $('#id_tags')[0], tags_table); |
||
78 | }); |
||
79 | |||
80 | $('#id_tags').keyup(function(event) { |
||
81 | if (event.keyCode === 13) { |
||
82 | addTag(model, object_id, $('#id_tags')[0], tags_table); |
||
83 | }; |
||
84 | }); |
||
85 | |||
86 | // tag autocomplete |
||
87 | $('#id_tags.typeahead').typeahead({ |
||
88 | minLength: 3, |
||
89 | highlight: true |
||
90 | }, { |
||
91 | name: 'tags-autocomplete', |
||
92 | source: function(query, processSync, processAsync) { |
||
93 | jsonRPC('Tag.filter', {name__startswith: query}, function(data) { |
||
94 | var processedData = []; |
||
95 | data.forEach(function(element) { |
||
96 | processedData.push(element.name); |
||
97 | }); |
||
98 | return processAsync(processedData); |
||
99 | }); |
||
100 | } |
||
101 | }); |
||
102 | } |
||
103 |
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.