UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

482 lines (473 loc) 18.6 kB
import { BidiModule } from '@angular/cdk/bidi'; import { OverlayConfig, ConnectionPositionPair, Overlay, OverlayModule } from '@angular/cdk/overlay'; import { DOCUMENT, CommonModule } from '@angular/common'; import { Directive, Injectable, forwardRef, EventEmitter, ElementRef, Component, ChangeDetectionStrategy, Optional, Inject, ChangeDetectorRef, ViewContainerRef, Input, Output, ViewChild, TemplateRef, ViewChildren, ContentChild, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms'; import { NzIconModule } from 'ng-zorro-antd/icon'; import { Subject, merge, fromEvent } from 'rxjs'; import { __decorate, __metadata } from 'tslib'; import { ENTER, LEFT_ARROW, RIGHT_ARROW, TAB, ESCAPE, UP_ARROW, DOWN_ARROW } from '@angular/cdk/keycodes'; import { TemplatePortal } from '@angular/cdk/portal'; import { DEFAULT_MENTION_BOTTOM_POSITIONS, DEFAULT_MENTION_TOP_POSITIONS } from 'ng-zorro-antd/core/overlay'; import { getMentions, getCaretCoordinates, InputBoolean } from 'ng-zorro-antd/core/util'; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ class NzMentionSuggestionDirective { } NzMentionSuggestionDirective.decorators = [ { type: Directive, args: [{ selector: '[nzMentionSuggestion]', exportAs: 'nzMentionSuggestion' },] } ]; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ class NzMentionService { constructor() { this.triggerChange$ = new Subject(); } triggerChanged() { return this.triggerChange$.asObservable(); } registerTrigger(trigger) { if (this.trigger !== trigger) { this.trigger = trigger; this.triggerChange$.next(trigger); } } ngOnDestroy() { this.triggerChange$.complete(); } } NzMentionService.decorators = [ { type: Injectable } ]; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ const NZ_MENTION_TRIGGER_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzMentionTriggerDirective), multi: true }; class NzMentionTriggerDirective { constructor(el, nzMentionService) { this.el = el; this.nzMentionService = nzMentionService; this.onChange = () => { }; this.onTouched = () => { }; this.onFocusin = new EventEmitter(); this.onBlur = new EventEmitter(); this.onInput = new EventEmitter(); this.onKeydown = new EventEmitter(); this.onClick = new EventEmitter(); } completeEvents() { this.onFocusin.complete(); this.onBlur.complete(); this.onInput.complete(); this.onKeydown.complete(); this.onClick.complete(); } focus(caretPos) { this.el.nativeElement.focus(); this.el.nativeElement.setSelectionRange(caretPos, caretPos); } insertMention(mention) { const value = this.el.nativeElement.value; const insertValue = mention.mention.trim() + ' '; const newValue = [value.slice(0, mention.startPos + 1), insertValue, value.slice(mention.endPos, value.length)].join(''); this.el.nativeElement.value = newValue; this.focus(mention.startPos + insertValue.length + 1); this.onChange(newValue); this.value = newValue; } writeValue(value) { this.value = value; if (typeof value === 'string') { this.el.nativeElement.value = value; } else { this.el.nativeElement.value = ''; } } registerOnChange(fn) { this.onChange = fn; } registerOnTouched(fn) { this.onTouched = fn; } ngAfterViewInit() { this.nzMentionService.registerTrigger(this); } ngOnDestroy() { this.completeEvents(); } } NzMentionTriggerDirective.decorators = [ { type: Directive, args: [{ selector: 'input[nzMentionTrigger], textarea[nzMentionTrigger]', exportAs: 'nzMentionTrigger', providers: [NZ_MENTION_TRIGGER_ACCESSOR], host: { autocomplete: 'off', '(focusin)': 'onFocusin.emit()', '(blur)': 'onBlur.emit()', '(input)': 'onInput.emit($event)', '(keydown)': 'onKeydown.emit($event)', '(click)': 'onClick.emit($event)' } },] } ]; NzMentionTriggerDirective.ctorParameters = () => [ { type: ElementRef }, { type: NzMentionService } ]; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ class NzMentionComponent { constructor(ngDocument, cdr, overlay, viewContainerRef, nzMentionService) { this.ngDocument = ngDocument; this.cdr = cdr; this.overlay = overlay; this.viewContainerRef = viewContainerRef; this.nzMentionService = nzMentionService; this.nzValueWith = value => value; this.nzPrefix = '@'; this.nzLoading = false; this.nzNotFoundContent = '无匹配结果,轻敲空格完成输入'; this.nzPlacement = 'bottom'; this.nzSuggestions = []; this.nzOnSelect = new EventEmitter(); this.nzOnSearchChange = new EventEmitter(); this.isOpen = false; this.filteredSuggestions = []; this.suggestionTemplate = null; this.activeIndex = -1; this.previousValue = null; this.cursorMention = null; this.overlayRef = null; } set suggestionChild(value) { if (value) { this.suggestionTemplate = value; } } get triggerNativeElement() { return this.trigger.el.nativeElement; } get focusItemElement() { var _a; const itemArr = (_a = this.items) === null || _a === void 0 ? void 0 : _a.toArray(); if (itemArr && itemArr[this.activeIndex]) { return itemArr[this.activeIndex].nativeElement; } return null; } ngOnInit() { this.nzMentionService.triggerChanged().subscribe(trigger => { this.trigger = trigger; this.bindTriggerEvents(); this.closeDropdown(); this.overlayRef = null; }); } ngOnChanges(changes) { if (changes.hasOwnProperty('nzSuggestions')) { if (this.isOpen) { this.previousValue = null; this.activeIndex = -1; this.resetDropdown(false); } } } ngOnDestroy() { this.closeDropdown(); } closeDropdown() { if (this.overlayRef && this.overlayRef.hasAttached()) { this.overlayRef.detach(); this.overlayOutsideClickSubscription.unsubscribe(); this.isOpen = false; this.cdr.markForCheck(); } } openDropdown() { this.attachOverlay(); this.isOpen = true; this.cdr.markForCheck(); } getMentions() { return this.trigger ? getMentions(this.trigger.value, this.nzPrefix) : []; } selectSuggestion(suggestion) { const value = this.nzValueWith(suggestion); this.trigger.insertMention({ mention: value, startPos: this.cursorMentionStart, endPos: this.cursorMentionEnd }); this.nzOnSelect.emit(suggestion); this.closeDropdown(); this.activeIndex = -1; } handleInput(event) { const target = event.target; this.trigger.onChange(target.value); this.trigger.value = target.value; this.resetDropdown(); } handleKeydown(event) { const keyCode = event.keyCode; if (this.isOpen && keyCode === ENTER && this.activeIndex !== -1 && this.filteredSuggestions.length) { this.selectSuggestion(this.filteredSuggestions[this.activeIndex]); event.preventDefault(); } else if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) { this.resetDropdown(); event.stopPropagation(); } else { if (this.isOpen && (keyCode === TAB || keyCode === ESCAPE)) { this.closeDropdown(); return; } if (this.isOpen && keyCode === UP_ARROW) { this.setPreviousItemActive(); event.preventDefault(); event.stopPropagation(); } if (this.isOpen && keyCode === DOWN_ARROW) { this.setNextItemActive(); event.preventDefault(); event.stopPropagation(); } } } handleClick() { this.resetDropdown(); } bindTriggerEvents() { this.trigger.onInput.subscribe((e) => this.handleInput(e)); this.trigger.onKeydown.subscribe((e) => this.handleKeydown(e)); this.trigger.onClick.subscribe(() => this.handleClick()); } suggestionsFilter(value, emit) { const suggestions = value.substring(1); /** * Should always emit (nzOnSearchChange) when value empty * * @[something]... @[empty]... @[empty] * ^ ^ ^ * preValue preValue (should emit) */ if (this.previousValue === value && value !== this.cursorMention[0]) { return; } this.previousValue = value; if (emit) { this.nzOnSearchChange.emit({ value: this.cursorMention.substring(1), prefix: this.cursorMention[0] }); } const searchValue = suggestions.toLowerCase(); this.filteredSuggestions = this.nzSuggestions.filter(suggestion => this.nzValueWith(suggestion).toLowerCase().includes(searchValue)); } resetDropdown(emit = true) { this.resetCursorMention(); if (typeof this.cursorMention !== 'string' || !this.canOpen()) { this.closeDropdown(); return; } this.suggestionsFilter(this.cursorMention, emit); const activeIndex = this.filteredSuggestions.indexOf(this.cursorMention.substring(1)); this.activeIndex = activeIndex >= 0 ? activeIndex : 0; this.openDropdown(); } setNextItemActive() { this.activeIndex = this.activeIndex + 1 <= this.filteredSuggestions.length - 1 ? this.activeIndex + 1 : 0; this.cdr.markForCheck(); this.scrollToFocusItem(); } setPreviousItemActive() { this.activeIndex = this.activeIndex - 1 < 0 ? this.filteredSuggestions.length - 1 : this.activeIndex - 1; this.cdr.markForCheck(); this.scrollToFocusItem(); } scrollToFocusItem() { if (this.focusItemElement) { this.focusItemElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' }); } } canOpen() { const element = this.triggerNativeElement; return !element.readOnly && !element.disabled; } resetCursorMention() { const value = this.triggerNativeElement.value.replace(/[\r\n]/g, ' ') || ''; const selectionStart = this.triggerNativeElement.selectionStart; const prefix = typeof this.nzPrefix === 'string' ? [this.nzPrefix] : this.nzPrefix; let i = prefix.length; while (i >= 0) { const startPos = value.lastIndexOf(prefix[i], selectionStart); const endPos = value.indexOf(' ', selectionStart) > -1 ? value.indexOf(' ', selectionStart) : value.length; const mention = value.substring(startPos, endPos); if ((startPos > 0 && value[startPos - 1] !== ' ') || startPos < 0 || mention.includes(prefix[i], 1) || mention.includes(' ')) { this.cursorMention = null; this.cursorMentionStart = -1; this.cursorMentionEnd = -1; } else { this.cursorMention = mention; this.cursorMentionStart = startPos; this.cursorMentionEnd = endPos; return; } i--; } } updatePositions() { const coordinates = getCaretCoordinates(this.triggerNativeElement, this.cursorMentionStart); const top = coordinates.top - this.triggerNativeElement.getBoundingClientRect().height - this.triggerNativeElement.scrollTop + (this.nzPlacement === 'bottom' ? coordinates.height - 6 : -6); const left = coordinates.left - this.triggerNativeElement.scrollLeft; this.positionStrategy.withDefaultOffsetX(left).withDefaultOffsetY(top); if (this.nzPlacement === 'bottom') { this.positionStrategy.withPositions([...DEFAULT_MENTION_BOTTOM_POSITIONS]); } if (this.nzPlacement === 'top') { this.positionStrategy.withPositions([...DEFAULT_MENTION_TOP_POSITIONS]); } this.positionStrategy.apply(); } subscribeOverlayOutsideClick() { return merge(this.overlayRef.outsidePointerEvents(), fromEvent(this.ngDocument, 'touchend')).subscribe((event) => { var _a; const clickTarget = event.target; if (this.isOpen && clickTarget !== this.trigger.el.nativeElement && !((_a = this.overlayRef) === null || _a === void 0 ? void 0 : _a.overlayElement.contains(clickTarget))) { this.closeDropdown(); } }); } attachOverlay() { if (!this.overlayRef) { this.portal = new TemplatePortal(this.suggestionsTemp, this.viewContainerRef); this.overlayRef = this.overlay.create(this.getOverlayConfig()); } if (this.overlayRef && !this.overlayRef.hasAttached()) { this.overlayRef.attach(this.portal); this.overlayOutsideClickSubscription = this.subscribeOverlayOutsideClick(); } this.updatePositions(); } getOverlayConfig() { return new OverlayConfig({ positionStrategy: this.getOverlayPosition(), scrollStrategy: this.overlay.scrollStrategies.reposition(), disposeOnNavigation: true }); } getOverlayPosition() { const positions = [ new ConnectionPositionPair({ originX: 'start', originY: 'bottom' }, { overlayX: 'start', overlayY: 'top' }), new ConnectionPositionPair({ originX: 'start', originY: 'top' }, { overlayX: 'start', overlayY: 'bottom' }) ]; this.positionStrategy = this.overlay .position() .flexibleConnectedTo(this.trigger.el) .withPositions(positions) .withFlexibleDimensions(false) .withPush(false); return this.positionStrategy; } } NzMentionComponent.decorators = [ { type: Component, args: [{ selector: 'nz-mention', exportAs: 'nzMention', template: ` <ng-content></ng-content> <ng-template #suggestions> <ul class="ant-mention-dropdown"> <li #items class="ant-mention-dropdown-item" *ngFor="let suggestion of filteredSuggestions; let i = index" [class.focus]="i === activeIndex" (mousedown)="$event.preventDefault()" (click)="selectSuggestion(suggestion)" > <ng-container *ngIf="suggestionTemplate; else defaultSuggestion"> <ng-container *ngTemplateOutlet="suggestionTemplate; context: { $implicit: suggestion }"></ng-container> </ng-container> <ng-template #defaultSuggestion>{{ nzValueWith(suggestion) }}</ng-template> </li> <li class="ant-mention-dropdown-notfound ant-mention-dropdown-item" *ngIf="filteredSuggestions.length === 0"> <span *ngIf="nzLoading"><i nz-icon nzType="loading"></i></span> <span *ngIf="!nzLoading">{{ nzNotFoundContent }}</span> </li> </ul> </ng-template> `, preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, providers: [NzMentionService] },] } ]; NzMentionComponent.ctorParameters = () => [ { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DOCUMENT,] }] }, { type: ChangeDetectorRef }, { type: Overlay }, { type: ViewContainerRef }, { type: NzMentionService } ]; NzMentionComponent.propDecorators = { nzValueWith: [{ type: Input }], nzPrefix: [{ type: Input }], nzLoading: [{ type: Input }], nzNotFoundContent: [{ type: Input }], nzPlacement: [{ type: Input }], nzSuggestions: [{ type: Input }], nzOnSelect: [{ type: Output }], nzOnSearchChange: [{ type: Output }], suggestionsTemp: [{ type: ViewChild, args: [TemplateRef, { static: false },] }], items: [{ type: ViewChildren, args: ['items', { read: ElementRef },] }], suggestionChild: [{ type: ContentChild, args: [NzMentionSuggestionDirective, { static: false, read: TemplateRef },] }] }; __decorate([ InputBoolean(), __metadata("design:type", Object) ], NzMentionComponent.prototype, "nzLoading", void 0); /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ const COMPONENTS = [NzMentionComponent, NzMentionTriggerDirective, NzMentionSuggestionDirective]; class NzMentionModule { } NzMentionModule.decorators = [ { type: NgModule, args: [{ imports: [BidiModule, CommonModule, FormsModule, OverlayModule, NzIconModule], declarations: [...COMPONENTS], exports: [...COMPONENTS] },] } ]; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ /** * Generated bundle index. Do not edit. */ export { NZ_MENTION_TRIGGER_ACCESSOR, NzMentionComponent, NzMentionModule, NzMentionService, NzMentionSuggestionDirective, NzMentionTriggerDirective }; //# sourceMappingURL=ng-zorro-antd-mention.js.map