1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-29 03:52:36 +02:00
TableFilter/src/modules/highlightKeywords.js

151 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-05-24 10:42:11 +02:00
import {createText, createElm, getText} from '../dom';
2016-05-15 04:56:12 +02:00
import {isArray} from '../types';
/**
* Highlight matched keywords upon filtering
*
* @export
* @class HighlightKeyword
*/
2016-03-23 09:02:48 +01:00
export class HighlightKeyword {
/**
* Creates an instance of HighlightKeyword
* @param {TableFilter} tf TableFilter instance
*/
constructor(tf) {
2016-03-23 09:02:48 +01:00
let f = tf.config();
/**
* Css class for highlighted term
* @type {String}
*/
this.highlightCssClass = f.highlight_css_class || 'keyword';
/**
* TableFilter instance
* @type {TableFilter}
*/
this.tf = tf;
/**
* TableFilter's emitter instance
* @type {Emitter}
*/
2015-12-29 07:13:08 +01:00
this.emitter = tf.emitter;
}
/**
* Initializes HighlightKeyword instance
*/
2016-03-23 09:02:48 +01:00
init() {
this.emitter.on(
['before-filtering', 'destroy'],
2016-03-23 09:02:48 +01:00
() => this.unhighlightAll()
);
2016-01-18 07:36:10 +01:00
this.emitter.on(
['highlight-keyword'],
2016-03-23 09:02:48 +01:00
(tf, cell, word) =>
2016-01-18 07:36:10 +01:00
this.highlight(cell, word, this.highlightCssClass)
);
}
/**
* Highlight occurences of searched term in passed node
* @param {Node} node
* @param {String} word Searched term
* @param {String} cssClass Css class name
2016-03-24 02:07:32 +01:00
*
* TODO: refactor this method
*/
2016-03-23 09:02:48 +01:00
highlight(node, word, cssClass) {
// Iterate into this nodes childNodes
2016-03-23 09:02:48 +01:00
if (node.hasChildNodes) {
let children = node.childNodes;
for (let i = 0; i < children.length; i++) {
this.highlight(children[i], word, cssClass);
}
}
2016-03-23 09:02:48 +01:00
if (node.nodeType === 3) {
2016-05-20 10:08:39 +02:00
let tempNodeVal = node.nodeValue.toLowerCase();
let tempWordVal = word.toLowerCase();
2016-10-10 09:16:39 +02:00
2016-03-23 09:02:48 +01:00
if (tempNodeVal.indexOf(tempWordVal) !== -1) {
let pn = node.parentNode;
if (pn && pn.className !== cssClass) {
// word not highlighted yet
2016-03-23 09:02:48 +01:00
let nv = node.nodeValue,
ni = tempNodeVal.indexOf(tempWordVal),
// Create a load of replacement nodes
2016-05-24 10:42:11 +02:00
before = createText(nv.substr(0, ni)),
2016-03-23 09:02:48 +01:00
docWordVal = nv.substr(ni, word.length),
2016-05-24 10:42:11 +02:00
after = createText(nv.substr(ni + word.length)),
hiwordtext = createText(docWordVal),
hiword = createElm('span');
hiword.className = cssClass;
hiword.appendChild(hiwordtext);
2016-03-23 09:02:48 +01:00
pn.insertBefore(before, node);
pn.insertBefore(hiword, node);
pn.insertBefore(after, node);
pn.removeChild(node);
}
}
}
}
/**
* Removes highlight to nodes matching passed string
* @param {String} word
* @param {String} cssClass Css class to remove
*/
2016-03-23 09:02:48 +01:00
unhighlight(word, cssClass) {
let highlightedNodes = this.tf.tbl.querySelectorAll(`.${cssClass}`);
for (let i = 0; i < highlightedNodes.length; i++) {
let n = highlightedNodes[i];
2016-05-24 10:42:11 +02:00
let nodeVal = getText(n),
2016-05-20 10:08:39 +02:00
tempNodeVal = nodeVal.toLowerCase(),
tempWordVal = word.toLowerCase();
2016-03-23 09:02:48 +01:00
if (tempNodeVal.indexOf(tempWordVal) !== -1) {
2016-10-10 09:16:39 +02:00
let parentNode = n.parentNode;
parentNode.replaceChild(createText(nodeVal), n);
parentNode.normalize();
}
}
}
/**
* Clear all occurrences of highlighted nodes
*/
2016-03-23 09:02:48 +01:00
unhighlightAll() {
if (!this.tf.highlightKeywords) {
return;
}
// iterate filters values to unhighlight all values
2016-03-23 09:02:48 +01:00
this.tf.getFiltersValue().forEach((val) => {
2016-05-15 04:56:12 +02:00
if (isArray(val)) {
2016-03-23 09:02:48 +01:00
val.forEach((item) =>
this.unhighlight(item, this.highlightCssClass));
} else {
this.unhighlight(val, this.highlightCssClass);
}
});
}
2015-12-29 07:13:08 +01:00
/**
* Remove feature
*/
2016-03-23 09:02:48 +01:00
destroy() {
this.emitter.off(
['before-filtering', 'destroy'],
2016-03-23 09:02:48 +01:00
() => this.unhighlightAll()
);
2016-01-18 07:36:10 +01:00
this.emitter.off(
['highlight-keyword'],
2016-03-23 09:02:48 +01:00
(tf, cell, word) =>
2016-01-18 07:36:10 +01:00
this.highlight(cell, word, this.highlightCssClass)
);
2015-12-29 07:13:08 +01:00
}
2016-03-23 09:02:48 +01:00
}