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 { 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) ); } }