Refactored code for file editing

master
Hauke D 7 years ago
parent f70e5a09f4
commit 2ef4af02cb

@ -165,10 +165,27 @@ function fetchUrl(url,cm) { // fetch the contents of a URL into a CodeMirror ins
}); });
} }
function fileRename () { function makeCodeWithFn (fn,targ,ro,nodel) {
var div = $(this); var div = $('<div/>',{class:"codewithfn"});
var newname = prompt("Please choose a new filename:",div.text()); //TODO: the "delete" and "rename" functions should probably be styled better
if (newname) div.text(newname); if (!nodel) {
$('<div/>',{class:"fakelink",style:"float:right;",text:"delete"})
.appendTo(div).click(function () {
if (confirm("Are you sure you want to remove this file?"))
div.remove() });
}
$('<div/>',{class:"filename",text:fn})
.appendTo(div).click(function () {
var filename = $(this);
var newname = prompt("Please choose a new filename:", filename.text());
if (newname) filename.text(newname);
});
div.append( $('<div/>',{style:"clear:both;"}) );
var ta = $('<textarea/>').appendTo(div);
targ.before(div);
var cm = makeCM(ta, !fn.match(/\.pl$/i), ro);
div.data('CodeMirrorInstance', cm);
return {div:div,ta:ta,cm:cm};
} }
function setupOutputFile (fn, text) { function setupOutputFile (fn, text) {
@ -180,18 +197,9 @@ function setupOutputFile (fn, text) {
cm = div.data('CodeMirrorInstance'); cm = div.data('CodeMirrorInstance');
} }
else { else {
var div = $('<div/>',{class:"codewithfn outputs"}); var cfn = makeCodeWithFn(fn, $('#outputhere'), 1);
//TODO: the "delete" and "rename" functions should probably be styled better cfn.div.addClass("outputs");
$('<div/>',{class:"fakelink",style:"float:right;",text:"delete"}) cm = cfn.cm;
.appendTo(div).click(function () {
if (confirm("Are you sure you want to remove this file?"))
div.remove() });
div.append( $('<div/>',{class:"filename",text:fn}).click(fileRename) );
div.append( $('<div/>',{style:"clear:both;"}) );
var ta = $('<textarea/>').appendTo(div);
$('#outputhere').before(div);
cm = makeCM(ta,1,1);
div.data('CodeMirrorInstance', cm);
} }
cm.setValue( text ? text : '' ); cm.setValue( text ? text : '' );
} }
@ -214,21 +222,12 @@ function setupInputFile (inp) {
throw 'Too many input files'; throw 'Too many input files';
} }
} }
var div = $('<div/>',{class:"codewithfn inputs"}); var cfn = makeCodeWithFn(fn, $('#inputhere'), 0);
$('<div/>',{class:"fakelink",style:"float:right;",text:"delete"}) cfn.div.addClass("inputs");
.appendTo(div).click(function () {
if (confirm("Are you sure you want to remove this file?"))
div.remove() });
div.append( $('<div/>',{class:"filename",text:fn}).click(fileRename) );
div.append( $('<div/>',{style:"clear:both;"}) );
var ta = $('<textarea/>').appendTo(div);
$('#inputhere').before(div);
var cm = makeCM(ta,1,0);
div.data('CodeMirrorInstance', cm);
if (inp["text"]) if (inp["text"])
cm.setValue(inp.text); cfn.cm.setValue(inp.text);
else if (inp["url"]) else if (inp["url"])
fetchUrl(inp.url,cm); fetchUrl(inp.url, cfn.cm);
} }
//TODO: implement a "get URL" feature //TODO: implement a "get URL" feature
@ -270,21 +269,16 @@ $(function () {
var fn = prompt("Please choose a filename for the new output file:"); var fn = prompt("Please choose a filename for the new output file:");
if (fn) setupOutputFile( fn ); if (fn) setupOutputFile( fn );
}); });
$('#script>.filename').click(fileRename);
// script // script
if ( hash["script"] || hash["script_url"] ) { if ( hash["script"] || hash["script_url"] ) {
var scriptdiv = $('#script'); var fn = hash["script_fn"] ? hash.script_fn : 'script.pl';
scriptdiv.show(); var cfn = makeCodeWithFn(fn, $('#perlctrl'), 0, 1);
var cm = makeCM($('#perlcode'),0,0); cfn.div.attr("id", "script");
scriptdiv.data('CodeMirrorInstance', cm);
if (hash["script"]) if (hash["script"])
cm.setValue(hash.script); cfn.cm.setValue(hash.script);
else if (hash["script_url"]) else if (hash["script_url"])
fetchUrl(hash.script_url,cm); fetchUrl(hash.script_url, cfn.cm);
else throw "internal error: this shouldn't happen";
if (hash["script_fn"]) // default value is in HTML below
$('>.filename',scriptdiv).text(hash.script_fn);
} }
// command line // command line
@ -348,12 +342,7 @@ $(function () {
<span id="addinput" class="fakelink">Add Input File</span> <span id="addinput" class="fakelink">Add Input File</span>
</div> </div>
<div id="script" class="codewithfn" style="display:none;"> <div id="perlctrl">
<div class="filename">script.pl</div>
<textarea id="perlcode"></textarea>
</div>
<div>
<button id="runperl"><code>perl</code> &#x25BA;</button> <button id="runperl"><code>perl</code> &#x25BA;</button>
<input type="text" id="argv" class="code" size="60" value='perl' /> <input type="text" id="argv" class="code" size="60" value='perl' />
</div> </div>

Loading…
Cancel
Save