NRuby
109 строк · 3.0 Кб
1Search = function(data, input, result) {2this.data = data;3this.input = input;4this.result = result;5
6this.current = null;7this.view = this.result.parentNode;8this.searcher = new Searcher(data.index);9this.init();10}
11
12Search.prototype = Object.assign({}, Navigation, new function() {13var suid = 1;14
15this.init = function() {16var _this = this;17var observer = function(e) {18switch(e.keyCode) {19case 38: // Event.KEY_UP20case 40: // Event.KEY_DOWN21return;22}23_this.search(_this.input.value);24};25this.input.addEventListener('keyup', observer);26this.input.addEventListener('click', observer); // mac's clear field27
28this.searcher.ready(function(results, isLast) {29_this.addResults(results, isLast);30})31
32this.initNavigation();33this.setNavigationActive(false);34}35
36this.search = function(value, selectFirstMatch) {37value = value.trim().toLowerCase();38if (value) {39this.setNavigationActive(true);40} else {41this.setNavigationActive(false);42}43
44if (value == '') {45this.lastQuery = value;46this.result.innerHTML = '';47this.result.setAttribute('aria-expanded', 'false');48this.setNavigationActive(false);49} else if (value != this.lastQuery) {50this.lastQuery = value;51this.result.setAttribute('aria-busy', 'true');52this.result.setAttribute('aria-expanded', 'true');53this.firstRun = true;54this.searcher.find(value);55}56}57
58this.addResults = function(results, isLast) {59var target = this.result;60if (this.firstRun && (results.length > 0 || isLast)) {61this.current = null;62this.result.innerHTML = '';63}64
65for (var i=0, l = results.length; i < l; i++) {66var item = this.renderItem.call(this, results[i]);67item.setAttribute('id', 'search-result-' + target.childElementCount);68target.appendChild(item);69};70
71if (this.firstRun && results.length > 0) {72this.firstRun = false;73this.current = target.firstChild;74this.current.classList.add('search-selected');75}76//TODO: ECMAScript77//if (jQuery.browser.msie) this.$element[0].className += '';78
79if (isLast) this.result.setAttribute('aria-busy', 'false');80}81
82this.move = function(isDown) {83if (!this.current) return;84var next = isDown ? this.current.nextElementSibling : this.current.previousElementSibling;85if (next) {86this.current.classList.remove('search-selected');87next.classList.add('search-selected');88this.input.setAttribute('aria-activedescendant', next.getAttribute('id'));89this.scrollIntoView(next, this.view);90this.current = next;91this.input.value = next.firstChild.firstChild.text;92this.input.select();93}94return true;95}96
97this.hlt = function(html) {98return this.escapeHTML(html).99replace(/\u0001/g, '<em>').100replace(/\u0002/g, '</em>');101}102
103this.escapeHTML = function(html) {104return html.replace(/[&<>]/g, function(c) {105return '&#' + c.charCodeAt(0) + ';';106});107}108
109});110
111