Passed
Push — master ( cddcf6...1d3855 )
by Alexander
01:59
created

tcms/static/js/utils.js (1 issue)

1
/*
2
    Used to update a select when something else changes.
3
*/
4
function updateSelect(data, selector, id_attr, value_attr) {
5
    var _select_tag = $(selector)[0];
6
    var new_options = '';
7
8
    // in some cases, e.g. TestRun search, the 1st <option> element is ---
9
    // which must always be there to indicate nothing selected
10
    if (_select_tag.options.length) {
11
        new_options = _select_tag.options[0].outerHTML;
12
    }
13
14
    data.forEach(function(element) {
15
        new_options += '<option value="' + element[id_attr] + '">' + element[value_attr] + '</option>';
16
    });
17
18
    _select_tag.innerHTML = new_options;
19
20
    try {
21
        $(selector).selectpicker('refresh');
22
    } catch(e) {
23
        console.warn(e);
24
    }
25
}
26
27
28
/*
29
    Used for on-change event handlers
30
    @sender - the element trigerring the on-change event
31
*/
32
function update_version_select_from_product(sender, version_selector) {
33
    if (version_selector === undefined) {
34
        version_selector = '#id_version';
35
    }
36
37
    var updateVersionSelectCallback = function(data) {
38
        updateSelect(data, version_selector, 'id', 'value')
39
    }
40
41
    var product_id = $('#id_product').val();
42
    if (product_id) {
43
        jsonRPC('Version.filter', {product: product_id}, updateVersionSelectCallback);
44
    } else {
45
        updateVersionSelectCallback([]);
46
    }
47
}
48
49
/*
50
    Used for on-change event handlers
51
*/
52
function update_build_select_from_product(keep_first) {
53
    var updateCallback = function(data) {
54
        updateSelect(data, '#id_build', 'build_id', 'name')
55
    }
56
57
    if (keep_first === true) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
58
        // pass
59
    } else {
60
        $('#id_build').find('option').remove();
61
    }
62
63
    var product_id = $('#id_product').val();
64
    if (product_id) {
65
        jsonRPC('Build.filter', {product: product_id}, updateCallback);
66
    } else {
67
        updateCallback([]);
68
    }
69
}
70
71
/*
72
    Used for on-change event handlers
73
*/
74
function update_category_select_from_product() {
75
    var updateCallback = function(data) {
76
        updateSelect(data, '#id_category', 'id', 'name')
77
    }
78
79
    var product_id = $('#id_product').val();
80
    if (product_id) {
81
        jsonRPC('Category.filter', {product: product_id}, updateCallback);
82
    } else {
83
        updateCallback([]);
84
    }
85
}
86
87
/*
88
    Used for on-change event handlers
89
*/
90
function update_component_select_from_product() {
91
    var updateCallback = function(data) {
92
        updateSelect(data, '#id_component', 'id', 'name')
93
    }
94
95
    var product_id = $('#id_product').val();
96
    if (product_id) {
97
        jsonRPC('Component.filter', {product: product_id}, updateCallback);
98
    } else {
99
        updateCallback([]);
100
    }
101
}
102
103
/*
104
    Split the input string by comma and return
105
    a list of trimmed values
106
*/
107
function splitByComma(input) {
108
    var result = [];
109
110
    input.split(',').forEach(function(element) {
111
        element = element.trim();
112
        if (element) {
113
            result.push(element);
114
        }
115
    });
116
    return result;
117
}
118
119
/*
120
    Given a params dictionary and a selector update
121
    the dictionary so we can search by tags!
122
    Used in search.js
123
*/
124
function updateParamsToSearchTags(selector, params) {
125
    var tag_list = splitByComma($(selector).val());
126
127
    if (tag_list.length > 0) {
128
        params['tag__name__in'] = tag_list;
129
    };
130
}
131
132
133
/*
134
    Replaces HTML characters for display in DataTables
135
136
    backslash(\), quotes('), double quotes (")
137
    https://github.com/kiwitcms/Kiwi/issues/78
138
139
    angle brackets (<>)
140
    https://github.com/kiwitcms/Kiwi/issues/234
141
*/
142
function escapeHTML(unsafe) {
143
  return unsafe.replace(/[&<>"']/g, function(m) {
144
    return ({
145
      '&': '&amp;',
146
      '<': '&lt;',
147
      '>': '&gt;',
148
      '"': '&quot;',
149
      '\'': '&#039;'
150
    })[m]
151
  });
152
}
153