From: Bill Erickson Date: Mon, 12 Aug 2019 21:30:19 +0000 (-0400) Subject: LPXXX fm-editor API consolidation WIP X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=8e5981210f108858775e574b061ad91793e79785;p=working%2FEvergreen.git LPXXX fm-editor API consolidation WIP Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/app/core/idl.service.ts b/Open-ILS/src/eg2/src/app/core/idl.service.ts index 56b8b90e1f..0537f05983 100644 --- a/Open-ILS/src/eg2/src/app/core/idl.service.ts +++ b/Open-ILS/src/eg2/src/app/core/idl.service.ts @@ -156,5 +156,14 @@ export class IdlService { } return null; } + + // Returns true if both objects have the same IDL class and pkey value. + pkeyMatches(obj1: IdlObject, obj2: IdlObject) { + if (!obj1 || !obj2) { return false; } + const idlClass = obj1.classname; + if (idlClass !== obj2.classname) { return false; } + const pkeyField = this.classes[idlClass].pkey || 'id'; + return obj1[pkeyField]() === obj2[pkeyField](); + } } diff --git a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts index c6cb25ef2c..739a2c48d1 100644 --- a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts +++ b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts @@ -82,11 +82,6 @@ export class FmRecordEditorComponent // IDL class hint (e.g. "aou") @Input() idlClass: string; - recId: any; - - // IDL record we are editing - record: IdlObject; - // Permissions extracted from the permacrud defs in the IDL // for the current IDL class modePerms: {[mode: string]: string}; @@ -156,10 +151,40 @@ export class FmRecordEditorComponent // Record ID to view/update. Value is dynamic. Records are not // fetched until .open() is called. + _recordId: any; @Input() set recordId(id: any) { - if (id) { this.recId = id; } + if (id) { + if (id !== this._recordId) { + this._recordId = id; + this.handleRecordChange(); + } + } else { + this._recordId = null; + this.record = null; + } } + // IDL record we are editing + _record: IdlObject; + + @Input() set record(r: IdlObject) { + if (r) { + if (!this.idl.pkeyMatches(this.record, r)) { + this._record = r; + this.handleRecordChange(); + } + } else { + this.recordId = null; + this._record = null; + } + } + + get record(): IdlObject { + return this._record; + } + + initDone: boolean; + // Comma-separated list of field names defining the order in which // fields should be rendered in the form. Any fields not represented // will be rendered alphabetically by label after the named fields. @@ -189,22 +214,27 @@ export class FmRecordEditorComponent } else { this.initRecord(); } + this.initDone = true; + } + + // If the record ID changes after ngOnInit has been called + // and we're using displayMode=inline, force the data to + // resync in real time + handleRecordChange() { + if (this.initDone && !this.isDialog()) { + this.initRecord(); + } } isDialog(): boolean { return this.displayMode === 'dialog'; } - // Set the record value and clear the recId value to - // indicate the record is our current source of data. + // DEPRECATED: This is a duplicate of set record(...) setRecord(record: IdlObject) { - this.record = record; - this.recId = null; - if (!this.isDialog()) { - // in inline mode, update the display immediately to reflect - // the change in source record. - this.initRecord(); - } + console.warn('fm-editor:setRecord() is deprecated. ' + + 'Use editor.record = abc or [record]="abc" instead'); + this.record = record; // this calls the setter } // Translate comma-separated string versions of various inputs @@ -238,18 +268,18 @@ export class FmRecordEditorComponent if (this.mode === 'update' || this.mode === 'view') { let promise; - if (this.record && this.recId === null) { + if (this.record && this.recordId === null) { promise = Promise.resolve(this.record); } else { promise = - this.pcrud.retrieve(this.idlClass, this.recId).toPromise(); + this.pcrud.retrieve(this.idlClass, this.recordId).toPromise(); } return promise.then(rec => { if (!rec) { return Promise.reject(`No '${this.idlClass}' - record found with id ${this.recId}`); + record found with id ${this.recordId}`); } this.record = rec; @@ -263,7 +293,7 @@ export class FmRecordEditorComponent // Create a new record from the stub record provided by the // caller or a new from-scratch record this.record = this.record || this.idl.create(this.idlClass); - this.recId = null; // avoid future confusion + this.recordId = null; // avoid future confusion return this.getFieldList(); } diff --git a/Open-ILS/src/eg2/src/app/staff/cat/vandelay/match-set-list.component.ts b/Open-ILS/src/eg2/src/app/staff/cat/vandelay/match-set-list.component.ts index c33999a282..8efe6b5d5b 100644 --- a/Open-ILS/src/eg2/src/app/staff/cat/vandelay/match-set-list.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/cat/vandelay/match-set-list.component.ts @@ -59,7 +59,7 @@ export class MatchSetListComponent implements AfterViewInit { this.grid.onRowActivate.subscribe( (matchSet: IdlObject) => { this.editDialog.mode = 'update'; - this.editDialog.recId = matchSet.id(); + this.editDialog.recordId = matchSet.id(); this.editDialog.open({size: 'lg'}) .subscribe(() => this.grid.reload()); } diff --git a/Open-ILS/src/eg2/src/app/staff/catalog/record/parts.component.ts b/Open-ILS/src/eg2/src/app/staff/catalog/record/parts.component.ts index 2f59374084..9766c7f8d3 100644 --- a/Open-ILS/src/eg2/src/app/staff/catalog/record/parts.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/catalog/record/parts.component.ts @@ -80,7 +80,7 @@ export class PartsComponent implements OnInit { this.partsGrid.onRowActivate.subscribe( (part: IdlObject) => { this.editDialog.mode = 'update'; - this.editDialog.recId = part.id(); + this.editDialog.recordId = part.id(); this.editDialog.open() .subscribe(ok => this.partsGrid.reload()); } @@ -89,7 +89,7 @@ export class PartsComponent implements OnInit { this.createNew = () => { const part = this.idl.create('bmp'); - part.record(this.recId); + part.record(this.recordId); this.editDialog.record = part; this.editDialog.mode = 'create'; diff --git a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts index 740d4d14b8..efbca9234f 100644 --- a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts @@ -365,7 +365,7 @@ export class SandboxComponent implements OnInit { showEditDialog(idlThing: IdlObject): Promise { this.editDialog.mode = 'update'; - this.editDialog.recId = idlThing['id'](); + this.editDialog.recordId = idlThing['id'](); return new Promise((resolve, reject) => { this.editDialog.open({size: 'lg'}).subscribe( ok => { diff --git a/Open-ILS/src/eg2/src/app/staff/share/admin-page/admin-page.component.ts b/Open-ILS/src/eg2/src/app/staff/share/admin-page/admin-page.component.ts index f920d7b3c2..c7798c4f5a 100644 --- a/Open-ILS/src/eg2/src/app/staff/share/admin-page/admin-page.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/share/admin-page/admin-page.component.ts @@ -240,7 +240,7 @@ export class AdminPageComponent implements OnInit { showEditDialog(idlThing: IdlObject): Promise { this.editDialog.mode = 'update'; - this.editDialog.recId = idlThing[this.pkeyField](); + this.editDialog.recordId = idlThing[this.pkeyField](); return new Promise((resolve, reject) => { this.editDialog.open({size: this.dialogSize}).subscribe( result => { @@ -284,7 +284,7 @@ export class AdminPageComponent implements OnInit { this.editDialog.mode = 'create'; // We reuse the same editor for all actions. Be sure // create action does not try to modify an existing record. - this.editDialog.recId = null; + this.editDialog.recordId = null; this.editDialog.record = null; this.editDialog.open({size: this.dialogSize}).subscribe( ok => {