Skip to content

Instantly share code, notes, and snippets.

@ckirkendall
Forked from matklad/broken.cljs
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckirkendall/0ee927f0cb6f629d6a3b to your computer and use it in GitHub Desktop.
Save ckirkendall/0ee927f0cb6f629d6a3b to your computer and use it in GitHub Desktop.
;;note the following code use fresnel lens library https://github.com/ckirkendall/fresnel
(def app (atom {:code ""
:state :initial
:advice nil
:active-line nil
:timer 0} ;; this increments every 200ms...
:validator (fn [{:keys [state advice]}]
(or (#{:checked :loading} state)
(nil? advice)))))
;; factor out render-loading
(defn render-result [node {:keys [state advice]}]
(log "render-result")
(let [transform (if (= state :loading) ef/remove-class ef/add-class)]
(ef/at node
".advice"
(ef/content
(cond
(= state :initial) "Paste some clojure code"
(= state :broken) "Failed to check the code =(\nAre parenthesis balanced?"
(empty? advice) "Looks OK to me"
:default (map advice-snip advice)))
".result" (transform "text-info")
".loader" (transform "invisible"))))
(defn render-loading [node time]
(log "render-loading")
(let [steps (mapv #(clojure.string/replace % " " " ")
["..." " .." " ." " " ". " ".. "])
dots (nth steps (mod time (count steps)))]
(ef/at node
".loader" (ef/content (str "Loading" dots)))))
(defn sub-map-lens [kys]
(reify Lens
(-fetch [this value] (select-keys value kys))
(-putback [this value subvalue] (merge value subvalue))))
(defaction home []
".result-wrapper > .loader" (bind/bind-view app render-loading [:timer]) ;; BUG
".result-wrapper" (bind/bind-view app render-result (sub-map-lens [:state :advice]))) ;; only last binding works
(def app (atom {:code ""
:data {:state :initial
:advice nil}
:active-line nil
:timer 0} ;; this increments every 200ms...
:validator (fn [{:keys [state advice]}]
(or (#{:checked :loading} state)
(nil? advice)))))
;; factor out render-loading
(defn render-result [node {:keys [state advice timer]}]
(log "render-result")
(let [transform (if (= state :loading) ef/remove-class ef/add-class)]
(ef/at node
".advice"
(ef/content
(cond
(= state :initial) "Paste some clojure code"
(= state :broken) "Failed to check the code =(\nAre parenthesis balanced?"
(empty? advice) "Looks OK to me"
:default (map advice-snip advice)))
".result" (transform "text-info")
".loader" (transform "invisible"))))
(defn render-loading [node time]
(log "render-loading")
(let [steps (mapv #(clojure.string/replace % " " " ")
["..." " .." " ." " " ". " ".. "])
dots (nth steps (mod time (count steps)))]
(ef/at node
".loader" (ef/content (str "Loading" dots)))))
(defaction home []
".result-wrapper > .loader" (bind/bind-view app render-loading [:timer]) ;; BUG
".result-wrapper" (bind/bind-view app render-result [:data]))) ;; only last binding works
(def app (atom {:code ""
:state :initial
:advice nil
:active-line nil
:timer 0} ;; this increments every 200ms...
:validator (fn [{:keys [state advice]}]
(or (#{:checked :loading} state)
(nil? advice)))))
(defn render-result [node {:keys [state advice timer]}]
(let [transform (if (= state :loading) ef/remove-class ef/add-class)
steps (mapv #(clojure.string/replace % " " " ")
["..." " .." " ." " " ". " ".. "])
dots (nth steps (mod timer (count steps)))]
(ef/at node
".advice"
(ef/content
(cond
(= state :initial) "Paste some clojure code"
(= state :broken) "Failed to check the code =(\nAre parenthesis balanced?"
(empty? advice) "Looks OK to me"
:default (map advice-snip advice)))
".result" (transform "text-info")
".loader" (ef/do-> ;; ... so it would be nice to execute only this part
(transform "invisible")
(ef/content (str "Loading" dots))))))
(defaction home []
".result-wrapper" (bind/bind-view app render-result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment