lib/public/js/parade-code-execution.js in parade-0.9.0 vs lib/public/js/parade-code-execution.js in parade-0.9.1
- old
+ new
@@ -1,18 +1,20 @@
window.CodeExecutor = Spine.Class.create({
init: function() {
+ this.visibleCodeBlocks = {};
+ this.executeCodeFor = {}
+
$.subscribe("code:execute",$.proxy(function(e,code) {
this.executeCode(code);
},this));
$.subscribe("code:execute:visible",$.proxy(function(e,code) {
this.executeVisibleCode();
},this));
},
- visibleCodeBlocks: function() {
- return { ruby : $('.execute .ruby pre:visible'),
- js : $('.execute .javascript pre:visible'),
- coffee : $('.execute .coffeescript pre:visible') }
+ registerVisibleCodeBlock: function(language,elements,execution) {
+ this.visibleCodeBlocks[language] = elements;
+ this.executeCodeFor[language] = execution;
},
executeVisibleCode: function() {
var codeBlocks = this.visibleCodeBlocks();
var supportedCodeBlockLanguages = Object.keys(codeBlocks);
@@ -30,39 +32,11 @@
var codeExecutor = this.executorForLanguage(code['lang']);
$.publish('code:execution:started');
codeExecutor(code['code']);
},
executorForLanguage: function(language) {
- return this.supportedLanguages()[language];
- },
- supportedLanguages: function() {
- return { ruby : this.executeRuby,
- js : this.executeJavaScript,
- coffee : this.executeCoffee };
- },
- executeJavaScript: function(code) {
- var result = eval(code);
- setTimeout(function() { $.publish('code:execution:finished'); }, 250 );
- if (result != null) { $.publish('code:execution:results',result); }
- },
- executeRuby: function(code) {
- $.get('/eval_ruby', { code: code }, function(result) {
- if (result != null) { $.publish('code:execution:results',result); }
- $.publish('code:execution:finished');
- });
- },
- executeCoffee: function(code) {
- // When Coffeescript completes it's work the final result is encapsulated
- // within it. To get around it, the result of the last evaluation needs to
- // be assigned to the window, which we can then use for the purposes of
- // displaying the results.
-
- var codeWithAssignmentToResults = code + ';window.result=result;'
- eval(CoffeeScript.compile(codeWithAssignmentToResults));
-
- setTimeout(function() { $.publish('code:execution:finished'); }, 250 );
- if (result != null) { $.publish('code:execution:results',result); }
+ return this.executeCodeFor[language];
}
});
window.CodeViewer = Spine.Class.create({
init: function() {
@@ -102,17 +76,31 @@
$(document).ready(function() {
codeExecutor = new CodeExecutor;
codeViewer = new CodeViewer;
+ codeExecutor.registerVisibleCodeBlock('js',$('.execute .javascript pre:visible'),function(code) {
+ var result = eval(code);
+ setTimeout(function() { $.publish('code:execution:finished'); }, 250 );
+ if (result != null) { $.publish('code:execution:results',result); }
+ });
+
$('.execute pre.javascript').live("click", function() {
var code = $(this).text();
$.publish('code:execute',{ lang: 'js', code: code, elem: $(this) });
});
- $('.execute pre.ruby').live("click", function() {
- var code = $(this).text();
- $.publish('code:execute',{ lang: 'ruby', code: code, elem: $(this) });
+ codeExecutor.registerVisibleCodeBlock('coffee',$('.execute .coffeescript pre:visible'), function(code) {
+ // When Coffeescript completes it's work the final result is encapsulated
+ // within it. To get around it, the result of the last evaluation needs to
+ // be assigned to the window, which we can then use for the purposes of
+ // displaying the results.
+
+ var codeWithAssignmentToResults = code + ';window.result=result;'
+ eval(CoffeeScript.compile(codeWithAssignmentToResults));
+
+ setTimeout(function() { $.publish('code:execution:finished'); }, 250 );
+ if (result != null) { $.publish('code:execution:results',result); }
});
$('.execute pre.coffeescript').live("click", function() {
var code = $(this).text();
$.publish('code:execute',{ lang: 'coffee', code: code, elem: $(this) });
\ No newline at end of file