Skip to content

Commit

Permalink
add uri code storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Oct 1, 2015
1 parent 5de6837 commit 72e0595
Showing 1 changed file with 108 additions and 18 deletions.
126 changes: 108 additions & 18 deletions client/commonFramework.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,92 @@ common.challengeType = common.challengeType || window.challengeType ?

common.challengeId = common.challengeId || window.challenge_Id;

common.challengeSeed = common.challengeSeed || window.challengeSeed ?
window.challengeSeed :
[];

common.seed = common.challengeSeed.reduce(function(seed, line) {
return seed + line + '\n';
}, '');

// store code in the URL
common.codeUri = (function(common, encode, decode, location, history) {
var codeUri = {
encode: function(code) {
return encode(code);
},
decode: function(code) {
try {
return decode(code);
} catch (ignore) {
return null;
}
},
isInQuery: function(query) {
var decoded = codeUri.decode(query);
if (!decoded || typeof decoded.split !== 'function') {
return false;
}
return decoded
.split('?')
.splice(1)
.reduce(function(found, param) {
var key = param.split('=')[0];
if (key === 'solution') {
return true;
}
return found;
}, false);
},
isAlive: function() {
return codeUri.isInQuery(location.search) ||
codeUri.isInQuery(location.hash);
},
parse: function() {
var query;
if (location.search && codeUri.isInQuery(location.search)) {
query = location.search.replace(/^\?/, '');
if (history && typeof history.replaceState === 'function') {
history.replaceState(
history.state,
null,
location.href.split('?')[0]
);
location.hash = '#?' + query;
}
} else {
query = location.hash.replace(/^\#\?/, '');
}
if (!query) {
return null;
}

return query
.split('&')
.reduce(function(solution, param) {
var key = param.split('=')[0];
var value = param.split('=')[1];
if (key === 'solution') {
return codeUri.decode(value);
}
return solution;
}, null);
},
querify: function(solution) {
location.hash = '?solution=' + codeUri.encode(solution);
return solution;
}
};

common.init.push(function() {
codeUri.parse();
});

return codeUri;
}(common, encodeURIComponent, decodeURIComponent, location, history));

// codeStorage
common.codeStorageFactory = (function($, localStorage) {
common.codeStorageFactory = (function($, localStorage, codeUri) {

var CodeStorageProps = {
version: 0.01,
Expand Down Expand Up @@ -58,7 +142,10 @@ common.codeStorageFactory = (function($, localStorage) {
updateStorage: function() {
if (typeof localStorage !== 'undefined') {
var value = this.editor.getValue();
// store in localStorage
localStorage.setItem(this.keyValue, value);
// also store code in URL
codeUri.querify(value);
} else {
console.log('no web storage');
}
Expand All @@ -83,7 +170,7 @@ common.codeStorageFactory = (function($, localStorage) {
}

return codeStorageFactory;
}($, localStorage));
}($, localStorage, common.codeUri));

common.codeOutput = (function(CodeMirror, document, challengeType) {
if (!CodeMirror) {
Expand Down Expand Up @@ -319,9 +406,15 @@ var editor = (function(CodeMirror, emmetCodeMirror, common) {
);
}
common.init.push(function() {
editorValue = codeStorage.isAlive() ?
codeStorage.getStoredValue() :
allSeeds;
var editorValue;
if (common.codeUri.isAlive()) {
console.log('in query');
editorValue = common.codeUri.parse();
} else {
editorValue = codeStorage.isAlive() ?
codeStorage.getStoredValue() :
common.seed;
}

editor.setValue(replaceSafeTags(editorValue));
editor.refresh();
Expand All @@ -331,16 +424,7 @@ var editor = (function(CodeMirror, emmetCodeMirror, common) {
}(window.CodeMirror, window.emmetCodeMirror, common));


var editorValue;
var challengeSeed = challengeSeed || [];
var tests = tests || [];
var allSeeds = '';

(function() {
challengeSeed.forEach(function(elem) {
allSeeds += elem + '\n';
});
})();

var libraryIncludes = "<script src='//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>" +
"<script src='/js/lib/chai/chai.js'></script>" +
Expand Down Expand Up @@ -547,7 +631,9 @@ function showCompletion() {
.delay(1000)
.queue(function(next) {
$(this).replaceWith(
'<div id="challenge-spinner" class="animated zoomInUp inner-circles-loader">submitting...</div>'
'<div id="challenge-spinner" ' +
'class="animated zoomInUp inner-circles-loader">' +
'submitting...</div>'
);
next();
});
Expand All @@ -573,7 +659,7 @@ function showCompletion() {
}

var resetEditor = function resetEditor() {
editor.setValue(replaceSafeTags(allSeeds));
editor.setValue(replaceSafeTags(common.seed));
$('#testSuite').empty();
bonfireExecute(true);
common.codeStorage.updateStorage();
Expand All @@ -588,7 +674,6 @@ if (attempts) {
var userTests;
var testSalt = Math.random();


var scrapeTests = function(userJavaScript) {

// insert tests from mongo
Expand Down Expand Up @@ -638,7 +723,11 @@ var createTestDisplay = function() {
}
for (var i = 0; i < userTests.length; i++) {
var didTestPass = !userTests[i].err;
var testText = userTests[i].text.split('message: ').pop().replace(/\'\);/g, '');
var testText = userTests[i].text
.split('message: ')
.pop()
.replace(/\'\);/g, '');

var testDoc = document.createElement('div');

var iconClass = didTestPass ?
Expand Down Expand Up @@ -674,6 +763,7 @@ var reassembleTest = function(test, data) {
};

var runTests = function(err, data) {
var editorValue = editor.getValue();
// userTests = userTests ? null : [];
var allTestsPassed = true;
pushed = false;
Expand Down

0 comments on commit 72e0595

Please sign in to comment.