summaryrefslogtreecommitdiffstats
path: root/extensions/ProdCompSearch/web/js/prod_comp_search.js
blob: ae735377982ef497af773eac9d24aac6eb963f71 (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
/* 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';
    $('.prod_comp_search').autocomplete({
        minLength: 3,
        delay: 500,
        source: function(request, response) {
            var el = this.element;
            var id = '#' + el.prop('id');
            $(id + '-throbber').show();
            $(id + '-no_components').hide();
            $(id + '-too_many_components').hide();
            $(id + '-error').hide();
            var url = 'rest/prod_comp_search/' + encodeURIComponent(request.term) +
                     '?limit=' + (el.data('max_results') + 1);
            if (BUGZILLA.api_token) {
                url += '&Bugzilla_api_token=' + encodeURIComponent(BUGZILLA.api_token);
            }
            $.ajax({
                url: url,
                contentType: 'application/json'
            })
            .done(function(data) {
                $(id + '-throbber').hide();
                if (data.error) {
                    $(id + '-error').show();
                    console.log(data.message);
                    return false;
                }
                if (data.products.length === 0) {
                    $(id + '-no_components').show();
                }
                else if (data.products.length > el.data('max_results')) {
                    $(id + '-too_many_components').show();
                }
                var current_product = "";
                var prod_comp_array = [];
                var base_params = [];
                if (el.data('format')) {
                    base_params.push('format=' + encodeURIComponent(el.data('format')));
                }
                if (el.data('cloned_bug_id')) {
                    base_params.push('cloned_bug_id=' + encodeURIComponent(el.data('cloned_bug_id')));
                }
                $.each(data.products, function() {
                    var params = base_params.slice();
                    params.push('product=' + encodeURIComponent(this.product));
                    if (this.product != current_product) {
                        prod_comp_array.push({
                            label: this.product,
                            url: el.data('script_name') + '?' + params.join('&')
                        });
                        current_product = this.product;
                    }
                    params.push('component=' + encodeURIComponent(this.component));
                    var url = el.data('script_name') + '?' + params.join('&');
                    if (el.data('anchor_component')) {
                        url += "#" + encodeURIComponent(this.component);
                    }
                    prod_comp_array.push({
                        label: this.product + ' :: ' + this.component,
                        url: url
                    });
                });
                response(prod_comp_array);
            })
            .fail(function(xhr, error_text) {
                if (xhr.responseJSON.error) {
                    error_text = xhr.responseJSON.message;
                }
                $(id + '-throbber').hide();
                $(id + '-comp_error').show();
                console.log(error_text);
            });
        },
        focus: function(event, ui) {
            event.preventDefault();
        },
        select: function(event, ui) {
            event.preventDefault();
            var el = $(this);
            el.val(ui.item.label);
            if (el.data('new_tab')) {
                window.open(ui.item.url, '_blank');
            }
            else {
                window.location.href = ui.item.url;
            }
        }
    })
    .focus(function(event) {
        var el = $(event.target);
        if (el.val().length >= el.autocomplete('option', 'minLength')) {
            el.autocomplete('search');
        }
    });
    $('.prod_comp_search:focus').select();
});