From 158d0ea54c50cde28d1e93b7b67aa454f66446ef Mon Sep 17 00:00:00 2001 From: paf <paf@titelfrei.de> Date: Mon, 12 Apr 2021 10:21:43 +0200 Subject: [PATCH] makes it finally happen: automatically select next block when used goto. --- src/app/group-monitor/booklet.service.spec.ts | 30 ++++--------------- src/app/group-monitor/booklet.service.ts | 25 ++++++---------- .../group-monitor/group-monitor.component.ts | 25 ++++++++++++++-- .../group-monitor/group-monitor.interfaces.ts | 1 + .../group-monitor/group-monitor.service.ts | 19 +++++++----- 5 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/app/group-monitor/booklet.service.spec.ts b/src/app/group-monitor/booklet.service.spec.ts index 719ddf26..d6dacc9e 100644 --- a/src/app/group-monitor/booklet.service.spec.ts +++ b/src/app/group-monitor/booklet.service.spec.ts @@ -4,7 +4,6 @@ import { TestBed } from '@angular/core/testing'; import { Observable, of } from 'rxjs'; import { BookletService } from './booklet.service'; import { BackendService } from './backend.service'; -import { TestSessionService } from './test-session.service'; import { unitTestExampleBooklets } from './test-data.spec'; class MockBackendService { @@ -41,31 +40,14 @@ describe('BookletService', () => { expect(BookletService.getFirstUnit(unitTestExampleBooklets.example_booklet_2.units.children[2])).toBeNull(); }); - describe('getNextBlock()', () => { - // eslint-disable-next-line @typescript-eslint/dot-notation,prefer-destructuring - const getCurrent = TestSessionService['getCurrent']; - - it('should get next block at root-level, when blockless unit is selected', () => { - const result = BookletService.getNextBlock( - getCurrent(unitTestExampleBooklets.example_booklet_1.units, 'unit-1'), - unitTestExampleBooklets.example_booklet_1 - ); - expect(result.id).toEqual('zara'); - }); - - it('should get next block at root-level, when unit in nested testlet is selected', () => { - const result = BookletService.getNextBlock( - getCurrent(unitTestExampleBooklets.example_booklet_1.units, 'unit-3'), - unitTestExampleBooklets.example_booklet_1 - ); - expect(result.id).toEqual('ellie'); + describe('getBlockById()', () => { + it('should return the block by id', () => { + const result = BookletService.getBlockById('block-2', unitTestExampleBooklets.example_booklet_1); + expect(result.id).toEqual('alf'); }); - it('should return null, if there is no next block on root-level', () => { - const result = BookletService.getNextBlock( - getCurrent(unitTestExampleBooklets.example_booklet_1.units, 'unit-9'), - unitTestExampleBooklets.example_booklet_1 - ); + it('should return null when blockId is not found in booklet', () => { + const result = BookletService.getBlockById('not-existing', unitTestExampleBooklets.example_booklet_1); expect(result).toBeNull(); }); }); diff --git a/src/app/group-monitor/booklet.service.ts b/src/app/group-monitor/booklet.service.ts index d608f6d5..7b192bf6 100644 --- a/src/app/group-monitor/booklet.service.ts +++ b/src/app/group-monitor/booklet.service.ts @@ -6,7 +6,7 @@ import { map, shareReplay } from 'rxjs/operators'; import { MainDataService } from '../maindata.service'; import { BackendService } from './backend.service'; import { - Booklet, BookletError, BookletMetadata, isUnit, Restrictions, Testlet, Unit, UnitContext + Booklet, BookletError, BookletMetadata, isUnit, Restrictions, Testlet, Unit } from './group-monitor.interfaces'; // eslint-disable-next-line import/extensions import { BookletConfig } from '../config/booklet-config'; @@ -40,27 +40,20 @@ export class BookletService { return null; } - static getNextBlock(current: UnitContext, booklet: Booklet): Testlet|null { - if (!current.ancestor) { - return null; - } - const startIndex = !current.ancestor.id ? - booklet.units.children.indexOf(current.unit) : booklet.units.children.indexOf(current.ancestor); - for (let i = startIndex + 1; i < booklet.units.children.length; i++) { - if (!isUnit(booklet.units.children[i])) { - return <Testlet>booklet.units.children[i]; - } - } - return null; + static getBlockById(blockId: string, booklet: Booklet): Testlet { + return <Testlet>booklet.units.children + .filter(testletOrUnit => !isUnit(testletOrUnit)) + .reduce((found: Testlet, block: Testlet) => ((block.blockId === blockId) ? block : found), null); } static addBookletStructureInformation(booklet: Booklet): void { booklet.species = BookletService.getBookletSpecies(booklet); booklet.units.children .filter(testletOrUnit => !isUnit(testletOrUnit)) - .forEach((testletOrUnit, index) => { - if (!isUnit(testletOrUnit)) { - testletOrUnit.blockId = `block ${index + 1}`; + .forEach((block: Testlet, index, blocks) => { + block.blockId = `block ${index + 1}`; + if (index < blocks.length - 1) { + block.nextBlockId = `block ${index + 2}`; } }); } diff --git a/src/app/group-monitor/group-monitor.component.ts b/src/app/group-monitor/group-monitor.component.ts index 3f0d8f38..ccd98042 100644 --- a/src/app/group-monitor/group-monitor.component.ts +++ b/src/app/group-monitor/group-monitor.component.ts @@ -14,9 +14,10 @@ import { BackendService } from './backend.service'; import { GroupData, TestViewDisplayOptions, - TestViewDisplayOptionKey, Selected, TestSession, TestSessionSetStats, CommandResponse, UIMessage + TestViewDisplayOptionKey, Selected, TestSession, TestSessionSetStats, CommandResponse, UIMessage, isBooklet } from './group-monitor.interfaces'; import { GroupMonitorService } from './group-monitor.service'; +import { BookletService } from './booklet.service'; @Component({ selector: 'app-group-monitor', @@ -212,10 +213,30 @@ export class GroupMonitorComponent implements OnInit, OnDestroy { text: 'Kein Zielblock ausgewählt' }); } else { - this.gms.testCommandGoto(this.selectedElement); + this.gms.testCommandGoto(this.selectedElement) + .subscribe(() => this.selectNextBlock()); } } + private selectNextBlock(): void { + if (!isBooklet(this.selectedElement.originSession.booklet)) { + return; + } + // if (!this.selectedElement.element.nextBlockId) { + // return; + // } + this.selectElement({ + element: this.selectedElement.element.nextBlockId ? + BookletService.getBlockById( + this.selectedElement.element.nextBlockId, + this.selectedElement.originSession.booklet + ) : null, + inversion: this.selectedElement.inversion, + originSession: this.selectedElement.originSession, + spreading: this.selectedElement.spreading + }); + } + unlockCommand(): void { this.gms.testCommandUnlock(); } diff --git a/src/app/group-monitor/group-monitor.interfaces.ts b/src/app/group-monitor/group-monitor.interfaces.ts index 8d086bbd..90a801d4 100644 --- a/src/app/group-monitor/group-monitor.interfaces.ts +++ b/src/app/group-monitor/group-monitor.interfaces.ts @@ -67,6 +67,7 @@ export interface Testlet { children: (Unit|Testlet)[]; descendantCount: number; blockId?: string; + nextBlockId?: string; } export interface Unit { diff --git a/src/app/group-monitor/group-monitor.service.ts b/src/app/group-monitor/group-monitor.service.ts index ac2afc17..4db7e54f 100644 --- a/src/app/group-monitor/group-monitor.service.ts +++ b/src/app/group-monitor/group-monitor.service.ts @@ -272,17 +272,20 @@ export class GroupMonitorService { ); } - testCommandGoto(selection: Selected): void { + testCommandGoto(selection: Selected): Observable<true> { const gfd = GroupMonitorService.groupForGoto(this.checked, selection); const allTestIds = this.checked.map(s => s.data.testId); - zip( + return zip( ...Object.keys(gfd).map(key => this.bs.command('goto', ['id', gfd[key].firstUnitId], gfd[key].testIds)) - ).subscribe(() => { - this._commandResponses$.next({ - commandType: 'goto', - testIds: allTestIds - }); - }); + ).pipe( + tap(() => { + this._commandResponses$.next({ + commandType: 'goto', + testIds: allTestIds + }); + }), + map(() => true) + ); } private static groupForGoto(sessionsSet: TestSession[], selection: Selected): GotoCommandData { -- GitLab