Skip to content

Commit

Permalink
Merge pull request #147 from sverweij/feature/local-storage
Browse files Browse the repository at this point in the history
Feature/local storage
  • Loading branch information
sverweij committed May 25, 2015
2 parents 29a1770 + f4266bd commit 4cc81ed
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 50 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ SCRIPT_SOURCES_WEB=$(SCRIPT_SOURCES_NODE) \
src/script/ui-control/controller-animator.js \
src/script/ui-control/controller-exporter.js \
src/script/ui-control/controller-raster-exporter.js \
src/script/ui-control/store.js \
src/script/utl/gaga.js \
src/script/utl/domquery.js \
src/script/mscgen-interpreter.js
Expand Down Expand Up @@ -171,7 +172,8 @@ $(PRODDIRS):

# file targets dev

src/style/interp.scss: src/lib/codemirror/_codemirror.scss \
src/style/interp.css: src/style/interp.scss \
src/lib/codemirror/_codemirror.scss \
src/lib/codemirror/theme/_midnight.scss \
src/style/snippets/_interpreter.scss \
src/style/snippets/_anim.scss \
Expand All @@ -185,7 +187,8 @@ src/style/interp.scss: src/lib/codemirror/_codemirror.scss \
src/fonts/controls.ttf \
src/fonts/controls.woff

