diff --git a/client/commonFramework.js b/client/commonFramework.js index ab0fe149337..88793bf3a59 100644 --- a/client/commonFramework.js +++ b/client/commonFramework.js @@ -1,4 +1,4 @@ -/* globals CodeMirror, challenge_Name */ +/* globals CodeMirror, challenge_Name, challengeType */ // codeStorage var codeStorageFactory = (function($, localStorage) { @@ -68,6 +68,12 @@ var codeStorageFactory = (function($, localStorage) { return codeStorageFactory; }($, localStorage)); +function replaceSafeTags(value) { + return value + .replace(/fccss/gi, ''); +} + var isInitRun = false; var initPreview = true; var editor; @@ -106,30 +112,30 @@ var allSeeds = ''; }); })(); -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() { - bonfireExecute(true); - return false; +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() { + bonfireExecute(true); + return false; + } }); -editor.setSize("100%", "auto"); +editor.setSize('100%', 'auto'); var libraryIncludes = "" + "" + @@ -141,39 +147,44 @@ var libraryIncludes = ""; - var delay; + // Initialize CodeMirror editor with a nice html5 canvas demo. -editor.on("keyup", function () { - clearTimeout(delay); - delay = setTimeout(updatePreview, 300); +editor.on('keyup', function() { + clearTimeout(delay); + delay = setTimeout(updatePreview, 300); }); -function workerError(error){ - var display = $('.runTimeError'); - var housing = $('#testSuite'); - if(display.html() != error){ - display.remove(); - housing.prepend("
" + error.replace(/j\$/gi, "$").replace(/jdocument/gi, "document").replace(/jjQuery/gi, "jQuery") + "
"); - display.hide().fadeIn(function(){ - setTimeout(function(){ - display.fadeOut(function(){ - display.remove(); - }); - }, 1000) +function workerError(error) { + var display = $('.runTimeError'); + var housing = $('#testSuite'); + if (display.html() !== error) { + display.remove(); + housing.prepend( + '
' + + error.replace(/j\$/gi, '$').replace(/jdocument/gi, 'document').replace(/jjQuery/gi, 'jQuery') + + '
' + ); + display.hide().fadeIn(function() { + setTimeout(function() { + display.fadeOut(function() { + display.remove(); }); - } + }, 1000); + }); + } } -function scopejQuery(s){ - return(s.replace(/\$/gi, "j$").replace(/document/gi, "jdocument").replace(/jQuery/gi, "jjQuery")); +function scopejQuery(str) { + return str + .replace(/\$/gi, 'j$') + .replace(/document/gi, 'jdocument') + .replace(/jQuery/gi, 'jjQuery'); } -function safeHTMLRun(test){ +function safeHTMLRun(test) { if(challengeType === "0"){ var previewFrame = document.getElementById('preview'); var preview = previewFrame.contentDocument || previewFrame.contentWindow.document; @@ -328,32 +339,36 @@ function showCompletion() { } var resetEditor = function resetEditor() { - editor.setValue(allSeeds.replace((/fccss/gi), '')); - $('#testSuite').empty(); - bonfireExecute(true); - codeStorage.updateStorage(); + editor.setValue(replaceSafeTags(allSeeds)); + $('#testSuite').empty(); + bonfireExecute(true); + codeStorage.updateStorage(); }; var attempts = 0; if (attempts) { - attempts = 0; + attempts = 0; } -if(challengeType !== "0") { - var codeOutput = CodeMirror.fromTextArea(document.getElementById("codeOutput"), { - lineNumbers: false, - mode: "text", - theme: 'monokai', - readOnly: 'nocursor', - lineWrapping: true - }); +if (challengeType !== '0') { + 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%"); + 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%"); } + var info = editor.getScrollInfo(); var after = editor.charCoords({ line: editor.getCursor().line + 1, @@ -584,21 +599,22 @@ $('#submitButton').on('click', function() { }); $(document).ready(function() { - var $preview = $('#preview'); - isInitRun = true; + var $preview = $('#preview'); + isInitRun = true; - editorValue = codeStorage.isAlive() ? - codeStorage.getStoredValue() : - allSeeds; + editorValue = codeStorage.isAlive() ? + codeStorage.getStoredValue() : + allSeeds; - myCodeMirror.setValue(editorValue.replace(/fccss/gi, '")); - if (typeof $preview.html() !== 'undefined') { - $preview.load(function(){ - if (initPreview) { - bonfireExecute(true); - } - }); - } else{ + myCodeMirror.setValue(replaceSafeTags(editorValue)); + + if (typeof $preview.html() !== 'undefined') { + $preview.load(function() { + if (initPreview) { bonfireExecute(true); - } + } + }); + } else { + bonfireExecute(true); + } });