1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {EventService} from '@eg/core/event.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ToastService} from '@eg/share/toast/toast.service';
10 import {ComboboxComponent} from '@eg/share/combobox/combobox.component';
11 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
12 import {Observable} from 'rxjs';
13 import {map} from 'rxjs/operators';
16 selector: 'eg-fund-tags',
17 templateUrl: './fund-tags.component.html'
19 export class FundTagsComponent implements OnInit {
21 @Input() fundId: number;
22 @Input() fundOwner: number;
24 @ViewChild('addSuccessString', { static: true }) addSuccessString: StringComponent;
25 @ViewChild('addErrorString', { static: true }) addErrorString: StringComponent;
26 @ViewChild('removeSuccessString', { static: true }) removeSuccessString: StringComponent;
27 @ViewChild('removeErrorString', { static: true }) removeErrorString: StringComponent;
28 @ViewChild('tagSelector', { static: false }) tagSelector: ComboboxComponent;
31 newTag: ComboboxEntry = null;
32 tagSelectorDataSource: (term: string) => Observable<ComboboxEntry>;
35 private idl: IdlService,
36 private evt: EventService,
37 private net: NetService,
38 private auth: AuthService,
39 private pcrud: PcrudService,
40 private org: OrgService,
41 private toast: ToastService
46 this.tagSelectorDataSource = term => {
49 const extra_args = { order_by : {} };
50 args[field] = {'ilike': `%${term}%`}; // could -or search on label
51 args['owner'] = this.org.ancestors(this.fundOwner, true);
52 extra_args['order_by']['acqft'] = field;
53 extra_args['limit'] = 100;
54 extra_args['flesh'] = 2;
55 const flesh_fields: Object = {};
56 flesh_fields['acqft'] = ['owner'];
57 extra_args['flesh_fields'] = flesh_fields;
58 return this.pcrud.search('acqft', args, extra_args).pipe(map(data => {
61 label: data.name() + ' (' + data.owner().shortname() + ')',
70 this.pcrud.search('acqftm', { fund: this.fundId }, {
77 res => this.tagMaps.push(res),
79 () => this.tagMaps.sort((a, b) => {
80 return a.tag().name() < b.tag().name() ? -1 : 1;
85 checkNewTagAlreadyMapped(): boolean {
86 if ( this.newTag == null) { return false; }
87 const matches: IdlObject[] = this.tagMaps.filter(tm => tm.tag().id() === this.newTag.id);
88 return matches.length > 0 ? true : false;
92 const ftm = this.idl.create('acqftm');
93 ftm.tag(this.newTag.id);
94 ftm.fund(this.fundId);
95 this.pcrud.create(ftm).subscribe(
97 this.addSuccessString.current()
98 .then(str => this.toast.success(str));
101 this.addErrorString.current()
102 .then(str => this.toast.danger(str));
106 this.tagSelector.selectedId = null;
111 removeTagMap(ftm: IdlObject) {
112 this.pcrud.remove(ftm).subscribe(
114 this.removeSuccessString.current()
115 .then(str => this.toast.success(str));
118 this.removeErrorString.current()
119 .then(str => this.toast.danger(str));
121 () => this._loadTagMaps()