1 import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
2 import {NgbAccordion} from '@ng-bootstrap/ng-bootstrap';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {SimpleReporterService} from './simple-reporter.service';
7 selector: 'eg-sr-field-chooser',
8 styleUrls: ['./sr-field-chooser.component.css'],
9 templateUrl: './sr-field-chooser.component.html'
12 export class SRFieldChooserComponent {
14 @Input() fieldType = 'display';
15 @Input() allFields: IdlObject[] = [];
16 @Input() fieldGroups: IdlObject[] = [];
17 @Input() orderByNames: string[] = [];
18 @Output() orderByNamesChange = new EventEmitter<string[]>();
19 @Input() selectedFields: IdlObject[] = [];
20 @Output() selectedFieldsChange = new EventEmitter<IdlObject[]>();
21 @Input() listFields: IdlObject[] = [];
23 @ViewChild('fieldChooser', { static: false }) fieldChooser: NgbAccordion;
24 @ViewChild('selectedList', { static: false }) selectedList: NgbAccordion;
27 private idl: IdlService,
28 private srSvc: SimpleReporterService
32 fieldIsSelected(field: IdlObject) {
33 return this.selectedFields.findIndex(el => el.name === field.name) > -1;
36 hideField(field: IdlObject) {
37 if ( typeof field.hide_from === 'undefined' ) {
40 return (field.hide_from.indexOf(this.fieldType) > -1);
43 toggleSelect(field: IdlObject) {
44 const idx = this.selectedFields.findIndex(el => el.name === field.name);
46 if ( field.forced_filter ) { return; } // These should just be hidden, but if not...
47 this.selectedFields.splice(idx, 1);
48 if ( this.fieldType === 'display' ) {
49 this.orderByNames.splice(this.orderByNames.findIndex(el => el === field.name), 1);
52 const f = { ...field };
54 if ( this.fieldType === 'display' ) {
55 f['alias'] = f.label; // can be edited
56 this.orderByNames.push(f.name);
58 this.selectedFields.push(f);
61 this.selectedFieldsChange.emit(this.selectedFields);
63 if ( this.fieldType === 'display' ) {
64 this.orderByNamesChange.emit(this.orderByNames);
68 updateField(field: IdlObject) {
69 const idx = this.selectedFields.findIndex(el => el.name === field.name);
70 this.selectedFields[idx] = field;
71 this.selectedFieldsChange.emit(this.selectedFields);
75 if ( idx > 0 ) { // should always be the case, but we check anyway
76 const hold: IdlObject = this.selectedFields[idx - 1];
77 this.selectedFields[idx - 1] = this.selectedFields[idx];
78 this.selectedFields[idx] = hold;
79 this.selectedFieldsChange.emit(this.selectedFields);
83 moveDown(idx: number) {
84 if ( idx < this.selectedFields.length ) { // see above comment
85 const hold: IdlObject = this.selectedFields[idx + 1];
86 this.selectedFields[idx + 1] = this.selectedFields[idx];
87 this.selectedFields[idx] = hold;
88 this.selectedFieldsChange.emit(this.selectedFields);