Code coverage report for site-build/static/search.js

Statements: 0% (0 / 101)      Branches: 0% (0 / 52)      Functions: 0% (0 / 18)      Lines: 0% (0 / 98)      Ignored: none     

All files » site-build/static/ » search.js
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                                                                                                                                                                                                                                                                                                                               
/*global document, window, XMLHttpRequest, searchIndexPath*/
function setupSearch(searchIndex) {
    var search = document.getElementById('search');
    var searchDropDown = document.getElementById('searchDropDown');
    var searchResults = document.getElementById('searchResults');
 
    var keys = {
        escape: 27,
        up: 38,
        down: 40,
        pageUp: 33,
        pageDown: 34,
        enter: 13,
        tab: 9
    };
 
    var renderedMatches = [];
    var query = '';
    var activeIndex = 0;
 
    searchDropDown.addEventListener('mousedown', function (e) {
        if (e.button === 0 && e.target.hasAttribute('data-index')) {
            var index = e.target.getAttribute('data-index');
            window.location.href = renderedMatches[index].url;
        }
    });
 
    searchDropDown.addEventListener('touchstart', function (e) {
        if (e.target.hasAttribute('data-index')) {
            var index = e.target.getAttribute('data-index');
            window.location.href = renderedMatches[index].url;
        }
    });
 
    function htmlEncode(text) {
        return text.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
    }
 
    function renderMatches(matches) {
        if (matches.length > 0) {
 
            var html = matches.map(function (match, index) {
                return '<li' + (activeIndex === index ? ' class="active"' : '') + ' data-index="' + index + '"'+  '>' + htmlEncode(match.label) + '</li>';
            }).join('');
 
 
            searchResults.innerHTML = html;
            searchDropDown.style.visibility = 'visible';
        } else {
            searchResults.innerHTML = '';
            searchDropDown.style.visibility = 'hidden';
        }
        renderedMatches = matches;
    }
 
    function preventDefault(e) {
        if (e.preventDefault) { e.preventDefault(); }
        return false;
    }
 
    function selectPrevious() {
        var newIndex = (activeIndex - 1) % renderedMatches.length;
 
        if (newIndex < 0) {
            newIndex = renderedMatches.length - 1;
        }
 
        if (activeIndex !== newIndex) {
            activeIndex = newIndex;
            renderMatches(renderedMatches);
        }
 
        return true;
    }
 
    function selectNext() {
        var newIndex = (activeIndex + 1) % renderedMatches.length;
 
        if (activeIndex !== newIndex) {
            activeIndex = newIndex;
            renderMatches(renderedMatches);
        }
 
        return true;
    }
 
    function clearSearch() {
        if (renderedMatches.length > 0) {
            query = '';
            search.value = '';
            renderMatches([]);
        }
    }
 
    function selectActive() {
        if (renderedMatches.length === 0) {
            return false;
        }
 
        window.location.href = renderedMatches[activeIndex].url;
        clearSearch();
 
        return true;
    }
 
    search.addEventListener('keydown', function (e) {
        if (e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) {
            // This is a modified key event. Don't react to those.
            return true;
        }
        switch (e.which) {
        case keys.up:
            if (selectPrevious()) {
                return preventDefault(e);
            }
            break;
        case keys.down:
            if (selectNext()) {
                return preventDefault(e);
            }
            break;
        case keys.enter:
            if (selectActive()) {
                return preventDefault(e);
            }
            break;
        case keys.tab:
            selectActive();
            break;
        }
        return true;
    });
 
    search.addEventListener('blur', function () {
        clearSearch();
    });
 
    search.addEventListener('keyup', function (e) {
        if (e.which === keys.escape) {
            query = '';
            search.value = '';
            renderMatches([]);
            search.blur();
        } else if (e.which !== keys.enter && search.value !== query) {
            query = search.value.toLowerCase();
            var matches = !query ? [] : searchIndex.filter(function (entry) {
                return entry.label.toLowerCase().indexOf(query) !== -1;
            });
 
            activeIndex = 0;
            renderMatches(matches.slice(0, 8));
        }
    });
 
    search.style.visibility = 'visible';
}
 
var getJSON = function(url, successHandler, errorHandler) {
  var xhr = new XMLHttpRequest();
  xhr.open('get', url, true);
  xhr.responseType = 'json';
  xhr.onload = function() {
    var status = xhr.status;
    if (status === 200) {
      if (successHandler) { successHandler(status, xhr.response); }
    } else {
      if (errorHandler) { errorHandler(status); }
    }
  };
  xhr.send();
};
 
getJSON('/searchIndex.json', function(status, data) {
    setupSearch(data);
});