Loading src/mention/mention.directive.ts +22 −19 Original line number Original line Diff line number Diff line Loading @@ -43,7 +43,7 @@ export class MentionDirective implements OnChanges { // the provided configuration object // the provided configuration object @Input() mentionConfig: MentionConfig = {items:[]}; @Input() mentionConfig: MentionConfig = {items:[]}; private activeConfig: MentionConfig;// = this.DEFAULT_CONFIG; private activeConfig: MentionConfig; private DEFAULT_CONFIG: MentionConfig = { private DEFAULT_CONFIG: MentionConfig = { items: [], items: [], Loading @@ -65,12 +65,12 @@ export class MentionDirective implements OnChanges { private triggerChars:{[key:string]:MentionConfig} = {}; private triggerChars:{[key:string]:MentionConfig} = {}; searchString: string; private searchString: string; startPos: number; private startPos: number; startNode; private startNode; searchList: MentionListComponent; private searchList: MentionListComponent; stopSearch: boolean; private searching: boolean; iframe: any; // optional private iframe: any; // optional constructor( constructor( private _element: ElementRef, private _element: ElementRef, Loading Loading @@ -148,10 +148,7 @@ export class MentionDirective implements OnChanges { blurHandler(event: any) { blurHandler(event: any) { this.stopEvent(event); this.stopEvent(event); this.stopSearch = true; this.stopSearch(); if (this.searchList) { this.searchList.hidden = true; } } } keyHandler(event: any, nativeElement: HTMLInputElement = this._element.nativeElement) { keyHandler(event: any, nativeElement: HTMLInputElement = this._element.nativeElement) { Loading Loading @@ -184,12 +181,12 @@ export class MentionDirective implements OnChanges { this.activeConfig = config; this.activeConfig = config; this.startPos = pos; this.startPos = pos; this.startNode = (this.iframe ? this.iframe.contentWindow.getSelection() : window.getSelection()).anchorNode; this.startNode = (this.iframe ? this.iframe.contentWindow.getSelection() : window.getSelection()).anchorNode; this.stopSearch = false; this.searching = true; this.searchString = null; this.searchString = null; this.showSearchList(nativeElement); this.showSearchList(nativeElement); this.updateSearchList(); this.updateSearchList(); } } else if (this.startPos >= 0 && !this.stopSearch) { else if (this.startPos >= 0 && this.searching) { if (pos <= this.startPos) { if (pos <= this.startPos) { this.searchList.hidden = true; this.searchList.hidden = true; } } Loading @@ -206,14 +203,12 @@ export class MentionDirective implements OnChanges { else if (event.keyCode === KEY_BACKSPACE && pos > 0) { else if (event.keyCode === KEY_BACKSPACE && pos > 0) { pos--; pos--; if (pos==this.startPos) { if (pos==this.startPos) { this.stopSearch = true; this.stopSearch(); } } this.searchList.hidden = this.stopSearch; } } else if (!this.searchList.hidden) { else if (!this.searchList.hidden) { if (event.keyCode === KEY_TAB || event.keyCode === KEY_ENTER) { if (event.keyCode === KEY_TAB || event.keyCode === KEY_ENTER) { this.stopEvent(event); this.stopEvent(event); this.searchList.hidden = true; const text = this.activeConfig.mentionSelect(this.searchList.activeItem); const text = this.activeConfig.mentionSelect(this.searchList.activeItem); // value is inserted without a trailing space for consistency // value is inserted without a trailing space for consistency // between element types (div and iframe do not preserve the space) // between element types (div and iframe do not preserve the space) Loading @@ -225,12 +220,12 @@ export class MentionDirective implements OnChanges { nativeElement.dispatchEvent(evt); nativeElement.dispatchEvent(evt); } } this.startPos = -1; this.startPos = -1; this.stopSearch(); return false; return false; } } else if (event.keyCode === KEY_ESCAPE) { else if (event.keyCode === KEY_ESCAPE) { this.stopEvent(event); this.stopEvent(event); this.searchList.hidden = true; this.stopSearch(); this.stopSearch = true; return false; return false; } } else if (event.keyCode === KEY_DOWN) { else if (event.keyCode === KEY_DOWN) { Loading @@ -249,7 +244,7 @@ export class MentionDirective implements OnChanges { this.stopEvent(event); this.stopEvent(event); return false; return false; } } else { else if (this.searching) { let mention = val.substring(this.startPos + 1, pos); let mention = val.substring(this.startPos + 1, pos); if (event.keyCode !== KEY_BACKSPACE) { if (event.keyCode !== KEY_BACKSPACE) { mention += charPressed; mention += charPressed; Loading @@ -262,6 +257,14 @@ export class MentionDirective implements OnChanges { } } } } stopSearch() { if (this.searchList) { this.searchList.hidden = true; } this.activeConfig = null; this.searching = false; } updateSearchList() { updateSearchList() { let matches: any[] = []; let matches: any[] = []; if (this.activeConfig && this.activeConfig.items) { if (this.activeConfig && this.activeConfig.items) { Loading Loading
src/mention/mention.directive.ts +22 −19 Original line number Original line Diff line number Diff line Loading @@ -43,7 +43,7 @@ export class MentionDirective implements OnChanges { // the provided configuration object // the provided configuration object @Input() mentionConfig: MentionConfig = {items:[]}; @Input() mentionConfig: MentionConfig = {items:[]}; private activeConfig: MentionConfig;// = this.DEFAULT_CONFIG; private activeConfig: MentionConfig; private DEFAULT_CONFIG: MentionConfig = { private DEFAULT_CONFIG: MentionConfig = { items: [], items: [], Loading @@ -65,12 +65,12 @@ export class MentionDirective implements OnChanges { private triggerChars:{[key:string]:MentionConfig} = {}; private triggerChars:{[key:string]:MentionConfig} = {}; searchString: string; private searchString: string; startPos: number; private startPos: number; startNode; private startNode; searchList: MentionListComponent; private searchList: MentionListComponent; stopSearch: boolean; private searching: boolean; iframe: any; // optional private iframe: any; // optional constructor( constructor( private _element: ElementRef, private _element: ElementRef, Loading Loading @@ -148,10 +148,7 @@ export class MentionDirective implements OnChanges { blurHandler(event: any) { blurHandler(event: any) { this.stopEvent(event); this.stopEvent(event); this.stopSearch = true; this.stopSearch(); if (this.searchList) { this.searchList.hidden = true; } } } keyHandler(event: any, nativeElement: HTMLInputElement = this._element.nativeElement) { keyHandler(event: any, nativeElement: HTMLInputElement = this._element.nativeElement) { Loading Loading @@ -184,12 +181,12 @@ export class MentionDirective implements OnChanges { this.activeConfig = config; this.activeConfig = config; this.startPos = pos; this.startPos = pos; this.startNode = (this.iframe ? this.iframe.contentWindow.getSelection() : window.getSelection()).anchorNode; this.startNode = (this.iframe ? this.iframe.contentWindow.getSelection() : window.getSelection()).anchorNode; this.stopSearch = false; this.searching = true; this.searchString = null; this.searchString = null; this.showSearchList(nativeElement); this.showSearchList(nativeElement); this.updateSearchList(); this.updateSearchList(); } } else if (this.startPos >= 0 && !this.stopSearch) { else if (this.startPos >= 0 && this.searching) { if (pos <= this.startPos) { if (pos <= this.startPos) { this.searchList.hidden = true; this.searchList.hidden = true; } } Loading @@ -206,14 +203,12 @@ export class MentionDirective implements OnChanges { else if (event.keyCode === KEY_BACKSPACE && pos > 0) { else if (event.keyCode === KEY_BACKSPACE && pos > 0) { pos--; pos--; if (pos==this.startPos) { if (pos==this.startPos) { this.stopSearch = true; this.stopSearch(); } } this.searchList.hidden = this.stopSearch; } } else if (!this.searchList.hidden) { else if (!this.searchList.hidden) { if (event.keyCode === KEY_TAB || event.keyCode === KEY_ENTER) { if (event.keyCode === KEY_TAB || event.keyCode === KEY_ENTER) { this.stopEvent(event); this.stopEvent(event); this.searchList.hidden = true; const text = this.activeConfig.mentionSelect(this.searchList.activeItem); const text = this.activeConfig.mentionSelect(this.searchList.activeItem); // value is inserted without a trailing space for consistency // value is inserted without a trailing space for consistency // between element types (div and iframe do not preserve the space) // between element types (div and iframe do not preserve the space) Loading @@ -225,12 +220,12 @@ export class MentionDirective implements OnChanges { nativeElement.dispatchEvent(evt); nativeElement.dispatchEvent(evt); } } this.startPos = -1; this.startPos = -1; this.stopSearch(); return false; return false; } } else if (event.keyCode === KEY_ESCAPE) { else if (event.keyCode === KEY_ESCAPE) { this.stopEvent(event); this.stopEvent(event); this.searchList.hidden = true; this.stopSearch(); this.stopSearch = true; return false; return false; } } else if (event.keyCode === KEY_DOWN) { else if (event.keyCode === KEY_DOWN) { Loading @@ -249,7 +244,7 @@ export class MentionDirective implements OnChanges { this.stopEvent(event); this.stopEvent(event); return false; return false; } } else { else if (this.searching) { let mention = val.substring(this.startPos + 1, pos); let mention = val.substring(this.startPos + 1, pos); if (event.keyCode !== KEY_BACKSPACE) { if (event.keyCode !== KEY_BACKSPACE) { mention += charPressed; mention += charPressed; Loading @@ -262,6 +257,14 @@ export class MentionDirective implements OnChanges { } } } } stopSearch() { if (this.searchList) { this.searchList.hidden = true; } this.activeConfig = null; this.searching = false; } updateSearchList() { updateSearchList() { let matches: any[] = []; let matches: any[] = []; if (this.activeConfig && this.activeConfig.items) { if (this.activeConfig && this.activeConfig.items) { Loading