var url = "https://speedtrans.pad.ma";
//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 npt2ms(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 = ms2npt(Video.get());
}
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 cleaned = cleanNewlines(txt);
var srt = strip(cleaned);
var srt_ = srt.split('\n\n');
var s;
for(s in srt_) {
st = srt_[s].split('\n');
if(st.length >=2) {
var n = st[0];
var i = strip(st[1].split(' --> ')[0]);
var o = strip(st[1].split(' --> ')[1]);
var t = st[2];
if(st.length > 2) {
for(j=3; j " + "0" + ms2npt(s.tcOutMs).replace(".", ",");
srt += "\n";
srt += text;
srt += "\n\n";
}
else if (fmt == 'enc') {
srt += srtNo + ms2frames(s.tcInMs) + " " + ms2frames(s.tcOutMs) + " " + text;
srt += "\n\n";
}
srtNo++;
}
}
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 Ox.encodeUTF8(srt);
}
TextArea.prototype.addTime = function(ms, start_no) {
//console.log(ms);
this.toSrt();
var s = [];
var spans = this.spans;
for (var i=0; i < spans.length;i++) {
s[i] = {
index: i,
tcOutMs: spans[i].tcOutMs + ms,
text: spans[i].text,
tcInMs: spans[i].tcInMs + ms
}
}
return spansToSrt(s, 'srt', start_no);
}
TextArea.prototype.toHTML = function() {
var spans = this.spans;
var html = '';
var span, txt;
for (var i=0; i\n';
txt += '';
txt += (i + 1).toString();
txt += "\n";
txt += '
\n';
txt += span.text + "\n";
txt += "
\n\n";
html += txt;
}
return Ox.encodeUTF8(html);
}
//creates new Span (tcIn and tcOut in npt format)
var Span = function(tcIn, tcOut, text, index) {
this.index = index;
this.tcOutMs = npt2ms(tcOut);
this.text = text;
this.tcInMs = npt2ms(tcIn);
}
var SeekBar = function(elem) {
this.elem = $('#' + elem);
this.pointerId = "seekPointer";
this.width = this.elem.width();
this.init()
}
SeekBar.prototype.init = function() {
var that = this;
var e = $('');
e.attr("id", that.pointerId);
e.draggable({
containment: 'parent',
axis: 'horizontally',
drag: function() {
clearInterval(videoListener);
var pos = that.get();
$('#timeCode').html(ms2npt(pos));
},
stop: function() {
var pos = that.get();
Video.set(pos);
videoListener = setInterval(Video.listener, 250);
}
});
that.elem.append(e);
this.pointerElem = e;
}
//gets current time-code (int) of seekbar position
SeekBar.prototype.get = function() {
var cssPos = parseInt(this.pointerElem.css("left"));
var pos = parseInt((cssPos / this.width) * Video.player.duration);
return pos;
}
//sets seek bar css pos according to current time-code
SeekBar.prototype.set = function(seconds) {
var cssPos = parseInt((seconds / Video.player.duration) * this.width);
this.elem.css("left", cssPos + "px");
}
/*
var KeyboardController = function() {
this.
}
*/
var User = function() {
this.padmaUser = typeof user != 'undefined' ? user : null;
this.hostname = 'chrome://speedtrans';
this.defaults = {
'fontSize': '13',
'theme': 'oxlight',
'recentVideo': '',
'recentSrt': '',
'autosave': 5,
'direction': 'ltr'
};
var autoSaveInterval = -1;
this.callbacks = {
'fontSize': function(newSize) {
textArea.elem.css("font-size", newSize + "px");
},
'theme': function(theme) {
if(['oxdark', 'oxmedium', 'oxlight'].indexOf(theme) == -1) {
console.log('only supporting oxlight, oxmedium, oxdark, but ' + theme + ' was requested');
theme = 'oxlight';
}
Ox.Theme(theme);
},
'recentVideo': $.noop,
'recentSrt': $.noop,
'direction': function(val) {
textArea.elem.css("direction", val);
}
}
}
User.prototype.init = function() {
var prefs = ['fontSize', 'theme', 'direction'];
for (var i = 0; i < prefs.length; i++) {
var key = prefs[i];
var val = this.get_pref(key);
this.callbacks[key](val);
}
}
User.prototype.set_pref = function(key, val) {
var that = this;
localStorage[key] = val;
this.callbacks[key](val);
}
User.prototype.get_pref = function(key) {
var that = this;
if (typeof localStorage[key] === 'undefined') {
localStorage[key] = that.defaults[key];
}
return localStorage[key];
}
User.prototype.set_prefs = function(keyvals) {
for (key in keyvals) {
if (keyvals.hasOwnProperty(key)) {
this.set_pref(key, keyvals[key]);
}
}
}
User.prototype.get_prefs = function(keys) {
var ret = {};
for (var i=0; i < keys.length; i++) {
var key = keys[i];
ret[key] = this.get_pref(key);
}
return ret;
}
User.prototype.set_txt = function(filename, txt) {
var that = this;
localStorage['txt_' + filename] = txt;
}
User.prototype.get_txt = function(filename) {
var that = this;
return localStorage['txt_' + filename];
}
function loadCuts(url, id) {
function pandora_api(action, data, callback) {
$.ajax({
type: "POST",
url: url,
data: {action: action, data: JSON.stringify(data)},
success: callback,
dataType: 'json'
});
}
pandora_api('get', {id: id, keys: ['cuts']}, function(result) {
var txt = '';
result.data.cuts.forEach(function(cut) {
textArea.insertTc(ms2npt(cut));
});
});
}
function loadSrt(file) {
var reader = new FileReader();
reader.onload = function(data) {
textArea.fromSrt(data.target.result);
};
reader.readAsText(file);
}