Home | History | Annotate | Download | only in js
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 (function() {
      6   var searchBox = document.getElementById('search_input');
      7   var samples = document.getElementsByClassName('sample');
      8   var SEARCH_PREFIX = 'search:';
      9 
     10   function filterSamples() {
     11     var searchText = searchBox.value.toLowerCase();
     12     window.location.hash = SEARCH_PREFIX + encodeURIComponent(searchText);
     13     for (var i = 0; i < samples.length; ++i) {
     14       var sample = samples[i];
     15       var sampleTitle = '';
     16       if (sample.getElementsByTagName('h2').length > 0)
     17         sampleTitle = sample.getElementsByTagName('h2')[0].textContent;
     18       if (sample.getAttribute('tags').toLowerCase().indexOf(searchText) < 0 &&
     19           sampleTitle.toLowerCase().indexOf(searchText) < 0)
     20         sample.style.display = 'none';
     21       else
     22         sample.style.display = '';
     23     }
     24   }
     25   function updateSearchBox(value) {
     26     searchBox.value = value;
     27     filterSamples();
     28   }
     29   searchBox.addEventListener('search', filterSamples);
     30   searchBox.addEventListener('keyup', filterSamples);
     31 
     32   var apiFilterItems = document.getElementById('api_filter_items');
     33   apiFilterItems.addEventListener('click', function(event) {
     34     if (event.target instanceof HTMLAnchorElement) {
     35       updateSearchBox(event.target.innerText);
     36     }
     37   });
     38 
     39   // If we have a #fragment that corresponds to a search, prefill the search box
     40   // with it.
     41   var fragment = window.location.hash.substr(1);
     42   if (fragment.substr(0, SEARCH_PREFIX.length) == SEARCH_PREFIX) {
     43     updateSearchBox(decodeURIComponent(fragment.substr(SEARCH_PREFIX.length)));
     44   }
     45 })();
     46