Commits

Miki Tebeka committed 58e09a8

Works (w/o errors)

Comments (0)

Files changed (3)

+(ns dr.evil
+  (:use compojure.core)
+  (:use ring.util.response)
+  (:use [clojure.contrib.json :only (json-str)]))
+
+(defn input-page []
+  (slurp "src/index.html"))
+
+(defn eval-expr [expr]
+  (try
+    (let [result (load-string (str "(in-ns 'dr.evil) " expr))]
+      { :result (with-out-str (println result)) :error false })
+    (catch Exception e
+      (let [result (with-out-str (.printStackTrace e))]
+        { :result result :error true }))))
+
+(defn evil [params]
+  (json-str (eval-expr (params "expr"))))
+
+(defroutes app
+  (GET "/" [] (input-page))
+  (POST "/" {params :params} (evil params))
+  (ANY "/*" [path] (redirect "/")))

src/dr_evil.clj

-(ns dr.evil
-  (:use [clojure.contrib.json :only (json-str)]))
-
-(defn input-page []
-  (slurp "src/index.html"))
-
-(defn eval-expr [expr]
-  (try
-    (let [result (load-string (str "(in-ns 'dr.evil) " expr))]
-      { :result (with-out-str (println result)) :error false })
-    (catch Exception e
-      (let [result (with-out-str (.printStackTrace e))]
-        { :result result :error true }))))
-
-(defn evil [params]
-  (json-str (eval-expr (params "expr"))))
-
-(defroutes app
-  (GET "/" [] (input-page))
-  (POST "/" {params :params} (evil params))
-  (ANY "/*" [path] (redirect "/"))
         src="http://flesler-plugins.googlecode.com/files/jquery.scrollTo-1.4.2-min.js">
     </script>
     <script>
+        function append_log(div) {
+            $('#log').append(div);
+            $('#log').scrollTo(div);
+        }
+
+        function on_result(data) {
+            var div = $('<div/>');
+            if (data.error) {
+                div.attr('class', 'error');
+                if (data.result.length == 0) {
+                    data.result = "Unknown Error!";
+                }
+            }
+            div.text(data.result);
+            append_log(div);
+        }
+
         function on_run() {
-            var value = $.trim($('#entry').val());
-            if (value.length == 0) {
+            var expr = $.trim($('#entry').val());
+            if (expr.length == 0) {
                 return;
             }
-            var div = $('<div/>').text('=> ' + value);
-            $('#log').append(div);
-            $('#log').scrollTo(div);
+            var div = $('<div/>').text('=> ' + expr);
+            append_log(div);
+            $.ajax({
+                type: 'POST',
+                url: '/',
+                data: { "expr" : expr },
+                success: on_result,
+                dataType: 'json'
+            });
         }
 
         function hook_enter_key(elem, handler) {