Make Save/Load buttons use files

This commit is contained in:
Joshua Bell 2015-12-02 20:23:19 -08:00
parent 2e27cda07b
commit 03bad52bdd
2 changed files with 32 additions and 10 deletions

View File

@ -111,9 +111,9 @@ By <a href="mailto:inexorabletash@gmail.com">Joshua Bell</a>
<form id="submission" method="post" enctype="text/plain" action="mailto:inexorabletash@gmail.com?subject=Applesoft%20Sample%20Submission">
<textarea name="source" id="source" style="display: none;">
</textarea>
<button id="btn_save" title="Save">&#x1f4be;</button>
<button id="btn_load" title="Load">&#x1f4c2;</button>
<input type="submit" id="btn_share" value="Share your sample!">
<input type="button" id="btn_save" value="Save Program" title="Save the current program. NOTE: Can only save one program.">
<input type="button" id="btn_load" value="Load Program" title="Load a previously saved program. WARNING: The current program will be lost!">
<input type="button" id="show_paper" value="Show output" title="Echo all output to a &quot;print-out&quot;, so you can copy/paste">
<input type="button" id="hide_paper" value="Hide output" title="Hide the &quot;print-out&quot;">
</form>

View File

@ -124,6 +124,7 @@ window.addEventListener('DOMContentLoaded', function () {
stopped = false;
updateUI();
$('#btn_stop').focus();
program.init({
tty: tty,
@ -147,14 +148,33 @@ window.addEventListener('DOMContentLoaded', function () {
loadFile('samples/' + sel.value + ".txt", setSource);
});
$('#btn_save').addEventListener('click', function () {
window.localStorage.setItem("save_program", getSource());
$('#btn_load').disabled = false;
var current_file_name;
$('#btn_save').addEventListener('click', function(e) {
e.preventDefault();
var a = document.createElement('a');
a.download = current_file_name || 'basic_program.txt';
a.href = 'data:text/plain;base64,' + window.btoa(getSource());
document.body.appendChild(a);
a.click();
a.parentElement.removeChild(a);
});
$('#btn_load').addEventListener('click', function () {
setSource(window.localStorage.getItem("save_program"));
$('#btn_load').addEventListener('click', function(e) {
e.preventDefault();
var input = document.createElement('input');
input.type = 'file';
input.addEventListener('change', function(e) {
var file = e.target.files[0];
current_file_name = file.name;
var reader = new FileReader();
reader.addEventListener('load', function() {
setSource(reader.result);
});
reader.readAsText(file);
});
document.body.appendChild(input);
input.click();
input.parentElement.removeChild(input);
});
$('#btn_load').disabled = (window.localStorage.getItem("save_program") === null);
// Add a "printer" on demand
var printer = null;
@ -181,12 +201,14 @@ window.addEventListener('DOMContentLoaded', function () {
var stopped = true;
function updateUI() {
var btnFocus = (document.activeElement === $("#btn_run") ||
document.activeElement === $("#btn_stop"));
$("#btn_stop").disabled = stopped ? "disabled" : "";
$("#btn_run").disabled = stopped ? "" : "disabled";
$("#lb_files").disabled = stopped ? "" : "disabled";
if (stopped) {
$("#btn_run").focus();
if (btnFocus) {
$(stopped ? "#btn_run" : "#btn_stop").focus();
} else {
tty.focus();
}