-
Notifications
You must be signed in to change notification settings - Fork 56
/
transcription_view.js
843 lines (764 loc) · 30.1 KB
/
transcription_view.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
'use strict';
var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var render = require('./utils').render;
var FileSaver = require('file-saver');
var moment = require('moment');
var fromSeconds = require('node-timecodes').fromSeconds;
var EDL = require('../../edl_composer/index');
//TODO remove all functions not used such as seletion orders one,
//remove commented out code.
/**
* Backbone view for transcription view for individual transcriptions
* @class TranscriptionView
* @constructor
* @extends Backbone.View
*/
module.exports = Backbone.View.extend({
tagName: 'div',
className: 'container-fluid',
//TODO: change this so that id is interpolated from model this.el.id or this.el.ciud
id: "transcription-n",//+this.model.id+"",
initialize: function() {
this.editable = false;
var tmpHighlights = this.model.get("highlights");
for (var i =0; i < tmpHighlights.length; i++ ){
var min = tmpHighlights[i].startTime;
var max = tmpHighlights[i].endTime;
//needs to grab the words of the element of the view not in the dom
this.$(".words").filter(function(){
return $(this).data('start-time') >= min && $(this).data('end-time') <= max;
}).addClass("highlight");
}
},
events:{
//playback keyboard shortcuts
"click #pausePlayVideo" : "pausePlayVideo",
"click #slowVideo" : "slowVideo",
"click #fastVideo" : "fastVideo",
"click #fastForwardVideo" : "fastForwardVideo",
"click #volumeDown" : "volumeDown",
"click #volumeUp" : "volumeUp",
"click #rewindVideo" : "rewindVideo",
//interactive transcript click word plays video
"click span.words" : "playWord",
"click .timecodes" : "playWord",
//search field
"keyup #searchCurrentTranscription" :"search",
//text selection in transcription
"mouseup .transcription" : "selectingWords",
"click #clearHighlights" : "clearHighlights",
//edit text in transcription mode
"click #editWords" : "makeEditable",
//hilight text in transcription mode
"click #highlightWords" : "makeHilightable",
//TODO: sequence preview not in use
//TODO: remove associated functions
// "click #playPreviewChrono" : "playPreviewChrono",
// "click #playPreviewSelOrd" : "playPreviewSelOrd",
//EDL Export
"click #exportEdlChronological" : "exportEdlChronological",
// "click #exportEdlSelectionOrder" : "exportEdlSelectionOrder",
//Text export
"click #exportPlainText" : "exportPlainText",
"click #exportTimecodedTranscription" : "exportTimecodedTranscription",
//Export Text EDL
"click #exportPlainTextEDL" : "exportPlainTextEDL",
// "click #exportPlainTextEDLSelOrder" : "exportPlainTextEDLSelOrder",
"click #exportPlainTimecodedTextEDL" : "exportPlainTimecodedTextEDL",
// "click #exportPlainTimecodedTextEDLSelOrder": "exportPlainTimecodedTextEDLSelOrder",
//JSON Export
"click #exportJsonEDL" : "exportJsonEDL",
// "click #exportJsonEDLSelOrder" : "exportJsonEDLSelOrder",
"click #exportJsonTranscription" : "exportJsonTranscription",
//Export Captions
"click #expoertCaptionsSrt" : "expoertCaptionsSrt",
//TODO: currently not in use
// "click #expoertCaptionsSrtEDL" : "expoertCaptionsSrtEDL",
//link to user manual
"click #edlUserManualInfo" : "edlUserManualInfo",
"focusout .speaker" : "updateSpeakerName"
},
updateSpeakerName: function(e){
//get transcription text from model
var transcription = this.model.attributes.text;
//get edited speaker name
var newSpeakerNAme = document.querySelector(".speaker").innerHTML;
// get new speaker name
// var escapedNewSpeakerNAme = $("<div>").text(newSpeakerNAme).text();
transcription[0].speaker = stripHTML(newSpeakerNAme);
//set updated transcription to current transcritpion model
this.model.set({text: transcription });
//save the updated transcription model
this.model.save({wait: false});
// helper function to remove html from input
// TODO: move in utilities
function stripHTML(html){
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
},
/**
* @function makeHilightable
* @description function to disable words contet ediable to allow to select text of transcription
*/
makeHilightable: function(){
var modeNotice = document.getElementById("hilightModeNoticeContainer");
var hilightNotice = "<div class='alert alert-info alert-dismissible hidden-print' role='alert' id='hilightModeNotice'>";
hilightNotice +="<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button>";
hilightNotice += "<strong>You are in hilight mode.</strong>";
hilightNotice +="<p>Select words to include in your Edit Decision List (EDL) video sequence.</p>";
hilightNotice +="</div>";
var status = false;
if( $("#highlightWords").hasClass("btn-primary")){
status = true;
}else{
status = false;
}
if(status){
//disable editing
//TODO: check if you can remove more thne one class at once with JQuery comand?
$("#highlightWords").removeClass("active btn-primary");
$("#highlightWords").addClass("btn-default");
//highlightWords
$("#editWords").removeClass("btn-default");
$("#editWords").addClass("active btn-primary");
//enable content editable
$('.words').attr('contenteditable','true');
}else{
//Editing active
$("#editWords").removeClass("active btn-primary");
$("#editWords").addClass("btn-default");
//highlightWords
$("#highlightWords").removeClass("btn-default");
$("#highlightWords").addClass("active btn-primary");
//disable editable on words
$('.words').attr('contenteditable','false');
//add hilight notice
modeNotice.innerHTML = hilightNotice;
}
},
/**
* @function makeEditable
* @description function to make words contet ediable to allow to edit the text of transcription
*/
makeEditable: function(){
var modeNotice = document.getElementById("hilightModeNoticeContainer");
//remove hilight mode notice
// add text editing notice
var editNotice= "<div class='alert alert-warning alert-dismissible hidden-print' role='alert' id='editModeNotice'> ";
editNotice +="<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button>";
editNotice +="<strong>You are in text edit mode.</strong>";
editNotice +="<p>Click on a word to edit. Tab or click to move to the next word.</p>";
editNotice +="</div>";
var status = false;
if( $("#editWords").hasClass("btn-primary")){
status = true;
}else{
status = false;
}
if(status){
//Editing active
$("#editWords").removeClass("active");
$("#editWords").removeClass("btn-primary");
$("#editWords").addClass("btn-default");
//highlightWords
$("#highlightWords").removeClass("btn-default");
$("#highlightWords").addClass("btn-primary");
$("#highlightWords").addClass("active");
//disable editable on words
$('.words').attr('contenteditable','false');
//remove edit notice
// modeNotice.removeChild(document.getElementById("editModeNotice")) ;
}else{
//disable editing
$("#highlightWords").removeClass("active");
$("#highlightWords").removeClass("btn-primary");
$("#highlightWords").addClass("btn-default");
//editWords
$("#editWords").removeClass("btn-default");
$("#editWords").addClass("btn-primary");
$("#editWords").addClass("active");
//enable content editable
$('.words').attr('contenteditable','true');
//remove hilight notice
//add edit notice
modeNotice.innerHTML = editNotice;
// listener on word changed
var contents = $('.words').html();
//TODO: input change to event that detect when focus out or somehting else so that i doesn't auto save as typing coz that is super slow...
//when using click for moving between words in edit mode, this auto selects the current word in content editbale.
//users have requested this feature to make it faster to, delete/replace word with correct one. they currently do cmd+a
$(".words").on("click", function(event){
//select all text in content edibale
document.execCommand('selectAll',false,null);
});
//same as above but when using tab to move between words
$('.words').on( 'keyup', function( e ) {
if( e.which == 9 ) {
//select all text in content edibale
document.execCommand('selectAll',false,null);
}
} );
$( ".words" ).on( "focusout", { model: this.model }, function(event) {
//pause word
var model = event.data.model;
var tmpWordId = $( this )[0].dataset.wordId;
var tmpWordText = $( this ).text();
//find word to update in transcription text attribute to replace/update and save
_.each(model.attributes.text, function(paragraphs){
_.each(paragraphs, function(paragraph){
_.each(paragraph, function(lines){
_.each(lines.line, function(word){
if(word.id == tmpWordId){
word.text = tmpWordText;
model.save({wait: false});
}
});
});
});
});
});
}
},
/**
* @function exportHelper
*/
exportHelper(config){
if(config.fileContent === ""){
alert("File " + config.fileName + " seems to be empty");
}
var fileName = config.fileName; //this.title _ + append time of day to name
var fileContent = config.fileContent;
var urlId = config.urlId;
var formBlob = new Blob([fileContent], { type: 'text/plain' });
FileSaver.saveAs(formBlob, config.fileName);
},
/**
* @function exportHelper
*/
nameFileHelper(name, ext){
var timeStr = moment().format('DD_MM_YYYY_HH-mm-ss');
return ""+name.replace(" ", "_") +"_"+ timeStr +"."+ext;
},
/**
* @function exportEdlChronological
* @description EDL - Chronological
*/
exportEdlChronological: function(e){
var edlSq = { "title": this.model.get("title")};
edlSq.events = [];
//ad line number to paper-cuts
//NOTE: here you can decide to sort them chronologically(ie by startTime)
//TODO: this could probably be moved in the model . eg return chrone order hiligths
for(var k = 0; k< this.model.attributes.highlights.length; k++){
var event = this.model.attributes.highlights[k];
event.id = k+1;
edlSq.events.push(event);
}
//end move in model
var edl = new EDL(edlSq)
var edlFileName = this.nameFileHelper(this.model.get("title")+"","edl");
this.exportHelper({fileName: edlFileName, fileContent: edl.compose(), urlId: "#exportEdlChronological"});
},
/**
* @function exportEdlSelectionOrder
* @description EDL - election order
*/
// exportEdlSelectionOrder: function(e){
// var edlSq = { "title": this.model.get("title"), offset: this.model.get("metadata").timecode};
// edlSq.events = [];
// //TODO: this could probably be moved in the model . eg return selection order hiligths
// var selections = this.model.get("highlights");
// var selectionsSorted = _.sortBy(selections, function(o) { return o.paperCutOrder; });
// for(var k = 0; k< selectionsSorted.length; k++){
// var event = selectionsSorted[k];
// event.id = k+1;
// edlSq.events.push(event);
// }
// // end of move in the model
// var edl = new EDL(edlSq);
// var edlFileName = this.nameFileHelper(this.model.get("title")+"_selection_order","edl");
// //if client side running inside NWJS then do this
// this.exportHelper({fileName: edlFileName, fileContent: edl.compose(), urlId: "#exportEdlSelectionOrder"});
// },
/**
* @function expoertCaptionsSrt
* @description Captions
*/
expoertCaptionsSrt: function(){
//ISSUE: not working client side only.
var fileName = this.nameFileHelper(this.model.get("title"),"srt");
var srtFileContent = this.model.constructor.returnSrtContent(this.model.get("text"));
this.exportHelper({fileName: fileName, fileContent: srtFileContent, urlId: "#expoertCaptionsSrt"});
},
/**
* @function expoertCaptionsSrtEDL
* @todo not in use, turn it back on by adding export options under captions
* @description Captions
*/
// expoertCaptionsSrtEDL: function(){
// //ISSUE: not working client side only.
// //ISSUE: papercuts selections, ammount of text per seelection might not be the optimal ammount for srt displaying on screen.
// var srtFileContent = this.model.constructor.returnEDLSrtJson(this.model.get("highlights"));
// var fileName = this.nameFileHelper(this.model.get("title")+"_EDL","srt");
// this.exportHelper({fileName: fileName, fileContent: srtFileContent, urlId: "#expoertCaptionsSrtEDL"});
// },
/**
* @function exportPlainText
* @description Plain Text - Transcription
*/
exportPlainText: function(){
//NOTE: temporary patch
//Better implementation is to fetch this from db, or have model function that
//organises and formats the text properly
//TODO: if want to add speaker names, before paragraph, get this from backend.
// var tmp = $(".words").text().replace(/%HESITATION /g, "\n\n");
var plainText = this.model.constructor.returnPlainText(this.model.attributes);
var plainTextFileName = this.nameFileHelper(this.model.get("title"),"txt");
this.exportHelper({fileName: plainTextFileName, fileContent: plainText, urlId: "#exportPlainText"});
},
/**
* @function exportTimecodedTranscription
* @description Timecoded - Transcription
*/
exportTimecodedTranscription: function(){
//TODO in model, make timecoded plain text demo
var tmp = "Timecoded text demo";
var plainTextTimecoded = this.model.constructor.returnPlainTextTimecoded(this.model.attributes);
var textFileName = this.nameFileHelper(this.model.get("title"),"txt");
this.exportHelper({fileName: textFileName, fileContent: plainTextTimecoded, urlId: "#exportTimecodedTranscription"});
},
/**
* @function exportPlainTextEDL
* @description EDL - Plain Text
*/
exportPlainTextEDL: function(){
// ISSUE: it doesn't work in client side browser only mode
var paperCuts = this.model.get("highlights");
var tmp =makePlainText(paperCuts);
var textFileName = this.nameFileHelper(this.model.get("title")+"_EDL","txt");
this.exportHelper({fileName: textFileName,fileContent: tmp, urlId: "#exportPlainTextEDL"});
function makePlainText(paperCuts){
var result ="";
for(var i =0; i< paperCuts.length; i++){
result += "["+paperCuts[i].speaker+"]" +"\n";
result += paperCuts[i].text +"\n\n";
}
return result;
}
},
/**
* @function exportPlainTextEDLSelOrder
*
*/
// exportPlainTextEDLSelOrder: function(){
// var paperCuts = this.model.get("highlights");
// var selectionsSorted = _.sortBy(paperCuts, function(o) { return o.paperCutOrder; });
// var selections = makePlainText(selectionsSorted);
// var textFileName = this.nameFileHelper(this.model.get("title")+"_EDL","txt");
// this.exportHelper({fileName: textFileName,fileContent: selections, urlId: "#exportPlainTextEDLSelOrder"});
// function makePlainText(paperCuts){
// var result ="";
// for(var i =0; i< paperCuts.length; i++){
// result += "["+paperCuts[i].speaker+"]" +"\n";
// result += paperCuts[i].text +"\n\n";
// }
// return result;
// }
// },
/**
* @function exportPlainTimecodedTextEDL
*/
exportPlainTimecodedTextEDL: function(){
//ISSUE: in browser client side mode, doesn't seem to be making the paper-cuts it just returns the words, why?
function makeTimecodedPlainText(paperCuts){
var result ="";
for(var i =0; i< paperCuts.length; i++){
//convert with timecode function
result += "["+fromSeconds(paperCuts[i].startTime) +"\t";
result += fromSeconds(paperCuts[i].endTime) +"\t";
result += paperCuts[i].speaker+"]" +"\n";
result += paperCuts[i].text +"\n\n";
}
return result;
}
var paperCuts = this.model.get("highlights");
var tmp =makeTimecodedPlainText(paperCuts);
var textFileName = this.nameFileHelper(this.model.get("title")+"_EDL","txt");
this.exportHelper({fileName: textFileName,fileContent: tmp, urlId: "#exportPlainTimecodedTextEDL"});
},
/**
* @function exportPlainTimecodedTextEDLSelOrder
* @description /JSON - Transcription
*/
exportJsonTranscription: function(){
var transcription = this.model.attributes;
var tmpTranscription = JSON.stringify(transcription,null,"\t");
var jsonFileName = this.nameFileHelper(this.model.get("title"),"json");
this.exportHelper({fileName: jsonFileName,fileContent: tmpTranscription, urlId: "#exportJsonTranscription"});
},
/**
* @function exportJsonEDL
* @description JSON - EDL
*/
exportJsonEDL: function(){
var paperCuts = this.model.get("highlights");
var tmpPaperCuts = JSON.stringify(paperCuts,null,"\t");
var jsonFileName = this.nameFileHelper(this.model.get("title")+"_EDL","json");
this.exportHelper({fileName: jsonFileName,fileContent: tmpPaperCuts, urlId: "#exportJsonEDL"});
},
/**
* @function exportJsonEDL
* @description JSON - EDL
*/
// exportJsonEDLSelOrder: function(){
// var paperCuts = this.model.get("highlights");
// var selectionsSorted = _.sortBy(paperCuts, function(o) { return o.paperCutOrder; });
// var tmpPaperCuts = JSON.stringify(selectionsSorted,null,"\t");
// var jsonFileName = this.nameFileHelper(this.model.get("title")+"_EDL","json");
// this.exportHelper({fileName: jsonFileName,fileContent: tmpPaperCuts, urlId: "#exportJsonEDLSelOrder"});
// },
/**
* @function edlUserManualInfo
*/
edlUserManualInfo: function(){
var url = 'https://opennewslabs.github.io/autoEdit_2/user_manual/usage.html#exporting-a-video-sequenceedl';
if( window.nw ){
window.nw.Shell.openExternal(url);
} else {
window.open(url);
}
},
/**
* @function selectingWords
*/
selectingWords: function(e){
// https://stackoverflow.com/questions/11300590/how-to-captured-selected-text-range-in-ios-after-text-selection-expansion
// https://jsfiddle.net/JasonMore/gWZfb/
var selectedRange = null;
if (window.getSelection) {
selectedRange = window.getSelection().getRangeAt(0).cloneContents();
} else {
selectedRange = document.getSelection().getRangeAt(0).cloneContents();
}
var selectedElements = $(selectedRange).find('span');
//first element of selection
var firstElement = $(selectedRange).find('span')[0];
var elemCount = $(selectedRange).find('span').length ;
//last element
var lastElIndex =elemCount - 1;
//last element of selection
var lastElement = $(selectedRange).find('span')[lastElIndex];
var tmpCounter = this.model.get("counterForPaperCuts");
for(var i = 0; i < elemCount; i++){
var element = $(".words[data-start-time='"+selectedElements[i].dataset.startTime.toString()+"']");
if(element.hasClass( "highlight" )){
element.removeClass("highlight");
//remove addedCounter from element
//decrease counter of addedCounter in transcription
//TODO: check if var `tmpCounter` needs to be redifined. with var or can remove var.
var tmpCounter = this.model.get("counterForPaperCuts");
// this.model.set({counterForPaperCuts: tmpCounter-1 })
element.removeData( "counterForPaperCuts" );
}else {
element.addClass("highlight");
//increment added counter in transcription
this.model.set({counterForPaperCuts: tmpCounter+1 });
element.data( "paper-cut-order", tmpCounter+1 );
}
}
var m = this.model;
//overwright/set the highlights
this.model.set({highlights: this.makePaperEdit($(".words").filter(".highlight"), m) });
//save the model
this.model.save({wait: false});
},
/**
* @function search
*/
search:function(e){
// alert("searching")
var searchedText = $(e.currentTarget).val();
var searchTextArray = searchedText.split(" ");
//TODO: need to resest the search
//make search as a
$(".words").removeClass("searched");
// $( 'p:contains('+searchedText+')' ).css( "text-decoration", "underline" );
if(searchTextArray.length > 0){
for(var i = 0; i < searchTextArray.length; i++){
$('.words[data-text='+searchTextArray[i]+']').addClass("searched");
}
}
},
/**
* @function playWord
*/
playWord: function(e){
var wordStartTime = e.currentTarget.dataset.startTime;
var videoIdElem="#"+e.currentTarget.dataset.videoId;
var videoElem = $(videoIdElem)[0];
videoElem.currentTime = wordStartTime;
videoElem.play();
var vid = document.getElementById(e.currentTarget.dataset.videoId);
vid.ontimeupdate = function() {
$("span.words").filter(function() {
if($(this).data("start-time") < $(videoIdElem)[0].currentTime){
$(this).removeClass( "text-muted" );
}else{
$(this).addClass("text-muted");
}
});
};
},
/**
* @function clearHighlights
*/
clearHighlights: function(){
var model = this.model;
var r = confirm("You are about to clear all of the highlighted selections from this transcription. Press Ok to continue or Cancel to abort");
if (r === true) {
// txt = "You pressed OK!";
$(".words").filter(".highlight").removeClass("highlight");
// .removeData( "counterForPaperCuts" );
// this.model.set({counterForPaperCuts: 0 })
model.set({highlights: [] });
model.save({wait: false});
} else {
// txt = "You pressed Cancel!";
alert("Relax, nothing was cleared");
}
},
/**
* @function makePaperEdit
* @description gets a a papercut sequence from hilighted words
*/
makePaperEdit: function (selection, model){
//needed for EDL
var modelTimecodeOffset = this.model.get("metadata").timecode;
/**
* @function groupContiguosWordsInPapercuts
* Groups words from selection into array of contiguos words
* uuses modified version of this code to divide contiguos numbers
https://stackoverflow.com/questions/22627125/grouping-consecutive-elements-together-using-javascript
*/
function groupContiguosWordsInPapercuts(selectedWords){
var result = [],
temp = [],
difference;
for (var i = 0; i < selectedWords.length; i += 1) {
$($(".words")[1]);
var wordId = $($(selectedWords[i]) ).data("word-id");
if (difference !== ( wordId- i)) {
if (difference !== undefined) {
result.push(temp);
temp = [];
}
difference = wordId - i;
}
temp.push(selectedWords[i]);
}
if (temp.length) {
result.push(temp);
}
return result;
}
/**
* @function makeSection
* @description Makes papercuts of group of words.
* by taking start of first word and end of last word.
*/
function makeSection(arrayOfPapercuts){
var result=[];
for(var i = 0; i< arrayOfPapercuts.length; i++){
var firstWord = arrayOfPapercuts[i][0];
var lastWord = arrayOfPapercuts[i][arrayOfPapercuts[i].length -1];
//TODO: iterate from first word to last word
//use 0 as starting point
//arrayOfPapercuts[i].length -1 as end point
//iterate over arrayOfPapercuts[i]
var words = [];
var selectionText = "";
for(var j =0; j< (arrayOfPapercuts[i].length -1 ); j++){
var wordText = $(arrayOfPapercuts[i][j]).data("text");
//TODO:recreate word from data attributes
var word = {
"id": $(arrayOfPapercuts[i][j]).data("word-id"),
"text": $(arrayOfPapercuts[i][j]).data("text"),
"startTime": $(arrayOfPapercuts[i][j]).data("start-time"),
"endTime": $(arrayOfPapercuts[i][j]).data("end-time")
};
//TODO: add the word to the words array
words.push(word);
selectionText+=wordText+" ";
}
model.attributes.counterForPaperCuts +=1;
var paperCut = {
id: i,
paperCutOrder: model.attributes.counterForPaperCuts,
startTime: $(firstWord).data("start-time"),
endTime: $(lastWord).data("end-time"),
reelName: $(firstWord).data("reel-name"),
clipName: $(firstWord).data("clip-name"),
speaker: $(firstWord).data("speaker"),
// wordId: $(firstWord).data("word-id"),
// lineId: $(firstWord).data("line-id"),
// paragraphId: $(firstWord).data("paragaph-id"),
transcriptionId: $(firstWord).data("transcription-id"),
videoId: $(firstWord).data("video-id"),
videoOgg: $(firstWord).data("src"),
audioFile: $(firstWord).data("audioFile"),
text: selectionText,
offset: modelTimecodeOffset,
fps: $(firstWord).data("fps"),
words: words
//TODO: Add the word array of selection to the papercut
};
result.push(paperCut);
}
return result;
}
return makeSection(groupContiguosWordsInPapercuts(selection));
},
//keyboard event using mouse trap backbone version
keyboardEvents: {
'command+k' : 'pausePlayVideo',
'ctrl+k' : 'pausePlayVideo',
'command+shift+k' : 'pausePlayVideo',
'ctrl+shift+k' : 'pausePlayVideo',
'command+j' : 'slowVideo',
'ctrl+j' : 'slowVideo',
'command+l' : 'fastVideo',
'ctrl+l' : 'fastVideo',
'command+shift+l' : 'fastForwardVideo',
'ctrl+shift+l' : 'fastForwardVideo',
'command+shift+right' : 'fastForwardVideo',
'ctrl+shift+right' : 'fastForwardVideo',
'command+shift+j' : 'rewindVideo',
'ctrl+shift+j' : 'rewindVideo',
'command+shift+left' : 'rewindVideo',
'ctrl+shift+left' : 'rewindVideo',
'command+shift+up' : 'volumeUp',
'ctrl+shift+up' : 'volumeUp',
'command+shift+down' : 'volumeDown',
'ctrl+shift+down' : 'volumeDown',
'shift+/' : 'showHideShortcuts'
},
/**
* @function Shortcuts
*/
showHideShortcuts: function(){
document.getElementById("shortcuts").click();
},
/**
* @function Keyboard shortcuts for playback -
*/
slowVideo: function(ev){
ev.preventDefault();
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
if(videoElem.playbackRate <= 0.5){
videoElem.playbackRate = 0.5 ;
}else{
videoElem.playbackRate = videoElem.playbackRate - 0.5 ;
}
if (videoElem.paused) {
videoElem.play();
}
this.updateSpeedDisplay(videoElem.playbackRate);
},
/**
* @function Keyboard shortcuts for playback -
*/
fastVideo: function(ev){
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
if(videoElem.playbackRate >= 4){
videoElem.playbackRate = 4 ;
}else{
videoElem.playbackRate = videoElem.playbackRate + 0.5 ;
}
if (videoElem.paused) {
videoElem.play();
}
this.updateSpeedDisplay(videoElem.playbackRate);
},
/**
* @function Keyboard shortcuts for playback - play/pause
*/
pausePlayVideo: function(ev){
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
if (videoElem.paused) {
videoElem.play();
} else {
videoElem.pause();
}
},
/**
* @function Keyboard shortcuts for volume - decrease
*/
volumeDown: function(ev){
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
if (videoElem.volume <= 0){
videoElem.volume = 0;
}else{
videoElem.volume -= 0.2;
}
},
/**
* @function Keyboard shortcuts for volume - increase
*/
volumeUp: function(ev){
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
videoElem.volume += 0.2;
},
/**
* @function Keyboard shortcuts for playback - fastforward
*/
fastForwardVideo: function(ev){
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
videoElem.currentTime += 5;
},
/**
* @function Keyboard shortcuts for playback - rewind
*/
rewindVideo: function(ev){
var videoIdElem="#videoId_"+this.model.get("id");
var videoElem = $(videoIdElem)[0];
videoElem.currentTime -= 5;
},
/**
* @function helper function to update diplay speed
*/
updateSpeedDisplay: function(speed){
//subtracting 1 because playback rate default / nromal is 1. but for display to user makes more sense to have zero as default
document.getElementById("speedInput").value = speed-1;
},
//end of playback volume control
/**
* @function render
*/
render: function(){
this.model.attributes.id = this.model.attributes._id;
var sectionTemplate = render('transcriptionShow', this.model.attributes);
this.$el.html(sectionTemplate);
//modify compiled template to update hilights.
var tmpHighlights = this.model.get("highlights");
for(var i = 0; i < tmpHighlights.length; i++ ){
var min = tmpHighlights[i].startTime ;
var max = tmpHighlights[i].endTime ;
//needs to grab the words of the element of the view not in the dom
this.$(".words").filter(function(){
return $(this).data('start-time') >= min && $(this).data('end-time') <= max;
}).addClass("highlight");
}
///end of modify compiled template to update hilights.
return this;
}
});