src/style/doc.scss: src/style/snippets/_header.scss \
src/style/doc.css: src/style/doc.scss \
src/style/snippets/_header.scss \
src/style/snippets/_documentation.scss \
src/style/snippets/_generics.scss \
src/style/snippets/_popup.scss \
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.76
0.9.78
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mscgen_js",
"version": "0.9.76",
"version": "0.9.78",
"description": "Turns text into sequence charts. A faithfull implementation of the mscgen sequence chart language in javascript.",
"dependencies": {
"amdefine": "^0.1.0",
Expand All @@ -9,15 +9,16 @@
"requirejs": "^2.1.17"
},
"devDependencies": {
"pegjs": "^0.8.0",
"node-sass": "^3.1.2",
"mocha": "^2.2.5",
"codeclimate-test-reporter": "^0.0.4",
"jshint": "^2.7.0",
"csslint": "^0.10.0",
"istanbul": "^0.3.14",
"plato": "^1.5.0",
"nsp": "^1.0.1"
"jshint": "^2.7.0",
"mocha": "^2.2.5",
"node-localstorage": "^0.5.0",
"node-sass": "^3.1.2",
"nsp": "^1.0.1",
"pegjs": "^0.8.0",
"plato": "^1.5.0"
},
"scripts": {
"test": "mocha -R dot src/script/test/",
Expand Down
29 changes: 17 additions & 12 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@
<article>
<section id="__input_and_controls">
<div>
<h3 class="fadable-hint"><span class="intronumber">1</span> <span class="hideonmobile">your</span> text <span class="hideonmobile">goes here </span> &#x0261f;</h3>
<h3 class="fadable-hint"><span class="intronumber">1</span> <span class="hideontablet">your</span> text <span class="hideontablet">goes here</span> &#x0261f;</h3>
<span class="debug hideonmobile" style="display: none">
<a id="__save" class="button" title="save chart
saves the current chart to local storage"><span class="icon-arrow-down"></span></a>
<a id="__load" class="button" title="load chart
loads the most recently saved chart from local storage"><span class="icon-folder"></span></a>
</span>
<div class="herpes">
<select title="list of samples" id="__samples">
<option value="none" selected>select an example...</option>
Expand Down Expand Up @@ -194,8 +201,6 @@ <h3 class="fadable-hint"><span class="intronumber">1</span> <span class="hideonm
<button id="__btn_uncolorize" title="Removes all color from the sequence chart
Note: !! removes comments and reformats your source!!">b&amp;w</button>
<span class="debug" style="display: none">
</span>
<button id="__btn_render" class="btn-primary"
title="Click to render. Only shows up when 'autorender' is off">
render
Expand All @@ -205,7 +210,7 @@ <h3 class="fadable-hint"><span class="intronumber">1</span> <span class="hideonm

<section style="display:inline-block;">
<h3 class="fadable-hint"
style="padding-right: 1em;"><span class="intronumber">2</span> <span class="hideonmobile">your</span> chart <span class="hideonmobile">shows up here</span> &#x0261f;</h3>
style="padding-right: 1em;"><span class="intronumber">2</span> <span class="hideontablet">your</span> chart <span class="hideontablet">shows up here</span> &#x0261f;</h3>
<span id="__output_buttons">
<a id="__show_svg"
class="button"
Expand All @@ -229,11 +234,6 @@ <h3 class="fadable-hint"><span class="intronumber">1</span> <span class="hideonm
title="Shows the chart in a separate window (jpeg).
Note: the url bar will contain the base64 encoded version of the jpeg">jpg</a>
<a id="__show_dot"
class="button"
target="_blank"
type="text/dot"
title="Shows the communications diagram variant of the chart (graphviz dot)">dot</a>
<a id="__show_anim"
class="button hideonmobile"
target="_blank"
Expand All @@ -242,18 +242,23 @@ <h3 class="fadable-hint"><span class="intronumber">1</span> <span class="hideonm
- works for classic mscgen compatible charts.
- support for inline expressions follows in a next release."><span class="icon-video-camera"></span></a> <!-- style="display: none" -->
<a id="__show_dot"
class="button hideontablet"
target="_blank"
type="text/dot"
title="Shows the communications diagram variant of the chart (graphviz dot)">dot</a>
<a id="__show_html"
class="debug button" style="display: none"
class="debug button hideontablet" style="display: none"
target="_blank"
type="text/plain"
title="Generates an html page with the chart embedded"><span class="icon-embed"></span></a>
<a id="__show_vanilla"
class="debug button" style="display: none"
class="debug button hideontablet" style="display: none"
target="_blank"
type="text/plain"
title="exports to vanilla mscgen"><span class="icon-arrow-down"></span></a>
<a id="__show_url"
class="debug button" style="display: none"
class="debug button hideontablet" style="display: none"
target="_blank"
type="text/plain"
title="create bookmarkable url
Expand Down
2 changes: 1 addition & 1 deletion src/script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The controller for the interpreter UI is less trivial.
## Testing
:page_with_curl: code in [test/](test)

About 200 automated tests (and counting) make sure refactoring most of
About 220 automated tests (and counting) make sure refactoring most of
the back end code (parsing and rendering) can be done safely.

The ui controller tests are inherently harder to test automated. This
Expand Down
2 changes: 2 additions & 0 deletions src/script/mscgen-interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require(["ui-control/interpreter-input-actions",
window.__btn_uncolorize.addEventListener("click", iactions.uncolorizeOnClick, false);
window.__btn_render.addEventListener("click", iactions.renderOnClick, false);
window.__samples.addEventListener("change", iactions.samplesOnChange, false);
window.__save.addEventListener("click", iactions.saveOnClick, false);
window.__load.addEventListener("click", iactions.loadOnClick, false);
}

function setupOutputEvents(){
Expand Down
69 changes: 69 additions & 0 deletions src/script/test/t_store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* global localStorage */
var store = require("../ui-control/store");
var assert = require("assert");

describe('store', function() {
var lLanguage = "initial language";
var lSource = "initial source";
var lAutoRender = false;
var lDebug = false;

var lState = {
getLanguage: function(){return "other language";},
setLanguage: function(pLanguage){lLanguage = pLanguage;},
getSource: function(){return "other source";},
setSource: function(pSource){lSource = pSource;},
getAutoRender: function(){return true;},
setAutoRender: function(pAutoRender){lAutoRender = pAutoRender;},
getDebug: function(){return true;},
setDebug: function(pDebug){lDebug = pDebug;}
};

describe('#load and save with no localStorage available', function() {
it('silently fail on load', function(){
store.load(lState);
assert.equal(lLanguage, "initial language");
assert.equal(lSource, "initial source");
assert.equal(lAutoRender, false);
assert.equal(lDebug, false);
});

it('silently fail on save', function(){
store.save(lState);
store.load(lState);
assert.equal(lLanguage, "initial language");
assert.equal(lSource, "initial source");
assert.equal(lAutoRender, false);
assert.equal(lDebug, false);
});
});

describe('#load and save', function() {
before(function(){
global.localStorage = new require('node-localstorage').LocalStorage('./throwmeaway');
localStorage.clear();
});

after(function(){
localStorage._deleteLocation();
});

it('leaves the state as is when localStorage has no item', function() {
store.load(lState);
assert.equal(lLanguage, "initial language");
assert.equal(lSource, "initial source");
assert.equal(lAutoRender, false);
assert.equal(lDebug, false);
});

it('saves the passed state', function(){
store.save(lState);
store.load(lState);
assert.equal(lLanguage, "other language");
assert.equal(lSource, "other source");
assert.equal(lAutoRender, true);
assert.equal(lDebug, true);
});

});
});
11 changes: 10 additions & 1 deletion src/script/ui-control/interpreter-input-actions.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* jshint browser:true */
/* global define */
define(["./interpreter-uistate",
"./store",
"../utl/gaga"
],
function(
uistate,
store,
gaga) {
"use strict";


return {
autorenderOnClick: function() {
uistate.setAutoRender(!(uistate.getAutoRender()));
Expand Down Expand Up @@ -45,6 +46,14 @@ define(["./interpreter-uistate",
samplesOnChange: function () {
uistate.setSample(window.__samples.value);
gaga.g('send', 'event', 'selectexample', window.__samples.value);
},
saveOnClick: function() {
store.save(uistate);
gaga.g('send', 'event', 'save', 'button');
},
loadOnClick: function(){
store.load(uistate);
gaga.g('send', 'event', 'load', 'button');
}
};
});
Expand Down
34 changes: 11 additions & 23 deletions src/script/ui-control/interpreter-uistate.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,40 +202,27 @@ function renderSource(pAST, pLanguage){
return lTargetSource;
}

function getSource(){
return gCodeMirror.getValue();
}

function setSource(pSource){
gCodeMirror.setValue(pSource);
}

function getSource(){
return gCodeMirror.getValue();
}

function setCursorInSource(pLine, pColumn){
gCodeMirror.setCursor(pLine, pColumn);
gCodeMirror.focus();
}

function setLanguage (pLanguage){
gLanguage = pLanguage;
gCodeMirror.setOption("mode", txt.language2Mode(pLanguage));
showLanguageState(getSource(), pLanguage, gAutoRender);
}

function getLanguage(){
return gLanguage;
}

function getAutoRender(){
return gAutoRender;
}

function setDebug(pBoolean){
gDebug = pBoolean;
}

function setAutoRender(pBoolean){
gAutoRender = pBoolean;
function setLanguage (pLanguage){
gLanguage = pLanguage;
gCodeMirror.setOption("mode", txt.language2Mode(pLanguage));
showLanguageState(getSource(), pLanguage, gAutoRender);
}

function setSample(pURL) {
Expand Down Expand Up @@ -371,13 +358,14 @@ function displayError (pError, pContext) {
errorOnClick: errorOnClick,

render: render,
getAutoRender: getAutoRender,
setAutoRender: setAutoRender,
getAutoRender: function(){return gAutoRender;},
setAutoRender: function(pBoolean){gAutoRender = pBoolean;},
getSource: getSource,
setSource: setSource,
getLanguage: getLanguage,
setLanguage: setLanguage,
setDebug: setDebug,
getDebug: function(){return gDebug;},
setDebug: function(pBoolean){gDebug = pBoolean;},
getAST: getAST,

showAutorenderState: showAutorenderState
Expand Down
72 changes: 72 additions & 0 deletions src/script/ui-control/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* jshint browser:true */
/* jshint node:true */

/* istanbul ignore else */
if ( typeof define !== 'function') {
var define = require('amdefine')(module);
}

define([],function() {
"use strict";

var STORAGE_KEY = "state";

function localStorageOK (){
return (typeof localStorage !== 'undefined');
}

function getState(){
if (localStorageOK()){
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY));
} catch (e){
// silently swallow
}
}
return null;
}

function save(pState){
if (localStorageOK()){
localStorage.setItem(STORAGE_KEY,
JSON.stringify({
language: pState.getLanguage(),
source: pState.getSource(),
autorender: pState.getAutoRender(),
debug: pState.getDebug()
})
);
}
}

function load(pState){
var lState = getState();
if (lState){
pState.setLanguage(lState.language);
pState.setSource(lState.source);
pState.setAutoRender(lState.autorender);
pState.setDebug(lState.debug);
}
}

return {
save: save,
load: load
};
});
/*
This file is part of mscgen_js.
mscgen_js is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mscgen_js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mscgen_js. If not, see <http://www.gnu.org/licenses/>.
*/
Loading

0 comments on commit 4cc81ed

Please sign in to comment.