-
Notifications
You must be signed in to change notification settings - Fork 3
/
temp.js
2868 lines (2396 loc) · 104 KB
/
temp.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////
/////GLOBAL VARIABLES
var rejectionOptions = new Set(["false",'""' , null , false , 'undefined','']);
var websiteAddress;
var polyanno_urls = {};
var polyanno_minimising = true;
var polyanno_current_username;
var imageSelected; //info.json format URL
var imageSelectedMetadata = []; ////???
var vectorSelected; //API URL
var vectorSelectedParent; //API URL
var currentCoords;
var polyanno_text_selected; //API URL of the image currently being displayed
//target variables
var polyanno_text_selectedParent; //API URL
var polyanno_text_selectedID; //DOM id
var polyanno_text_selectedHash; //parent API URL + ID
var polyanno_text_selectedFragment; //HTML Selection Object
var polyanno_text_type_selected;
//URLs
var targetSelected; //array
var targetType;
var polyanno_siblingArray;
///[editor, vector, span] colours
var polyanno_highlight_colours_array = ["#EC0028","#EC0028","#EC0028"];
var polyanno_default_colours_array = ["buttonface","#03f","transparent"];
var editorsOpen = []; //those targets currently open in editors
/*
{
id: id_string,
}
*/
////POLYANNO OBJECTS
var Polyanno = {
transcriptions: [],
translations: [],
annotations: []
};
///set vectors getter to Leaflet getLayer JSONs
//set image
polyanno_ld.prototype = {
"@context": [
"http://www.w3.org/ns/anno.jsonld"
]
};
var polyanno_annotation = function(opts) {
this = new polyanno_ld;
this._id = opts._id;
opts.id = polyanno_urls.annotations.conact(opts._id);
this.type = "Annotation";
this.body = {
type: [],
language: []
};
this.target = [];
};
var polyanno_OAM_subdoc = function(existing, value) {
var subdoc = existing;
if (isUseless(value.id)) {
console.error("There should be a unique URI provided as the id field of all bodies and targets in annotations");
subdoc.id = Math.random().toString().substring(2);
}
else {
subdoc.id = value.id;
};
if (!isUseless(value.format)) {
subdoc.format = value.format;
};
if ((!isUseless(value.language)) && (typeof value.language == Array)) {
subdoc.language = value.language;
};
if (!isUseless(value.processingLanguage)) {
subdoc.processingLanguage = value.processingLanguage;
};
if ((!isUseless(value.type)) && (typeof value.type == Array)) {
subdoc.type = value.type;
};
if (["auto", "ltr", "rtl"].includes(value.textDirection)) {
subdoc.textDirection = value.textDirection;
};
};
Object.defineProperty(polyanno_annotation, "type", {
set: function(value) {
if (!value.includes("Annotation")) {
console.error("Annotations must include 'Annotation' in their Type string.");
}
else {
this.type = value;
};
}
});
Object.defineProperty(polyanno_annotation, "body", {
set: function(value) {
this.body = polyanno_OAM_subdoc(this.body, value);
}
});
Object.defineProperty(polyanno_annotation, "target", {
set: function(value) {
if (typeof value == Array) {
for (var i=0; i < value.length; i++) {
var this_target = value[i];
///check if target already exists to update
///
this.target.push(polyanno_OAM_subdoc({}, this_target));
}
};
}
});
var selectingVector = false; //used to indicate if the user is currently searching for a vector to link or not
/*
{ siblings: polyanno_siblingArray,
parent_anno : polyanno_siblingArray[0].parent,
parent_vector : checkForVectorTarget(parent_anno)
}*/
var findingcookies = document.cookie;
var $langSelector = false;
var $imeSelector = false;
////leaflet
var polyanno_map;
var baseLayer;
var allDrawnItems = new L.FeatureGroup();
var temp_merge_shape = new L.FeatureGroup();
var controlOptions = {
draw: {
polyline: false, //disables the polyline and marker feature as this is unnecessary for annotation of text as it cannot enclose it
marker: false,
},
edit: {
featureGroup: allDrawnItems, //passes draw controlOptions to the FeatureGroup of editable layers
}
};
var popupVectorMenu;
//to track when editing
var currentlyEditing = false;
var currentlyDeleting = false;
//merging variables
var polyanno_merging_vectors = false;
var polyanno_temp_merge_shape = false; ///Leaflet layer not just GeoJSON
var polyanno_merging_array = [];
var polyanno_merging_transcription = []; ///equal to the children array of parent anno
var polyanno_merging_translation = []; ///equal to the children array of parent anno
////HTML VARIABLES
var polyanno_transcribe_symbol = "<span class='glyphicon glyphicon-pencil'></span> <span class='glyphicon glyphicon-list-alt'></span>";
var polyanno_translate_symbol = "<span class='glyphicon glyphicon-pencil'></span> <span class='glyphicon glyphicon-globe'></span>";
var polyanno_select_fragment_symbol = "<span class='glyphicon glyphicon-scissors'></span> <span class='glyphicon glyphicon-text-background'></span>";
var polyanno_discuss_symbol = "<span class='glyphicon glyphicon-comment'></span>";
var polyanno_new_anno_symbol = "<span class='glyphicon glyphicon-pencil'></span> <span class='glyphicon glyphicon-plus'></span>";
var polyanno_fragment_alternatives_symbol = "<span class='glyphicon glyphicon-text-background'></span> <span class='glyphicon glyphicon-align-left'></span>";
var polyanno_merging_vectors_symbol = "<span class='glyphicon glyphicon-map-marker'></span> <span class='glyphicon glyphicon-stop'></span> <span class='glyphicon glyphicon-object-align-horizontal'></span>";
var polyanno_linking_transcription_to_vectors_symbol = "<span class='glyphicon glyphicon-link'></span> <span class='glyphicon glyphicon-list-alt'></span> <span class='glyphicon glyphicon-stop'></span>";
var polyanno_linking_translation_to_vectors_symbol = "<span class='glyphicon glyphicon-link'></span> <span class='glyphicon glyphicon-globe'></span></span> <span class='glyphicon glyphicon-stop'></span>";
var polyanno_show_alternatives_symbol = "<span class='glyphicon glyphicon-chevron-down'></span> <span class='glyphicon glyphicon-text-background'></span> <span class='glyphicon glyphicon-align-left'></span>";
var polyanno_top_bar_HTML = `
<div class="col-md-6 polyanno-bar-buttons">
<div class="row">
<div class="btn-group polyanno-language-buttons" role="group" aria-label="...">
<button class="btn btn-default polyanno-discussion-btn"><span class="glyphicon glyphicon-comment"></span></button>
<button id="polyanno-merge-shapes-enable" class="btn btn-default polyanno-merge-shapes-btn">
`+polyanno_merging_vectors_symbol+`
</button>
<div class="btn-group polyanno-merging-buttons" role="group" aria-label="polyanno-merging-buttons">
<button class="btn btn-primary polyanno-merge-shapes-submit-btn">Submit</button>
<button class="btn btn-primary polyanno-merge-shapes-cancel-btn">Cancel</button>
</div>
<!-- <button class="btn btn-default polyanno-image-open"><span class="glyphicon glyphicon-picture"></span></button> -->
<button class="btn btn-default polyanno-add-keyboard" type="button">
<span class="glyphicon glyphicon-plus"></span>
<span class="glyphicon glyphicon-th"></span><span class="glyphicon glyphicon-th"></span>
</button> <!--add keyboard characters-->
<button class="btn btn-default polyanno-add-ime polyanno-IME-options-closed" type="button">
<span class="glyphicon glyphicon-transfer"></span>
<span class="glyphicon glyphicon-th"></span><span class="glyphicon glyphicon-th"></span>
</button> <!--add IME options-->
</div>
<div class="polyanno-enable-IME">
</div>
</div>
</div>
<div class="col-md-6 dragondrop-min-bar">
</div>
`;
var openHTML = "<div class='popupAnnoMenu'>";
var transcriptionOpenHTML = `<a class="openTranscriptionMenu polyanno-standard-btn btn btn-default" onclick="checkEditorsOpen('vector', 'transcription');
polyanno_map.closePopup();">`+polyanno_transcribe_symbol+`</a><br>`;
var translationOpenHTML = `<a class="openTranslationMenu polyanno-standard-btn btn btn-default" onclick="checkEditorsOpen('vector', 'translation');
polyanno_map.closePopup();">`+polyanno_translate_symbol+`</a>`;
var endHTML = "</div>";
var popupVectorMenuHTML = openHTML + transcriptionOpenHTML + translationOpenHTML + endHTML;
var polyanno_image_viewer_HTML = `<div id='polyanno_map' class="row"></div>`;
var polyanno_merging_transcription_HTML = `
<div class="row">
<h2>The New Transcription:</h2>
</div>
<div id="polyanno_merging_transcription" class="row"></div>`;
var polyanno_merging_translation_HTML = `
<div class="row">
<h2>The New Translation:</h2>
</div>
<div id="polyanno_merging_translation" class="row"></div>`;
var polyannoVoteOverlayHTML = `<div class='polyanno-voting-overlay' >
<button type='button' class='btn btn-default polyanno-standard-btn voteBtn polyannoVotingUpButton'>
<span class='glyphicon glyphicon-thumbs-up' aria-hidden='true' ></span>
</button>
<button type='button' class='btn btn-default polyanno-standard-btn polyannoVotesUpBadge'>
<span class='badge'></span>
</button>
</div>`;
var closeButtonHTML = `<span class='closePopoverMenuBtn glyphicon glyphicon-remove'></span>`;
var transcriptionIconHTML = `<span class='glyphicon glyphicon-list-alt'></span>
<span>Transcription</span>`;
var translationIconHTML = `<span class='glyphicon glyphicon-globe'></span>
<span>Translation</span>`;
var popupSelectingVectorHTML = `
<div id="popupTranscriptionNewMenu" class="popupAnnoMenu">
<a class="btn btn-default polyanno-standard-btn" onclick="polyanno_setting_selecting_vector(); polyanno_map.closePopup();">Submit</a>
<a class="btn btn-default polyanno-standard-btn" onclick="polyanno_reset_selecting_vector(); polyanno_map.closePopup();">Cancel</a>
</div>
`;
var popupTranscriptionNewMenuHTML = `
<!-- New Transcription Text Select Popup Menu -->
<div id="popupTranscriptionNewMenu" class="popupAnnoMenu">
<div data-role="main" class="ui-content">
<a class="openTranscriptionMenuNew transcriptionTarget editorPopover btn btn-default polyanno-standard-btn">`+polyanno_select_fragment_symbol+`</a></br>
<a class="polyanno-add-discuss btn btn-default polyanno-standard-btn"><span class="glyphicon glyphicon glyphicon-comment"></span> Discuss</a>
</div>
</div>
`;
var popupTranslationNewMenuHTML = `
<!-- New Translation Text Select Popup Menu -->
<div id="popupTranslationNewMenu" class="popupAnnoMenu" >
<div data-role="main" class="ui-content">
<a class="openTranslationMenuNew translationTarget editorPopover ui-btn ui-corner-all polyanno-standard-btn">`+polyanno_select_fragment_symbol+`</a></br>
<a class="polyanno-add-discuss btn btn-default polyanno-standard-btn"><span class="glyphicon glyphicon glyphicon-comment"></span> Discuss</a>
</div>
</div>
`;
var popupTranscriptionChildrenMenuHTML = `
<!-- Children Transcription Text Select Popup Menu-->
<div id="popupTranscriptionChildrenMenu" class="popupAnnoMenu">
<div data-role="main" class="ui-content">
<a class="openTranscriptionMenuOld editorPopover btn btn-default polyanno-standard-btn" onclick="polyanno_open_existing_text_transcription_menu();">`+polyanno_fragment_alternatives_symbol+`</a>
<a class="polyanno-add-discuss btn btn-default polyanno-standard-btn"><span class="glyphicon glyphicon glyphicon-comment"></span> Discuss</a>
</div>
</div>
`;
var popupTranslationChildrenMenuHTML = `
<!-- Children Translation Text Select Popup Menu -->
<div id="popupTranslationChildrenMenu" class="popupAnnoMenu">
<div data-role="main" class="ui-content">
<a class="openTranslationMenuOld editorPopover btn btn-default polyanno-standard-btn" onclick="polyanno_open_existing_text_translation_menu();">`+polyanno_fragment_alternatives_symbol+`</a>
<a class="polyanno-add-discuss btn btn-default polyanno-standard-btn"><span class="glyphicon glyphicon glyphicon-comment"></span> Discuss</a>
</div>
</div>
`;
var polyannoEditorHandlebarHTML = `
<button class="btn polyanno-options-dropdown-toggle dragondrop-handlebar-obj polyanno-colour-change col-md-2" type="button" >
<span class="glyphicon glyphicon-cog"></span>
<span class="caret"></span>
</button>
`;
var polyannoEditorHTML_partone = `
<div class="textEditorMainBox row ui-content">
<div class="col-md-12">
`;
////need to change to discussion and put tags into Polyglot alone
var polyannoEditorHTML_options_partone = `<div class="row polyanno-options-row">
<div class="col-md-2 polyanno-options-buttons">
<button class="btn btn-default polyanno-metadata-tags-btn"><span class="glyphicon glyphicon-tags"></span></button>
</div>`;
var polyannoEditorHTML_options_parttwo = `</div>`;
var polyannoEditorHTML_options = polyannoEditorHTML_options_partone + polyannoEditorHTML_options_parttwo;
var polyannoEditorHTML_partfinal = `
<div class="row polyanno-vector-link-row">
<button type='button' class='btn polyannoEditorDropdownBtn polyannoLinkVectorBtn'>
Draw a Shape For This Text On the Image!
</button>
</div>
<div class="row polyanno-top-voted polyanno-text-display">
<div class='polyanno-voting-overlay' >
<button type='button' class='btn btn-default voteBtn polyannoVotingUpButton'>
<span class='glyphicon glyphicon-thumbs-up' aria-hidden='true' ></span>
</button>
<button type='button' class='btn btn-default polyannoVotesUpBadge'>
<span class='badge'></span>
</button>
</div>
</div>
<div class="row polyanno-alternatives-toggle-row">
<button type='button' class='btn polyannoEditorDropdownBtn polyannoToggleAlternatives '>
`+polyanno_show_alternatives_symbol+`
</button>
</div>
<div class="row polyanno-list-alternatives-row">
</div>
<div class="row polyanno-add-new-toggle-row">
<button type='button' class='btn polyannoEditorDropdownBtn polyannoAddAnnotationToggle'>
`+polyanno_new_anno_symbol+`
</button>
</div>
<div class="row polyanno-add-new-row">
<div class='polyannoAddNewContainer col-md-12'>
<textarea id='testingKeys' class='newAnnotation row' placeholder='Add new annotation text here'></textarea><br>
<button type='button' class='btn addAnnotationSubmit polyannoEditorDropdownBtn row'><span class='glyphicon glyphicon-ok-circle'></span></button>
</div>
</div>
<div class="row polyanno-add-new-cancel-row">
<button type='button' class='btn polyannoEditorDropdownBtn polyannoAddAnnotationCancel'>
Cancel
</button>
</div>
<div class="row polyanno-metadata-tags-row">
</div>
</div>
</div>
`;
atu_the_input = $("#polyanno-dummy-textarea");
/////GENERIC FUNCTIONS
var isUseless = function(something) {
if (rejectionOptions.has(something) || rejectionOptions.has(typeof something)) { return true; }
else { return false; };
};
var getTargetJSON = function(target, callback_function) {
if ( isUseless(target) ) { return null; }
else {
var targetJSON;
$.ajax({
type: "GET",
dataType: "json",
url: target,
async: false,
success:
function (data) {
targetJSON = data;
}
});
return targetJSON;
};
};
var updateAnno = function(targetURL, targetData, callback_function) {
$.ajax({
type: "PUT",
url: targetURL,
async: false,
dataType: "json",
data: targetData,
success:
function (data) {
if (!isUseless(callback_function)) {
callback_function();
};
}
});
};
var fieldMatching = function(searchArray, field, fieldValue) {
if (isUseless(searchArray) || isUseless(field) || isUseless(fieldValue)) { return false }
else {
var theMatch = false;
searchArray.forEach(function(childDoc){
if ((childDoc[field] == fieldValue) || (new RegExp(fieldValue).test(childDoc[field])) ) {
theMatch = childDoc;
};
});
return theMatch;
};
};
var asyncPush = function(addArray, oldArray) {
var theArray = oldArray;
var mergedArray = function() {
addArray.forEach(function(addDoc){
theArray.push(addDoc);
});
if (theArray.length = (oldArray.length + addArray.length)) {
return theArray;
};
};
return mergedArray();
};
var arrayIDCompare = function(arrayA, arrayB) {
return arrayA.forEach(function(doc){
var theCheck = fieldMatching(arrayB, "id", arrayA.id);
if ( isUseless(theCheck) ) { return false }
else {
return [doc, theCheck];
};
});
};
var findField = function(target, field) {
if ( isUseless(field) || isUseless(target) || isUseless(target[field]) ) { return false }
else { return target[field] };
};
var checkFor = function(target, field) {
var theChecking = getTargetJSON(target);
return findField(theChecking, field);
};
var searchCookie = function(field) {
var fieldIndex = findingcookies.lastIndexOf(field);
if (fieldIndex == -1) { return false; }
else {
var postField = findingcookies.substring(fieldIndex+field.length);
var theValueEncoded = postField.split(";", 1);
var theValue = theValueEncoded[0];
return theValue;
};
};
////GENERAL ANNOTATION FUNCTIONS
var replaceChildText = function(oldText, spanID, newInsert, oldInsert) {
var idIndex = oldText.indexOf(spanID);
var startIndex = oldText.indexOf(oldInsert, idIndex);
var startHTML = oldText.slice(0, startIndex);
var EndIndex = startIndex + oldInsert.length;
var endHTML = oldText.substring(EndIndex);
var newText = startHTML + newInsert+ endHTML;
return newText;
};
var findClassID = function(classString, IDstring) {
var IDindex = classString.search(IDstring) + IDstring.length;
var IDstart = classString.substring(IDindex);
var theID = IDstart.split(" ", 1);
return theID[0];
};
var checkForVectorTarget = function(theText, the_target_type) {
var findByBodyURL = polyanno_urls.annotation.concat("body/"+encodeURIComponent(theText));
//var the_regex = '/.*'+the_target_type+'.*/';
var theChecking = checkFor(findByBodyURL, "target");
if ( isUseless(theChecking[0]) ) { return false }
else {
return fieldMatching(theChecking, "format", 'image/SVG').id;
};
};
var polyanno_annos_of_target = function(target, baseURL, callback_function) {
var targetParam = encodeURIComponent(target);
var aSearch = baseURL.concat("targets/"+targetParam);
$.ajax({
type: "GET",
dataType: "json",
url: aSearch,
async: false,
success:
function (data) {
if (!isUseless(data.list[0])) {
polyanno_search_annos_by_ids(data.list, callback_function);
}
else if (!isUseless(callback_function)) {
callback_function();
};
}
});
};
var polyanno_search_annos_by_ids = function(list, callback_function) {
/*
list = [{
anno: {}
body: {}
target: {}
}]
*/
var newlist = [];
for (var i=0; i <list.length; i++) {
newlist.push(list[i].body);
};
callback_function(newlist);
};
var updateVectorSelection = function(the_vector_url) {
///this is the process for linking vectors to text segments
var textData = {target: [{id: the_vector_url, format: "image/SVG"}]};
selectingVector.siblings.forEach(function(child){
updateAnno(child[0].body.id, textData);
});
var editorID = fieldMatching(editorsOpen, "tSelectedParent", selectingVector.parent_anno).editor;
//need to ensure asynchronicity here
selectingVector = false;
$(editorID).find(".polyanno-vector-link-row").css("display", "none");
///update editorsOpen to activate highlighting
};
var polyanno_voting_reload_editors = function(updatedTranscription, editorID, targetID) {
if (updatedTranscription) {
polyanno_text_selected = targetID;
closeEditorMenu(editorID, targetID);
};
if (updatedTranscription && (!isUseless(vectorSelected))) {
var updateTargetData = {};
updateTargetData[polyanno_text_type_selected] = targetID;
updateAnno(vectorSelected, updateTargetData);
};
///////if the parent is open in an editor rebuild carousel with new transcription
editorsOpen.forEach(function(editorOpen){
editorOpen.children.forEach(function(eachChild){
if ( eachChild.id == polyanno_text_selectedID ){
closeEditorMenu(editorOpen.editor, editorOpen.body.id);
};
});
});
};
var votingFunction = function(vote, votedID, currentTopText, editorID) {
var theVote = findBaseURL() + "voting/" + vote;
var targetID = findBaseURL().concat(votedID); ///API URL of the annotation voted on
var votedTextBody = $("#"+votedID).html();
var targetData = {
parent: polyanno_text_selectedParent, ///it is this that is updated containing the votedText within its body
children: [{
id: polyanno_text_selectedID, //ID of span location
fragments: [{
id: targetID
}]
}],
votedText: votedTextBody, topText: currentTopText
};
$.ajax({
type: "PUT",
url: theVote,
async: false,
dataType: "json",
data: targetData,
success:
function (data) {
polyanno_voting_reload_editors(data.reloadText, editorID, targetID);
}
});
};
var polyanno_find_highest_ranking_frag_child = function(location_json) {
var the_child = fieldMatching(location_json.fragments, "rank", 0);
return findField(the_child, "id");
};
var findHighestRankingChild = function(the_parent_json_children, locationID) {
var theLocation = fieldMatching(the_parent_json_children, "id", locationID);
return polyanno_find_highest_ranking_frag_child(theLocation);
};
///// TEXT SELECTION
var outerElementTextIDstring;
var newContent;
var newNodeInsertID;
var startParentID;
function getSelected() {
if(window.getSelection) { return window.getSelection() }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
};
var insertSpanDivs = function() {
$(outerElementTextIDstring).html(newContent);
polyanno_text_selectedID = newNodeInsertID;
};
var findBaseURL = function() {
if (polyanno_text_type_selected == "transcription") { return polyanno_urls.transcription; }
else if (polyanno_text_type_selected == "translation") { return polyanno_urls.translation; };
};
var polyanno_new_anno_via_selection = function(baseURL) {
//need to refer specifically to body text of that transcription - make body independent soon so no need for the ridiculously long values??
polyanno_text_selectedHash = polyanno_text_selectedParent.concat("#"+polyanno_text_selectedID);
targetSelected = [polyanno_text_selectedHash];
var targetData = {
text: polyanno_text_selectedFragment, metadata: imageSelectedMetadata, parent: polyanno_text_selectedParent,
target: [
{id: polyanno_text_selectedHash, format: "text/html"},
{id: polyanno_text_selectedParent, format: "application/json"},
{id: imageSelected, format: "application/json" }
]
};
var thisEditorString = $("#"+polyanno_text_selectedID).closest(".textEditorPopup").attr("id");
$.ajax({
type: "POST",
url: baseURL,
async: false,
data: targetData,
success:
function (data) {
var createdText = data.url;
polyanno_text_selected = createdText;
polyanno_add_annotationdata(data.text, false, thisEditorString, [data.url], [false], [polyanno_text_selectedParent], polyanno_siblingArray);
}
});
};
var polyanno_new_anno_for_child_of_merge = function(this_url, this_data, callback) {
$.ajax({
type: "POST",
url: this_url,
async: false,
data: this_data,
success:
function (data) {
var createdText = data.url;
polyanno_add_annotationdata(data.text, false, false, [data.url], [false], [this_data.parent], [false], callback); ///giving the vector as false so it can be called with both the new anno AND the parent
}
});
};
var polyanno_new_annos_via_linking = function(merged_vector) {
var linked_transcriptions = $("#polyanno_merging_transcription").html();
var linked_translations = $("#polyanno_merging_translation").html();
var transcription_data = {text: linked_transcriptions, children: polyanno_merging_transcription, metadata: imageSelectedMetadata,
target: [
{id: merged_vector, format: "image/SVG" },
{id: imageSelected, format: "application/json" } ]
};
var translation_data = {text: linked_translations, children: polyanno_merging_translation, metadata: imageSelectedMetadata,
target: [
{id: merged_vector, format: "image/SVG" },
{id: imageSelected, format: "application/json" } ]
};
var createdTranslation;
var createdTranscription;
var vector_children_counter = 0;
var polyanno_update_vector_children_iteratively = function () {
/////////separate into two parts so the anno ids can be created and added
var this_layer = polyanno_merging_array[vector_children_counter];
var this_layer_id = this_layer._leaflet_id;
textTarget[0].id = this_layer_id;
var these_properties = this_layer.toGeoJSON().properties;
var the_data = { parent: merged_vector };
var new_text_data = { text: " ", target: textTarget, metadata: imageSelectedMetadata };
if (isUseless(these_properties)) {
this_layer.toGeoJSON().properties = {};
new_text_data.parent = createdTranscription;
};
the_data.transcription = these_properties.transcription;
the_data.translation = these_properties.translation;
if (isUseless(these_properties.transcription)) {
new_text_data.parent = createdTranscription;
///need to set polyanno_text_selectedID
polyanno_new_anno_for_child_of_merge(polyanno_urls.transcription, new_text_data, polyanno_update_vector_children_iteratively);
//this will POST to t, then POST to anno, then leave PUT to parent whilst returning to this loop
}
else if (isUseless(these_properties.transcription)) {
new_text_data.parent = createdTranslation;
///need to set polyanno_text_selectedID
polyanno_new_anno_for_child_of_merge(polyanno_urls.translation, new_text_data, polyanno_update_vector_children_iteratively);
//this will POST to t, then POST to anno, then leave PUT to parent whilst returning to this loop
}
else if (vector_children_counter == (polyanno_merging_array.length - 1)) {
updateAnno(this_layer_id, the_data);
}
else {
vector_children_counter += 1;
updateAnno(this_layer_id, the_data, polyanno_update_vector_children_iteratively );
};
};
var polyanno_new_translation_via_linking = function() {
$.ajax({
type: "POST",
url: polyanno_urls.translation,
async: false,
data: translation_data,
success:
function (data) {
createdTranslation = data.url;
polyanno_add_annotationdata(data.text, false, false, [data.url], [merged_vector], [false], [false], polyanno_update_vector_children_iteratively );
}
});
};
$.ajax({
type: "POST",
url: polyanno_urls.transcription,
async: false,
data: transcription_data,
success:
function (data) {
createdTranscription = data.url;
polyanno_add_annotationdata(data.text, false, false, [data.url], [merged_vector], [false], [false], polyanno_new_translation_via_linking);
}
});
};
var setpolyanno_text_selectedID = function(theText) {
var findByBodyURL = polyanno_urls.annotation + "body/"+encodeURIComponent(theText);
var the_regex = '/.*'+findBaseURL()+'.*/';
var theTarget = fieldMatching(checkFor(findByBodyURL, "target"), "format", "text/html");
if ( theTarget != false ) {
polyanno_text_selectedHash = theTarget.id;
polyanno_text_selectedID = polyanno_text_selectedHash.substring(polyanno_text_selectedParent.length + 1); //the extra one for the hash
return theTarget.id;
};
};
var newSpanClass = function(startParentClass) {
if (startParentClass.includes('transcription-text')) {
return "transcription-text opentranscriptionChildrenPopup";
}
else if (startParentClass.includes('translation-text')) {
return "translation-text opentranslationChildrenPopup";
}
else {
return null;
};
};
var strangeTrimmingFunction = function(thetext) {
if(thetext && (thetext = new String(thetext).replace(/^\s+|\s+$/g,''))) {
return thetext.toString();
};
};
var newTextPopoverOpen = function(theTextIDstring, theParent) {
$('#polyanno-page-body').on("click", function(event) {
if ($(event.target).hasClass("popupAnnoMenu") == false) {
$(theTextIDstring).popover("hide");
}
});
$('.openTranscriptionMenuNew').on("click", function(event) {
///
insertSpanDivs();
polyanno_text_selectedParent = polyanno_urls.transcription.concat(theParent);
polyanno_text_type_selected = "transcription";
targetType = "transcription";
$(theTextIDstring).popover('hide');
polyanno_new_anno_via_selection(polyanno_urls.transcription);
});
$('.openTranslationMenuNew').on("click", function(event) {
insertSpanDivs();
polyanno_text_selectedParent = polyanno_urls.translation.concat(theParent);
polyanno_text_type_selected = "translation";
targetType = "translation";
$(theTextIDstring).popover('hide');
polyanno_new_anno_via_selection(polyanno_urls.translation);
});
$('.closeThePopover').on("click", function(event){
$(theTextIDstring).popover("hide");
});
};
var initialiseNewTextPopovers = function(theTextIDstring, theParent) {
$(theTextIDstring).popover({
trigger: 'manual',
placement: 'top',
html : true,
container: 'body',
title: closeButtonHTML,
content: popupTranscriptionNewMenuHTML
});
$(theTextIDstring).popover('show');
$(theTextIDstring).on("shown.bs.popover", function(ev) {
newTextPopoverOpen(theTextIDstring, theParent);
});
};
var initialiseOldTextPopovers = function(theTextIDstring) {
$(theTextIDstring).popover({
trigger: 'manual', //////
placement: 'top',
html : true,
title: closeButtonHTML,
content: popupTranscriptionChildrenMenuHTML
});
$(theTextIDstring).popover('show');
};
var setOESC = function(outerElementHTML, previousSpanContent, previousSpan) {
var outerElementStartContent;
if (previousSpan == "null" || previousSpan == null) {outerElementStartContent = previousSpanContent}
else {
var previousSpanAll = previousSpan.outerHTML;
var StartIndex = outerElementHTML.indexOf(previousSpanAll) + previousSpanAll.length;
outerElementStartContent = outerElementHTML.slice(0, StartIndex).concat(previousSpanContent);
};
return outerElementStartContent;
};
var setOEEC = function(outerElementHTML, nextSpanContent, nextSpan) {
var outerElementEndContent;
if (nextSpan == "null" || nextSpan == null) {outerElementEndContent = nextSpanContent}
else {
var EndIndex = outerElementHTML.indexOf(nextSpan.outerHTML);
outerElementEndContent = nextSpanContent.concat(outerElementHTML.substring(EndIndex));
};
return outerElementEndContent;
};
var setNewTextVariables = function(selection, classCheck) {
var startNode = selection.anchorNode; // the text type Node that the beginning of the selection was in
var startNodeText = startNode.textContent; // the actual textual body of the startNode - removes all html element tags contained
var startNodeTextEndIndex = startNodeText.toString().length;
startParentID = startNode.parentElement.id;
var startParentClass = startNode.parentElement.parentElement.className;
var nodeLocationStart = selection.anchorOffset; //index from within startNode text where selection starts
var nodeLocationEnd = selection.focusOffset; //index from within endNode text where selection ends
var endNode = selection.focusNode; //the text type Node that end of the selection was in
var endNodeText = endNode.textContent;
var endParentID = endNode.parentElement.id; //the ID of the element type Node that the text ends in
outerElementTextIDstring = "#" + startParentID; //will be encoded URI of API?
if (classCheck.includes('opentranscriptionChildrenPopup')) {
initialiseOldTextPopovers(outerElementTextIDstring);
}
else if (classCheck.includes('opentranslationChildrenPopup')) {
initialiseOldTextPopovers(outerElementTextIDstring);
}
else if (startParentID != endParentID) {
}
else {
newNodeInsertID = Math.random().toString().substring(2);
var newSpan = "<a class='" + newSpanClass(startParentClass) + " ' id='" + newNodeInsertID + "' >" + selection + "</a>";
var outerElementHTML = $(outerElementTextIDstring).html().toString();
///CONTENT BEFORE HIGHLIGHT IN THE TEXT TYPE NODE
var previousSpanContent = startNodeText.slice(0, nodeLocationEnd);
/////this including to the end of the selected text???
//CONTENT BEFORE HIGHLIGHT IN THE ELEMENT TYPE NODE
var previousSpan = startNode.previousElementSibling; //returns null if none i.e. this text node is first node in element node
var outerElementStartContent = setOESC(outerElementHTML, previousSpanContent, previousSpan);
///CONTENT AFTER HIGHLIGHT IN THE TEXT TYPE NODE
var nextSpanContent;
////this is starting before the start of the selected text???
if (endNode == startNode) { nextSpanContent = startNodeText.slice(nodeLocationStart, startNodeTextEndIndex)}
else {nextSpanContent = endNodeText.slice(0, nodeLocationStart)};
///CONTENT AFTER HIGHLIGHT IN ELEMENT TYPE NODE
var nextSpan = endNode.nextElementSibling; //returns null if none i.e. this text node is the last in the element node
var outerElementEndContent = setOEEC(outerElementHTML, nextSpanContent, nextSpan );
newContent = outerElementStartContent + newSpan + outerElementEndContent;
polyanno_text_selectedFragment = strangeTrimmingFunction(selection);
initialiseNewTextPopovers(outerElementTextIDstring, startParentID);
};
};
///// VIEWER WINDOWS
var polyanno_shake_the_popups = function() {
$(".annoPopup").effect("shake", {
direction: "right",
distance: 10,
times: 2
});
};
var createEditorPopupBox = function() {
var dragon_opts = {
"minimise": polyanno_minimising,
"initialise_min_bar": false,
"beforeclose": preBtnClosing
};
var polyannoEditorHTML = polyannoEditorHTML_partone + polyannoEditorHTML_options + polyannoEditorHTML_partfinal;
var popupIDstring = add_dragondrop_pop("textEditorPopup", polyannoEditorHTML, "polyanno-page-body", dragon_opts, polyannoEditorHandlebarHTML);
$(popupIDstring).show("drop", null, null,polyanno_shake_the_popups)