@ng-select/ng-option-highlight
Version:
## Build
104 lines (100 loc) • 2.76 kB
JavaScript
import { Directive, ElementRef, Renderer2, Input, NgModule } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgOptionHighlightDirective {
/**
* @param {?} elementRef
* @param {?} renderer
*/
constructor(elementRef, renderer) {
this.elementRef = elementRef;
this.renderer = renderer;
this.element = this.elementRef.nativeElement;
}
/**
* @return {?}
*/
ngOnChanges() {
if (this._canHighlight) {
this._highlightLabel();
}
}
/**
* @return {?}
*/
ngAfterViewInit() {
this.label = this.element.innerHTML;
if (this._canHighlight) {
this._highlightLabel();
}
}
/**
* @private
* @return {?}
*/
_highlightLabel() {
/** @type {?} */
const label = this.label;
if (!this.term) {
this._setInnerHtml(label);
return;
}
/** @type {?} */
const alternationString = this.term.replace(' ', '|');
/** @type {?} */
const termRegex = new RegExp(alternationString, 'gi');
this._setInnerHtml(label.replace(termRegex, `<span class=\"highlighted\">$&</span>`));
}
/**
* @private
* @return {?}
*/
get _canHighlight() {
return this._isDefined(this.term) && this._isDefined(this.label);
}
/**
* @private
* @param {?} html
* @return {?}
*/
_setInnerHtml(html) {
this.renderer.setProperty(this.elementRef.nativeElement, 'innerHTML', html);
}
/**
* @private
* @param {?} value
* @return {?}
*/
_isDefined(value) {
return value !== undefined && value !== null;
}
}
NgOptionHighlightDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngOptionHighlight]'
},] }
];
/** @nocollapse */
NgOptionHighlightDirective.ctorParameters = () => [
{ type: ElementRef },
{ type: Renderer2 }
];
NgOptionHighlightDirective.propDecorators = {
term: [{ type: Input, args: ['ngOptionHighlight',] }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgOptionHighlightModule {
}
NgOptionHighlightModule.decorators = [
{ type: NgModule, args: [{
declarations: [NgOptionHighlightDirective],
exports: [NgOptionHighlightDirective]
},] }
];
export { NgOptionHighlightDirective, NgOptionHighlightModule };
//# sourceMappingURL=ng-select-ng-option-highlight.js.map