Permalink
Browse files

Add tutorial files

  • Loading branch information...
1 parent 189a8f4 commit 0e5a7706673e1d1ee5ee9262791b8d8d731f60de @ericbmerritt committed Aug 10, 2011
View
@@ -0,0 +1,4 @@
+#! /bin/sh
+
+mkdir -p ./war/js/deps/
+cljsc "src/" '{:optimizations :advanced :output-dir "war/js" :output-to "war/js/deps/tutorial.js"}'
View
@@ -0,0 +1,4 @@
+#! /bin/sh
+
+mkdir -p ./war/js/deps/
+cljsc "src/" '{:output-dir "war/js" :output-to "war/js/deps/tutorial.js"}'
View
@@ -0,0 +1,5 @@
+#! /bin/sh
+
+cd war
+
+python -m SimpleHTTPServer
@@ -0,0 +1,98 @@
+; Copyright (c) Rich Hickey. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns tutorial.dom-helpers
+ (:require [clojure.string :as string]
+ [goog.dom :as dom]))
+
+(defn get-element
+ "Return the element with the passed id."
+ [id]
+ (dom/getElement (name id)))
+
+(defn append
+ "Append all children to parent."
+ [parent & children]
+ (do (doseq [child children]
+ (dom/appendChild parent child))
+ parent))
+
+(defn set-text
+ "Set the text content for the passed element returning the
+ element. If a keyword is passed in the place of e, the element with
+ that id will be used and returned."
+ [e s]
+ (let [e (if (keyword? e) (get-element e) e)]
+ (doto e (dom/setTextContent s))))
+
+(defn normalize-args [tag args]
+ (let [parts (string/split tag #"(\.|#)")
+ [tag attrs] [(first parts)
+ (apply hash-map (map #(cond (= % ".") :class
+ (= % "#") :id
+ :else %)
+ (rest parts)))]]
+ (if (map? (first args))
+ [tag (merge attrs (first args)) (rest args)]
+ [tag attrs args])))
+
+;; TODO: replace call to .strobj with whatever we come up with for
+;; creating js objects from Clojure maps.
+
+(defn element
+ "Create a dom element using a keyword for the element name and a map
+ for the attributes. Append all children to parent. If the first
+ child is a string then the string will be set as the text content of
+ the parent and all remaining children will be appended."
+ [tag & args]
+ (let [[tag attrs children] (normalize-args tag args)
+ parent (dom/createDom (name tag)
+ (.strobj (reduce (fn [m [k v]]
+ (assoc m k v))
+ {}
+ (map #(vector (name %1) %2)
+ (keys attrs)
+ (vals attrs)))))
+ [parent children] (if (string? (first children))
+ [(set-text (element tag attrs) (first children))
+ (rest children)]
+ [parent children])]
+ (apply append parent children)))
+
+(defn remove-children
+ "Remove all children from the element with the passed id."
+ [id]
+ (let [parent (dom/getElement (name id))]
+ (do (dom/removeChildren parent))))
+
+(defn html
+ "Create a dom element from an html string."
+ [s]
+ (dom/htmlToDocumentFragment s))
+
+(defn- element-arg? [x]
+ (or (keyword? x)
+ (map? x)
+ (string? x)))
+
+(defn build
+ "Build up a dom element from nested vectors."
+ [x]
+ (if (vector? x)
+ (let [[parent children] (if (keyword? (first x))
+ [(apply element (take-while element-arg? x))
+ (drop-while element-arg? x)]
+ [(first x) (rest x)])
+ children (map build children)]
+ (apply append parent children))
+ x))
+
+(defn insert-at
+ "Insert a child element at a specific location."
+ [parent child index]
+ (dom/insertChildAt parent child index))
View
@@ -0,0 +1,14 @@
+(ns tutorial.notepad
+ (:require [tutorial.notepad.note :as note]
+ [tutorial.dom-helpers :as dom]))
+
+(defn main
+ []
+ (let [note-data [{:title "Note 1" :content "Content of Note 1"}
+ {:title "Note 2" :content "Content of Note 2"}]
+ note-container (dom/get-element :notes)]
+
+ (note/make-notes note-data note-container)))
+
+(main)
+
@@ -0,0 +1,35 @@
+(ns tutorial.notepad.note
+ (:require [tutorial.dom-helpers :as dom]
+ [goog.ui.Zippy :as zippy]))
+
+(defn init
+ [title content node-container]
+
+ {:title title :content content :parent node-container})
+
+(defn make-note-dom
+ [self]
+
+ (let [header-element (dom/build
+ [:div {:style "background-color:#EEE"}
+ (:title self)])
+ content-element (dom/build
+ [:div (:content self)])
+ new-note (dom/build
+ [:div header-element content-element])
+ zippy (goog.ui.Zippy. header-element content-element)]
+
+ (dom/append (:parent self)
+ new-note)
+ ;; Return an updated self object with the above declarations
+ (-> self
+ (assoc :header-element header-element)
+ (assoc :content-element content-element)
+ (assoc :zippy zippy))))
+
+(defn make-notes
+ [data node-container]
+ (doseq [cont data]
+ (let [self
+ (init (:title cont) (:content cont) node-container)]
+ (make-note-dom self))))
View
@@ -0,0 +1,26 @@
+<!doctype html>
+<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
+<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
+<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
+<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
+<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
+<head>
+ <meta charset="UTF-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
+
+ <title>Google Closure Tutorial for Clojurescript</title>
+ <meta name="description" content="Demo showing off the ClojureScript hotness">
+ <!--[if lt IE 9]>
+ <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
+ <![endif]-->
+
+</head>
+<body>
+ <header>
+ <div id="notes"></div>
+
+ <script type="text/javascript" src="js/deps/tutorial.js"></script>
+
+</body>
+</html>
+
View
@@ -0,0 +1,29 @@
+<!doctype html>
+<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
+<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
+<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
+<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
+<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
+<head>
+ <meta charset="UTF-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
+
+ <title>Google Closure Tutorial for Clojurescript</title>
+ <meta name="description" content="Demo showing off the ClojureScript hotness">
+ <!--[if lt IE 9]>
+ <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
+ <![endif]-->
+
+</head>
+<body>
+ <header>
+ <div id="notes"></div>
+
+ <script type="text/javascript" src="js/goog/base.js"></script>
+ <script type="text/javascript" src="js/deps/tutorial.js"></script>
+ <script type="text/javascript">
+ goog.require('tutorial.notepad');
+ </script>
+</body>
+</html>
+

0 comments on commit 0e5a770

Please sign in to comment.