//elem = string (elementId to make TextArea)
var TextArea = function(elem) {
this.elem = $('#' + elem);
var that = this;
//this.elem = new Ox.Input({'type': 'textarea', 'id': 'textArea'}).attr("id", elem).appendTo('#txtWrapper');
this.hasFocus = false;
this.width = this.elem.width();
this.spans = [];
this.init()
}
TextArea.prototype.init = function() {
var that = this;
var e = this.elem;
var tc;
e.focus(function() {
that.hasFocus = true;
}).blur(function() {
that.hasFocus = false;
});
e.dblclick(function(e) {
var tc = that.isTc();
if (tc) {
Video.set(tc);
}
});
}
//returns tc in ms if cursor is at a time-code, else returns false
TextArea.prototype.isTc = function() {
var that = this;
var e = this.elem;
var eDom = e.get(0);
var val = e.val();
var pos = eDom.selectionStart;
var word = this.getWord(pos);
if (isValidTimecode(word)) {
return Ox.parseDuration(word);
} else {
return false;
}
}
//inserts current timecode at cursor position
TextArea.prototype.insertTc = function(tcNpt) {
var that = this;
var e = that.elem;
var eDom = e.get(0);
var scrollTop = eDom.scrollTop;
var val = this.elem.val();
var pos = eDom.selectionStart;
if(!tcNpt) {
tcNpt = Ox.formatDuration(Video.get(), 3);
}
var newVal = val.substring(0,pos) + "\n" + tcNpt + "\n" + val.substring(pos, val.length);
e.val(newVal);
e.focus();
eDom.selectionStart = pos + tcNpt.length + 2;
eDom.selectionEnd = pos + tcNpt.length + 2;
eDom.scrollTop = scrollTop + 15;
}
//gets the word at character pos (int) [in val (str)]
TextArea.prototype.getWord = function(pos, val) {
if (!val) {
val = this.elem.val();
}
var c;
var i = pos;
var j = pos;
while (c != " " && c != "\n") {
if (i==0) {
i = -1;
break;
}
i--;
c = val.substring(i,i+1);
}
var firstLetter = i+1;
var d;
while (d != " " && d != "\n") {
if (j >= val.length) {
break;
}
j++;
d = val.substring(j,j+1);
}
var lastLetter = j;
var word = val.substring(firstLetter, lastLetter);
return word;
}
function cleanNewlines(str) {
var s = str.replace(/\r\n|\r|\n/g, '\n');
return s;
}
//takes an srt as param, loads into txtarea
TextArea.prototype.fromSrt = function(txt) {
var that = this;
this.spans = [];
var srt = Ox.parseSRT(txt);
Ox.print(srt);
srt.forEach(function(s) {
that.spans.push(new Span(s['in'], s.out, s.text, that.spans.length));
});
var out = '';
var spans = this.spans;
spans.forEach(function(sp, span) {
out += Ox.formatDuration(sp.tcInMs, 3) + "\n";
out += sp.text;
out += "\n";
//If the outpoint of current span is equal to inpoint of next span, dont print out timecode, and just add the extra \n to go to next span.
if (span < spans.length - 1) {
var p = parseInt(span) + 1;
if (spans[p].tcInMs != sp.tcOutMs) {
out += Ox.formatDuration(sp.tcOutMs, 3) + "\n\n";
} else {
out += "\n";
}
} else if (parseInt(sp.tcOutMs) < Video.player.duration) {
out += "\n" + Ox.formatDuration(sp.tcOutMs, 3) + "\n\n";
}
});
this.elem.val(out);
if (this.elem.val == '' || this.spans.length == 0) {
return false;
} else {
return true;
}
}
function strip(s) {
return s.replace(/^\s+|\s+$/g,"");
}
function formatEnc(data) {
var r = '', n = 1;
data.forEach(function(item) {
r += n + ' ' + formatFrames(item['in']) + ' ' + formatFrames(item.out) + ' ' + item.text;
r += '\n\n';
n++;
});
return r;
}
function spansToSrt(arr, fmt, start_no) {
if (typeof start_no == 'undefined') {
start_no = 1;
}
var srt = [];
arr.forEach(function(s) {
if (s.text.trim() == '') {
} else {
var text = s.text.trim();
linebreaksRegex = new RegExp('\n+', "g")
text = text.replace(linebreaksRegex, "\n");
if (!s.tcOutMs) {
s.tcOutMs = Video.player.duration;
}
srt.push({
'in': s.tcInMs,
out: s.tcOutMs,
text: text
});
}
});
if (fmt == 'srt') {
return Ox.formatSRT(srt);
} else if (fmt == 'enc') {
return formatEnc(srt);
}
return srt;
}
//returns textarea formatted to .srt
TextArea.prototype.toSrt = function(fmt) {
if (!fmt) var fmt = 'srt';
var text = cleanNewlines(this.elem.val());
var lines = [];
lines = text.split("\n");
var i=0;
var j=0;
var spans = this.spans = [];
while (i < lines.length) {
var l = lines[i];
if (isValidTimecode(l.trim())) {
var tcIn = l.trim();
var t = "";
var thisLine = '';
while (!isValidTimecode(thisLine.trim())) {
i++;
if (i >= lines.length) {
break;
}
thisLine = lines[i];
if (!isValidTimecode(thisLine.trim())) {
t += thisLine + "\n";
}
}
var tcOut = $.trim(thisLine);
spans[j] = new Span(tcIn, tcOut, t, j);
j++;
} else {
i++;
}
}
this.spans = spans;
var srt = spansToSrt(spans, fmt);
//console.log(srt);
return srt;
}
TextArea.prototype.toHTML = function() {
var spans = this.spans;
var html = '';
var span, txt;
for (var i=0; i
\n';
txt += span.text + "\n";
txt += "