-
-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #441 from ng-select/sanitize-options
Sanitize options
- Loading branch information
Showing
6 changed files
with
62 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,62 @@ | ||
import * as searchHelper from './search-helper'; | ||
import { | ||
AfterViewInit, | ||
Directive, | ||
ElementRef, | ||
Input, | ||
OnChanges, | ||
Renderer2 | ||
Renderer2, | ||
SimpleChanges | ||
} from '@angular/core'; | ||
import { isDefined } from './value-utils'; | ||
|
||
@Directive({ | ||
selector: '[ngOptionHighlight]' | ||
}) | ||
export class NgOptionHighlightDirective implements OnChanges { | ||
export class NgOptionHighlightDirective implements OnChanges, AfterViewInit { | ||
|
||
@Input('ngOptionHighlight') term: string; | ||
@Input('innerHTML') label: any; | ||
|
||
constructor(private elementRef: ElementRef, private renderer: Renderer2) { } | ||
private element: HTMLElement; | ||
private label: string; | ||
|
||
ngOnChanges(): void { | ||
this._highlightLabelWithSearchTerm(); | ||
constructor( | ||
private elementRef: ElementRef, | ||
private renderer: Renderer2) { | ||
this.element = this.elementRef.nativeElement; | ||
} | ||
|
||
private _highlightLabelWithSearchTerm(): void { | ||
const label: string = this.label ? this.label.toString() : ''; | ||
if (!label || !this.term) { | ||
ngOnChanges(changes: SimpleChanges) { | ||
if (isDefined(changes.term.currentValue) && isDefined(this.label)) { | ||
this._highlightLabelWithSearchTerm(); | ||
} | ||
} | ||
|
||
ngAfterViewInit() { | ||
this.label = this.element.innerHTML; | ||
} | ||
|
||
private _highlightLabelWithSearchTerm() { | ||
const label = this.label; | ||
if (!this.term) { | ||
this._setInnerHtml(label); | ||
return; | ||
} | ||
|
||
const indexOfTerm = searchHelper.stripSpecialChars(label) | ||
.toLowerCase() | ||
.indexOf(searchHelper.stripSpecialChars(this.term).toLowerCase()); | ||
if (indexOfTerm > -1) { | ||
this._setInnerHtml( | ||
label.substring(0, indexOfTerm) | ||
+ '<span class=\'highlighted\'>' + label.substr(indexOfTerm, this.term.length) + '</span>' | ||
+ `<span class="highlighted">${label.substr(indexOfTerm, this.term.length)}</span>` | ||
+ label.substring(indexOfTerm + this.term.length, label.length)); | ||
} else { | ||
this._setInnerHtml(label); | ||
} | ||
} | ||
|
||
private _setInnerHtml(html): void { | ||
private _setInnerHtml(html) { | ||
this.renderer.setProperty(this.elementRef.nativeElement, 'innerHTML', html); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters