Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 1x 2x 2x 5x | import { Injectable } from '@angular/core'; import { Observable, of } from 'rxjs'; import { map, shareReplay } from 'rxjs/operators'; import { BackendService } from '../backend.service'; import { Booklet, BookletError, BookletMetadata, isTestlet, isUnit, Restrictions, Testlet, Unit } from '../group-monitor.interfaces'; import { BookletConfig } from '../../shared/classes/booklet-config.class'; @Injectable() export class BookletService { booklets: { [k: string]: Observable<Booklet | BookletError> } = {}; constructor( private bs: BackendService ) { } getBooklet(bookletName = ''): Observable<Booklet | BookletError> { Iif (typeof this.booklets[bookletName] !== 'undefined') { return this.booklets[bookletName]; } if (bookletName === '') { this.booklets[bookletName] = of<Booklet | BookletError>({ error: 'missing-id', species: null }); } else { this.booklets[bookletName] = this.bs.getBooklet(bookletName) .pipe( // eslint-disable-next-line max-len map((response: string | BookletError) => (typeof response === 'string' ? BookletService.parseBookletXml(response) : response)), shareReplay(1) ); } return this.booklets[bookletName]; } private static parseBookletXml(xmlString: string): Booklet | BookletError { try { const domParser = new DOMParser(); const xmlStringWithOutBom = xmlString.replace(/^\uFEFF/gm, ''); const bookletElement = domParser.parseFromString(xmlStringWithOutBom, 'text/xml').documentElement; Iif (bookletElement.nodeName !== 'Booklet') { // console.warn('XML-root is not `Booklet`'); return { error: 'xml', species: null }; } const units = BookletService.xmlGetChildIfExists(bookletElement, 'Units'); Iif (units == null) { return { error: 'xml', species: null }; } const metadata = BookletService.parseMetadata(bookletElement); Iif (metadata == null) { return { error: 'xml', species: null }; } const parsedBooklet: Booklet = { units: BookletService.parseTestlet(units), metadata: metadata, config: BookletService.parseBookletConfig(bookletElement), species: '' }; BookletService.addBookletStructureInformation(parsedBooklet); return parsedBooklet; } catch (error) { // console.warn('Error reading booklet XML:', error); return { error: 'xml', species: null }; } } private static addBookletStructureInformation(booklet: Booklet): void { booklet.species = BookletService.getBookletSpecies(booklet); booklet.units.children .filter(isTestlet) .forEach((block: Testlet, index, blocks) => { block.blockId = `block ${index + 1}`; Iif (index < blocks.length - 1) { block.nextBlockId = `block ${index + 2}`; } }); } private static getBookletSpecies(booklet: Booklet): string { return `species: ${booklet.units.children.filter(testletOrUnit => !isUnit(testletOrUnit)).length}`; } private static parseBookletConfig(bookletElement: Element): BookletConfig { const bookletConfigElements = BookletService.xmlGetChildIfExists(bookletElement, 'BookletConfig', true); const bookletConfig = new BookletConfig(); Iif (bookletConfigElements) { bookletConfig.setFromXml(bookletConfigElements); } return bookletConfig; } private static parseMetadata(bookletElement: Element): BookletMetadata | null { const metadataElement = BookletService.xmlGetChildIfExists(bookletElement, 'Metadata'); Iif (!metadataElement) { return null; } return { id: BookletService.xmlGetChildTextIfExists(metadataElement, 'Id'), label: BookletService.xmlGetChildTextIfExists(metadataElement, 'Label'), description: BookletService.xmlGetChildTextIfExists(metadataElement, 'Description', true) }; } private static parseTestlet(testletElement: Element): Testlet { return { id: testletElement.getAttribute('id') || '', label: testletElement.getAttribute('label') || '', restrictions: BookletService.parseRestrictions(testletElement), children: BookletService.xmlGetDirectChildrenByTagName(testletElement, ['Unit', 'Testlet']) .map(BookletService.parseUnitOrTestlet), descendantCount: BookletService.xmlCountChildrenOfTagNames(testletElement, ['Unit']) }; } private static parseUnitOrTestlet(unitOrTestletElement: Element): (Unit | Testlet) { Iif (unitOrTestletElement.tagName === 'Unit') { return { id: unitOrTestletElement.getAttribute('alias') || unitOrTestletElement.getAttribute('id') || '', label: unitOrTestletElement.getAttribute('label') || '', labelShort: unitOrTestletElement.getAttribute('labelshort') || '' }; } return BookletService.parseTestlet(unitOrTestletElement); } private static parseRestrictions(testletElement: Element): Restrictions { const restrictions: Restrictions = {}; const restrictionsElement = BookletService.xmlGetChildIfExists(testletElement, 'Restrictions', true); Iif (!restrictionsElement) { return restrictions; } const codeToEnterElement = restrictionsElement.querySelector('CodeToEnter'); Iif (codeToEnterElement) { restrictions.codeToEnter = { code: codeToEnterElement.getAttribute('code') || '', message: codeToEnterElement.textContent || '' }; } const timeMaxElement = restrictionsElement.querySelector('TimeMax'); Iif (timeMaxElement) { restrictions.timeMax = { minutes: parseFloat(timeMaxElement.getAttribute('minutes') || '') }; } return restrictions; } private static xmlGetChildIfExists(element: Element, childName: string, isOptional = false): Element | null { const elements = BookletService.xmlGetDirectChildrenByTagName(element, [childName]); Iif (!elements.length && !isOptional) { throw new Error(`Missing field: '${childName}'`); } return elements.length ? elements[0] : null; } private static xmlGetChildTextIfExists(element: Element, childName: string, isOptional = false): string { const childElement = BookletService.xmlGetChildIfExists(element, childName, isOptional); return (childElement && childElement.textContent) ? childElement.textContent : ''; } private static xmlGetDirectChildrenByTagName(element: Element, tagNames: string[]): Element[] { return [].slice.call(element.childNodes) .filter((elem: Element) => (elem.nodeType === 1)) .filter((elem: Element) => (tagNames.indexOf(elem.tagName) > -1)); } private static xmlCountChildrenOfTagNames(element: Element, tagNames: string[]): number { return element.querySelectorAll(tagNames.join(', ')).length; } } |