mirror of
https://github.com/koalyptus/TableFilter.git
synced 2026-03-18 00:19:50 +01:00
Initial commit
This commit is contained in:
parent
2faac66509
commit
cda05accb5
10 changed files with 10173 additions and 94 deletions
|
|
@ -2,30 +2,29 @@ import Dom from '../dom';
|
|||
import Str from '../string';
|
||||
import Types from '../types';
|
||||
|
||||
export class HighlightKeyword{
|
||||
export class HighlightKeyword {
|
||||
|
||||
/**
|
||||
* HighlightKeyword, highlight matched keyword
|
||||
* @param {Object} tf TableFilter instance
|
||||
*/
|
||||
constructor(tf) {
|
||||
var f = tf.config();
|
||||
let 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(){
|
||||
init() {
|
||||
this.emitter.on(
|
||||
['before-filtering', 'destroy'],
|
||||
()=> this.unhighlightAll()
|
||||
() => this.unhighlightAll()
|
||||
);
|
||||
this.emitter.on(
|
||||
['highlight-keyword'],
|
||||
(tf, cell, word)=>
|
||||
(tf, cell, word) =>
|
||||
this.highlight(cell, word, this.highlightCssClass)
|
||||
);
|
||||
}
|
||||
|
|
@ -36,37 +35,36 @@ export class HighlightKeyword{
|
|||
* @param {String} word Searched term
|
||||
* @param {String} cssClass Css class name
|
||||
*/
|
||||
highlight(node, word, cssClass){
|
||||
highlight(node, word, cssClass) {
|
||||
// Iterate into this nodes childNodes
|
||||
if(node.hasChildNodes){
|
||||
var children = node.childNodes;
|
||||
for(var i=0; i<children.length; i++){
|
||||
if (node.hasChildNodes) {
|
||||
let children = node.childNodes;
|
||||
for (let 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){
|
||||
if (node.nodeType === 3) {
|
||||
let tempNodeVal = Str.lower(node.nodeValue);
|
||||
let tempWordVal = Str.lower(word);
|
||||
if (tempNodeVal.indexOf(tempWordVal) !== -1) {
|
||||
let pn = node.parentNode;
|
||||
if (pn && pn.className !== cssClass) {
|
||||
// word not highlighted yet
|
||||
var nv = node.nodeValue,
|
||||
let 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)),
|
||||
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.insertBefore(before, node);
|
||||
pn.insertBefore(hiword, node);
|
||||
pn.insertBefore(after, node);
|
||||
pn.removeChild(node);
|
||||
this.highlightedNodes.push(hiword.firstChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -77,63 +75,47 @@ export class HighlightKeyword{
|
|||
* @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),
|
||||
unhighlight(word, cssClass) {
|
||||
let highlightedNodes = this.tf.tbl.querySelectorAll(`.${cssClass}`);
|
||||
for (let i = 0; i < highlightedNodes.length; i++) {
|
||||
let n = highlightedNodes[i];
|
||||
let nodeVal = Dom.getText(n),
|
||||
tempNodeVal = Str.lower(nodeVal),
|
||||
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);
|
||||
}
|
||||
|
||||
if (tempNodeVal.indexOf(tempWordVal) !== -1) {
|
||||
n.parentNode.replaceChild(Dom.text(nodeVal), n);
|
||||
}
|
||||
}
|
||||
for(var k=0; k<arrRemove.length; k++){
|
||||
highlightedNodes.splice(arrRemove[k], 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all occurrences of highlighted nodes
|
||||
*/
|
||||
unhighlightAll(){
|
||||
if(!this.tf.highlightKeywords){
|
||||
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.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(){
|
||||
destroy() {
|
||||
this.emitter.off(
|
||||
['before-filtering', 'destroy'],
|
||||
()=> this.unhighlightAll()
|
||||
() => this.unhighlightAll()
|
||||
);
|
||||
this.emitter.off(
|
||||
['highlight-keyword'],
|
||||
(tf, cell, word)=>
|
||||
(tf, cell, word) =>
|
||||
this.highlight(cell, word, this.highlightCssClass)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue