sample_search.js revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1/**
2 * Copyright (c) 2010 The Chromium Authors. All rights reserved.  Use of this
3 * source code is governed by a BSD-style license that can be found in the
4 * LICENSE file.
5 */
6
7function testSearchSupport() {
8  var i = document.createElement("input");
9  i.setAttribute("type", "search");
10  return i.type !== "text";
11};
12
13function filterSamples() {
14  var clearlink = document.getElementById('clearlink');
15  var searchinput = document.getElementById('searchinput');
16  var noresults = document.getElementById('noresults');
17
18  var searchtext = searchinput.value.toUpperCase();
19  if (!canclear && searchtext != "" ) {
20    clearlink.style.display = "inline";
21  } else {
22    clearlink.style.display = "none";
23  }
24  if (searchtext == currentfilter) {
25    return;
26  } else {
27    currentfilter = searchtext;
28    window.location.hash = searchinput.value;
29  }
30
31  noresults.style.display = 'none';
32  var num_found = 0;
33  for (var key in search_data) {
34    if (search_data.hasOwnProperty(key)) {
35      var sampleentry = document.getElementById(key);
36      if (search_data[key].indexOf(searchtext) == -1) {
37        sampleentry.style.display = "none";
38      } else {
39        sampleentry.style.display = "block";
40        num_found += 1;
41      }
42    }
43  }
44  if (num_found == 0) {
45    noresults.style.display = 'block';
46  }
47  removeSelected();
48};
49
50function removeSelected() {
51  var anchors = document.getElementsByTagName('a');
52  for (var i = 0, anchor; anchor = anchors[i]; i++) {
53    if (anchor.className == "selected") {
54      anchor.className = "";
55    }
56  }
57};
58
59function setFilter(text, target) {
60  var searchinput = document.getElementById('searchinput');
61  searchinput.value = text;
62  filterSamples();
63  target.className = "selected";
64  searchinput.focus();
65};
66
67function clearFilter() {
68  var searchinput = document.getElementById('searchinput');
69  searchinput.value = "";
70  filterSamples();
71  searchinput.focus();
72};
73
74function initSearch() {
75  var searchinput = document.getElementById('searchinput');
76  if (canclear) {
77    searchinput.addEventListener('click', filterSamples, false);
78  }
79
80  if (window.location.hash.length > 1) {
81    var hash = window.location.hash.substring(1);
82    var elem = document.getElementById(hash);
83    if (elem) {
84      elem.scrollIntoView();
85    } else {
86      setFilter(hash);
87    }
88  }
89};
90
91var currentfilter = "";
92var canclear = testSearchSupport();
93window.addEventListener('load', initSearch, false);
94