1 import {Component, Input, Output, EventEmitter, OnInit, ViewChild} from '@angular/core';
2 import {NgbAccordion} from '@ng-bootstrap/ng-bootstrap';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {SimpleReporterService} from './simple-reporter.service';
6 import {SRFieldComponent} from './sr-field.component';
9 selector: 'eg-sr-field-chooser',
10 styleUrls: ['./sr-field-chooser.component.css'],
11 templateUrl: './sr-field-chooser.component.html'
14 export class SRFieldChooserComponent implements OnInit {
16 @Input() fieldType = 'display';
17 @Input() allFields: IdlObject[] = [];
18 @Input() fieldGroups: IdlObject[] = [];
19 @Input() orderByNames: string[] = [];
20 @Output() orderByNamesChange = new EventEmitter<string[]>();
21 @Input() selectedFields: IdlObject[] = [];
22 @Output() selectedFieldsChange = new EventEmitter<IdlObject[]>();
23 @Input() listFields: IdlObject[] = [];
25 @ViewChild('fieldChooser', { static: false }) fieldChooser: NgbAccordion;
26 @ViewChild('selectedList', { static: false }) selectedList: NgbAccordion;
29 private idl: IdlService,
30 private srSvc: SimpleReporterService
37 fieldIsSelected(field: IdlObject) {
38 return this.selectedFields.findIndex(el => el.name === field.name) > -1;
41 hideField(field: IdlObject) {
42 if ( typeof field.hide_from === 'undefined' ) {
45 return (field.hide_from.indexOf(this.fieldType) > -1);
48 toggleSelect(field: IdlObject) {
49 const idx = this.selectedFields.findIndex(el => el.name === field.name);
51 if ( field.forced_filter ) { return; } // These should just be hidden, but if not...
52 this.selectedFields.splice(idx, 1);
53 if ( this.fieldType === 'display' ) {
54 this.orderByNames.splice(this.orderByNames.findIndex(el => el === field.name), 1);
57 const f = { ...field };
59 if ( this.fieldType === 'display' ) {
60 f['alias'] = f.label; // can be edited
61 this.orderByNames.push(f.name);
63 this.selectedFields.push(f);
66 this.selectedFieldsChange.emit(this.selectedFields);
68 if ( this.fieldType === 'display' ) {
69 this.orderByNamesChange.emit(this.orderByNames);
73 updateField(field: IdlObject) {
74 const idx = this.selectedFields.findIndex(el => el.name === field.name);
75 this.selectedFields[idx] = field;
76 this.selectedFieldsChange.emit(this.selectedFields);
80 if ( idx > 0 ) { // should always be the case, but we check anyway
81 const hold: IdlObject = this.selectedFields[idx - 1];
82 this.selectedFields[idx - 1] = this.selectedFields[idx];
83 this.selectedFields[idx] = hold;
84 this.selectedFieldsChange.emit(this.selectedFields);
88 moveDown(idx: number) {
89 if ( idx < this.selectedFields.length ) { // see above comment
90 const hold: IdlObject = this.selectedFields[idx + 1];
91 this.selectedFields[idx + 1] = this.selectedFields[idx];
92 this.selectedFields[idx] = hold;
93 this.selectedFieldsChange.emit(this.selectedFields);