Simplify template.cljs a little

This commit is contained in:
Dan Holmsand 2015-10-09 13:32:30 +02:00
parent 47f49868ae
commit 88d961563a

View File

@ -39,13 +39,13 @@
:for "htmlFor" :for "htmlFor"
:charset "charSet"}) :charset "charSet"})
(defn obj-get [o k] (defn cache-get [o k]
(when ^boolean (.hasOwnProperty o k) (when ^boolean (.hasOwnProperty o k)
(aget o k))) (aget o k)))
(defn cached-prop-name [k] (defn cached-prop-name [k]
(if (named? k) (if (named? k)
(if-some [k' (obj-get prop-name-cache (name k))] (if-some [k' (cache-get prop-name-cache (name k))]
k' k'
(aset prop-name-cache (name k) (aset prop-name-cache (name k)
(util/dash-to-camel k))) (util/dash-to-camel k)))
@ -68,30 +68,32 @@
(coll? x) (clj->js x) (coll? x) (clj->js x)
(ifn? x) (fn [& args] (ifn? x) (fn [& args]
(apply x args)) (apply x args))
true (clj->js x))) :else (clj->js x)))
(defn set-id-class [props id class] (defn oset [o k v]
(let [p (if (nil? props) #js{} props)] (doto (if (nil? o) #js{} o)
(when (and (some? id) (aset k v)))
(nil? (.' p :id)))
(.! p :id id)) (defn oget [o k]
(when (some? class) (if (nil? o) nil (aget o k)))
(let [old (.' p :className)]
(.! p :className (if (some? old) (defn set-id-class [p id-class]
(str class " " old) (let [id (.' id-class :id)
class)))) p (if (and (some? id)
p)) (nil? (oget p "id")))
(oset p "id" id)
p)]
(if-some [class (.' id-class :className)]
(let [old (oget p "className")]
(oset p "className" (if (nil? old)
class
(str class " " old))))
p)))
(defn convert-props [props id-class] (defn convert-props [props id-class]
(let [id (.' id-class :id) (-> props
class (.' id-class :className) convert-prop-value
no-id-class (and (nil? id) (nil? class))] (set-id-class id-class)))
(if (and no-id-class (empty? props))
nil
(let [objprops (convert-prop-value props)]
(if no-id-class
objprops
(set-id-class objprops id class))))))
;;; Specialization for input components ;;; Specialization for input components
@ -215,22 +217,26 @@
:id id :id id
:className class})) :className class}))
(defn try-get-key [x]
;; try catch to avoid clojurescript peculiarity with
;; sorted-maps with keys that are numbers
(try (get x :key)
(catch :default e)))
(defn get-key [x] (defn get-key [x]
(when (map? x) (when (map? x)
;; try catch to avoid clojurescript peculiarity with (try-get-key x)))
;; sorted-maps with keys that are numbers
(try (get x :key)
(catch :default e))))
(defn key-from-vec [v] (defn key-from-vec [v]
(if-some [k (some-> (meta v) get-key)] (if-some [k (-> (meta v) get-key)]
k k
(-> v (nth 1 nil) get-key))) (-> v (nth 1 nil) get-key)))
(defn reag-element [tag v] (defn reag-element [tag v]
(let [c (comp/as-class tag) (let [c (comp/as-class tag)
jsprops #js{:argv v}] jsprops #js{:argv v}]
(some->> v key-from-vec (.! jsprops :key)) (when-some [key (key-from-vec v)]
(.! jsprops :key key))
(.' util/react createElement c jsprops))) (.' util/react createElement c jsprops)))
(defn adapt-react-class [c] (defn adapt-react-class [c]
@ -242,7 +248,7 @@
(def tag-name-cache #js{}) (def tag-name-cache #js{})
(defn cached-parse [x] (defn cached-parse [x]
(if-some [s (obj-get tag-name-cache x)] (if-some [s (cache-get tag-name-cache x)]
s s
(aset tag-name-cache x (parse-tag x)))) (aset tag-name-cache x (parse-tag x))))
@ -258,11 +264,10 @@
(-> [(reagent-input) argv comp jsprops first-child] (-> [(reagent-input) argv comp jsprops first-child]
(with-meta (meta argv)) (with-meta (meta argv))
as-element) as-element)
(let [key (some-> (meta argv) get-key) (let [key (-> (meta argv) get-key)
p (if (nil? key) p (if (nil? key)
jsprops jsprops
(doto (if (nil? jsprops) #js{} jsprops) (oset jsprops "key" key))]
(.! :key key)))]
(make-element argv comp p first-child)))))) (make-element argv comp p first-child))))))
(defn str-coll [coll] (defn str-coll [coll]
@ -279,7 +284,7 @@
(defn vec-to-elem [v] (defn vec-to-elem [v]
(assert (pos? (count v)) (hiccup-err v "Hiccup form should not be empty")) (assert (pos? (count v)) (hiccup-err v "Hiccup form should not be empty"))
(let [tag (nth v 0)] (let [tag (nth v 0 nil)]
(assert (valid-tag? tag) (hiccup-err v "Invalid Hiccup form")) (assert (valid-tag? tag) (hiccup-err v "Invalid Hiccup form"))
(cond (cond
(hiccup-tag? tag) (hiccup-tag? tag)
@ -287,7 +292,7 @@
pos (.indexOf n ">")] pos (.indexOf n ">")]
(case pos (case pos
-1 (native-element (cached-parse n) v 1) -1 (native-element (cached-parse n) v 1)
0 (let [comp (nth v 1)] 0 (let [comp (nth v 1 nil)]
;; Support [:> comp ...] ;; Support [:> comp ...]
(assert (= ">" n) (hiccup-err v "Invalid Hiccup tag")) (assert (= ">" n) (hiccup-err v "Invalid Hiccup tag"))
(assert (or (string? comp) (fn? comp)) (assert (or (string? comp) (fn? comp))
@ -372,7 +377,7 @@
0 (.' util/react createElement comp jsprops) 0 (.' util/react createElement comp jsprops)
1 (.' util/react createElement comp jsprops 1 (.' util/react createElement comp jsprops
(as-element (nth argv first-child))) (as-element (nth argv first-child nil)))
(.apply (.' util/react :createElement) nil (.apply (.' util/react :createElement) nil
(reduce-kv (fn [a k v] (reduce-kv (fn [a k v]