1 import {Component, Input, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 selector: 'eg-edi-attr-set-edit-dialog',
9 templateUrl: './edi-attr-set-edit-dialog.component.html'
12 export class EdiAttrSetEditDialogComponent
13 extends DialogComponent implements OnInit {
15 @Input() mode = 'create';
16 @Input() attrSetId: number;
17 @Input() cloneSource: number;
23 private idl: IdlService,
24 private pcrud: PcrudService,
25 private modal: NgbModal
31 this.onOpen$.subscribe(() => this._initRecord());
34 private _initRecord() {
37 this.clonedLabel = '';
38 if (this.mode === 'update') {
39 this.pcrud.retrieve('aeas', this.attrSetId, {
41 flesh_fields: { aeas: ['attr_maps'] }
44 this._generateAttrInputs();
46 } else if (this.mode === 'clone') {
47 this.pcrud.retrieve('aeas', this.cloneSource, {
49 flesh_fields: { aeas: ['attr_maps'] }
51 this.clonedLabel = res.label();
52 this.attrSet = this.idl.create('aeas');
53 this.attrSet.attr_maps([]);
54 res.attr_maps().forEach((m) => {
55 const newMap = this.idl.create('aeasm');
56 newMap.attr(m.attr());
57 this.attrSet.attr_maps().push(newMap);
59 this._generateAttrInputs();
61 } else if (this.mode === 'create') {
62 this.attrSet = this.idl.create('aeas');
63 this.attrSet.attr_maps([]);
64 this._generateAttrInputs();
68 _generateAttrInputs() {
69 const hasAttr: {[key: string]: boolean} = {};
70 const hasAttrId: {[key: string]: number} = {};
71 this.attrSet.attr_maps().forEach((m) => {
72 hasAttr[m.attr()] = true;
73 hasAttrId[m.attr()] = m.id();
75 this.pcrud.retrieveAll('aea', {order_by: {aea: 'key'}}).subscribe(attr => {
82 if (attr.key() in hasAttr) {
84 inp.id = hasAttrId[attr.key()];
86 this.attrInputs.push(inp);
91 if (this.attrSet.id() === undefined || this.attrSet.id() === null) {
92 this.attrSet.isnew(true);
94 this.attrSet.ischanged(true);
96 this.pcrud.autoApply([this.attrSet]).subscribe(res => {
97 const setId = this.mode === 'update' ? res : res.id();
98 const updates: IdlObject[] = [];
99 if (this.mode === 'create' || this.mode === 'clone') {
100 this.attrInputs.forEach((inp) => {
102 const aesm = this.idl.create('aeasm');
104 aesm.attr_set(setId);
110 // updating an existing set
111 this.attrInputs.forEach((inp) => {
114 // used to be wanted, but no longer
115 const aesm = this.idl.create('aeasm');
117 aesm.isdeleted(true);
120 } else if (inp.selected) {
121 // no ID, must be newly checked
122 const aesm = this.idl.create('aeasm');
124 aesm.attr_set(setId);
130 this.pcrud.autoApply(updates).subscribe(
131 ret => this.close(true),
132 err => this.close(err),
133 () => this.close(true)
135 }, err => this.close(false));