Compare commits

..

No commits in common. "a3a09507a6acbf2113e5895a5e908a0a15f2ffaf" and "57e0a37c78ec10546ec476d5c3119c4fb7d126ae" have entirely different histories.

3 changed files with 108 additions and 134 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "farmmaps-lib-app", "name": "farmmaps-lib-app",
"version": "2.0.5", "version": "2.0.4",
"scripts": { "scripts": {
"ng": "ng", "ng": "ng",
"start": "ng serve", "start": "ng serve",

View File

@ -1,11 +1,3 @@
<div class="tags"> <div class="tags">
<span class="tag rounded bg-primary text-white" *ngFor="let tag of tags;"><span>{{tag}}</span> <i <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"/>
(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> </div>

View File

@ -1,9 +1,8 @@
import {Component, ElementRef, forwardRef, Input, ViewChild} from '@angular/core'; import { Component, Input, forwardRef,ElementRef,ViewChild } from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; import { ControlValueAccessor, NG_VALUE_ACCESSOR,NgModel } from '@angular/forms';
import {Observable, of} from 'rxjs'; import { Observable,of } from 'rxjs';
import {catchError, debounceTime, distinctUntilChanged, switchMap, tap} from 'rxjs/operators'; import { tap,catchError,debounceTime,distinctUntilChanged,switchMap } from 'rxjs/operators'
import {TypeaheadService} from '../../services/typeahead.service'; import { TypeaheadService } from '../../services/typeahead.service';
import {NgbTypeahead} from '@ng-bootstrap/ng-bootstrap';
@Component({ @Component({
selector: 'fm-tag-input', selector: 'fm-tag-input',
@ -18,11 +17,9 @@ import {NgbTypeahead} from '@ng-bootstrap/ng-bootstrap';
] ]
}) })
export class TagInputComponent implements ControlValueAccessor { export class TagInputComponent implements ControlValueAccessor {
@Input() tags: string[] = []; @Input() tags: string[]
@ViewChild('tagInputElement', {static: true}) tagInputElement: ElementRef; @ViewChild('taginput', { static: true }) tagInputElement: ElementRef;
@ViewChild(NgbTypeahead, {static: true}) typeahead: NgbTypeahead;
public tag: string; public tag: string;
searching = false; searching = false;
searchFailed = false; searchFailed = false;
@ -31,52 +28,39 @@ export class TagInputComponent implements ControlValueAccessor {
} }
tagExists(tag) { tagExists(tag) {
if (tag.length === 0) { if (tag.length == 0) return true;
return true; for (let t of this.tags) {
} if (t.toLowerCase() == tag.toLowerCase()) return true;
for (const t of this.tags) {
if (t.toLowerCase() === tag.toLowerCase()) {
return true;
}
} }
return false; 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) { handleDeleteTag(tag) {
const tags = []; let tags = [];
for (const t of this.tags) { for (let t of this.tags) {
if (t !== tag) { if (t != tag) tags.push(t);
tags.push(t);
}
} }
this.tags = tags; this.tags = tags;
this.propagateChange(tags); this.propagateChange(tags);
} }
handleBlur(event, keepFocus = true) { handleAddTag(event) {
if (!this.typeahead.isPopupOpen()) { if (!this.tagExists(this.tag)) {
this.addTag(this.tag, keepFocus); this.tags.push(this.tag);
} else { this.propagateChange(this.tags);
this.tag = '';
} }
this.tag = "";
this.tagInputElement.nativeElement.focus();
} }
handleKeyUp(event: KeyboardEvent) { handleCheckAddTag(event: KeyboardEvent) {
if (event.keyCode === 188) { if (event.keyCode == 188) {
const tag = this.tag.substr(0, this.tag.length - 1); // strip , let tag = this.tag.substr(0, this.tag.length - 1); // strip ,
this.addTag(tag); if (!this.tagExists(tag)) {
this.tags.push(tag);
this.propagateChange(this.tags);
}
this.tag = "";
} }
} }
@ -86,37 +70,35 @@ export class TagInputComponent implements ControlValueAccessor {
this.propagateChange(this.tags); this.propagateChange(this.tags);
} }
event.preventDefault(); event.preventDefault();
this.tag = ''; this.tag = "";
} }
propagateChange = (_: any) => { propagateChange = (_: any) => { };
};
registerOnChange(fn) { registerOnChange(fn) {
this.propagateChange = fn; this.propagateChange = fn;
} }
findTag = (text$: Observable<string>) => findTag = (text$: Observable<string>) =>
text$.pipe( text$.pipe(
distinctUntilChanged(), debounceTime(200),
debounceTime(200), distinctUntilChanged(),
tap(() => this.searching = true), tap(() => this.searching = true),
switchMap(term => term.length < 1 ? of([]) : switchMap(term => term.length < 1 ? of([]) :
this.typeaheadService.getTagTypeaheadItems(term).pipe( this.typeaheadService.getTagTypeaheadItems(term).pipe(
tap(() => this.searchFailed = false), tap(() => this.searchFailed = false),
catchError(() => { catchError(() => {
this.searchFailed = true; this.searchFailed = true;
return of([]); return of([]);
})) }))
), ),
tap(() => this.searching = false) tap(() => this.searching = false)
); );
writeValue(value: any) { writeValue(value: any) {
this.tags = value; this.tags = value;
this.tag = ''; this.tag = "";
} }
registerOnTouched() { registerOnTouched() { }
}
} }