Permalink
Browse files

Added load/save functionality

  • Loading branch information...
brymck committed Jun 24, 2011
1 parent f19bfe5 commit 7f9a0e54acaecbae2dd204f6ff519d14fc6c26c4
Showing with 85 additions and 6 deletions.
  1. +10 −1 index.html
  2. +75 −5 js/sandbox.js
View
@@ -9,6 +9,11 @@
<h1>Sandbox</h1>
<form id="sandbox_form" accept-charset="utf-8">
<ul>
<li>
<select id="saved">
<option></option>
</select>
</li>
<li>
<label for="code">Stuff to eval:</label><br>
<textarea id="code" rows="12" cols="80" autofocus></textarea>
@@ -29,7 +34,11 @@ <h1>Sandbox</h1>
<label for="timer" class="fixed_width">Time elapsed:</label>
<input type="text" id="timer" value="" readonly>
</li>
<li><input type="submit" value="Run (Ctrl+Shift+R)"></li>
<li>
<input type="submit" value="Run (Ctrl+Shift+R)">
<input type="button" id="save_btn" value="Save (Ctrl+Shift+S)">
<input type="button" id="del_btn" value="Delete (Ctrl+Shift+D)">
</li>
</ul>
</form>
View
@@ -1,34 +1,98 @@
var Sandbox = (function() {
var KeyCodes = {
R: 82
D: 68,
R: 82,
S: 83
};
var form = document.getElementById("sandbox_form");
var code = document.getElementById("code");
var iter = document.getElementById("iter");
var timer = document.getElementById("timer");
var keycode = document.getElementById("keycode");
var error = document.getElementById("error");
var saved = document.getElementById("saved");
var saveBtn = document.getElementById("save_btn");
var delBtn = document.getElementById("del_btn");
function addListeners() {
form.onsubmit = Sandbox.run;
form.onkeydown = keyHandler;
saved.onchange = loadSave;
saveBtn = Sandbox.save;
delBtn = Sandbox.remove;
}
function keyHandler(e) {
if (e.which === KeyCodes.R) {
if (e.ctrlKey && e.shiftKey) {
Sandbox.run();
e.preventDefault();
if (e.ctrlKey && e.shiftKey) {
switch (e.which) {
case KeyCodes.D:
Sandbox.remove();
e.preventDefault();
break;
case KeyCodes.R:
Sandbox.run();
e.preventDefault();
break;
case KeyCodes.S:
e.preventDefault();
Sandbox.save();
break;
default:
break;
}
}
keycode.value = e.which;
}
function loadSave(e) {
Sandbox.load(e.target.value);
}
function listSaves(loaded) {
var keys = [];
if (!("" in localStorage)) {
localStorage[""] = "";
}
// Clear existing options
while (saved.firstChild) {
saved.removeChild(saved.firstChild);
}
// Alphabetize keys in local storage
for (var key in localStorage) {
keys.push(key);
}
keys.sort();
for (var i = 0; i < keys.length; i++) {
var option = document.createElement("option"),
text = document.createTextNode(keys[i]);
option.selected = (loaded === keys[i]);
option.appendChild(text);
saved.appendChild(option);
}
}
return {
init: function() {
listSaves();
addListeners();
},
load: function(name) {
code.value = (name === "" ? "" : JSON.parse(localStorage[name]));
},
remove: function(name) {
name = name || saved.value;
if (confirm("Are you sure you wish to delete '" + name + "'?")) {
code.value = "";
delete localStorage[name];
listSaves();
}
},
run: function() {
try {
var count = parseInt(iter.value, 10),
@@ -46,6 +110,12 @@ var Sandbox = (function() {
} catch(e) {
error.value = e.message;
}
},
save: function() {
var name = prompt("Please enter a name for this code:");
localStorage[name] = JSON.stringify(code.value);
listSaves(name);
}
}
})();

0 comments on commit 7f9a0e5

Please sign in to comment.