Cleanup config handling

This commit is contained in:
Dan Holmsand 2015-09-15 13:36:02 +02:00
parent ea75be2952
commit 38250c763b
1 changed files with 20 additions and 16 deletions

View File

@ -46,23 +46,25 @@
(defonce history nil) (defonce history nil)
(defn demo-handler [state [id x y :as event]] (defn demo-handler [state [id x :as event]]
(case id (case id
:set-content (let [title (if y :set-content (let [page x
(str (:title-prefix state) y) title (:title page)
(str (:default-title state)))] _ (assert (vector? (:content page)))
(assert (vector? x)) title (if title
(str (:title-prefix state) title)
(str (:default-title state)))
page (assoc page :title title)]
(when r/is-client (when r/is-client
(set! js/document.title title)) (set! js/document.title title))
(assoc state :main-content x :title title)) (assoc state :current-page page))
:set-page (do (assert (string? x)) :set-page (let [path x
(let [{pages :pages} state _ (assert (string? path))
p (get pages x (get pages "/index.html"))] ps (:pages state)
(-> state p (get ps path (get ps "/index.html"))]
(assoc :page-path x) (recur state [:set-content p]))
(recur [:set-content (:content p) (:title p)])))) :goto-page (let [path x
:goto-page (do _ (assert (string? path))]
(assert (string? x))
(when-some [h history] (when-some [h history]
(.setToken h x) (.setToken h x)
(r/next-tick #(set! js/document.body.scrollTop 0)) (r/next-tick #(set! js/document.body.scrollTop 0))
@ -118,7 +120,7 @@
child]) child])
(defn main-content [] (defn main-content []
(let [{comp :main-content} @config] (let [comp (get-in @config [:current-page :content])]
(assert (vector? comp)) (assert (vector? comp))
comp)) comp))
@ -152,12 +154,14 @@
(defn gen-page [page-path conf] (defn gen-page [page-path conf]
(dispatch [:set-page page-path]) (dispatch [:set-page page-path])
(let [b (:body conf) (let [conf (merge conf @config)
b (:body conf)
_ (assert (vector? b)) _ (assert (vector? b))
bhtml (r/render-component-to-string b)] bhtml (r/render-component-to-string b)]
(str "<!doctype html>\n" (str "<!doctype html>\n"
(html-template (assoc conf (html-template (assoc conf
:page-conf {:page-path page-path} :page-conf {:page-path page-path}
:title (-> conf :current-page :title)
:body-html bhtml))))) :body-html bhtml)))))
(defn fs [] (js/require "fs")) (defn fs [] (js/require "fs"))