1 import {Component, Input, Output, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {map, mergeMap} from 'rxjs/operators';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {EventService, EgEvent} from '@eg/core/event.service';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {DialogComponent} from '@eg/share/dialog/dialog.component';
13 /* Support barcode completion for barcoded asset/actor data.
15 * When multiple barcodes match, the user is presented with a selection
16 * dialog to chose the desired barcode.
18 * <eg-barcode-select #barcodeSelect></eg-barcode-select>
20 * @ViewChild('barcodeSelect') private barcodeSelect: BarcodeSelectComponent;
22 * this.barcodeSelect.getBarcode(value)
23 * .then(barcode => console.log('found barcode', barcode));
26 export interface BarcodeSelectResult {
28 // Will be the originally requested barcode when no match is found.
31 // Will be null when no match is found.
36 selector: 'eg-barcode-select',
37 templateUrl: './barcode-select.component.html',
40 export class BarcodeSelectComponent extends DialogComponent {
42 matches: BarcodeSelectResult[];
43 selected: BarcodeSelectResult;
44 inputs: {[id: number]: boolean};
47 private modal: NgbModal,
48 private evt: EventService,
49 private org: OrgService,
50 private net: NetService,
51 private pcrud: PcrudService,
52 private auth: AuthService
56 const id = Object.keys(this.inputs).map(i => Number(i))
57 .filter(i => this.inputs[i] === true)[0];
60 this.selected = this.matches.filter(match => match.id === id)[0];
67 // Returns promise of barcode
68 // When multiple barcodes match, the user is asked to select one.
69 // Returns promise of null if no match is found or the user cancels
70 // the selection process.
71 getBarcode(class_: 'asset' | 'actor',
72 barcode: string): Promise<BarcodeSelectResult> {
77 const result: BarcodeSelectResult = {
82 let promise = this.net.request(
84 'open-ils.actor.get_barcodes',
85 this.auth.token(), this.auth.user().ws_ou(),
86 class_, barcode.trim()
89 promise = promise.then(results => {
91 if (!results) { return result; }
93 results.forEach(res => {
94 if (!this.evt.parse(res)) {
95 this.matches.push(res);
99 if (this.matches.length === 0) {
102 } else if (this.matches.length === 1) {
103 return this.matches[0];
106 return this.open().toPromise();