AW-3836 fixed tags component
Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit
Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit
This commit is contained in:
parent
57e0a37c78
commit
0c4259d72e
@ -1,3 +1,11 @@
|
||||
<div class="tags">
|
||||
<span class="tag rounded bg-primary text-white" *ngFor="let tag of tags;"><span>{{tag}}</span> <i (click)="handleDeleteTag(tag)" class="fal fa-times" aria-hidden="true"></i></span><input type="text" #taginput (blur)="handleAddTag($event)" (keyup)="handleCheckAddTag($event)" [(ngModel)]="tag" [ngbTypeahead]="findTag" (selectItem)="handleSelect($event)" placeholder="New tag"/>
|
||||
<span class="tag rounded bg-primary text-white" *ngFor="let tag of tags;"><span>{{tag}}</span> <i
|
||||
(click)="handleDeleteTag(tag)" class="fal fa-times" aria-hidden="true"></i></span><input
|
||||
type="text" #tagInputElement
|
||||
(blur)="handleBlur($event, false)"
|
||||
(keyup)="handleKeyUp($event)"
|
||||
[(ngModel)]="tag"
|
||||
[ngbTypeahead]="findTag"
|
||||
(selectItem)="handleSelect($event)"
|
||||
placeholder="New tag"/>
|
||||
</div>
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { Component, Input, forwardRef,ElementRef,ViewChild } from '@angular/core';
|
||||
import { ControlValueAccessor, NG_VALUE_ACCESSOR,NgModel } from '@angular/forms';
|
||||
import { Observable,of } from 'rxjs';
|
||||
import { tap,catchError,debounceTime,distinctUntilChanged,switchMap } from 'rxjs/operators'
|
||||
import { TypeaheadService } from '../../services/typeahead.service';
|
||||
import {Component, ElementRef, forwardRef, Input, ViewChild} from '@angular/core';
|
||||
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {catchError, debounceTime, distinctUntilChanged, switchMap, tap} from 'rxjs/operators';
|
||||
import {TypeaheadService} from '@farmmaps/common';
|
||||
import {NgbTypeahead} from '@ng-bootstrap/ng-bootstrap';
|
||||
|
||||
@Component({
|
||||
selector: 'fm-tag-input',
|
||||
@ -17,9 +18,11 @@ import { TypeaheadService } from '../../services/typeahead.service';
|
||||
]
|
||||
})
|
||||
|
||||
export class TagInputComponent implements ControlValueAccessor {
|
||||
@Input() tags: string[]
|
||||
@ViewChild('taginput', { static: true }) tagInputElement: ElementRef;
|
||||
export class TagInputComponent implements ControlValueAccessor {
|
||||
@Input() tags: string[] = [];
|
||||
@ViewChild('tagInputElement', {static: true}) tagInputElement: ElementRef;
|
||||
@ViewChild(NgbTypeahead, {static: true}) typeahead: NgbTypeahead;
|
||||
|
||||
public tag: string;
|
||||
searching = false;
|
||||
searchFailed = false;
|
||||
@ -28,39 +31,52 @@ export class TagInputComponent implements ControlValueAccessor {
|
||||
}
|
||||
|
||||
tagExists(tag) {
|
||||
if (tag.length == 0) return true;
|
||||
for (let t of this.tags) {
|
||||
if (t.toLowerCase() == tag.toLowerCase()) return true;
|
||||
if (tag.length === 0) {
|
||||
return true;
|
||||
}
|
||||
for (const t of this.tags) {
|
||||
if (t.toLowerCase() === tag.toLowerCase()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
addTag(tag, keepFocus = true) {
|
||||
if (!this.tagExists(tag)) {
|
||||
this.tags.push(tag);
|
||||
this.propagateChange(this.tags);
|
||||
}
|
||||
this.tag = '';
|
||||
|
||||
if (keepFocus) {
|
||||
this.tagInputElement.nativeElement.focus();
|
||||
}
|
||||
}
|
||||
|
||||
handleDeleteTag(tag) {
|
||||
let tags = [];
|
||||
for (let t of this.tags) {
|
||||
if (t != tag) tags.push(t);
|
||||
const tags = [];
|
||||
for (const t of this.tags) {
|
||||
if (t !== tag) {
|
||||
tags.push(t);
|
||||
}
|
||||
}
|
||||
this.tags = tags;
|
||||
this.propagateChange(tags);
|
||||
}
|
||||
|
||||
handleAddTag(event) {
|
||||
if (!this.tagExists(this.tag)) {
|
||||
this.tags.push(this.tag);
|
||||
this.propagateChange(this.tags);
|
||||
handleBlur(event, keepFocus = true) {
|
||||
if (!this.typeahead.isPopupOpen()) {
|
||||
this.addTag(this.tag, keepFocus);
|
||||
} else {
|
||||
this.tag = '';
|
||||
}
|
||||
this.tag = "";
|
||||
this.tagInputElement.nativeElement.focus();
|
||||
}
|
||||
|
||||
handleCheckAddTag(event: KeyboardEvent) {
|
||||
if (event.keyCode == 188) {
|
||||
let tag = this.tag.substr(0, this.tag.length - 1); // strip ,
|
||||
if (!this.tagExists(tag)) {
|
||||
this.tags.push(tag);
|
||||
this.propagateChange(this.tags);
|
||||
}
|
||||
this.tag = "";
|
||||
handleKeyUp(event: KeyboardEvent) {
|
||||
if (event.keyCode === 188) {
|
||||
const tag = this.tag.substr(0, this.tag.length - 1); // strip ,
|
||||
this.addTag(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,35 +86,37 @@ export class TagInputComponent implements ControlValueAccessor {
|
||||
this.propagateChange(this.tags);
|
||||
}
|
||||
event.preventDefault();
|
||||
this.tag = "";
|
||||
this.tag = '';
|
||||
}
|
||||
|
||||
propagateChange = (_: any) => { };
|
||||
propagateChange = (_: any) => {
|
||||
}
|
||||
|
||||
registerOnChange(fn) {
|
||||
this.propagateChange = fn;
|
||||
}
|
||||
|
||||
findTag = (text$: Observable<string>) =>
|
||||
text$.pipe(
|
||||
debounceTime(200),
|
||||
distinctUntilChanged(),
|
||||
tap(() => this.searching = true),
|
||||
switchMap(term => term.length < 1 ? of([]) :
|
||||
this.typeaheadService.getTagTypeaheadItems(term).pipe(
|
||||
tap(() => this.searchFailed = false),
|
||||
catchError(() => {
|
||||
this.searchFailed = true;
|
||||
return of([]);
|
||||
}))
|
||||
),
|
||||
tap(() => this.searching = false)
|
||||
);
|
||||
text$.pipe(
|
||||
distinctUntilChanged(),
|
||||
debounceTime(200),
|
||||
tap(() => this.searching = true),
|
||||
switchMap(term => term.length < 1 ? of([]) :
|
||||
this.typeaheadService.getTagTypeaheadItems(term).pipe(
|
||||
tap(() => this.searchFailed = false),
|
||||
catchError(() => {
|
||||
this.searchFailed = true;
|
||||
return of([]);
|
||||
}))
|
||||
),
|
||||
tap(() => this.searching = false)
|
||||
)
|
||||
|
||||
writeValue(value: any) {
|
||||
this.tags = value;
|
||||
this.tag = "";
|
||||
this.tag = '';
|
||||
}
|
||||
|
||||
registerOnTouched() { }
|
||||
registerOnTouched() {
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user