aboutsummaryrefslogtreecommitdiffstats
path: root/data/lib/queries.js
blob: ef053518d0f91d0ee61f5c0bc2c51e64ceabb612 (plain) (blame)
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
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";

function getSel() {
  var text = "";
  var selectionObject = window.getSelection();
  if (selectionObject.rangeCount > 0) {
    text = selectionObject.getRangeAt(0).toString().trim();
  }
  return text;
}

/**
 * Opens a new tab with a query for the given text in the selected component
 *
 * @param text
 *          to be searched for
 * @param component
 *          String with the component name (maybe latter regexp?)
 * @param product
 *          (optional) string with the product name, if undefined, search in all
 *          products
 * @return None
 *
 */
function queryInNewTab(text, component, product, equivComps) {
  var urlStr = "https://" + window.location.hostname
      + "/buglist.cgi?query_format=advanced";
  if (product) {
    urlStr += "&product=" + product.trim();
  }
  if (component) {
    if (equivComps) {
      var equivCompsArr = equivComps.filter(function(REstr) {
        return new RegExp(REstr).test(component);
      }, this);
      if (equivCompsArr.length > 0) {
        component = equivCompsArr[0];
      }
    }
    urlStr += "&component=" + component.trim();
  }

  // using more complicated query tables here, because they can be more easily
  // edited
  // for further investigative searches
  if (text) {
    text = encodeURIComponent(text.trim());
    var searchText = "&field0-0-0=longdesc&type0-0-0=substring&value0-0-0="
        + text
        + "&field0-0-1=attach_data.thedata&type0-0-1=substring&value0-0-1="
        + text
        + "&field0-0-2=status_whiteboard&type0-0-2=substring&value0-0-2="
        + text;
    urlStr += searchText;
    self.postMessage(new Message("OpenURLinTab", urlStr)); // utils.js is
    // always avaiulable
  }
}

/**
 * Get the text to search for and prepare other things for the real executive
 * function this.queryInNewTab, and run it.
 */
function queryForSelection() {
  var text = getSel();
  if (!text) {
    self.postMessage(new Message("GetClipboard", "queryLocal"));
  }
  else {
    if (equivalentComponents) {
      queryInNewTab(text, getComponent(), getProduct(),
          equivalentComponents);
    }
    else {
      queryInNewTab(text, getComponent(), getProduct());
    }
  }
}

/**
 *
 */
function queryUpstreamCallback(text, queryUpBug) {
  var searchData = filterByRegexp(queryUpBug, getComponent());
  if (!searchData) {
    return; // not sure why it should happen, but certainly better
  }
  var urlBase = searchData.url;
  text = searchData.searchBy + ":" + searchData.fillIn + " "
      + text.trim();
  if (searchData.fillIn == "$$$") {
    text = text.replace("$$$", getComponent());
  }
  text = encodeURIComponent(text).replace("%20", "+");
  self.postMessage(new Message("OpenURLinTab", urlBase + text));
}

/**
 * Search simple query in the upstream bugzilla appropriate for the component
 *
 * @return none
 */
function queryUpstream(qUpBug) {
  if (!qUpBug) {
    alert("We don't have constantData.queryUpstreamBug set up!");
    return null;
  }
  var text = getSel();
  if (!text) {
    self
        .postMessage(new Message("GetClipboard", "queryUpstream"));
  }
  else {
    queryUpstreamCallback(text, qUpBug);
  }
}

//vim: set ts=2 et sw=2 textwidth=80: