Add the ability to pass parameters to lightboxes.

This commit is contained in:
Andrey Antukh 2015-12-24 19:36:15 +02:00
parent bd79eb9af1
commit 08e02800d2

View file

@ -12,9 +12,11 @@
(defonce +current+ (atom nil)) (defonce +current+ (atom nil))
(defn set! (defn open!
[kind] ([kind]
(reset! +current+ kind)) (open! kind nil))
([kind params]
(reset! +current+ (merge {:name kind} params))))
(defn close! (defn close!
[] []
@ -24,7 +26,7 @@
;; UI ;; UI
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti render-lightbox identity) (defmulti render-lightbox :name)
(defmethod render-lightbox :default [_] nil) (defmethod render-lightbox :default [_] nil)
(defn- on-esc-clicked (defn- on-esc-clicked
@ -33,30 +35,34 @@
(close!))) (close!)))
(defn- lightbox-will-mount (defn- lightbox-will-mount
[state] [own]
(events/listen js/document (let [key (events/listen js/document
EventType.KEYDOWN EventType.KEYDOWN
on-esc-clicked) on-esc-clicked)]
state) (assoc own ::eventkey key)))
(defn- lightbox-will-umount (defn- lightbox-will-umount
[state] [own]
(events/unlisten js/document (let [key (::eventkey own)]
EventType.KEYDOWN (events/unlistenByKey key)
on-esc-clicked) (dissoc own ::eventkey)))
state)
(defn- lightbox-transfer-state
[old-own own]
(assoc own ::eventkey (::eventkey old-own)))
(defn- lightbox-render (defn- lightbox-render
[own] [own]
(let [name (rum/react +current+)] (let [params (rum/react +current+)]
(html (html
[:div.lightbox {:class (when (nil? name) "hide")} [:div.lightbox {:class (when (nil? params) "hide")}
(render-lightbox name)]))) (render-lightbox params)])))
(def ^:static lightbox (def ^:static lightbox
(util/component (util/component
{:name "lightbox" {:name "lightbox"
:render lightbox-render :render lightbox-render
:transfer-state lightbox-transfer-state
:will-mount lightbox-will-mount :will-mount lightbox-will-mount
:will-unmount lightbox-will-umount :will-unmount lightbox-will-umount
:mixins [rum/reactive]})) :mixins [rum/reactive]}))