oxspeed/js/ui/textArea.js

252 lines
8.8 KiB
JavaScript
Raw Permalink Normal View History

2013-03-04 08:44:48 +00:00
pandora.ui.textArea = function() {
var that = Ox.Input({
type: 'textarea',
width: 400,
height: 400,
changeOnKeypress: true
}).bindEvent("change", function() {
Ox.print("textarea changed");
that.speedtrans.save();
that.changed = true;
});
2013-03-04 08:44:48 +00:00
var $video = pandora.$ui.videoPlayer;
that.changed = false;
2013-03-04 08:44:48 +00:00
that.speedtrans = {
spans: [],
2013-03-05 11:50:51 +00:00
storage: '',
2013-03-04 08:44:48 +00:00
insertTc: function() {
var eDom = that.find("textarea").get(0); //FIXME
var scrollTop = eDom.scrollTop;
var val = that.value();
var pos = eDom.selectionStart;
var tcNpt = ms2npt(pandora.$ui.videoPlayer.options("position") * 1000);
var newVal = val.substring(0,pos) + "\n" + tcNpt + "\n" + val.substring(pos, val.length);
that.value(newVal);
that.find("textarea").focus(); //FIXME
eDom.selectionStart = pos + tcNpt.length + 2;
eDom.selectionEnd = pos + tcNpt.length + 2;
eDom.scrollTop = scrollTop + 15;
},
isTc: function() {
var eDom = that.find("textarea").get(0);
var val = that.value();
var pos = eDom.selectionStart;
var word = that.speedtrans.getWord(pos, val);
if (isValidTimecode(word)) {
return npt2ms(word);
} else {
return false;
}
},
getWord: function(pos, 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;
},
toSrt: function(fmt, txt) {
2013-03-04 08:44:48 +00:00
if (!fmt) var fmt = 'srt';
if (txt === undefined) {
txt = that.value();
}
var text = cleanNewlines(txt);
2013-03-04 08:44:48 +00:00
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] = {
tcInMs: npt2ms(tcIn),
tcOutMs: npt2ms(tcOut),
text: t,
index: j
};
//this.spans.push(spans[j]);
j++;
} else {
i++;
}
}
this.spans = spans;
var duration = pandora.$ui.videoPlayer.options("duration");
//Ox.print(duration, spans, fmt);
2013-03-04 08:44:48 +00:00
var srt = spansToSrt(duration, spans, fmt);
// console.log(srt);
return srt;
},
2013-03-05 11:50:51 +00:00
fromSrt: function(txt) {
var annots = Ox.parseSRT(txt);
var i = 0;
var spans = Ox.map(annots, function(v) {
var obj = {
'tcInMs': parseInt(v['in'] * 1000),
'tcOutMs': parseInt(v.out * 1000),
'text': v.text,
'index': i
}
i++;
return obj;
});
//console.log(spans);
2013-03-05 11:50:51 +00:00
var out = '';
for (span in spans) {
if (spans.hasOwnProperty(span)) {
var sp = spans[span];
out += ms2npt(sp.tcInMs) + "\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) > 1) {
// if (spans[p].tcInMs != sp.tcOutMs) {
2013-03-05 11:50:51 +00:00
out += ms2npt(sp.tcOutMs) + "\n\n";
} else {
out += "\n";
}
}
}
}
//console.log(out);
2013-03-05 11:50:51 +00:00
that.value(out);
},
save: function() {
//console.log(that.value());
2013-03-05 11:50:51 +00:00
//var videoName = pandora.$ui.videoPlayer.videoName;
//console.log(videoName);
//console.log(that.storage);
//FIXME get type
var typ = pandora.$ui.selectAnnotationType.value();
var currStorage = pandora.storage(that.storage);
currStorage[typ] = that.value();
// pandora.storage(that.storage, {typ: that.value()});
pandora.storage(that.storage, currStorage);
2013-03-04 08:44:48 +00:00
},
load: function(options) {
console.log(options);
// if (options.type == 'local') {
// var file = options.file;
// var name = file.name;
// var url = window.URL.createObjectURL(file);
// } else if (options.type == 'remote') {
// var url = options.url;
// var name = options.name;
// }
var val = pandora.storage(options.name) || {};
var typ = options.typ || Object.keys(val)[0] || pandora.utils.getUntitledName();
2013-03-05 11:50:51 +00:00
that.storage = options.name;
that.currentTrack = typ;
pandora.$ui.selectAnnotationType.value(typ);
if (!val[typ]) {
val[typ] = '';
};
that.value(val[typ]);
pandora.storage(that.storage, val);
2013-03-04 08:44:48 +00:00
var videoOptions = {
'url': options.url
};
console.log(videoOptions);
2013-03-04 08:44:48 +00:00
pandora.$ui.videoPlayer = pandora.ui.videoPlayer(videoOptions);
//pandora.$ui.videoPlayer = videoElem;
pandora.$ui.videoPanel.replaceElement(0, pandora.$ui.videoPlayer);
//Ox.print(that.speedtrans.getLayers());
clearInterval(pandora.$ui.annotationsPanel.interval);
pandora.$ui.annotationsPanel.replaceWith(
pandora.$ui.annotationsPanel = pandora.ui.annotationsPanel(that.speedtrans.getLayers())
);
pandora.$ui.videoPanel.replaceElement(1, pandora.$ui.annotationsPanel);
pandora.utils.updateFolderMenus();
2013-03-04 08:44:48 +00:00
//console.log(filename);
//$video.options("video", filename);
},
getLayers: function() {
var $textArea = pandora.$ui.textArea;
var speedtrans = pandora.$ui.textArea.speedtrans;
var storageName = $textArea.storage;
if (!storageName) {
return [];
}
var storage = pandora.storage(storageName);
var keys = Object.keys(storage);
return $.map(keys, function(key) {
if (!key) { return null; }
return {
'id': key,
'title': key,
'items': speedtrans.toSrt('layers', storage[key]),
}
});
2013-03-04 08:44:48 +00:00
}
};
that.bindEvent(Ox.extend(pandora.utils.commonKeyboardEvents, {
2013-03-04 08:44:48 +00:00
key_control_x: function() {
that.speedtrans.insertTc();
}//,
// key_tab: function() {
// Ox.print("tab pressed");
// return false;
// }
}));
that.keydown(function(e) {
Ox.print("keydown");
if (e.keyCode == 9) {
e.preventDefault();
that.speedtrans.insertTc();
2013-03-04 08:44:48 +00:00
}
});
2013-03-04 08:44:48 +00:00
that.dblclick(function() {
//console.log("dblclicked");
2013-03-04 08:44:48 +00:00
var tc = that.speedtrans.isTc();
//Ox.print($video);
//Ox.print(tc);
2013-03-04 08:44:48 +00:00
if (tc) {
//GLOB = $video;
pandora.$ui.videoPlayer.options("position", tc / 1000);
2013-03-04 08:44:48 +00:00
}
});
return that;
}