summaryrefslogtreecommitdiffstats
path: root/extensions/ProdCompSearch/web/js/prod_comp_search.js
blob: f58a8d330d181d9e2b41165bed37e60985aa63d3 (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
/* 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

var ProdCompSearch = {
    script_name: 'enter_bug.cgi',
    script_choices: ['enter_bug.cgi', 'describecomponents.cgi'],
    format: null,
    cloned_bug_id: null
};

YUI({
    base: 'js/yui3/',
    combine: false
}).use("node", "json-stringify", "autocomplete", "escape",
       "datasource-io", "datasource-jsonschema", function (Y) {
    var counter = 0,
        dataSource = null,
        autoComplete = null;

    var resultListFormat = function(query, results) {
        return Y.Array.map(results, function(result) {
            var data = result.raw;
            result.text = data.product + ' :: ' + data.component;
            return Y.Escape.html(result.text);
        });
    };

    var requestTemplate = function(query) {
        counter = counter + 1;
        var json_object = {
            version: "1.1",
            method : "PCS.prod_comp_search",
            id : counter,
            params : { search: query }
        };
        return Y.JSON.stringify(json_object);
    };

    var dataSource = new Y.DataSource.IO({
        source: 'jsonrpc.cgi',
        ioConfig: {
            method: "POST",
            headers: { 'Content-Type': 'application/json' }
        }
    });

    dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator : "result.products",
            resultFields : [ "product", "component" ]
        }
    });

    var input = Y.one('#prod_comp_search');

    input.plug(Y.Plugin.AutoComplete, {
        activateFirstItem: false,
        enableCache: true,
        source: dataSource,
        minQueryLength: 3,
        queryDelay: 0.05,
        resultFormatter: resultListFormat,
        suppressInputUpdate: true,
        maxResults: 25,
        scrollIntoView: true,
        requestTemplate: requestTemplate,
        on: {
            query: function(e) {
                Y.one("#prod_comp_throbber").removeClass('bz_default_hidden');
                Y.one("#prod_comp_no_components").addClass('bz_default_hidden');
            },
            results: function(e) {
                Y.one("#prod_comp_throbber").addClass('bz_default_hidden');
                input.ac.set('activateFirstItem', e.results.length == 1);
                if (e.results.length == 0) {
                    Y.one("#prod_comp_no_components").removeClass('bz_default_hidden');
                }
            },
            select: function(e) {
                // Only redirect if the script_name is a valid choice
                if (Y.Array.indexOf(ProdCompSearch.script_choices, ProdCompSearch.script_name) == -1)
                    return;

                var data = e.result.raw;
                var url = ProdCompSearch.script_name + 
                          "?product=" + encodeURIComponent(data.product) +
                          "&component=" +  encodeURIComponent(data.component);
                if (ProdCompSearch.script_name == 'enter_bug.cgi') {
                    if (ProdCompSearch.format)
                        url += "&format=" + encodeURIComponent(ProdCompSearch.format);
                    if (ProdCompSearch.cloned_bug_id)
                        url += "&cloned_bug_id=" + encodeURIComponent(ProdCompSearch.cloned_bug_id);
                }
                window.location.href = url;
            }
        }
    });

    input.on('focus', function (e) {
        if (e.target.value && e.target.value.length > 3) {
            dataSource.load(e.target.value);
        }
    });
});