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
| ;(function(window) {
'use strict';
var document = window.document;
function textSearcher(query_selector, input_field, search_count_output, result_class) {
this._init(query_selector, input_field, search_count_output, result_class);
return {
_init: this._init.bind(this),
_search: this._search.bind(this),
_destroy: this._destroy.bind(this),
}
}
textSearcher.prototype = {
_init: function(query_selector, input_field, search_count_output, result_class) {
var document_nodes = document.querySelectorAll(query_selector);
this.searchable_nodes = [];
this.search_instances = [];
for (var node_index = 0; node_index < document_nodes.length; node_index++) {
var node = document_nodes[node_index];
if (node.offsetParent !== null && node.offsetHeight > 0 && node.childNodes.length && node.innerText.length) {
this.searchable_nodes.push(node);
}
}
this.searchable_nodes_length = this.searchable_nodes.length;
if (input_field && (input_field = document.querySelectorAll(input_field)[0])) {
this.input_field = input_field;
this.input_field.addEventListener("keyup", this.searchInputValue.bind(this));
}
if (search_count_output && (search_count_output = document.querySelectorAll(search_count_output)[0])) {
this.search_count_output = search_count_output;
}
this.result_class = result_class || "js-textSearcher-highlight";
return null;
},
_search: function(search_value) {
if (typeof search_value == "undefined") {
if (this.input_field) {
search_value = this.input_field.value;
} else {
console.error("You can only call this method without a value if an input field is bound");
return false;
}
}
var search_value_length = search_value.length,
search_regex = new RegExp(search_value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), "gi"),
node_index = 0;
this.search_count = 0;
var instance_index = 0;
while (instance_index < this.search_instances.length) {
this.search_instances[instance_index].revert();
instance_index++;
}
this.search_instances = [];
if (search_value_length) {
while (node_index < this.searchable_nodes_length) {
var node = this.searchable_nodes[node_index];
var instance = findAndReplaceDOMText(node, {
find: search_regex,
replace: function(portion, match) {
var el = document.createElement('span');
el.className = this.result_class;
el.innerHTML = portion.text;
return el;
}.bind(this)
});
this.search_count += instance.reverts.length;
this.search_instances.push(instance);
node_index++;
}
}
if (this.search_count_output) {
this.search_count_output.textContent = this.search_count;
}
},
_destroy: function() {
if (this.input_field) {
this.input_field.removeEventListener("keyup", this.searchInputValue);
}
},
searchInputValue(event) {
this._search(event.target.value);
}
}
window.textSearcher = textSearcher;
}) (window);
var searcher = new textSearcher("body > .content", ".search-input", ".search-count");
searcher._search(); |
Partager