This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperson.ts
166 lines (148 loc) · 5.95 KB
/
person.ts
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
import { EditorState } from 'prosemirror-state';
import { DOMParser as ProseMirrorDOMParser } from 'prosemirror-model';
import { gapCursor } from 'prosemirror-gapcursor';
import { history } from 'prosemirror-history';
import { dropCursor } from 'prosemirror-dropcursor';
import { keymap } from 'prosemirror-keymap';
import { baseKeymap } from 'prosemirror-commands';
import * as bioConfig from './config/author-bio.config';
import { Affiliation } from './affiliation';
import { getTextContentFromPath, makeSchemaFromConfig } from './utils';
import { buildInputRules } from './plugins/input-rules';
import { BackmatterEntity } from './backmatter-entity';
import { JSONObject } from '../types/utility.types';
import { SelectionPlugin } from './plugins/selection.plugins';
import { v5 } from 'uuid';
// required for creating consistant uuids from a string
const UUID_NAMESPACE = '123e4567-e89b-12d3-a456-426614174000';
export class Person extends BackmatterEntity {
firstName: string;
lastName: string;
suffix?: string;
isAuthenticated?: boolean;
orcid?: string;
email?: string;
bio?: EditorState;
isCorrespondingAuthor?: boolean;
affiliations?: string[];
hasCompetingInterest?: boolean;
competingInterestStatement?: string;
constructor(data?: JSONObject | Element, notesXml?: Element) {
super();
this.createEntity(data);
if (data instanceof Element && notesXml instanceof Element) {
this.setCompetingInterests(data, notesXml);
}
}
public getDisplayName(): string {
return [this.firstName, this.lastName, this.suffix].filter((_) => _).join(' ');
}
public getAffiliationsLabels(affiliations: Affiliation[]): string[] {
if (affiliations.length <= 1) {
return [];
}
return this.affiliations
.map((affiliationId) => {
const affiliation = affiliations.find(({ id }) => id === affiliationId);
return affiliation ? affiliation.label : undefined;
})
.filter(Boolean)
.sort((a, b) => Number(a) - Number(b));
}
clone(): Person {
const person = new Person();
person._id = this._id;
person.firstName = this.firstName;
person.lastName = this.lastName;
person.suffix = this.suffix;
person.isAuthenticated = this.isAuthenticated;
person.orcid = this.orcid;
person.email = this.email;
person.bio = this.bio;
person.isCorrespondingAuthor = this.isCorrespondingAuthor;
person.affiliations = [...this.affiliations];
person.hasCompetingInterest = this.hasCompetingInterest;
person.competingInterestStatement = this.competingInterestStatement;
return person;
}
protected fromXML(xml: Element): void {
const orcidEl = xml.querySelector('contrib-id[contrib-id-type="orcid"]');
this.firstName = getTextContentFromPath(xml, 'name > given-names');
this.lastName = getTextContentFromPath(xml, 'name > surname');
this.suffix = getTextContentFromPath(xml, 'name > suffix');
this.isAuthenticated = orcidEl ? orcidEl.getAttribute('authenticated') === 'true' : false;
this.orcid = orcidEl ? this.getOrcid(orcidEl.textContent) : '';
this.bio = this.createBioEditorStateFromXml(xml.querySelector('bio'));
this.email = getTextContentFromPath(xml, 'email');
this.isCorrespondingAuthor = xml.getAttribute('corresp') === 'yes';
this.affiliations = Array.from(xml.querySelectorAll('xref[ref-type="aff"]')).map((xRef) =>
xRef.getAttribute('rid')
);
this._id = v5(xml.textContent || '', UUID_NAMESPACE);
}
protected fromJSON(json: JSONObject): void {
this._id = (json._id as string) || this.id;
this.firstName = json.firstName as string;
this.lastName = json.lastName as string;
this.suffix = json.suffix as string;
this.isAuthenticated = json.isAuthenticated as boolean;
this.orcid = json.orcid as string;
this.bio = json.bio
? this.createBioEditorStateFromJSON(json.bio as JSONObject)
: this.createBioEditorStateFromXml();
this.email = json.email as string;
this.isCorrespondingAuthor = json.isCorrespondingAuthor as boolean;
this.affiliations = (json.affiliations as string[]) || [];
}
protected createBlank(): void {
this.firstName = '';
this.lastName = '';
this.suffix = '';
this.isAuthenticated = false;
this.orcid = '';
this.bio = this.createBioEditorStateFromXml();
this.email = '';
this.isCorrespondingAuthor = false;
this.affiliations = [];
}
private getOrcid(orcidUrl: string): string {
const matches = orcidUrl.match(/(([0-9]{4}-?){4})/g);
if (matches) {
return matches[0];
}
return '';
}
private setCompetingInterests(dataXml: Element, notesXml: Element): void {
const competingInterestEl = Array.from(notesXml.querySelectorAll('[fn-type="COI-statement"]')).find(
(fnEl: Element) => {
const id = fnEl.getAttribute('id');
return dataXml.querySelector(`xref[ref-type="author-notes"][rid="${id}"]`);
}
);
this.hasCompetingInterest = competingInterestEl
? competingInterestEl.textContent !== 'No competing interests declared'
: false;
this.competingInterestStatement = competingInterestEl ? competingInterestEl.textContent.trim() : '';
}
private createBioEditorStateFromXml(bio?: Element): EditorState {
const schema = makeSchemaFromConfig(bioConfig.topNode, bioConfig.nodes, bioConfig.marks);
return EditorState.create({
doc: bio ? ProseMirrorDOMParser.fromSchema(schema).parse(bio) : undefined,
schema,
plugins: [buildInputRules(), gapCursor(), dropCursor(), keymap(baseKeymap), history(), SelectionPlugin]
});
}
private createBioEditorStateFromJSON(json: JSONObject): EditorState {
const blankState = this.createBioEditorStateFromXml();
return EditorState.fromJSON(
{
schema: blankState.schema,
plugins: blankState.plugins
},
json
);
}
}
export function createAuthorsState(authorsXml: Element[], notesXml: Element | undefined): Person[] {
return authorsXml.map((xml) => new Person(xml, notesXml));
}