/* globals CodeMirror, challenge_Name, challengeType */ // codeStorage var codeStorageFactory = (function($, localStorage) { var CodeStorageProps = { version: 0.01, keyVersion: 'saveVersion', keyValue: null, updateWait: 2000, updateTimeoutId: null }; var CodeStorage = { hasSaved: function() { return this.updateTimeoutId === null; }, onSave: function(func) { this.eventArray.push(func); }, setSaveKey: function(key) { this.keyValue = key + 'Val'; }, getStoredValue: function() { return '' + localStorage.getItem(this.keyValue); }, setEditor: function(editor) { this.editor = editor; }, isAlive: function() { var val = this.getStoredValue(); return val !== 'null' && val !== 'undefined' && (val && val.length > 0); }, updateStorage: function() { if (typeof localStorage !== 'undefined') { var value = this.editor.getValue(); localStorage.setItem(this.keyValue, value); } else { console.log('no web storage'); } this.updateTimeoutId = null; } }; function codeStorageFactory(editor, challengeName) { var codeStorage = Object.create(CodeStorage); $.extend(codeStorage, CodeStorageProps); codeStorage.setEditor(editor); codeStorage.setSaveKey(challengeName); return codeStorage; } var savedVersion = localStorage.getItem(CodeStorageProps.keyVersion); if (savedVersion === null) { localStorage.setItem( CodeStorageProps.keyVersion, CodeStorageProps.version ); } return codeStorageFactory; }($, localStorage)); function replaceSafeTags(value) { return value .replace(/fccss/gi, ''); } 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 codeStorage = codeStorageFactory(editor, challenge_Name); var myCodeMirror = editor; editor.on('keyup', function() { clearTimeout(codeStorage.updateTimeoutId); codeStorage.updateTimeoutId = setTimeout( codeStorage.updateStorage, codeStorage.updateWait ); }); var editorValue; var challengeSeed = challengeSeed || null; var tests = tests || []; var allSeeds = ''; (function() { challengeSeed.forEach(function(elem) { allSeeds += elem + '\n'; }); })(); 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'); var libraryIncludes = "" + "" + "" + "" + "" + "" + "" + "" + ""; var editorValueForIFrame; var iFrameScript = ""; var delay; // Initialize CodeMirror editor with a nice html5 canvas demo. 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') +
'