forked from OpenPecha/Lopenling-App-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnotation.js
279 lines (248 loc) · 6.98 KB
/
Annotation.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// @flow
import uuidV4 from "uuid/v4";
import Witness from "lib/Witness";
import User from "lib/User";
export const ANNOTATION_TYPES: { [string]: string } = {
variant: "V",
note: "N",
marker: "M",
pageBreak: "P",
lineBreak: "L",
question: "Q",
};
export const TEMPORARY_TYPE: string = "T";
export function getNaturalId(
type: string,
creatorUserId: number | null,
creatorWitnessId: number,
workingWitnessId: number,
start: number,
length: number
) {
//const creatorType = (userCreated) ? 'U' : 'W';
return [
type,
creatorUserId,
creatorWitnessId,
workingWitnessId,
start,
length,
].join("-");
}
export function getUniqueId() {
return uuidV4();
}
export type AnnotationUniqueId = string;
export default class Annotation {
_id: number | null;
witness: Witness;
start: number;
length: number;
content: string;
creatorWitness: Witness;
creatorUser: User | null;
type: string;
_uniqueId: AnnotationUniqueId | null;
basedOn: Annotation | null;
_isSaved: boolean;
modified: ?Date;
/**
* Text annotation
* @param {number|null} id
* @param {Witness} workingWitness
* @param {number} start
* @param {number} length
* @param {string} content
* @param {string} [type=ANNOTATION_TYPES.variant] - one of ANNOTATION_TYPES
* @param {Witness} creatorWitness
* @param {User|null} creatorUser
* @param {string|null} [uniqueId=null] - UUID4. Generates a new one if not provided.
* @param {Annotation|null} [basedOn=null] - The annotation this is based on (if any).
* @param {string|null} created
*/
constructor(
id: number | null,
workingWitness: Witness,
start: number,
length: number,
content: string | null,
type: string = ANNOTATION_TYPES.variant,
creatorWitness: Witness,
creatorUser: User | null = null,
uniqueId: AnnotationUniqueId | null = null,
basedOn: Annotation | null = null,
created: String | null = null
) {
this._id = id;
this.witness = workingWitness;
if (!workingWitness.isWorking) {
//console.warn("Set workingWitness that is not a working witness: %o, %o", this, workingWitness);
}
this.start = Number(start);
this.length = Number(length);
if (!content) content = "";
this.content = content;
this.creatorWitness = creatorWitness;
this.creatorUser = creatorUser;
this.type = type;
this._uniqueId = uniqueId;
this.basedOn = basedOn;
this._isSaved = false;
this.created = created;
}
get id(): number | null {
return this._id;
}
set id(newId: string | number) {
this._id = Number(newId);
}
get naturalId(): string {
const creatorWitnessId = this.creatorWitness
? this.creatorWitness.id
: 0;
const creatorUserId = this.creatorUser ? this.creatorUser.id : 0;
const witnessId = this.witness ? this.witness.id : 0;
return getNaturalId(
this.type,
creatorUserId,
creatorWitnessId,
witnessId,
this.start,
this.length
);
}
get uniqueId(): string {
if (this._uniqueId === null) {
this._uniqueId = getUniqueId();
}
return this._uniqueId;
}
set uniqueId(newUniqueId: AnnotationUniqueId) {
this._uniqueId = newUniqueId;
}
get isTemporary(): boolean {
return false;
}
toString(): string {
return [this.id, this.start, this.length, this.content].join("_");
}
get creator(): Witness | User {
if (this.creatorUser) {
return this.creatorUser;
} else {
return this.creatorWitness;
}
}
getSourceName(): string {
if (this.creatorUser) {
return this.creatorUser.name;
} else {
// is witness
return this.creatorWitness.source.name;
}
}
get contentEnd(): number {
let end = this.start;
if (this.content && this.content.length > 0)
end += this.content.length - 1;
return end;
}
get end(): number {
let end = this.start;
if (this.length > 0) end += this.length - 1;
return end;
}
isWithinRange(start: number, length: number): boolean {
const rangeEnd = start + length - 1;
if (
(this.start <= start &&
this.contentEnd >= start &&
this.contentEnd <= rangeEnd) ||
(this.start >= start && this.contentEnd <= rangeEnd) ||
(this.start <= rangeEnd && this.contentEnd >= rangeEnd)
) {
return true;
} else {
return false;
}
}
get isVariant(): boolean {
return this.type === ANNOTATION_TYPES.variant;
}
get isInsertion(): boolean {
return this.isVariant && Number(this.length) === 0;
}
get isDeletion(): boolean {
return (
!this.isInsertion &&
this.isVariant &&
(!this.content || this.content.length === 0)
);
}
isType(type: string) {
return this.type === type;
}
get userCreated(): boolean {
return !(this.creatorUser === null);
}
get isSaved(): boolean {
return this._isSaved;
}
set isSaved(newIsSaved: boolean) {
this._isSaved = newIsSaved;
}
save() {
this._isSaved = true;
}
get isBaseAnnotation(): boolean {
return !this.userCreated && this.creatorWitness.isBase;
}
get isWorkingAnnotation(): boolean {
return !this.userCreated && this.creatorWitness.isWorking;
}
}
/**
* Intended to be used when editing/adding an annotation.
*/
export class TemporaryAnnotation extends Annotation {
/**
*
* @param {Annotation} basedOn - The annotation this is amending. Set to null if new.
* @param {Witness} witness
* @param {number} start
* @param {number} length
* @param {string} content
* @param {string} type - one of ANNOTATION_TYPES
* @param {Witness|User|null} creatorWitness
* @param {string|null} uniqueId - UUID4
*/
constructor(
basedOn: Annotation | null,
witness: Witness,
start: number,
length: number,
content: string | null,
type: string = ANNOTATION_TYPES.variant,
creatorWitness: Witness,
creatorUser: User | null = null,
uniqueId: string | null = null,
created: string | null = null
) {
super(
null,
witness,
start,
length,
content,
type,
creatorWitness,
creatorUser,
uniqueId,
created
);
this.basedOn = basedOn;
}
get isTemporary(): boolean {
return true;
}
}