-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathentitiesManager.js
479 lines (420 loc) · 15.6 KB
/
entitiesManager.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
'use strict';
var $ = require('jquery');
var Entity = require('./entity');
/**
* @class EntitiesManager
* @param {Writer} writer
*/
function EntitiesManager(writer) {
this.w = writer;
this.reset();
this.w.event('processingDocument').subscribe(function() {
this.reset();
}.bind(this));
this.w.event('entityAdded').subscribe(function(entityId) {
// don't highlight the entity because we might be doing bulk additions
// this.highlightEntity(entityId);
}.bind(this));
this.w.event('entityEdited').subscribe(function(entityId) {
// don't highlight the entity because it will move the cursor outside of the entity when the user is editing
// this.highlightEntity(entityId);
}.bind(this));
this.w.event('entityRemoved').subscribe(function(entityId) {
this.highlightEntity();
}.bind(this));
this.w.event('entityPasted').subscribe(function(entityId) {
this.highlightEntity(entityId);
}.bind(this));
}
EntitiesManager.prototype = {
constructor: EntitiesManager,
/**
* Creates and adds an entity to the collection.
* @fires Writer#entityAdded
* @param {Object|Entity} config The entity config.
* @param {Range} [range] If a range is provided, the actual tag is also added
* @returns {Entity} The newly created Entity
*/
addEntity: function(config, range) {
var entity;
if (config.constructor.name === 'Entity') {
entity = config;
} else {
if (config.id === undefined) {
config.id = this.w.getUniqueId('dom_');
}
if (config.tag === undefined) {
config.tag = this.w.schemaManager.mapper.getParentTag(config.type);
}
entity = new Entity(config);
}
var requiredAttributes = this.w.schemaManager.mapper.getRequiredAttributes(config.type);
for (var attName in requiredAttributes) {
entity.setAttribute(attName, requiredAttributes[attName]);
}
this.w.schemaManager.mapper.updatePropertiesFromAttributes(entity);
if (range !== undefined) {
this.w.tagger.addEntityTag(entity, range);
}
if (entity.getContent() === undefined) {
entity.setContent(this.getTextContentForEntity(entity.id));
}
this.entities[entity.id] = entity;
this.w.event('entityAdded').publish(entity.id);
return entity;
},
/**
* Edits the entity using the supplied info.
* @param {Entity} entity The entity
* @param {Object} info The entity info
* @param {Object} info.attributes Key/value pairs of attributes
* @param {Object} info.properties Key/value pairs of Entity properties
* @param {Object} info.customValues Any additional custom values
* @returns {Entity} The edited Entity
*/
editEntity: function(entity, info) {
if (info.properties && info.properties.type && info.properties.type !== entity.getType()) {
// changing type, remove old requiredAttributes
var requiredAttributes = this.w.schemaManager.mapper.getRequiredAttributes(entity.getType());
for (var attName in requiredAttributes) {
entity.removeAttribute(attName);
}
}
// set attributes
entity.setAttributes(info.attributes);
// set properties
if (info.properties !== undefined) {
for (var key in info.properties) {
entity.setProperty(key, info.properties[key]);
}
}
this.w.schemaManager.mapper.updatePropertiesFromAttributes(entity);
// type might have changed so need to set these
var requiredAttributes = this.w.schemaManager.mapper.getRequiredAttributes(entity.getType());
for (var attName in requiredAttributes) {
entity.setAttribute(attName, requiredAttributes[attName]);
}
// set custom values
for (var key in info.customValues) {
entity.setCustomValue(key, info.customValues[key]);
}
//update modified date
entity.setDateModified();
entity.setDidUpdate(true);
return entity;
},
/**
* Remove an entity from the collection.
* NB: does not remove any associated tags in the document.
* @fires Writer#entityRemoved
* @param {String} id Then entity ID.
* @param id
*/
removeEntity: function(id) {
if (this.entities[id] !== undefined) {
delete this.entities[id];
this.w.event('entityRemoved').publish(id);
}
},
/**
* Gets an entity by its ID.
* @param {String} id The entity ID.
* @returns {Entity}
*/
getEntity: function(id) {
return this.entities[id];
},
/**
* Sets an entity by ID.
* @param {String} id The entity ID.
* @param {Entity} entity The entity.
*/
setEntity: function(id, entity) {
if (entity instanceof Entity) {
this.entities[id] = entity;
} else {
console.warn('entitiesManager: trying to set a non-Entity object.');
}
},
/**
* Returns a clone of the entity.
* @param {String} id The entity ID.
* @returns {Entity}
*/
cloneEntity: function(id) {
var clone = this.entities[id].clone();
clone.id = this.w.getUniqueId('dom_');
// TODO get new URIs
return clone;
},
/**
* Gets all the entities.
* @returns {Object}
*/
getEntities: function() {
return this.entities;
},
/**
* Gets all the entities, sorted by a particular method.
* @param {String} [sortingMethod] Either "seq" (sequential), "cat" (categorical), or "alpha" (alphabetical). Default is "seq".
* @returns {Array}
*/
getEntitiesArray: function(sortingMethod) {
sortingMethod = sortingMethod === undefined ? 'seq' : sortingMethod;
var sortedEntities = [];
if (sortingMethod === 'cat') {
var entArray = Object.values(this.entities);
var categories = {};
entArray.forEach(function(entry) {
var type = entry.getType();
if (categories[type] === undefined) {
categories[type] = [];
}
categories[type].push(entry);
});
for (var type in categories) {
var category = categories[type];
for (var i = 0; i < category.length; i++) {
var entry = category[i];
sortedEntities.push(entry);
}
}
} else if (sortingMethod === 'alpha') {
sortedEntities = Object.values(this.entities).sort(function(a, b) {
var charA = a.getTitle().charAt(0).toLowerCase();
var charB = b.getTitle().charAt(0).toLowerCase();
if (charA < charB) return -1;
if (charA > charB) return 1;
return 0;
});
} else {
var entityTags = $('[_entity][class~=start]', this.w.editor.getBody()); // sequential ordering
entityTags.each(function(index, el) {
var entry = this.getEntity($(el).attr('name'));
if (entry !== undefined) {
sortedEntities.push(entry);
}
}.bind(this));
}
return sortedEntities;
},
/**
* Iterate through all entities.
* Callback is passed the ID and the Entity as arguments.
* @param {Function} callback
*/
eachEntity: function(callback) {
$.each(this.entities, callback);
},
/**
* Gets the currently highlighted entity ID.
* @returns {String} Entity ID
*/
getCurrentEntity: function() {
return this.currentEntity;
},
/**
* Sets the currently highlighted entity ID.
* @param {String} entityId
*/
setCurrentEntity: function(entityId) {
this.currentEntity = entityId;
},
/**
* Gets all the content of the text nodes that the entity surrounds.
* @param {String} entityId
* @returns {String} The text content
*/
getTextContentForEntity: function(entityId) {
var entityTextContent = '';
$('[name='+entityId+']', this.w.editor.getBody()).each(function(i, el) {
entityTextContent += el.textContent;
});
return entityTextContent;
},
/**
* Sets the URI property and corresponding attribute
* @param {String} entityId
* @param {String} uri
*/
setURIForEntity: function(entityId, uri) {
var entity = this.getEntity(entityId);
entity.setURI(uri);
var uriMapping = this.w.schemaManager.mapper.getAttributeForProperty(entity.getType(), 'uri');
if (uriMapping) {
entity.setAttribute(uriMapping, uri);
}
},
/**
* Sets the lemma property and corresponding attribute
* @param {String} entityId
* @param {String} lemma
*/
setLemmaForEntity: function(entityId, lemma) {
var entity = this.getEntity(entityId);
entity.setLemma(lemma);
var lemmaMapping = this.w.schemaManager.mapper.getAttributeForProperty(entity.getType(), 'lemma');
if (lemmaMapping) {
entity.setAttribute(lemmaMapping, lemma);
}
},
/**
* Sets the certainty property and corresponding attribute
* @param {String} entityId
* @param {String} certainty
*/
setCertaintyForEntity: function(entityId, certainty) {
var entity = this.getEntity(entityId);
entity.setCertainty(certainty);
var certaintyMapping = this.w.schemaManager.mapper.getAttributeForProperty(entity.getType(), 'certainty');
if (certaintyMapping) {
entity.setAttribute(certaintyMapping, certainty);
}
},
removeHighlights: function() {
var prevHighlight = $('.entityHighlight', this.w.editor.getBody());
if (prevHighlight.length !== 0) {
prevHighlight.each(function(index, el) {
var $p = $(el);
if ($p.hasClass('noteWrapper')) {
$p.removeClass('entityHighlight');
} else {
var parent = $p.parent()[0];
if ($p.contents().length !== 0) {
$p.contents().unwrap();
} else {
$p.remove();
}
parent.normalize();
}
});
}
if (this.currentEntity !== null) {
this.w.event('entityUnfocused').publish(this.currentEntity);
}
},
/**
* Highlights an entity or removes the highlight from a previously highlighted entity.
* @fires Writer#entityUnfocused
* @fires Writer#entityFocused
* @param {String} [id] The entity ID.
* @param [bm] TinyMce bookmark
* @param {Boolean} [doScroll] True to scroll to the entity
*/
highlightEntity: function(id, bm, doScroll) {
// clear previous highlight
this.removeHighlights();
this.currentEntity = null;
if (id) {
this.currentEntity = id;
var entityTags = $('[name="'+id+'"]', this.w.editor.getBody());
if (entityTags.length > 0) {
var entity = this.getEntity(id);
var type = entity.getType();
// clear selection
var rng = this.w.editor.dom.createRng();
this.w.editor.selection.setRng(rng);
if (entity.isNote()) {
entityTags.parent('.noteWrapper').removeClass('hide').addClass('entityHighlight');
} else {
entityTags.wrap('<span class="entityHighlight '+type+'"/>');
entityTags.parents('.noteWrapper').removeClass('hide'); // if the entity is inside a note, make sure that it's shown
}
if (bm) {
// maintain the original caret position
this.w.editor.selection.moveToBookmark(bm);
} else {
// move inside entity
rng = this.w.editor.dom.createRng();
rng.setStart(entityTags[0], 0);
rng.collapse(true);
this.w.editor.selection.setRng(rng);
}
if (doScroll) {
var val = entityTags.offset().top;
$(this.w.editor.getDoc().documentElement).scrollTop(val);
}
this.w.event('entityFocused').publish(id);
}
}
},
/**
* Check to see if any of the entities overlap.
* @returns {Boolean}
*/
doEntitiesOverlap: function() {
// remove highlights
this.highlightEntity();
var overlap = false;
this.eachEntity(function(id, entity) {
var markers = this.w.editor.dom.select('[name="'+id+'"]');
if (markers.length > 1) {
var start = markers[0];
var end = markers[markers.length-1];
if (start.parentNode !== end.parentNode) {
overlap = true;
return false; // stop looping through entities
}
}
}.bind(this));
return overlap;
},
/**
* Removes entities that overlap other entities.
*/
removeOverlappingEntities: function() {
this.highlightEntity();
this.eachEntity(function(id, entity) {
var markers = this.w.editor.dom.select('[name="'+id+'"]');
if (markers.length > 1) {
var start = markers[0];
var end = markers[markers.length-1];
if (start.parentNode !== end.parentNode) {
this.w.tagger.removeEntity(id);
}
}
}.bind(this));
},
/**
* Converts boundary entities (i.e. entities that overlapped) to tag entities, if possible.
* TODO review
*/
convertBoundaryEntitiesToTags: function() {
this.eachEntity(function(id, entity) {
var markers = this.w.editor.dom.select('[name="'+id+'"]');
if (markers.length > 1) {
var canConvert = true;
var parent = markers[0].parentNode;
for (var i = 0; i < markers.length; i++) {
if (markers[i].parentNode !== parent) {
canConvert = false;
break;
}
}
if (canConvert) {
var $tag = $(this.w.editor.dom.create('span', {}, ''));
var atts = markers[0].attributes;
for (var i = 0; i < atts.length; i++) {
var att = atts[i];
$tag.attr(att.name, att.value);
}
$tag.addClass('end');
$tag.attr('id', $tag.attr('name'));
$tag.attr('_tag', entity.getTag());
// TODO add entity.getAttributes() as well?
$(markers).wrapAll($tag);
$(markers).contents().unwrap();
// TODO normalize child text?
}
}
}.bind(this));
},
/**
* Removes all the entities.
*/
reset: function() {
this.currentEntity = null;
this.entities = {};
}
};
module.exports = EntitiesManager;