Files
paperless-ngx/src-ui/src/app/components/common/input/select/select.component.ts
T

118 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-03-11 10:53:32 -08:00
import {
Component,
EventEmitter,
forwardRef,
Input,
Output,
} from '@angular/core'
import { NG_VALUE_ACCESSOR } from '@angular/forms'
import { AbstractInputComponent } from '../abstract-input'
2020-10-28 18:02:30 +01:00
@Component({
2022-03-11 10:53:32 -08:00
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectComponent),
multi: true,
},
],
2020-10-28 18:02:30 +01:00
selector: 'app-input-select',
templateUrl: './select.component.html',
2022-03-11 10:53:32 -08:00
styleUrls: ['./select.component.scss'],
2020-10-28 18:02:30 +01:00
})
export class SelectComponent extends AbstractInputComponent<number> {
constructor() {
super()
2021-04-03 09:30:29 -07:00
this.addItemRef = this.addItem.bind(this)
2022-03-11 10:53:32 -08:00
}
2020-10-28 18:02:30 +01:00
_items: any[]
2020-10-28 18:02:30 +01:00
@Input()
set items(items) {
if (this.value && items.find((i) => i.id === this.value) === undefined) {
items.push({
id: this.value,
name: $localize`Private`,
private: true,
})
}
this._items = items
}
get items(): any[] {
return this._items
}
2020-10-28 18:02:30 +01:00
@Input()
textColor: any
@Input()
backgroundColor: any
2020-10-28 20:21:00 +01:00
@Input()
allowNull: boolean = false
2021-01-29 16:48:51 +01:00
@Input()
suggestions: number[]
@Input()
placeholder: string
2022-11-13 07:23:33 -08:00
@Input()
multiple: boolean = false
@Input()
bindLabel: string = 'name'
2020-10-28 20:21:00 +01:00
@Output()
createNew = new EventEmitter<string>()
2021-04-03 09:30:29 -07:00
public addItemRef: (name) => void
2021-01-29 16:48:51 +01:00
private _lastSearchTerm: string
2021-04-03 09:30:29 -07:00
get allowCreateNew(): boolean {
2020-10-28 20:21:00 +01:00
return this.createNew.observers.length > 0
}
get isPrivate(): boolean {
return this.items?.find((i) => i.id === this.value)?.private
}
2021-01-29 16:48:51 +01:00
getSuggestions() {
if (this.suggestions && this.items) {
2022-03-11 10:53:32 -08:00
return this.suggestions
.filter((id) => id != this.value)
.map((id) => this.items.find((item) => item.id == id))
2021-01-29 16:48:51 +01:00
} else {
return []
}
}
2021-04-03 09:30:29 -07:00
addItem(name: string) {
if (name) this.createNew.next(name)
else this.createNew.next(this._lastSearchTerm)
this.clearLastSearchTerm()
}
clickNew() {
this.createNew.next(this._lastSearchTerm)
this.clearLastSearchTerm()
}
clearLastSearchTerm() {
this._lastSearchTerm = null
}
onSearch($event) {
this._lastSearchTerm = $event.term
}
onBlur() {
setTimeout(() => {
this.clearLastSearchTerm()
2022-03-11 10:53:32 -08:00
}, 3000)
}
2020-10-28 18:02:30 +01:00
}