2019-01-17 21:41:26 +01:00
|
|
|
/* global define, document, Element, window */
|
2018-08-02 00:18:39 +02:00
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
/* ! Jets.js - v0.14.1 - 2018-06-22
|
2018-08-02 00:18:39 +02:00
|
|
|
* http://NeXTs.github.com/Jets.js/
|
|
|
|
* Copyright (c) 2015 Denis Lukov; Refactored 2018 Paul Anthony Webb; Licensed MIT */
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
(function(root, definition) {
|
2018-08-02 00:18:39 +02:00
|
|
|
if (typeof module !== "undefined") module.exports = definition();
|
|
|
|
else if (typeof define === "function" && typeof define.amd === "object") define(definition);
|
|
|
|
else root["Jets"] = definition();
|
2018-10-06 22:53:01 +02:00
|
|
|
}(this, function() {
|
2018-08-02 00:18:39 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function Jets(opts) {
|
|
|
|
if (!(this instanceof Jets)) return new Jets(opts);
|
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
diacriticsMap: {},
|
|
|
|
hideBy: "display: none",
|
|
|
|
searchSelector: "*AND"
|
|
|
|
};
|
|
|
|
|
|
|
|
const self = this;
|
2018-10-06 22:53:01 +02:00
|
|
|
|
2018-08-02 00:18:39 +02:00
|
|
|
self.options = {};
|
|
|
|
|
|
|
|
[
|
|
|
|
"addImportant",
|
|
|
|
"callSearchManually",
|
|
|
|
"columns",
|
|
|
|
"diacriticsMap",
|
|
|
|
"didSearch",
|
|
|
|
"hideBy",
|
|
|
|
"invert",
|
|
|
|
"manualContentHandling",
|
|
|
|
"searchInSpecificColumn",
|
|
|
|
"searchSelector"
|
|
|
|
].forEach(name => self.options[name] = opts[name] || defaults[name]);
|
|
|
|
|
|
|
|
if (this.options.searchSelector.length > 1) {
|
|
|
|
const searchSelector = self.options["searchSelector"].trim();
|
|
|
|
|
|
|
|
self.options.searchSelector = searchSelector.substr(0, 1);
|
|
|
|
self.options.searchSelectorMode = searchSelector.substr(1).toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.content_tag = document.querySelectorAll(opts.contentTag);
|
|
|
|
if (!self.content_tag) throw new Error("Error! Could not find contentTag element");
|
|
|
|
self.content_param = opts.contentTag;
|
|
|
|
self.search_tag = document.querySelector(opts.searchTag);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!self.search_tag &&
|
|
|
|
!self.options.callSearchManually
|
|
|
|
) throw new Error("Error! Provide one of search methods: searchTag or callSearchManually and call .search(\"phrase\") manually");
|
|
|
|
|
|
|
|
let last_search_query = self.search_tag && self.search_tag.value || "";
|
|
|
|
|
|
|
|
self.search = (search_query, optional_column) => {
|
|
|
|
const new_search_query =
|
|
|
|
self.options.callSearchManually &&
|
2018-10-06 22:53:01 +02:00
|
|
|
typeof search_query !== "undefined" ?
|
|
|
|
search_query :
|
|
|
|
self.search_tag ?
|
|
|
|
self.search_tag.value :
|
|
|
|
"";
|
2018-08-02 00:18:39 +02:00
|
|
|
|
|
|
|
if (last_search_query === (last_search_query = new_search_query)) return;
|
|
|
|
(0, self._applyCSS(last_search_query, optional_column));
|
|
|
|
self.options.didSearch && self.options.didSearch(last_search_query);
|
|
|
|
};
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
self._onSearch = function(event) {
|
2018-08-02 00:18:39 +02:00
|
|
|
if (event.type === "keydown") return setTimeout(self.search, 0);
|
|
|
|
self.search();
|
|
|
|
};
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
self.destroy = function() {
|
2018-08-02 00:18:39 +02:00
|
|
|
if (!self.options.callSearchManually) self._processEventListeners("remove");
|
|
|
|
self._destroy();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!self.options.callSearchManually) self._processEventListeners("add");
|
|
|
|
|
|
|
|
self._addStyleTag();
|
|
|
|
self._setJets();
|
|
|
|
self._applyCSS(last_search_query);
|
|
|
|
}
|
|
|
|
|
|
|
|
Jets.prototype = {
|
|
|
|
constructor: Jets,
|
2018-10-06 22:53:01 +02:00
|
|
|
_processEventListeners: function(action) {
|
2018-08-02 00:18:39 +02:00
|
|
|
[
|
|
|
|
"change",
|
|
|
|
"input",
|
|
|
|
"keydown"
|
2018-10-06 22:53:01 +02:00
|
|
|
].forEach(function(event_type) {
|
2018-08-02 00:18:39 +02:00
|
|
|
this.search_tag[action + "EventListener"](event_type, this._onSearch);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_applyCSS: function(search_query, optional_column) {
|
2018-08-02 00:18:39 +02:00
|
|
|
const options = this.options;
|
|
|
|
|
|
|
|
const search_phrase = this.replaceDiacritics(
|
|
|
|
search_query
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/\s\s+/g, " ")
|
|
|
|
).replace(/\\/g, "\\\\");
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
const words = options.searchSelectorMode ?
|
|
|
|
search_phrase.split(" ").filter((item, pos, arr) => arr.indexOf(item) === pos) :
|
|
|
|
[search_phrase];
|
2018-08-02 00:18:39 +02:00
|
|
|
|
|
|
|
const is_strict_selector = options.searchSelectorMode === "AND";
|
|
|
|
const selectors = new Array(words.length);
|
|
|
|
|
|
|
|
for (let i = 0, ii = words.length; i < ii; i++) {
|
|
|
|
selectors[i] =
|
|
|
|
(is_strict_selector ? this.content_param + ">" : "") +
|
|
|
|
(options.invert ? "" : ":not(") +
|
|
|
|
"[data-jets" + (typeof optional_column !== "undefined" ? "-col-" + optional_column : "") + options.searchSelector + `="${words[i]}"]` +
|
|
|
|
(options.invert ? "" : ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
const hide_rules =
|
|
|
|
options.hideBy
|
|
|
|
.split(";")
|
|
|
|
.filter(Boolean)
|
|
|
|
.map(rule => rule + (options.addImportant ? "!important" : ""));
|
|
|
|
|
|
|
|
const css_rule = (is_strict_selector ? "" : this.content_param + ">") + selectors.join(is_strict_selector ? "," : "") + "{" + hide_rules.join(";") + "}";
|
|
|
|
|
|
|
|
this.styleTag.innerHTML = search_phrase.length ? css_rule : "";
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_addStyleTag: function() {
|
2018-08-02 00:18:39 +02:00
|
|
|
this.styleTag = document.createElement("style");
|
|
|
|
document.head.appendChild(this.styleTag);
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_getText: function(tag) {
|
2018-08-02 00:18:39 +02:00
|
|
|
return tag && (tag.textContent || tag.innerText) || "";
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_sanitize: function(text) {
|
|
|
|
return this.replaceDiacritics(text).trim()
|
|
|
|
.replace(/\s+/g, " ")
|
|
|
|
.toLowerCase();
|
2018-08-02 00:18:39 +02:00
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_getContentTags: function(query) {
|
2018-08-02 00:18:39 +02:00
|
|
|
return Array.prototype.slice.call(this.content_tag).reduce((all, elem) => {
|
|
|
|
return all.concat(Array.prototype.slice.call(elem.querySelectorAll(query || ":scope > *")));
|
|
|
|
}, []);
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_handleSpecificColumns: function(tag, set) {
|
2018-08-02 00:18:39 +02:00
|
|
|
const self = this;
|
2018-10-06 22:53:01 +02:00
|
|
|
|
2018-08-02 00:18:39 +02:00
|
|
|
if (!self.options.searchInSpecificColumn) return;
|
|
|
|
|
|
|
|
Array.prototype.slice.call(tag.children).map((children, i) => {
|
|
|
|
if (
|
|
|
|
self.options.columns &&
|
|
|
|
self.options.columns.length &&
|
|
|
|
self.options.columns.indexOf(i) === -1
|
|
|
|
) return;
|
|
|
|
|
|
|
|
tag[(set || "remove") + "Attribute"]("data-jets-col-" + i, set && self._sanitize(self._getText(children)));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_setJets: function(query, force) {
|
2018-08-02 00:18:39 +02:00
|
|
|
const self = this;
|
|
|
|
const tags = self._getContentTags(force ? "" : query);
|
|
|
|
let text;
|
|
|
|
|
|
|
|
for (const tag of tags) {
|
|
|
|
if (tag.hasAttribute("data-jets") && !force) continue;
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
text = this.options.manualContentHandling ?
|
|
|
|
this.options.manualContentHandling(tag) :
|
|
|
|
self.options.columns &&
|
|
|
|
self.options.columns.length ?
|
|
|
|
self.options.columns.map(column => self._getText(tag.children[column])).join(" ") :
|
|
|
|
self._getText(tag);
|
2018-08-02 00:18:39 +02:00
|
|
|
|
|
|
|
tag.setAttribute("data-jets", self._sanitize(text));
|
|
|
|
self._handleSpecificColumns(tag, "set");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
replaceDiacritics: function(text) {
|
2018-08-02 00:18:39 +02:00
|
|
|
const diacritics = this.options.diacriticsMap;
|
|
|
|
|
|
|
|
for (const letter in diacritics) if (diacritics.hasOwnProperty(letter)) {
|
|
|
|
for (let i = 0, ii = diacritics[letter].length; i < ii; i++) {
|
|
|
|
text = text.replace(new RegExp(diacritics[letter][i], "g"), letter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
update: function(force) {
|
2018-08-02 00:18:39 +02:00
|
|
|
this._setJets(":scope > :not([data-jets])", force);
|
|
|
|
},
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
_destroy: function() {
|
2018-08-02 00:18:39 +02:00
|
|
|
this.styleTag.parentNode && document.head.removeChild(this.styleTag);
|
|
|
|
const tags = this._getContentTags();
|
|
|
|
|
|
|
|
for (const tag of tags) {
|
|
|
|
tag.removeAttribute("data-jets");
|
|
|
|
this._handleSpecificColumns(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// :scope polyfill
|
|
|
|
// https://stackoverflow.com/a/17989803/1221082
|
2018-10-06 22:53:01 +02:00
|
|
|
;(function(doc, proto) {
|
2018-08-02 00:18:39 +02:00
|
|
|
try {
|
|
|
|
doc.querySelector(":scope body");
|
|
|
|
} catch (err) {
|
|
|
|
["querySelector", "querySelectorAll"].forEach(method => {
|
|
|
|
const nativ = proto[method];
|
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
proto[method] = function(selectors) {
|
2018-08-02 00:18:39 +02:00
|
|
|
if (/(^|,)\s*:scope/.test(selectors)) {
|
|
|
|
const id = this.getAttribute("id");
|
2018-10-06 22:53:01 +02:00
|
|
|
|
2018-08-02 00:18:39 +02:00
|
|
|
this.id = "ID_" + Date.now();
|
|
|
|
selectors = selectors.replace(/((^|,)\s*):scope/g, "$1#" + this.getAttribute("id"));
|
|
|
|
|
|
|
|
const result = doc[method](selectors);
|
2018-10-06 22:53:01 +02:00
|
|
|
|
2018-08-02 00:18:39 +02:00
|
|
|
this.id = id;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nativ.call(this, selectors);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})(window.document, Element.prototype);
|
|
|
|
|
|
|
|
return Jets;
|
|
|
|
}));
|