-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
helper.cljc
250 lines (227 loc) · 8.97 KB
/
helper.cljc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(ns figwheel.main.helper
(:require
#?@(:cljs
[[goog.dom :as gdom]
[goog.dom.classlist :as classlist]
[goog.net.XhrIo :as xhr]
[goog.object :as gobj]]
:clj [[clojure.java.io :as io]
[figwheel.server.ring :refer [best-guess-script-path]]])
[clojure.string :as string])
#?(:cljs
(:import
[goog Promise])))
#?(:cljs
(do
(def sidebar-link-class "figwheel_main_content_link")
(def sidebar-focus-class "figwheel_main_content_link_focus")
(defn connected-signal []
(some-> (gdom/getElement "connect-status")
(classlist/add "connected")))
(defn on-disconnect []
(some-> (gdom/getElement "connect-status")
(classlist/remove "connected")))
(defonce resource-atom (atom {}))
(defn get-content [url]
(Promise.
(fn [succ e]
(if-let [res (get @resource-atom url)]
(succ res)
(xhr/send url
(fn [resp]
(some-> resp
(gobj/get "currentTarget")
(.getResponseText)
(#(do (swap! resource-atom assoc url %)
%))
succ)))))))
(defn load-content [url div-id]
(.then (get-content url)
(fn [content]
(gdom/getElement div-id)
(set! (.-innerHTML (gdom/getElement div-id))
content))))
(defn focus-anchor! [anchor-tag]
(.forEach (gdom/getElementsByTagNameAndClass "a" sidebar-link-class)
(fn [a]
(classlist/remove a sidebar-focus-class)
(when (= a anchor-tag)
(classlist/add a sidebar-focus-class)))))
(defn init-sidebar-link-actions! []
(.forEach (gdom/getElementsByTagNameAndClass "a" sidebar-link-class)
(fn [a]
;; pre-fetch
(when-let [content-loc (.-rel a)]
(get-content content-loc)
(.addEventListener
a "click"
(fn [e]
(.preventDefault e)
(focus-anchor! a)
(when-let [content-loc (.-rel (.-target e))]
(load-content content-loc "main-content"))))))))
(defn on-connect [e]
(init-sidebar-link-actions!)
(connected-signal))
(defonce initialize
(do
(.addEventListener js/document.body "figwheel.repl.connected"
on-connect)
(.addEventListener js/document.body "figwheel.repl.disconnected"
on-disconnect)))
))
#?(:clj
(do
(defn main-wrapper [{:keys [body output-to header] :as options}]
(format
"<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
</head>
<body>
<div id=\"app\">
<link href=\"com/bhauman/figwheel/helper/css/style.css\" rel=\"stylesheet\" type=\"text/css\">
<link href=\"com/bhauman/figwheel/helper/css/coderay.css\" rel=\"stylesheet\" type=\"text/css\">
<header>
<div class=\"container\">
<div id=\"connect-status\">
<img class=\"logo\" width=\"60\" height=\"60\" src=\"com/bhauman/figwheel/helper/imgs/cljs-logo-120b.png\">
<div class=\"connect-text\"><span id=\"connect-msg\"></span></div>
</div>
<div class=\"logo-text\">%s</div>
</div>
<div class=\"container top-level-nav\">
<a href=\"http://figwheel.org\" target=\"_blank\">Figwheel Main</a>
<a href=\"https://clojurescript.org\" target=\"_blank\">CLJS</a>
<a href=\"https://cljs.info\" target=\"_blank\">Cheatsheet</a>
<a href=\"https://kanaka.github.io/clojurescript/web/synonym.html\" target=\"_blank\">Synonyms</a>
</div>
</header>
<div class=\"container\">
<aside>
<a class=\"figwheel_main_content_link\" href=\"javascript:\" rel=\"figwheel/helper/welcome\">Welcome</a>
<a class=\"figwheel_main_content_link\" href=\"javascript:\" rel=\"com/bhauman/figwheel/helper/content/creating_a_build_cli_tools.html\">Create a build</a>
<a class=\"figwheel_main_content_link\" href=\"javascript:\" rel=\"com/bhauman/figwheel/helper/content/creating_a_build_lein.html\">Create a build (lein)</a>
<a class=\"figwheel_main_content_link\" href=\"javascript:\" rel=\"com/bhauman/figwheel/helper/content/css_reloading.html\">Live Reload CSS</a>
</aside>
<section id=\"main-content\">
%s
</section>
</div>
<footer>
<div class=\"container flex-column\">
<div class=\"off-site-resources\">
<h6>Figwheel Main</h6>
<a href=\"https://github.com/bhauman/figwheel-main/tree/master\" target=\"_blank\">Figwheel Main Home / Readme</a>
<a href=\"https://github.com/bhauman/figwheel-main/blob/master/doc/figwheel-main-options.md\" target=\"_blank\">Config Options</a>
</div>
<div class=\"off-site-resources\">
<h6>Clojurescript</h6>
<a href=\"https://clojurescript.org\" target=\"_blank\">ClojureScript Home</a>
<a href=\"https://cljs.info\" target=\"_blank\">API Cheatsheet</a>
<a href=\"https://kanaka.github.io/clojurescript/web/synonym.html\" target=\"_blank\">JavaScript Synonyms</a>
</div>
<div class=\"off-site-resources\">
<h6>Community</h6>
<a href=\"http://clojurians.slack.com\" target=\"_blank\">#clojurescript on Slack</a>
<a href=\"http://clojurians.slack.com\" target=\"_blank\">#figwheel-main on Slack</a>
<a href=\"http://clojurians.net\" target=\"_blank\">get Clojurians Slack invite</a>
<a href=\"http://groups.google.com/group/clojurescript\" target=\"_blank\">ClojureScript Google Group</a>
<a href=\"https://clojureverse.org/\" target=\"_blank\">ClojureVerse</a>
</div>
</div>
</footer>
%s
</div> <!-- end of app div -->
<script type=\"text/javascript\">%s</script>
</body>
</html>"
(str header)
(str body)
(str
(when-not (:dev-mode options)
"<script src=\"com/bhauman/figwheel/helper.js\"></script>"))
(str
(when (and output-to
(.isFile (io/file output-to)))
(-> (slurp output-to)
(string/replace #"<\/script" "<\\\\/script"))))))
(defn main-action [req options]
{:status 200
:headers {"Content-Type" "text/html"}
:body (main-wrapper options)})
(defn dev-endpoint [req]
(let [method-uri ((juxt :request-method :uri) req)]
(when (= method-uri [:get "/"])
(main-action req {:output-to "target/public/cljs-out/helper-main.js"
:dev-mode true
:header "Dev Mode"
:body (slurp (io/resource "public/com/bhauman/figwheel/helper/content/repl_welcome.html"))}))))
(defn middleware [handler options]
(fn [req]
(let [method-uri ((juxt :request-method :uri) req)]
(cond
(and (= [:get "/figwheel/helper/welcome"] method-uri) (:body options))
{:status 200
:headers {"Content-Type" "text/html"}
:body (:body options)}
(= method-uri [:get "/"])
(main-action req options)
:else (handler req)))))
(defn missing-index-middleware [handler options]
(fn [r]
(let [method-uri ((juxt :request-method :uri) r)]
(cond
(and (= [:get "/figwheel/helper/welcome"] method-uri)
(:body options))
{:status 200
:headers {"Content-Type" "text/html"}
:body (:body options)}
:else
(let [res (handler r)]
(if (and (= [:get "/"] method-uri)
(= 404 (:status res)))
(main-action r options)
res))))))
(defn default-index-code [output-to]
(let [path (best-guess-script-path output-to)]
(str
"<div class=\"CodeRay\">"
"<div class=\"code\">"
"<pre>"
"<!DOCTYPE html>\n"
"<html>\n"
" <head>\n"
" <meta charset=\"UTF-8\">\n"
" </head>\n"
" <body>\n"
" <div id=\"app\">\n"
" </div>\n"
" <script src=\""
(if path path
(str "[correct-path-to "
(or output-to "main.js file")
"]"))
"\" type=\"text/javascript\"></script>\n"
" </body>\n"
"</html>\n"
"</pre></div></div>")))
(defn missing-index [handler options]
(missing-index-middleware
handler
(merge
{:header "Figwheel Default Dev Page"
:body (str
(slurp (io/resource "public/com/bhauman/figwheel/helper/content/missing_index.html"))
(default-index-code (:output-to options)))}
options)))
(defn serve-only-middleware [handler options]
(missing-index-middleware
handler
(merge
{:header "Server Only Page"
:body (slurp (io/resource "public/com/bhauman/figwheel/helper/content/server_only_welcome.html"))}
options)))
)) ;; clj conditional reader end