summaryrefslogtreecommitdiffstats
path: root/extensions/BMO/web/js/choose_product.js
blob: 6fe98097144a7900cc841db8e50d5731fa11212d (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
/* 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. 
 */

YAHOO.bugzilla.prodCompSearch = {
    counter : 0,
    format  : '',
    dataSource : null,
    autoComplete: null,
    generateRequest : function (enteredText) {
      YAHOO.bugzilla.prodCompSearch.counter = YAHOO.bugzilla.prodCompSearch.counter + 1;
      YAHOO.util.Connect.setDefaultPostHeader('application/json', true);
      var json_object = {
          method : "BMO.prod_comp_search",
          id : YAHOO.bugzilla.prodCompSearch.counter,
          params : [ {
            search : decodeURIComponent(enteredText)
          } ]
      };
      YAHOO.util.Dom.removeClass('prod_comp_throbber', 'hidden');
      return YAHOO.lang.JSON.stringify(json_object);
    },
    resultListFormat : function(oResultData, enteredText, sResultMatch) {
        var url  = "enter_bug.cgi?product=" + encodeURIComponent(oResultData[0]) +
                   "&component=" +  encodeURIComponent(oResultData[1]);
        if (YAHOO.bugzilla.prodCompSearch.format) {
            url = url + "&format=" + encodeURIComponent(YAHOO.bugzilla.prodCompSearch.format);
        }
        return ("<a href=\"" + url + "\"><b>" +
                _escapeHTML(oResultData[0]) + "</b> :: " + 
                _escapeHTML(oResultData[1]) + "</a>");
    },
    init_ds : function(){
        this.dataSource = new YAHOO.util.XHRDataSource("jsonrpc.cgi");
        this.dataSource.connTimeout = 30000;
        this.dataSource.connMethodPost = true;
        this.dataSource.connXhrMode = "cancelStaleRequests";
        this.dataSource.maxCacheEntries = 5;
        this.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        this.dataSource.responseSchema = {
            resultsList : "result.products",
            metaFields : { error: "error", jsonRpcId: "id"},
            fields : [ "product", "component" ]
        };
    },
    init : function( field, container, format) {
        if( this.dataSource == null ){
            this.init_ds();
        }
        this.format = format;
        this.autoComplete = new YAHOO.widget.AutoComplete(field, container, this.dataSource);
        this.autoComplete.generateRequest = this.generateRequest;
        this.autoComplete.formatResult = this.resultListFormat;
        this.autoComplete.minQueryLength = 3;
        this.autoComplete.autoHighlight = false;
        this.autoComplete.queryDelay = 0.05;
        this.autoComplete.useIFrame = true;
        this.autoComplete.maxResultsDisplayed = 25;
        this.autoComplete.suppressInputUpdate = true;
        this.autoComplete.doBeforeLoadData = function(sQuery, oResponse, oPayload) {
            YAHOO.util.Dom.addClass('prod_comp_throbber', 'hidden');
            return true;
        };
        this.autoComplete.textboxFocusEvent.subscribe(function () {
            var input = YAHOO.util.Dom.get(field);
            if (input.value && input.value.length > 3) {
                this.sendQuery(input.value);
            }
        });
    }
}