speedtrans/js/classes.js

398 lines
10 KiB
JavaScript
Raw Normal View History

2015-04-21 14:25:36 +00:00
//elem = string (elementId to make TextArea)
var TextArea = function(elem) {
2016-02-29 09:14:47 +00:00
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()
}
2015-04-21 14:25:36 +00:00
TextArea.prototype.init = function() {
2016-02-29 09:14:47 +00:00
var that = this;
var e = this.elem;
var tc;
e.focus(function() {
that.hasFocus = true;
2015-04-21 14:25:36 +00:00
}).blur(function() {
2016-02-29 09:14:47 +00:00
that.hasFocus = false;
2015-04-21 14:25:36 +00:00
});
2016-02-29 09:14:47 +00:00
e.dblclick(function(e) {
var tc = that.isTc();
if (tc) {
Video.set(tc);
}
2015-04-21 14:25:36 +00:00
});
2016-02-29 09:14:47 +00:00
}
2015-04-21 14:25:36 +00:00
//returns tc in ms if cursor is at a time-code, else returns false
TextArea.prototype.isTc = function() {
2016-02-29 09:14:47 +00:00
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)) {
2016-02-29 11:56:25 +00:00
return Ox.parseDuration(word);
2015-04-21 14:25:36 +00:00
} else {
2016-02-29 09:14:47 +00:00
return false;
2015-04-21 14:25:36 +00:00
}
2016-02-29 09:14:47 +00:00
}
2015-04-21 14:25:36 +00:00
//inserts current timecode at cursor position
TextArea.prototype.insertTc = function(tcNpt) {
2016-02-29 09:14:47 +00:00
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) {
2016-02-29 11:56:25 +00:00
tcNpt = Ox.formatDuration(Video.get(), 3);
2016-02-29 09:14:47 +00:00
}
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;
}
2015-04-21 14:25:36 +00:00
//gets the word at character pos (int) [in val (str)]
TextArea.prototype.getWord = function(pos, val) {
2016-02-29 09:14:47 +00:00
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);
2015-04-21 14:25:36 +00:00
}
2016-02-29 09:14:47 +00:00
var firstLetter = i+1;
var d;
while (d != " " && d != "\n") {
if (j >= val.length) {
break;
}
j++;
d = val.substring(j,j+1);
2015-04-21 14:25:36 +00:00
}
2016-02-29 09:14:47 +00:00
var lastLetter = j;
var word = val.substring(firstLetter, lastLetter);
return word;
}
2015-04-21 14:25:36 +00:00
function cleanNewlines(str) {
2016-02-29 09:14:47 +00:00
var s = str.replace(/\r\n|\r|\n/g, '\n');
return s;
}
2015-04-21 14:25:36 +00:00
//takes an srt as param, loads into txtarea
TextArea.prototype.fromSrt = function(txt) {
2016-02-29 09:14:47 +00:00
var that = this;
this.spans = [];
2016-02-29 11:56:25 +00:00
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));
});
2016-02-29 09:14:47 +00:00
var out = '';
var spans = this.spans;
2016-02-29 11:56:25 +00:00
spans.forEach(function(sp, span) {
out += Ox.formatDuration(sp.tcInMs, 3) + "\n";
out += sp.text;
out += "\n";
2015-04-21 14:25:36 +00:00
//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.
2016-02-29 11:56:25 +00:00
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";
2016-02-29 09:14:47 +00:00
}
2016-02-29 11:56:25 +00:00
} else if (parseInt(sp.tcOutMs) < Video.player.duration) {
out += "\n" + Ox.formatDuration(sp.tcOutMs, 3) + "\n\n";
2015-04-21 14:25:36 +00:00
}
2016-02-29 11:56:25 +00:00
});
2016-02-29 09:14:47 +00:00
this.elem.val(out);
if (this.elem.val == '' || this.spans.length == 0) {
return false;
} else {
return true;
}
2015-04-21 14:25:36 +00:00
}
function strip(s) {
2016-02-29 09:14:47 +00:00
return s.replace(/^\s+|\s+$/g,"");
}
2015-04-21 14:25:36 +00:00
2016-02-29 11:56:25 +00:00
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;
2016-02-29 09:14:47 +00:00
}
2015-04-21 14:25:36 +00:00
function spansToSrt(arr, fmt, start_no) {
2016-02-29 09:14:47 +00:00
if (typeof start_no == 'undefined') {
start_no = 1;
2015-04-21 14:25:36 +00:00
}
2016-02-29 11:56:25 +00:00
var srt = [];
arr.forEach(function(s) {
2016-02-29 09:14:47 +00:00
if (s.text.trim() == '') {
2016-02-29 11:56:25 +00:00
} else {
2016-02-29 09:14:47 +00:00
var text = s.text.trim();
linebreaksRegex = new RegExp('\n+', "g")
text = text.replace(linebreaksRegex, "\n");
if (!s.tcOutMs) {
2016-02-29 09:36:39 +00:00
s.tcOutMs = Video.player.duration;
2016-02-29 09:14:47 +00:00
}
2016-02-29 11:56:25 +00:00
srt.push({
'in': s.tcInMs,
out: s.tcOutMs,
text: text
});
2015-04-21 14:25:36 +00:00
}
2016-02-29 11:56:25 +00:00
});
if (fmt == 'srt') {
return Ox.formatSRT(srt);
} else if (fmt == 'enc') {
return formatEnc(srt);
}
2016-02-29 09:14:47 +00:00
return srt;
}
2015-04-21 14:25:36 +00:00
//returns textarea formatted to .srt
TextArea.prototype.toSrt = function(fmt) {
2016-02-29 09:14:47 +00:00
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++;
2015-04-21 14:25:36 +00:00
}
}
2016-02-29 09:14:47 +00:00
this.spans = spans;
var srt = spansToSrt(spans, fmt);
//console.log(srt);
2016-02-29 11:56:25 +00:00
return srt;
2015-12-20 23:54:18 +00:00
}
2015-04-21 14:25:36 +00:00
TextArea.prototype.toHTML = function() {
2016-02-29 09:14:47 +00:00
var spans = this.spans;
var html = '';
var span, txt;
for (var i=0; i<spans.length; i++) {
span = spans[i];
txt = '';
txt += '<p>\n';
2016-02-29 11:56:25 +00:00
txt += '<a href="https://pad.ma/PADMA_VIDEO_ID/' + Ox.formatDuration(span.tcInMs, 3) + "-" + Ox.formatDuration(span.tcOutMs, 3) + '">';
2016-02-29 09:14:47 +00:00
txt += (i + 1).toString();
txt += "</a>\n";
txt += '<br />\n';
txt += span.text + "\n";
txt += "</p>\n\n";
html += txt;
}
return Ox.encodeUTF8(html);
2015-12-20 23:54:18 +00:00
}
2015-04-21 14:25:36 +00:00
//creates new Span (tcIn and tcOut in npt format)
var Span = function(tcIn, tcOut, text, index) {
2016-02-29 09:14:47 +00:00
this.index = index;
2016-02-29 11:56:25 +00:00
this.tcOutMs = Ox.isString(tcOut) ? Ox.parseDuration(tcOut) : tcOut;
2016-02-29 09:14:47 +00:00
this.text = text;
2016-02-29 11:56:25 +00:00
this.tcInMs = Ox.isString(tcOut) ? Ox.parseDuration(tcIn) : tcIn;
2016-02-29 09:14:47 +00:00
}
2015-04-21 14:25:36 +00:00
var SeekBar = function(elem) {
2016-02-29 09:14:47 +00:00
this.elem = $('#' + elem);
this.pointerId = "seekPointer";
this.width = this.elem.width();
this.init()
}
2015-04-21 14:25:36 +00:00
SeekBar.prototype.init = function() {
2016-02-29 09:14:47 +00:00
var that = this;
var e = $('<div />');
e.attr("id", that.pointerId);
e.draggable({
containment: 'parent',
axis: 'horizontally',
drag: function() {
clearInterval(videoListener);
var pos = that.get();
2016-02-29 11:56:25 +00:00
$('#timeCode').html(Ox.formatDuration(pos, 3));
2016-02-29 09:14:47 +00:00
},
stop: function() {
var pos = that.get();
Video.set(pos);
videoListener = setInterval(Video.listener, 250);
}
});
that.elem.append(e);
this.pointerElem = e;
}
2015-04-21 14:25:36 +00:00
//gets current time-code (int) of seekbar position
SeekBar.prototype.get = function() {
2016-02-29 09:14:47 +00:00
var cssPos = parseInt(this.pointerElem.css("left"));
2016-02-29 09:36:39 +00:00
var pos = parseInt((cssPos / this.width) * Video.player.duration);
2016-02-29 09:14:47 +00:00
return pos;
}
2015-04-21 14:25:36 +00:00
//sets seek bar css pos according to current time-code
2016-02-29 09:36:39 +00:00
SeekBar.prototype.set = function(seconds) {
var cssPos = parseInt((seconds / Video.player.duration) * this.width);
2016-02-29 09:14:47 +00:00
this.elem.css("left", cssPos + "px");
}
2015-04-21 14:25:36 +00:00
/*
var KeyboardController = function() {
this.
2016-02-29 09:14:47 +00:00
}
2015-04-21 14:25:36 +00:00
*/
var User = function() {
2016-02-29 09:14:47 +00:00
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);
}
2015-04-21 14:25:36 +00:00
}
}
User.prototype.init = function() {
2016-02-29 09:14:47 +00:00
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);
}
2015-04-21 14:25:36 +00:00
}
User.prototype.set_pref = function(key, val) {
2016-02-29 09:14:47 +00:00
var that = this;
2016-02-29 09:16:59 +00:00
localStorage[key] = val;
2016-02-29 09:14:47 +00:00
this.callbacks[key](val);
2015-04-21 14:25:36 +00:00
}
User.prototype.get_pref = function(key) {
2016-02-29 09:14:47 +00:00
var that = this;
2016-02-29 09:16:59 +00:00
if (typeof localStorage[key] === 'undefined') {
localStorage[key] = that.defaults[key];
2016-02-29 09:14:47 +00:00
}
2016-02-29 09:16:59 +00:00
return localStorage[key];
2015-04-21 14:25:36 +00:00
}
User.prototype.set_prefs = function(keyvals) {
2016-02-29 09:14:47 +00:00
for (key in keyvals) {
if (keyvals.hasOwnProperty(key)) {
this.set_pref(key, keyvals[key]);
}
}
2015-04-21 14:25:36 +00:00
}
User.prototype.get_prefs = function(keys) {
2016-02-29 09:14:47 +00:00
var ret = {};
for (var i=0; i < keys.length; i++) {
var key = keys[i];
ret[key] = this.get_pref(key);
}
return ret;
2015-04-21 14:25:36 +00:00
}
User.prototype.set_txt = function(filename, txt) {
2016-02-29 09:14:47 +00:00
var that = this;
2016-02-29 09:16:59 +00:00
localStorage['txt_' + filename] = txt;
2015-04-21 14:25:36 +00:00
}
User.prototype.get_txt = function(filename) {
2016-02-29 09:14:47 +00:00
var that = this;
2016-02-29 09:16:59 +00:00
return localStorage['txt_' + filename];
2015-04-21 14:25:36 +00:00
}
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) {
2016-02-29 11:56:25 +00:00
textArea.insertTc(Ox.formatDuration(cut));
2015-04-21 14:25:36 +00:00
});
});
}
2015-12-20 23:54:18 +00:00
function loadSrt(file) {
var reader = new FileReader();
reader.onload = function(data) {
textArea.fromSrt(data.target.result);
};
2016-02-23 12:33:07 +00:00
reader.readAsText(file);
2015-04-21 14:25:36 +00:00
}