-
Notifications
You must be signed in to change notification settings - Fork 10
/
dom.cljs
84 lines (71 loc) · 3.08 KB
/
dom.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(ns stylefy.impl.dom
(:require [dommy.core :as dommy]
[garden.core :refer [css]]
[cljs.core.async :as async] ; Mandatory for running tests
[stylefy.impl.cache :as cache]
[stylefy.impl.log :as log]
[stylefy.impl.state :as state])
(:require-macros
[cljs.core.async.macros :refer [go]]))
(def stylefy-node-id "#_stylefy-styles_")
(def stylefy-constant-node-id "#_stylefy-constant-styles_")
(def stylefy-base-node (atom nil)) ; Used when running multiple instances of stylefy on the same page
(def stylefy-instance-id (atom nil)) ; Used when running multiple instances of stylefy on the same page
(def uninitialised-styles (atom {:style []
:font-face []
:keyframes []
:tag []
:class []}))
(defn init-multi-instance [{:keys [multi-instance] :as _options}]
(let [base-node (:base-node multi-instance)
instance-id (:instance-id multi-instance)]
(assert (or (nil? instance-id)
(string? instance-id))
(str "instance-id must be string. Got: " (pr-str base-node instance-id)))
(reset! stylefy-base-node base-node)
(reset! stylefy-instance-id instance-id)))
(defn get-stylefy-node [node-id base-node instance-id]
(let [final-id (str node-id (when instance-id (str instance-id)))]
(if (nil? base-node)
(dommy/sel1 final-id)
(dommy/sel1 base-node final-id))))
(defprotocol Dom
; Init
(load-uninitialised-styles [this uninitialised-styles])
(load-cache [this])
; Add styles
(add-style [this style])
(add-class [this class-as-css])
(add-tag [this tag-as-css])
(add-font-face [this font-face-as-css])
(add-keyframes [this identifier keyframes-as-css])
; DOM management
(update-dom [this])
(update-dom-if-needed [this])
(style-in-dom? [this style-hash])
(style-by-hash [this style-hash]))
(defn save-uninitialised-style [key style-as-css]
(reset! uninitialised-styles
(assoc @uninitialised-styles
key
(conj (key @uninitialised-styles) style-as-css))))
; Temporary "dummy" DOM handler is used until stylefy is initialised.
; The main purpose of this "dummy" handler is to save the styles
; that are created before the real DOM handler has been initialised.
(defrecord UninitialisedDom []
Dom
; Init
(load-uninitialised-styles [this uninitialised-styles])
(load-cache [this])
; Add styles
(add-style [this style] (save-uninitialised-style :style style))
(add-class [this class-as-css] (save-uninitialised-style :class class-as-css))
(add-tag [this tag-as-css] (save-uninitialised-style :tag tag-as-css))
(add-font-face [this font-face-as-css] (save-uninitialised-style :font-face font-face-as-css))
(add-keyframes [this identifier keyframes-as-css] (save-uninitialised-style :keyframes [identifier keyframes-as-css]))
; DOM management
(update-dom [this])
(update-dom-if-needed [this])
(style-in-dom? [this style-hash] false)
(style-by-hash [this style-hash]))
(def dom (atom (->UninitialisedDom)))