1 import {Component, OnInit, Input, ViewChild, ViewEncapsulation} from'@angular/core';
2 import {Router} from '@angular/router';
3 import {Observable, Observer, of} from 'rxjs';
4 import {map} from 'rxjs/operators';
5 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
6 import {GridDataSource} from '@eg/share/grid/grid';
7 import {GridComponent} from '@eg/share/grid/grid.component';
8 import {Pager} from '@eg/share/util/pager';
9 import {AuthService} from '@eg/core/auth.service';
10 import {IdlObject, IdlService} from '@eg/core/idl.service';
11 import {PcrudService} from '@eg/core/pcrud.service';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
16 templateUrl: './field-documentation.component.html'
19 export class FieldDocumentationComponent implements OnInit {
21 idlEntries: any[] = [];
22 fieldOptions: any = {};
23 @Input() selectedClass: any;
24 @Input() fields: [] = [];
25 gridDataSource: GridDataSource;
26 @ViewChild('fieldClassSelector', {static: true}) fieldClassSelector: any;
27 @ViewChild('fieldSelector', {static: true}) fieldSelector: any;
28 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
29 @ViewChild('fieldDocGrid', { static: true }) fieldDocGrid: GridComponent;
30 @ViewChild('updateSuccessString', { static: true }) updateSuccessString: StringComponent;
31 @ViewChild('createSuccessString', { static: false }) createSuccessString: StringComponent;
32 @ViewChild('createFailedString', { static: false }) createFailedString: StringComponent;
33 @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
36 private auth: AuthService,
37 private idl: IdlService,
38 private pcrud: PcrudService,
39 private toast: ToastService
43 this.gridDataSource = new GridDataSource();
44 Object.values(this.idl.classes).forEach(idlClass => {
46 Object.values(idlClass['field_map']).forEach(field => {
47 // We can safely ignore virtual fields...
48 if (!field['virtual']) {
51 label: field['label'] ? field['label'] : field['name']
55 if (idlClass['label']) {
56 this.idlEntries.push({
57 label: idlClass['label'],
63 this.idlEntries.sort((a, b) => {
64 let textA = a.label.toUpperCase();
65 let textB = b.label.toUpperCase();
66 return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
68 if (this.selectedClass) this.setGrid();
69 this.fieldDocGrid.onRowActivate.subscribe((fieldDoc: IdlObject) => {
70 this.showEditDialog(fieldDoc);
76 template:this.fieldClassSelector,
78 fieldentries: this.idlEntries,
79 selectedEntry: this.selectedClass
85 template: this.fieldSelector,
94 setClass(idlClass, entry?) {
95 if (this.editDialog.record) this.editDialog.record.fm_class(idlClass.id);
96 this.fieldOptions.fm_class.customTemplate.context.selectedEntry = idlClass;
97 this.fields = idlClass.fields;
99 if (entry && entry.field()) this.setField(
100 idlClass.fields.find(o => o.id == entry.field()),
105 if (this.editDialog.record) this.editDialog.record.field(entry.id);
106 this.fieldOptions.field.customTemplate.context.selectedEntry = entry;
110 this.gridDataSource.data = [];
111 this.setCurrentFieldDoc();
114 setCurrentFieldDoc() {
115 if (this.selectedClass) {
116 this.fields = this.selectedClass.fields;
117 this.pcrud.search('fdoc',
118 {fm_class: this.selectedClass.id}
119 ).subscribe(fdocs => {
120 this.gridDataSource.data.push(fdocs);
125 setFieldOptions(field) {
126 field.owner(this.auth.user().ws_ou());
127 this.fieldOptions.fm_class.customTemplate.context.selectedEntry = this.selectedClass;
128 this.fieldOptions.field.customTemplate.context.fields = this.selectedClass ? this.selectedClass.fields : [];
129 this.fieldOptions.field.customTemplate.context.record = field;
130 if (this.selectedClass) {
131 this.setClass(this.selectedClass, field);
135 showEditDialog(field: IdlObject): Promise<any> {
136 this.editDialog.mode = 'update';
137 this.editDialog.recordId = field.id();
138 this.setFieldOptions(field);
139 return new Promise((resolve, reject) => {
140 this.editDialog.open({size: 'lg'}).subscribe(result => {
141 this.updateSuccessString.current()
142 .then(str => this.toast.success(str));
146 this.updateFailedString.current()
147 .then(str => this.toast.danger(str));
152 editSelected(fields: IdlObject[]) {
153 const editOneFieldDoc = (fieldDoc: IdlObject) => {
154 if (!fieldDoc) return;
156 this.showEditDialog(fieldDoc).then(
157 () => editOneFieldDoc(fields.shift())
161 editOneFieldDoc(fields.shift());
165 this.editDialog.mode = 'create';
166 this.editDialog.recordId = null;
167 this.editDialog.record = this.idl.create('fdoc');
168 this.setFieldOptions(this.editDialog.record);
169 this.editDialog.open({size: 'lg'}).subscribe(
171 this.createSuccessString.current()
172 .then(str => this.toast.success(str));
176 if (!rejection.dismissed) {
177 this.createFailedString.current()
178 .then(str => this.toast.danger(str));