summaryrefslogtreecommitdiffstats
path: root/extensions/ProdCompSearch/web/js/prod_comp_search.js
blob: 1b7e396a83673a775149b02ae587485dc2dc7cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0. */

// Product and component search to file a new bug

$(function() {
    'use strict';

    function hideNotifications(target) {
        var id = '#' + $(target).prop('id');
        var that = $(id);
        if (that.data('counter') === 0)
            that.removeClass('autocomplete-running');
        $(id + '-no_results').hide();
        $(id + '-too_many_results').hide();
        $(id + '-error').hide();
    }

    function searchComplete(query, suggestions) {
        var that = $(this);
        var id = '#' + that.prop('id');

        that.data('counter', that.data('counter') - 1);
        hideNotifications(this);
        if (document.activeElement != this)
            that.devbridgeAutocomplete('hide');
        if (that.data('error')) {
            searchError.call(that[0], null, null, null, that.data('error'));
            that.data('error', '');
        }

        if (suggestions.length === 0) {
            $(id + '-no_results').show();
            $(document).trigger('pcs:no_results', [ that ]);
        }
        else if (suggestions.length > that.data('max_results')) {
            $(id + '-too_many_results').show();
            $(document).trigger('pcs:too_many_results', [ that ]);
        }
        else {
            $(document).trigger('pcs:results', [ that, suggestions ]);
        }
    }

    function searchError(q, jqXHR, textStatus, errorThrown) {
        var that = $(this);
        that.data('counter', that.data('counter') - 1);
        hideNotifications(this);
        if (errorThrown !== 'abort') {
            $('#' + that.attr('id') + '-error').show();
            console.log(errorThrown);
        }
    }

    $('.prod_comp_search')
        .each(function() {
            var that = $(this);
            var params = {
                limit: (that.data('max_results') + 1)
            };
            if (BUGZILLA.api_token) {
                params.Bugzilla_api_token = BUGZILLA.api_token;
            }
            that.devbridgeAutocomplete({
                serviceUrl: function(query) {
                    return 'rest/prod_comp_search/' + encodeURIComponent(query);
                },
                params: params,
                deferRequestBy: 250,
                minChars: 3,
                maxHeight: 500,
                tabDisabled: true,
                autoSelectFirst: true,
                triggerSelectOnValidInput: false,
                width: '',
                transformResult: function(response) {
                    response = $.parseJSON(response);
                    if (response.error) {
                        that.data('error', response.message);
                        return { suggestions: [] };
                    }
                    return {
                        suggestions: $.map(response.products, function(dataItem) {
                            if (dataItem.component) {
                                return {
                                    value: dataItem.product + ' :: ' + dataItem.component,
                                    data : dataItem
                                };
                            }
                            else {
                                return {
                                    value: dataItem.product,
                                    data : dataItem
                                };
                            }
                        })
                    };
                },
                formatResult: function(suggestion, currentValue) {
                    var value = (suggestion.data.component ? suggestion.data.component : suggestion.data.product);
                    var escaped = value.htmlEncode();
                    if (suggestion.data.component) {
                        return '- ' + escaped;
                    }
                    else {
                        return '<b>' + escaped + '</b>';
                    }
                    return suggestion.data.component ? '-&nbsp;' + escaped : escaped;
                },
                beforeRender: function(container) {
                    container.css('min-width', that.outerWidth() - 2 + 'px');
                },
                onSearchStart: function(params) {
                    var that = $(this);
                    params.match = $.trim(params.match);
                    that.addClass('autocomplete-running');
                    that.data('counter', that.data('counter') + 1);
                    that.data('error', '');
                    hideNotifications(this);
                },
                onSearchComplete: searchComplete,
                onSearchError: searchError,
                onSelect: function(suggestion) {
                    var that = $(this);
                    if (that.data('ignore-select'))
                        return;

                    var params = [];
                    if (that.data('format'))
                        params.push('format=' + encodeURIComponent(that.data('format')));
                    if (that.data('cloned_bug_id'))
                        params.push('cloned_bug_id=' + encodeURIComponent(that.data('cloned_bug_id')));
                    params.push('product=' + encodeURIComponent(suggestion.data.product));
                    if (suggestion.data.component)
                        params.push('component=' + encodeURIComponent(suggestion.data.component));

                    var url = that.data('script_name') + '?' + params.join('&');
                    if (that.data('anchor_component') && suggestion.data.component)
                        url += "#" + encodeURIComponent(suggestion.data.component);
                    document.location.href = url;
                }
            });
        })
        .data('counter', 0);
});