Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ClojureScript! #22

Closed
wants to merge 1 commit into from

2 participants

@jsab

Hi Raynes,

I translated the javascript code to cljs. It is a mostly literal translation. In the future we can look at how we can make it more idiomatic cljs. Let me know what you think.

Jean-Sebastien

@Raynes Raynes closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 27, 2012
  1. @jsab
This page is out of date. Refresh to see the latest.
View
2  .gitignore
@@ -3,3 +3,5 @@ pom.xml
lib
classes
*~
+/.lein*
+/resources/public/javascript/tryclojure.js
View
9 project.clj
@@ -2,9 +2,18 @@
:description "A simple web-based Clojure REPL for trying out Clojure without having to install it."
:dependencies [[org.clojure/clojure "1.4.0"]
[noir "1.3.0-beta3"]
+ [jayq "0.1.0-alpha4"]
[commons-lang/commons-lang "2.5"]
[clojail "0.5.1"]]
:jvm-opts ["-Djava.security.policy=example.policy""-Xmx80M"]
+ :plugins [[lein-cljsbuild "0.2.0"]]
+ :cljsbuild {
+ :builds [{
+ :source-path "src"
+ :compiler {
+ :output-to "resources/public/javascript/tryclojure.js"
+ :optimizations :whitespace
+ :pretty-print true}}]}
:main tryclojure.server)
View
183 resources/public/javascript/tryclojure.js
@@ -1,183 +0,0 @@
-var pageNum = -1;
-var page = null;
-var pages = [
- {
- verify: function(data) { return false; }
- },
- {
- verify: function(data) { return data.expr == "(+ 3 3)"; }
- },
- {
- verify: function(data) { return data.expr == "(/ 10 3)"; }
- },
- {
- verify: function(data) { return data.expr == "(/ 10 3.0)"; }
- },
- {
- verify: function(data) { return data.expr == "(+ 1 2 3 4 5 6)"; }
- },
- {
- verify: function (data) { return data.expr == "(defn square [x] (* x x))"; }
- },
- {
- verify: function (data) { return data.expr == "(square 10)"; }
- },
- {
- verify: function (data) { return data.expr == "((fn [x] (* x x)) 10)"; }
- },
- {
- verify: function (data) { return data.expr == "(def square (fn [x] (* x x)))"; }
- },
- {
- verify: function (data) { return data.expr == "(map inc [1 2 3 4])"; }
- },
- {
- verify: function (data) { return false; }
- }
-];
-
-function showPage(n) {
- var res = pages[n];
- if (res) {
- pageNum = n;
- page = res;
-
- var block = $("#changer");
- block.fadeOut(function(e) {
- block.load("/tutorial", { 'n' : n+1 }, function() {
- block.fadeIn();
- changerUpdated();
- });
- });
- }
-}
-
-function setupLink(url) {
- return function(e) { $("#changer").load(url, function(data) { $("#changer").html(data); }); }
-}
-
-function setupExamples(controller) {
- $(".code").click(function(e) {
- controller.promptText($(this).text());
- });
-}
-
-function getStep(n, controller) {
- $("#tuttext").load("tutorial", { step: n }, function() { setupExamples(controller); });
-}
-
-function eval_clojure(code) {
- var data;
- $.ajax({
- url: "eval.json",
- data: { expr : code },
- async: false,
- success: function(res) { data = res; }
- });
- return data;
-}
-
-function html_escape(val) {
- var result = val;
- result = result.replace(/\n/g, "<br/>");
- result = result.replace(/[<]/g, "&lt;");
- result = result.replace(/[>]/g, "&gt;");
- return result;
-}
-
-function doCommand(input, report) {
- switch (input) {
- case 'tutorial':
- showPage(0);
- report();
- return true;
- case 'back':
- if (pageNum > 0) {
- showPage(pageNum - 1);
- report();
- return true;
- } else {
- return false;
- }
- case 'next':
- if (pageNum >= 0 && pageNum < pages.length - 1) {
- showPage(pageNum + 1);
- report();
- return true;
- } else {
- return false;
- }
- case 'restart':
- if (pageNum > 0) {
- showPage(0);
- report();
- return true;
- } else {
- return false;
- }
- default:
- return false;
- }
-}
-
-function onValidate(input) {
- return (input != "");
-}
-
-function onHandle(line, report) {
- var input = $.trim(line);
-
- // handle commands
- if (doCommand(input, report)) return;
-
- // perform evaluation
- var data = eval_clojure(input);
-
- // handle error
- if (data.error) {
- return [{msg: data.message, className: "jquery-console-message-error"}];
- }
-
- // handle page
- if (page && page.verify(data)) {
- showPage(pageNum + 1);
- }
-
- // display expr results
- return [{msg: data.result, className: "jquery-console-message-value"}];
-}
-
-/**
- * This should be called anytime the changer div is updated so it can rebind event listeners.
- * Currently this is just to make the code elements clickable.
- */
-function changerUpdated() {
- $("#changer code.expr").each(function() {
- $(this).css("cursor", "pointer");
- $(this).attr("title", "Click to insert '" + $(this).text() + "' into the console.");
- $(this).click(function(e) {
- controller.promptText($(this).text());
- controller.inner.click();
- });
- });
-}
-
-var controller;
-
-$(document).ready(function() {
- controller = $("#console").console({
- welcomeMessage:'Enter some Clojure code to be evaluated.',
- promptLabel: 'Clojure> ',
- commandValidate: onValidate,
- commandHandle: onHandle,
- autofocus:true,
- animateScroll:true,
- promptHistory:true
- });
-
- $("#about").click(setupLink("about"));
- $("#links").click(setupLink("links"));
- $("#home").click(setupLink("home"));
-
- changerUpdated();
-});
View
127 src/client/home.cljs
@@ -0,0 +1,127 @@
+(ns tryclojure.client.home
+ (:use [jayq.core :only [$]]
+ [jayq.util :only [clj->js]])
+ (:use-macros [jayq.macros :only [ready]]))
+
+(def page-num (atom -1))
+(def page (atom nil))
+(def controller (atom nil))
+
+(def pages (concat
+ [{:verify (fn [data] false)}]
+ (map #(hash-map :verify %)
+ ["(+ 3 3)"
+ "(/ 10 3)"
+ "(/ 10 3.0)"
+ "(+ 1 2 3 4 5 6)"
+ "(defn square [x] (* x x))"
+ "(square 10)"
+ "((fn [x] (* x x)) 10)"
+ "(def square (fn [x] (* x x)))"
+ "(map inc [1 2 3 4])"])
+ [{:verify (fn [data] false)}]))
+
+(defn show-page [n]
+ (when-let [res (nth @pages n)]
+ (swap! page-num (constantly n))
+ (swap! page (constantly res))
+ (let [block ($ "#changer")]
+ (.fadeOut block
+ (fn [e]
+ (.load block
+ (clj->js {:n (+ 1 n)})
+ #(do (.fadeIn block)
+ (changer-updated))))))))
+
+(defn setup-link [url]
+ (fn [e]
+ (.load ($ "#changer")
+ url
+ (fn [data]
+ (.html ($ "#changer") data)))))
+
+(defn setup-examples [controller]
+ (.click ($ ".code")
+ (fn [e]
+ (.promptText controller (this-as this (.text ($ this)))))))
+
+(defn get-step [n controller]
+ (.load ($ "#tuttext")
+ "tutorial"
+ (clj->js {:step n})
+ #(setup-examples controller)))
+
+(defn eval-clojure [code]
+ (let [data (atom nil)]
+ (.ajax js/jQuery
+ (clj->js {:url "eval.json"
+ :data {:expr code}
+ :async false
+ :success (fn [res] (swap! data (constantly res)))}))
+ @data))
+
+(defn do-command [input report]
+ (case input
+ "tutorial" (do (show-page 0)
+ (report)
+ true)
+ "back" (if (> @page-num 0)
+ (do (show-page (- @page-num 1))
+ (report)
+ true)
+ false)
+ "next" (if (and (>= @page-num 0)
+ (< @page-num (- (count @pages) 1)))
+ (do (show-page (+ 1 @page-num))
+ (report)
+ true)
+ false)
+ "restart" (if (> @page-num 0)
+ (do (show-page 0)
+ (report)
+ true)
+ false)
+ false))
+
+(defn on-validate [input]
+ (not= input ""))
+
+(defn on-handle [line report]
+ (let [input (.trim js/jQuery line)]
+ (when-not (do-command input command)
+ (let [data (eval-clojure input)]
+ (if (.-error data)
+ (clj->js [{:msg (.-message data)
+ :className "jquery-console-message-error"}])
+ (do
+ (when (and @page (.verify @page data))
+ (show-page (+ 1 @page-num)))
+ (clj->js [{:msg (.-result data)
+ :className "jquery-console-message-value"}])))))))
+
+(defn changer-updated []
+ (.each ($ "#changer code.expr")
+ #(this-as this
+ (.css ($ this) "cursor" "pointer")
+ (.attr ($ this) "title" (str "Click to insert '" (.text ($ this)) "' into the console." ))
+ (.click ($ this)
+ (fn [e]
+ (.promptText @controller (.text ($ this)))
+ (.click (.-inner @controller)))))))
+
+
+
+(ready (swap! controller
+ #(.console ($ "#console")
+ (clj->js
+ {:welcomeMessage "Enter sme Clojure code to be evaluated."
+ :promptLabel "Clojure>"
+ :commandValidate on-validate
+ :commandHandle on-handle
+ :autofocus true
+ :animateScroll true
+ :promptHistory true})))
+ (.click ($ "#home") (setup-link "home"))
+ (.click ($ "#links") (setup-link "links"))
+ (.click ($ "#about") (setup-link "about"))
+ (changer-updated))
Something went wrong with that request. Please try again.