mirror of
https://github.com/koalyptus/TableFilter.git
synced 2024-06-08 08:52:32 +02:00
139 lines
4.5 KiB
JavaScript
139 lines
4.5 KiB
JavaScript
import Dom from '../dom';
|
|
import Str from '../string';
|
|
import Types from '../types';
|
|
|
|
export class HighlightKeyword{
|
|
|
|
/**
|
|
* HighlightKeyword, highlight matched keyword
|
|
* @param {Object} tf TableFilter instance
|
|
*/
|
|
constructor(tf) {
|
|
var f = tf.config();
|
|
//defines css class for highlighting
|
|
this.highlightCssClass = f.highlight_css_class || 'keyword';
|
|
this.highlightedNodes = [];
|
|
|
|
this.tf = tf;
|
|
this.emitter = tf.emitter;
|
|
}
|
|
|
|
init(){
|
|
this.emitter.on(
|
|
['before-filtering', 'destroy'],
|
|
()=> this.unhighlightAll()
|
|
);
|
|
this.emitter.on(
|
|
['highlight-keyword'],
|
|
(tf, cell, word)=>
|
|
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
|
|
*/
|
|
highlight(node, word, cssClass){
|
|
// Iterate into this nodes childNodes
|
|
if(node.hasChildNodes){
|
|
var children = node.childNodes;
|
|
for(var i=0; i<children.length; i++){
|
|
this.highlight(children[i], word, cssClass);
|
|
}
|
|
}
|
|
|
|
if(node.nodeType === 3){
|
|
var tempNodeVal = Str.lower(node.nodeValue);
|
|
var tempWordVal = Str.lower(word);
|
|
if(tempNodeVal.indexOf(tempWordVal) != -1){
|
|
var pn = node.parentNode;
|
|
if(pn && pn.className != cssClass){
|
|
// word not highlighted yet
|
|
var nv = node.nodeValue,
|
|
ni = tempNodeVal.indexOf(tempWordVal),
|
|
// Create a load of replacement nodes
|
|
before = Dom.text(nv.substr(0, ni)),
|
|
docWordVal = nv.substr(ni,word.length),
|
|
after = Dom.text(nv.substr(ni+word.length)),
|
|
hiwordtext = Dom.text(docWordVal),
|
|
hiword = Dom.create('span');
|
|
hiword.className = cssClass;
|
|
hiword.appendChild(hiwordtext);
|
|
pn.insertBefore(before,node);
|
|
pn.insertBefore(hiword,node);
|
|
pn.insertBefore(after,node);
|
|
pn.removeChild(node);
|
|
this.highlightedNodes.push(hiword.firstChild);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes highlight to nodes matching passed string
|
|
* @param {String} word
|
|
* @param {String} cssClass Css class to remove
|
|
*/
|
|
unhighlight(word, cssClass){
|
|
var arrRemove = [];
|
|
var highlightedNodes = this.highlightedNodes;
|
|
for(var i=0; i<highlightedNodes.length; i++){
|
|
var n = highlightedNodes[i];
|
|
if(!n){
|
|
continue;
|
|
}
|
|
var tempNodeVal = Str.lower(n.nodeValue),
|
|
tempWordVal = Str.lower(word);
|
|
if(tempNodeVal.indexOf(tempWordVal) !== -1){
|
|
var pn = n.parentNode;
|
|
if(pn && pn.className === cssClass){
|
|
var prevSib = pn.previousSibling,
|
|
nextSib = pn.nextSibling;
|
|
if(!prevSib || !nextSib){ continue; }
|
|
nextSib.nodeValue = prevSib.nodeValue + n.nodeValue +
|
|
nextSib.nodeValue;
|
|
prevSib.nodeValue = '';
|
|
n.nodeValue = '';
|
|
arrRemove.push(i);
|
|
}
|
|
}
|
|
}
|
|
for(var k=0; k<arrRemove.length; k++){
|
|
highlightedNodes.splice(arrRemove[k], 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear all occurrences of highlighted nodes
|
|
*/
|
|
unhighlightAll(){
|
|
if(!this.tf.highlightKeywords){
|
|
return;
|
|
}
|
|
// iterate filters values to unhighlight all values
|
|
this.tf.getFiltersValue().forEach((val)=> {
|
|
if(Types.isArray(val)){
|
|
val.forEach((item)=>
|
|
this.unhighlight(item, this.highlightCssClass));
|
|
} else {
|
|
this.unhighlight(val, this.highlightCssClass);
|
|
}
|
|
});
|
|
this.highlightedNodes = [];
|
|
}
|
|
|
|
destroy(){
|
|
this.emitter.off(
|
|
['before-filtering', 'destroy'],
|
|
()=> this.unhighlightAll()
|
|
);
|
|
this.emitter.off(
|
|
['highlight-keyword'],
|
|
(tf, cell, word)=>
|
|
this.highlight(cell, word, this.highlightCssClass)
|
|
);
|
|
}
|
|
} |