Permalink
Browse files

compiled templates can now pull from url, file, or classpath resource

  • Loading branch information...
1 parent 8e7104f commit f11dac2698971370bfd62c929aad62cae011f6e5 @ckirkendall committed May 22, 2012
@@ -49,9 +49,9 @@
(em/clone-for [fr (vec fruit-data)]
["*:first-child"] (em/content (first fr))
["*:last-child"] (em/content (str (second fr))))))
+
-
-(em/defsnippet success :compiled "../sample/resources/public/templates/test-grid.html"
+(em/defsnippet success :compiled "enfocus/html/test-grid.html"
["tbody > *:first-child > td span"] [])
(em/defsnippet row "templates/test-grid.html" ["tbody > *:first-child"]
@@ -110,7 +110,7 @@
["#heading1"] (em/set-attr :id "new-heading1")
["#heading2"] (em/set-attr :id "new-heading2")
["#test-content2 tfoot tr > *:last-child"] (em/content (str 20))
- ;; ["#test-content3"] (em/content (template3 {"banana" 12 "apple" 6}))
+ ["#test-content3"] (em/content (template3 {"banana" 12 "apple" 6}))
["#test-content4"] (em/set-style :background "#00dd00" :font-size "10px")
["#test-content5"] (em/set-style :background "#dd0000" :font-size "10px")
["#test-content5"] (em/remove-style :background :font-size)
@@ -157,5 +157,5 @@
(em/at js/document
[:.heading (ef/attr= :foo "true")] (em/content msg))
(em/wait-for-load (test-grid)))
-
+
(set! (.-onload js/window) #(funtimes "THIS IS A TEST"))
@@ -0,0 +1,90 @@
+<html>
+<head>
+<title>Test grid for enfocus</title>
+
+<link type="text/css" rel="stylesheet" media="all" href="../css/test.css" />
+</head>
+<body>
+<div id="content">
+<table>
+<thead>
+ <tr>
+ <th>Test</th>
+ <th>Result</th>
+ </tr>
+</thead>
+<tbody>
+ <tr id="test1">
+ <td>loading a remote template</td>
+ <td><span class="success">success</span></td>
+ </tr>
+ <tr id="test2">
+ <td>Adding a template to live dom</td>
+ <td><span class="success">success</span></td>
+ </tr>
+ <tr id="test3">
+ <td>Bring in a content with a snippit</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test4">
+ <td>Replacing content using <b>content</b></td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test5">
+ <td>Replacing content using <b>html-content</b></td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test6">
+ <td>Setting attribute</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test7" foo="fail">
+ <td>Remove attribute</td>
+ <td></td>
+ </tr>
+ <tr id="test8" >
+ <td>Add class</td>
+ <td></td>
+ </tr>
+ <tr id="test9" class="bad">
+ <td>Remove class</td>
+ <td></td>
+ </tr>
+ <tr id="test10">
+ <td>do-> remove and after</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test11">
+ <td>do-> remove and before</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test12">
+ <td>substitute</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test13">
+ <td>do-> content append</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test14">
+ <td>do-> content prepend</td>
+ <td><span class="fail">fail</span></td>
+ </tr>
+ <tr id="test15">
+ <td>wrap transformation</td>
+ <td><span id="wrap-span">fail</span></td>
+ </tr>
+ <tr id="test16">
+ <td>unwrap transformation</td>
+ <td><span class="success">
+ <span id="wrapper" class="fail">
+ <span class="wrapped">success</span>
+ <span class="wrapped">if green</span>
+ </span>
+ </span></td>
+ </tr>
+</tbody>
+</table>
+</div>
+</body>
+</html>
@@ -1,4 +1,5 @@
-(ns enfocus.macros)
+(ns enfocus.macros
+ (:require [clojure.java.io :as io]))
;##############################################
; All main transformations and functions are
@@ -40,9 +41,11 @@
(defn- create-extraction-call [id-sym pnod-sym map-sym forms]
(map (fn [[ky sel tran]]
- (list 'clojure.core/swap! map-sym 'clojure.core/assoc ky (list
- (if tran tran 'enfocus.macros/remove-node)
- (list 'enfocus.core/css-select id-sym pnod-sym sel))))
+ (list 'clojure.core/swap!
+ map-sym
+ 'clojure.core/assoc ky (list
+ (if tran tran 'enfocus.macros/remove-node)
+ (list 'enfocus.core/css-select id-sym pnod-sym sel))))
(partition 3 forms)))
@@ -52,25 +55,39 @@
new-form (create-transform-call id-sym pnode-sym forms)]
`(defn ~sym ~args
(let [[~id-sym ~pnode-sym] (if (fn? ~nod) (~nod) ["" ~nod])
- ~pnode-sym (if ~tmp-dom (enfocus.core/create-hidden-dom ~pnode-sym) ~pnode-sym)]
+ ~pnode-sym (if ~tmp-dom
+ (enfocus.core/create-hidden-dom ~pnode-sym)
+ ~pnode-sym)]
~@new-form
(if ~tmp-dom
(do
(enfocus.core/reset-ids ~id-sym ~pnode-sym)
(enfocus.core/remove-node-return-child ~pnode-sym))
~pnode-sym)))))
-
-(defn ensure-mode [[mode & _ :as body]]
- ;; 'mode' argument is not required and defaults to :remote, for
- ;; backward compatiblity.
+(defn find-url
+ "Given a string, returns a URL. Attempts to resolve as a classpath-relative
+ path, then as a path relative to the working directory or a URL string"
+ [path-or-url]
+ (or (io/resource path-or-url)
+ (try (io/as-url path-or-url)
+ (catch java.net.MalformedURLException e
+ false))
+ (io/as-url (io/as-file path-or-url))))
@superbobry

superbobry May 22, 2012

Contributor

Great! I wanted to do the same, but didn't know how to implement it :)

+
+
+(defn ensure-mode
+ " 'mode' argument is not required and defaults to :remote, for
+ backward compatability"
+ [[mode & _ :as body]]
(if (keyword? mode)
body
(cons :remote body)))
-(defn load-local-dom [path]
+(defn load-local-dom
"same as 'load-remote-dom', but work for local files"
- (let [text (slurp path)]
+ [path]
+ (let [text (slurp (io/reader (find-url path)))]
`(when (nil? (@enfocus.core/tpl-cache ~path))
(let [[sym# txt#] (enfocus.core/replace-ids ~text)
data# (goog.dom/htmlToDocumentFragment txt#)]

0 comments on commit f11dac2

Please sign in to comment.