import {Component, Input, OnInit} from "@angular/core";
import {StringUtils} from "../../../utils/string-utils.class";
import {Organization, Project} from "../../../utils/result-preview/result-preview";
export interface Keyword {
field: string,
value: string,
tokenized: boolean
}
@Component({
selector: 'highlight',
template: `
{{token}}
{{token}}
`
})
export class HighlightComponent implements OnInit{
@Input() keywords: Keyword[];
@Input() field: string;
@Input() element: string | Project[] | Organization[];
@Input() html = false;
public text: string;
public separators: string[] = [' ', '-', ',', '.'];
constructor() {
}
ngOnInit(): void {
if(typeof this.element === "string") {
this.text = this.element;
} else {
/*if(this.element instanceof Project) {
this.text = this.element.
}*/
}
}
public split(): string[] {
return StringUtils.split(this.text, this.separators);
}
/**
* Returns true if the word given is matched with any keyword
*
* @param word
*/
isHighlighted(word: string) {
if (this.keywords) {
for (let keyword of this.keywords) {
if (!keyword.field || keyword.field === this.field) {
if (keyword.tokenized && keyword.value.toLowerCase() === word.toLowerCase()) {
return true;
} else if (!keyword.tokenized && word.toLowerCase().includes(keyword.value.toLowerCase())) {
return true;
}
}
}
}
return false;
}
}