1 import {Component, OnInit, AfterViewInit, ViewChild, Input, Output, EventEmitter} from '@angular/core';
2 import {tap} from 'rxjs/operators';
3 import {Pager} from '@eg/share/util/pager';
4 import {IdlObject, IdlService} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {LineitemService} from './lineitem.service';
8 import {ComboboxComponent, ComboboxEntry} from '@eg/share/combobox/combobox.component';
9 import {ItemLocationService} from '@eg/share/item-location-select/item-location-select.service';
10 import {ItemLocationSelectComponent} from '@eg/share/item-location-select/item-location-select.component';
13 templateUrl: 'copy-attrs.component.html',
14 selector: 'eg-lineitem-copy-attrs'
16 export class LineitemCopyAttrsComponent implements OnInit {
18 @Input() lineitem: IdlObject;
19 fundEntries: ComboboxEntry[];
20 circModEntries: ComboboxEntry[];
22 private _copy: IdlObject;
23 @Input() set copy(c: IdlObject) { // acqlid
24 if (c === undefined) {
26 } else if (c === null) {
29 // Enture cbox entries are populated before the copy is
30 // applied so the cbox has the minimal set of values it
31 // needs at copy render time.
32 this.setInitialOptions(c);
37 get copy(): IdlObject {
41 // A row of batch edit inputs
42 @Input() batchMode = false;
44 // One of several rows embedded in the main LI list page.
46 @Input() embedded = false;
48 // Emits an 'acqlid' object;
49 @Output() batchApplyRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
50 @Output() deleteRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
51 @Output() receiveRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
52 @Output() unReceiveRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
53 @Output() cancelRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
55 @ViewChild('locationSelector') locationSelector: ItemLocationSelectComponent;
56 @ViewChild('circModSelector') circModSelector: ComboboxComponent;
57 @ViewChild('fundSelector') fundSelector: ComboboxComponent;
60 private idl: IdlService,
61 private net: NetService,
62 private auth: AuthService,
63 private loc: ItemLocationService,
64 private liService: LineitemService
69 if (this.batchMode) { // stub batch copy
70 this.copy = this.idl.create('acqlid');
71 this.copy.isnew(true);
75 // When a batch selector value changes, duplicate the selected
76 // value into our selector entries, so if/when the value is
77 // chosen we (and our pile of siblings) are not required to
78 // re-fetch them from the server.
79 this.liService.batchOptionWanted.subscribe(option => {
80 const field = Object.keys(option)[0];
81 if (field === 'location') {
82 this.locationSelector.comboBox.addAsyncEntry(option[field]);
83 } else if (field === 'circ_modifier') {
84 this.circModSelector.addAsyncEntry(option[field]);
85 } else if (field === 'fund') {
86 this.fundSelector.addAsyncEntry(option[field]);
92 valueChange(field: string, entry: ComboboxEntry) {
94 const announce: any = {};
95 this.copy.ischanged(true);
101 case 'collection_code':
102 this.copy[field](entry);
106 this.copy[field](entry ? entry.id() : null);
110 this.copy[field](entry ? entry.id() : null);
111 if (this.batchMode) {
112 announce[field] = entry;
113 this.liService.batchOptionWanted.emit(announce);
117 case 'circ_modifier':
119 this.copy[field](entry ? entry.id : null);
120 if (this.batchMode) {
121 announce[field] = entry;
122 this.liService.batchOptionWanted.emit(announce);
128 // Tell our inputs about the values we know we need
129 // Values will be pre-cached in the liService
130 setInitialOptions(copy: IdlObject) {
133 const fund = this.liService.fundCache[copy.fund()];
134 this.fundEntries = [{id: fund.id(), label: fund.code(), fm: fund}];
137 if (copy.circ_modifier()) {
138 const mod = this.liService.circModCache[copy.circ_modifier()];
139 this.circModEntries = [{id: mod.code(), label: mod.name(), fm: mod}];
143 fieldIsDisabled(field: string) {
144 if (this.batchMode) { return false; }
146 if (this.embedded || // inline expandy view
147 this.copy.isdeleted() ||
148 this.disposition() !== 'pre-order') {
155 disposition(): 'canceled' | 'delayed' | 'received' | 'on-order' | 'pre-order' {
156 if (!this.copy || !this.lineitem) {
158 } else if (this.copy.cancel_reason()) {
159 if (this.copy.cancel_reason().keep_debits() === 't') {
164 } else if (this.copy.recv_time()) {
166 } else if (this.lineitem.state() === 'on-order') {
168 } else { return 'pre-order'; }