]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/marcrecord.ts
LP1849212: Users can attach brief bib records and e-resources to courses
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / marcrecord.ts
1 import {EventEmitter} from '@angular/core';
2
3 /* Wrapper class for our external MARC21.Record JS library. */
4
5 declare var MARC21;
6
7 // MARC breaker delimiter
8 const DELIMITER = '$';
9
10 export interface MarcSubfield    // code, value, position
11     extends Array<string|number> { 0: string; 1: string; 2: number; }
12
13 // Only contains the attributes/methods we need so far.
14 export interface MarcField {
15     fieldId?: number;
16     data?: string;
17     tag?: string;
18     ind1?: string;
19     ind2?: string;
20     subfields?: MarcSubfield[];
21
22     // For authority validation
23     authValid: boolean;
24     authChecked: boolean;
25
26     // Fields are immutable when it comes to controlfield vs.
27     // data field.  Stamp the value when stamping field IDs.
28     isCtrlField: boolean;
29
30     // Pass-through to marcrecord.js
31     isControlfield(): boolean;
32     indicator?: (ind: number) => any;
33
34     deleteExactSubfields(...subfield: MarcSubfield[]): number;
35 }
36
37 export class MarcRecord {
38
39     id: number; // Database ID when known.
40     deleted: boolean;
41     record: any; // MARC21.Record object
42     breakerText: string;
43
44     // Let clients know some fixed field shuffling may have occured.
45     // Emits the fixed field code.
46     fixedFieldChange: EventEmitter<string>;
47
48     get leader(): string {
49         return this.record.leader;
50     }
51
52     set leader(l: string) {
53         this.record.leader = l;
54     }
55
56     get fields(): MarcField[] {
57        return this.record.fields;
58     }
59
60     set fields(f: MarcField[]) {
61         this.record.fields = f;
62     }
63
64     constructor(xml?: string) {
65         this.record = new MARC21.Record({marcxml: xml, delimiter: DELIMITER});
66         this.breakerText = this.record.toBreaker();
67         this.fixedFieldChange = new EventEmitter<string>();
68     }
69
70     toXml(): string {
71         return this.record.toXmlString();
72     }
73
74     toBreaker(): string {
75         return this.record.toBreaker();
76     }
77
78     recordType(): string {
79         return this.record.recordType();
80     }
81
82     absorbBreakerChanges() {
83         this.record = new MARC21.Record(
84             {marcbreaker: this.breakerText, delimiter: DELIMITER});
85     }
86
87     extractFixedField(fieldCode: string): string {
88         return this.record.extractFixedField(fieldCode);
89     }
90
91     setFixedField(fieldCode: string, fieldValue: string): string {
92         const response = this.record.setFixedField(fieldCode, fieldValue);
93         this.fixedFieldChange.emit(fieldCode);
94         return response;
95     }
96
97     // Give each field an identifier so it may be referenced later.
98     stampFieldIds() {
99         this.fields.forEach(f => this.stampFieldId(f));
100     }
101
102     // Stamp field IDs the the initial isCtrlField state.
103     stampFieldId(field: MarcField) {
104         if (!field.fieldId) {
105             field.fieldId = Math.floor(Math.random() * 10000000);
106         }
107
108         if (field.isCtrlField === undefined) {
109             field.isCtrlField = field.isControlfield();
110         }
111     }
112
113     field(spec: string, wantArray?: boolean): MarcField | MarcField[] {
114         return this.record.field(spec, wantArray);
115     }
116
117     appendFields(...newFields: MarcField[]) {
118         this.record.appendFields.apply(this.record, newFields);
119         this.stampFieldIds();
120     }
121
122     insertFieldsBefore(field: MarcField, ...newFields: MarcField[]) {
123         this.record.insertFieldsBefore.apply(
124             this.record, [field].concat(newFields));
125         this.stampFieldIds();
126     }
127
128     insertFieldsAfter(field: MarcField, ...newFields: MarcField[]) {
129         this.record.insertFieldsAfter.apply(
130             this.record, [field].concat(newFields));
131         this.stampFieldIds();
132     }
133
134     insertOrderedFields(...newFields: MarcField[]) {
135         this.record.insertOrderedFields.apply(this.record, newFields);
136         this.stampFieldIds();
137     }
138
139     generate008(): MarcField {
140         return this.record.generate008();
141     }
142
143
144     deleteFields(...fields: MarcField[]) {
145         this.record.deleteFields.apply(this.record, fields);
146     }
147
148     getField(id: number): MarcField {
149         return this.fields.filter(f => f.fieldId === id)[0];
150     }
151
152     getPreviousField(id: number): MarcField {
153         for (let idx = 0; idx < this.fields.length; idx++) {
154             if (this.fields[idx].fieldId === id) {
155                 return this.fields[idx - 1];
156             }
157         }
158     }
159
160     getNextField(id: number): MarcField {
161         for (let idx = 0; idx < this.fields.length; idx++) {
162             if (this.fields[idx].fieldId === id) {
163                 return this.fields[idx + 1];
164             }
165         }
166     }
167
168     // Turn an field-ish object into a proper MARC.Field
169     newField(props: any): MarcField {
170         const field = new MARC21.Field(props);
171         this.stampFieldId(field);
172         return field;
173     }
174
175     cloneField(field: any): MarcField {
176         const props: any = {tag: field.tag};
177
178         if (field.isControlfield()) {
179             props.data = field.data;
180
181         } else {
182             props.ind1 = field.ind1;
183             props.ind2 = field.ind2;
184             props.subfields = this.cloneSubfields(field.subfields);
185         }
186
187         return this.newField(props);
188     }
189
190     cloneSubfields(subfields: MarcSubfield[]): MarcSubfield[] {
191         const root = [];
192         subfields.forEach(sf => root.push([].concat(sf)));
193         return root;
194     }
195
196     // Returns a list of values for the tag + subfield combo
197     subfield(tag: string, subfield: string): string {
198         return this.record.subfield(tag, subfield);
199     }
200 }
201