From 9bcb1dd89a3da045e8823db71c6a617aa2d1d214 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Tue, 30 May 2023 11:11:24 -0400 Subject: [PATCH] LP2018410 DOM AutoId Service Usage: Signed-off-by: Bill Erickson --- .../eg2/src/app/share/util/autoid.service.spec.ts | 20 ++++++++++++++++++++ .../src/eg2/src/app/share/util/autoid.service.ts | 17 +++++++++++++++++ .../eg2/src/app/staff/sandbox/sandbox.component.html | 6 ++++++ .../eg2/src/app/staff/sandbox/sandbox.component.ts | 5 ++++- 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/eg2/src/app/share/util/autoid.service.spec.ts create mode 100644 Open-ILS/src/eg2/src/app/share/util/autoid.service.ts diff --git a/Open-ILS/src/eg2/src/app/share/util/autoid.service.spec.ts b/Open-ILS/src/eg2/src/app/share/util/autoid.service.spec.ts new file mode 100644 index 0000000000..8801766681 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/share/util/autoid.service.spec.ts @@ -0,0 +1,20 @@ +import {AutoIdService} from './autoid.service'; + +let autoId: AutoIdService; + +beforeEach(() => { + autoId = new AutoIdService(); +}); + +describe('AutoId', () => { + it('Should always increase', () => { + expect(autoId.next() < autoId.next()).toBe(true); + }); + it('Should always increase', () => { + expect(autoId.next() < autoId.next()).toBe(true); + }); + it('Should always increase', () => { + expect(autoId.next() < autoId.next()).toBe(true); + }); +}); + diff --git a/Open-ILS/src/eg2/src/app/share/util/autoid.service.ts b/Open-ILS/src/eg2/src/app/share/util/autoid.service.ts new file mode 100644 index 0000000000..c49e29a88a --- /dev/null +++ b/Open-ILS/src/eg2/src/app/share/util/autoid.service.ts @@ -0,0 +1,17 @@ +import {Injectable} from '@angular/core'; + +@Injectable({providedIn: 'root'}) +export class AutoIdService { + static seed = 0; + + // Returns the next generated ID. + next(elm?: any): string { + // If the element has an ID, keep it. Otherwise, we end up + // auto-generating IDs repeatedly for the same element for + // every page load. + if (elm && elm.id) { return elm.id; } + + AutoIdService.seed++; + return `auto-${AutoIdService.seed}`; + } +} diff --git a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html index e9737bc48c..a06e71519c 100644 --- a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html +++ b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html @@ -2,6 +2,7 @@ + + + + + +