summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/js/sample_search.js
blob: 3b950947147ab4f39b77bb5677e77b6f8572b396 (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
/**
 * Copyright (c) 2010 The Chromium Authors. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
 */

function testSearchSupport() {
  var i = document.createElement("input");
  i.setAttribute("type", "search");
  return i.type !== "text";
};

function filterSamples() {
  var clearlink = document.getElementById('clearlink');
  var searchinput = document.getElementById('searchinput');
  var noresults = document.getElementById('noresults');

  var searchtext = searchinput.value.toUpperCase();
  if (!canclear && searchtext != "" ) {
    clearlink.style.display = "inline";
  } else {
    clearlink.style.display = "none";
  }
  if (searchtext == currentfilter) {
    return;
  } else {
    currentfilter = searchtext;
  }

  noresults.style.display = 'none';
  var num_found = 0;
  for (var key in search_data) {
    if (search_data.hasOwnProperty(key)) {
      var sampleentry = document.getElementById(key);
      if (search_data[key].indexOf(searchtext) == -1) {
        sampleentry.style.display = "none";
      } else {
        sampleentry.style.display = "block";
        num_found += 1;
      }
    }
  }
  if (num_found == 0) {
    noresults.style.display = 'block';
  }
  removeSelected();
};

function removeSelected() {
  var anchors = document.getElementsByTagName('a');
  for (var i = 0, anchor; anchor = anchors[i]; i++) {
    if (anchor.className == "selected") {
      anchor.className = "";
    }
  }
};

function setFilter(text, target) {
  var searchinput = document.getElementById('searchinput');
  searchinput.value = text;
  filterSamples();
  target.className = "selected";
  searchinput.focus();
};

function clearFilter() {
  var searchinput = document.getElementById('searchinput');
  searchinput.value = "";
  filterSamples();
  searchinput.focus();
};

function initSearch() {
  var searchinput = document.getElementById('searchinput');
  if (canclear) {
    searchinput.addEventListener('click', filterSamples, false);
  }

  if (window.location.hash.length > 1) {
    setFilter(window.location.hash.substring(1));
  }
};

var currentfilter = "";
var canclear = testSearchSupport();
window.addEventListener('load', initSearch, false);