diff --git a/client/commonFramework.js b/client/commonFramework.js
index 6e5bb4f2cf..3a8a0c808a 100644
--- a/client/commonFramework.js
+++ b/client/commonFramework.js
@@ -1,6 +1,23 @@
-/* globals jailed, CodeMirror, challenge_Id, challenge_Name, challengeType */
+// common namespace
+// all classes should be stored here
+var common = common || {
+ // init is an array of functions that are
+ // called at the beginning of dom ready
+ init: []
+};
+
+common.challengeName = common.challengeName || window.challenge_Name ?
+ window.challenge_Name :
+ '';
+
+common.challengeType = common.challengeType || window.challengeType ?
+ window.challengeType :
+ 0;
+
+common.challengeId = common.challengeId || window.challenge_Id;
+
// codeStorage
-var codeStorageFactory = (function($, localStorage) {
+common.codeStorageFactory = (function($, localStorage) {
var CodeStorageProps = {
version: 0.01,
@@ -68,8 +85,45 @@ var codeStorageFactory = (function($, localStorage) {
return codeStorageFactory;
}($, localStorage));
-var sandBox = (function() {
+common.codeOutput = (function(CodeMirror, document, challengeType) {
+ if (!CodeMirror) {
+ return {};
+ }
+ if (
+ challengeType === '0' ||
+ challengeType === '7'
+ ) {
+ return {};
+ }
+ var codeOutput = CodeMirror.fromTextArea(
+ document.getElementById('codeOutput'),
+ {
+ lineNumbers: false,
+ mode: 'text',
+ theme: 'monokai',
+ readOnly: 'nocursor',
+ lineWrapping: true
+ }
+ );
+ codeOutput.setValue(
+ '/**\n' +
+ ' * Your output will go here.\n' +
+ ' * Console.log() -type statements\n' +
+ ' * will appear in your browser\'s\n' +
+ ' * DevTools JavaScript console.\n' +
+ ' */'
+ );
+
+ codeOutput.setSize('100%', '100%');
+
+ return codeOutput;
+}(window.CodeMirror, window.document, common.challengeType || 0));
+
+var sandBox = (function(jailed, codeOutput) {
+ if (!jailed) {
+ return {};
+ }
var plugin = null;
var sandBox = {
@@ -150,7 +204,7 @@ var sandBox = (function() {
endLoading();
console.log('resetting on fatal plugin error');
- if (challengeType === 0) {
+ if (common.challengeType === 0) {
codeOutput.setValue(
'Sorry, your code is either too slow, has a fatal error, ' +
'or contains an infinite loop.'
@@ -163,7 +217,7 @@ var sandBox = (function() {
reset();
sandBox.submit = submit;
return sandBox;
-}());
+}(window.jailed, common.codeOutput));
function replaceSafeTags(value) {
return value
@@ -180,33 +234,105 @@ var BDDregex = new RegExp(
var isInitRun = false;
var initPreview = true;
-var editor;
-editor = CodeMirror.fromTextArea(document.getElementById('codeEditor'), {
- lineNumbers: true,
- mode: 'text',
- theme: 'monokai',
- runnable: true,
- matchBrackets: true,
- autoCloseBrackets: true,
- scrollbarStyle: 'null',
- lineWrapping: true,
- gutters: ['CodeMirror-lint-markers']
-});
+var editor = (function(CodeMirror, emmetCodeMirror, common) {
+ var codeStorageFactory = common.codeStorageFactory;
+ if (!CodeMirror) {
+ return {};
+ }
-var codeStorage = codeStorageFactory(editor, challenge_Name);
-var myCodeMirror = editor;
+ var editor = CodeMirror.fromTextArea(document.getElementById('codeEditor'), {
+ lineNumbers: true,
+ mode: 'text',
+ theme: 'monokai',
+ runnable: true,
+ matchBrackets: true,
+ autoCloseBrackets: true,
+ scrollbarStyle: 'null',
+ lineWrapping: true,
+ gutters: ['CodeMirror-lint-markers']
+ });
+
+ editor.setSize('100%', 'auto');
+
+ var codeStorage = common.codeStorage =
+ codeStorageFactory(editor, common.challengeName);
+
+ editor.on('keyup', function() {
+ clearTimeout(codeStorage.updateTimeoutId);
+ codeStorage.updateTimeoutId = setTimeout(
+ codeStorage.updateStorage.bind(codeStorage),
+ codeStorage.updateWait
+ );
+ });
+
+ // Initialize CodeMirror editor with a nice html5 canvas demo.
+ editor.on('keyup', function() {
+ clearTimeout(delay);
+ delay = setTimeout(updatePreview, 300);
+ });
+
+ editor.setOption('extraKeys', {
+ Tab: function(cm) {
+ if (cm.somethingSelected()) {
+ cm.indentSelection('add');
+ } else {
+ var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
+ cm.replaceSelection(spaces);
+ }
+ },
+ 'Shift-Tab': function(cm) {
+ if (cm.somethingSelected()) {
+ cm.indentSelection('subtract');
+ } else {
+ var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
+ cm.replaceSelection(spaces);
+ }
+ },
+ 'Ctrl-Enter': function() {
+ isInitRun = false;
+ bonfireExecute(true);
+ return false;
+ }
+ });
+
+
+ var info = editor.getScrollInfo();
+
+ var after = editor.charCoords({
+ line: editor.getCursor().line + 1,
+ ch: 0
+ }, 'local').top;
+
+ if (info.top + info.clientHeight < after) {
+ editor.scrollTo(null, after - info.clientHeight + 3);
+ }
+
+ if (emmetCodeMirror) {
+ emmetCodeMirror(
+ editor,
+ {
+ 'Cmd-E': 'emmet.expand_abbreviation',
+ Tab: 'emmet.expand_abbreviation_with_tab',
+ Enter: 'emmet.insert_formatted_line_break_only'
+ }
+ );
+ }
+ common.init.push(function() {
+ editorValue = codeStorage.isAlive() ?
+ codeStorage.getStoredValue() :
+ allSeeds;
+
+ editor.setValue(replaceSafeTags(editorValue));
+ editor.refresh();
+ });
+
+ return editor;
+}(window.CodeMirror, window.emmetCodeMirror, common));
-editor.on('keyup', function() {
- clearTimeout(codeStorage.updateTimeoutId);
- codeStorage.updateTimeoutId = setTimeout(
- codeStorage.updateStorage.bind(codeStorage),
- codeStorage.updateWait
- );
-});
var editorValue;
-var challengeSeed = challengeSeed || null;
+var challengeSeed = challengeSeed || [];
var tests = tests || [];
var allSeeds = '';
@@ -216,42 +342,6 @@ var allSeeds = '';
});
})();
-if (typeof emmetCodeMirror !== 'undefined') {
- var defaultKeymap = {
- 'Cmd-E': 'emmet.expand_abbreviation',
- 'Tab': 'emmet.expand_abbreviation_with_tab',
- 'Enter': 'emmet.insert_formatted_line_break_only'
- };
-
- emmetCodeMirror(editor, defaultKeymap);
-}
-
-editor.setOption('extraKeys', {
- Tab: function(cm) {
- if (cm.somethingSelected()) {
- cm.indentSelection('add');
- } else {
- var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
- cm.replaceSelection(spaces);
- }
- },
- 'Shift-Tab': function(cm) {
- if (cm.somethingSelected()) {
- cm.indentSelection('subtract');
- } else {
- var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
- cm.replaceSelection(spaces);
- }
- },
- 'Ctrl-Enter': function() {
- isInitRun = false;
- bonfireExecute(true);
- return false;
- }
-});
-
-editor.setSize('100%', 'auto');
-
var libraryIncludes = "" +
"" +
"" +
@@ -294,7 +384,8 @@ function scopejQuery(str) {
}
function safeHTMLRun(test) {
- if (challengeType === '0') {
+ var codeStorage = common.codeStorage;
+ if (common.challengeType === '0') {
var previewFrame = document.getElementById('preview');
var preview = previewFrame.contentDocument || previewFrame.contentWindow.document;
if (editor.getValue().match(/\