Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use localstorage to remember Web REPL history #705

Merged
merged 9 commits into from Apr 12, 2012
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -201,6 +201,9 @@ Use the launch-webserver script to start the webserver and web-repl.
Point your browser to `http://localhost:2000/`.
Try `plot(cumsum(randn(1000)))`

### Run it Online
[julia.forio.com](http://julia.forio.com)

### Pre-installed lighttpd

If you want to use your own `lighttpd`, then the process is something like this:
Expand Down
2 changes: 2 additions & 0 deletions ui/website/index.htm
Expand Up @@ -7,6 +7,8 @@
<link href="styles.css" rel="stylesheet" type="text/css" />
<link href="favicon.ico" rel="shortcut icon" />
<link href="http://fonts.googleapis.com/css?family=Droid+Sans+Mono&v2" rel="stylesheet" type="text/css">
<script type="text/javascript" src="localstorage.js"></script>
<script type="text/javascript" src="modernizr-2.5.3.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.json.js"></script>
<script type="text/javascript" src="jquery.autoresize.js"></script>
Expand Down
96 changes: 96 additions & 0 deletions ui/website/localstorage.js
@@ -0,0 +1,96 @@
if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined') (function () {

var Storage = function (type) {
function createCookie(name, value, days) {
var date, expires;

if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
i, c;

for (i=0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}

if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}

function setData(data) {
data = JSON.stringify(data);
if (type == 'session') {
window.name = data;
} else {
createCookie('localStorage', data, 365);
}
}

function clearData() {
if (type == 'session') {
window.name = '';
} else {
createCookie('localStorage', '', 365);
}
}

function getData() {
var data = type == 'session' ? window.name : readCookie('localStorage');
return data ? JSON.parse(data) : {};
}


// initialise if there's already data
var data = getData();

return {
length: 0,
clear: function () {
data = {};
this.length = 0;
clearData();
},
getItem: function (key) {
return data[key] === undefined ? null : data[key];
},
key: function (i) {
// not perfect, but works
var ctr = 0;
for (var k in data) {
if (ctr == i) return k;
else ctr++;
}
return null;
},
removeItem: function (key) {
delete data[key];
this.length--;
setData(data);
},
setItem: function (key, value) {
data[key] = value+''; // forces the value to a string
this.length++;
setData(data);
}
};
};

if (typeof window.localStorage == 'undefined') window.localStorage = new Storage('local');
if (typeof window.sessionStorage == 'undefined') window.sessionStorage = new Storage('session');

})();