mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-09 09:58:20 -05:00
138 lines
2.7 KiB
TypeScript
138 lines
2.7 KiB
TypeScript
import {
|
|
Component,
|
|
EventEmitter,
|
|
forwardRef,
|
|
Input,
|
|
Output,
|
|
} from '@angular/core'
|
|
import { NG_VALUE_ACCESSOR } from '@angular/forms'
|
|
import { AbstractInputComponent } from '../abstract-input'
|
|
|
|
@Component({
|
|
providers: [
|
|
{
|
|
provide: NG_VALUE_ACCESSOR,
|
|
useExisting: forwardRef(() => SelectComponent),
|
|
multi: true,
|
|
},
|
|
],
|
|
selector: 'app-input-select',
|
|
templateUrl: './select.component.html',
|
|
styleUrls: ['./select.component.scss'],
|
|
})
|
|
export class SelectComponent extends AbstractInputComponent<number> {
|
|
constructor() {
|
|
super()
|
|
this.addItemRef = this.addItem.bind(this)
|
|
}
|
|
|
|
_items: any[]
|
|
|
|
@Input()
|
|
set items(items) {
|
|
this._items = items
|
|
if (items && this.value) this.checkForPrivateItems(this.value)
|
|
}
|
|
|
|
writeValue(newValue: any): void {
|
|
if (newValue && this._items) {
|
|
this.checkForPrivateItems(newValue)
|
|
this.items = [...this._items] // we need to explicitly re-set items
|
|
}
|
|
super.writeValue(newValue)
|
|
}
|
|
|
|
checkForPrivateItems(value: any) {
|
|
if (Array.isArray(value) && value.length > 0) {
|
|
value.forEach((id) => this.checkForPrivateItem(id))
|
|
} else {
|
|
this.checkForPrivateItem(value)
|
|
}
|
|
}
|
|
|
|
checkForPrivateItem(id) {
|
|
if (this._items.find((i) => i.id === id) === undefined) {
|
|
this._items.push({
|
|
id: id,
|
|
name: $localize`Private`,
|
|
private: true,
|
|
})
|
|
}
|
|
}
|
|
|
|
get items(): any[] {
|
|
return this._items
|
|
}
|
|
|
|
@Input()
|
|
textColor: any
|
|
|
|
@Input()
|
|
backgroundColor: any
|
|
|
|
@Input()
|
|
allowNull: boolean = false
|
|
|
|
@Input()
|
|
suggestions: number[]
|
|
|
|
@Input()
|
|
placeholder: string
|
|
|
|
@Input()
|
|
multiple: boolean = false
|
|
|
|
@Input()
|
|
bindLabel: string = 'name'
|
|
|
|
@Output()
|
|
createNew = new EventEmitter<string>()
|
|
|
|
public addItemRef: (name) => void
|
|
|
|
private _lastSearchTerm: string
|
|
|
|
get allowCreateNew(): boolean {
|
|
return this.createNew.observers.length > 0
|
|
}
|
|
|
|
get isPrivate(): boolean {
|
|
return this.items?.find((i) => i.id === this.value)?.private
|
|
}
|
|
|
|
getSuggestions() {
|
|
if (this.suggestions && this.items) {
|
|
return this.suggestions
|
|
.filter((id) => id != this.value)
|
|
.map((id) => this.items.find((item) => item.id == id))
|
|
} else {
|
|
return []
|
|
}
|
|
}
|
|
|
|
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()
|
|
}, 3000)
|
|
}
|
|
}
|