From 2f9eb55c36a848a45eb0f391e68542e9545c41b6 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 5 Apr 2018 07:22:43 -0400 Subject: [PATCH] Use Hiccup to render HTML instead of making a complicated static app --- project.clj | 26 +++--------------------- resources/public/index.html | 10 --------- src_clj/sketchbook/handler.clj | 21 ++++++++----------- src_clj/sketchbook/random.clj | 5 ++++- src_clj/sketchbook/sketches.clj | 9 +-------- src_clj/sketchbook/views.clj | 36 +++++++++++++++++++++++++++++++++ src_cljs/sketchbook/core.cljs | 35 -------------------------------- src_cljs/sketchbook/db.cljs | 5 ----- src_cljs/sketchbook/events.cljs | 27 ------------------------- src_cljs/sketchbook/subs.cljs | 12 ----------- 10 files changed, 52 insertions(+), 134 deletions(-) delete mode 100644 resources/public/index.html create mode 100644 src_clj/sketchbook/views.clj delete mode 100644 src_cljs/sketchbook/core.cljs delete mode 100644 src_cljs/sketchbook/db.cljs delete mode 100644 src_cljs/sketchbook/events.cljs delete mode 100644 src_cljs/sketchbook/subs.cljs diff --git a/project.clj b/project.clj index 59a5dd7..e3b51d4 100644 --- a/project.clj +++ b/project.clj @@ -4,7 +4,6 @@ :min-lein-version "2.0.0" :source-paths ["src_clj"] :dependencies [[org.clojure/clojure "1.8.0"] - [org.clojure/clojurescript "1.10.238"] [compojure "1.6.0"] [ring/ring-defaults "0.3.1"] [ring-server "0.5.0"] @@ -12,25 +11,6 @@ [org.apache.commons/commons-math3 "3.6.1"] [org.clojure/core.async "0.4.474"] [quil "2.6.0"] - [cljs-ajax "0.7.3"] - [reagent "0.8.0-alpha2"] - [re-frame "0.10.5"] - [day8.re-frame/http-fx "0.1.6"]] - :plugins [[lein-ring "0.9.7"] - [lein-figwheel "0.5.15"]] - :cljsbuild {:builds [{:id "dev" - :source-paths ["src_cljs"] - :figwheel true - :compiler {:main "sketchbook.core" - :optimizations :none - :asset-path "js/generated/out" - :output-to "resources/public/js/generated/index.js" - :output-dir "resources/public/js/generated/out"}}]} - :ring {:handler sketchbook.handler/app} - :profiles - {:dev {:source-paths ["src_cljs"] - :dependencies [[javax.servlet/servlet-api "2.5"] - [ring/ring-mock "0.3.0"] - [figwheel-sidecar "0.5.8"] - [com.cemerick/piggieback "0.2.2"]] - :repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}}}) + [hiccup "1.0.5"]] + :plugins [[lein-ring "0.9.7"]] + :ring {:handler sketchbook.handler/app}) diff --git a/resources/public/index.html b/resources/public/index.html deleted file mode 100644 index f579e0b..0000000 --- a/resources/public/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - -
- - - \ No newline at end of file diff --git a/src_clj/sketchbook/handler.clj b/src_clj/sketchbook/handler.clj index e877eaa..af745fb 100644 --- a/src_clj/sketchbook/handler.clj +++ b/src_clj/sketchbook/handler.clj @@ -1,12 +1,13 @@ (ns sketchbook.handler (:require [compojure.core :refer :all] [compojure.route :as route] - [ring.util.response :refer [response content-type]] + [ring.util.response :refer [response content-type resource-response]] [ring.middleware.defaults :refer [wrap-defaults site-defaults]] [ring.middleware.json :refer [wrap-json-response]] [ring.server.standalone :refer [serve]] [clojure.core.async :refer [ app-routes (wrap-defaults site-defaults) - (wrap-json-response) - (wrap-index))) + (wrap-json-response))) (defn -main [& args] (serve app)) diff --git a/src_clj/sketchbook/random.clj b/src_clj/sketchbook/random.clj index 6f5f1be..71a5a45 100644 --- a/src_clj/sketchbook/random.clj +++ b/src_clj/sketchbook/random.clj @@ -5,7 +5,10 @@ (def rng (ISAACRandom.)) (defn set-seed [seed] - (.setSeed rng seed)) + (try + (.setSeed rng seed) + (catch ArrayIndexOutOfBoundsException e + (set-seed seed)))) (defn random ([max] (random 0 max)) diff --git a/src_clj/sketchbook/sketches.clj b/src_clj/sketchbook/sketches.clj index e8482fc..3196585 100644 --- a/src_clj/sketchbook/sketches.clj +++ b/src_clj/sketchbook/sketches.clj @@ -9,18 +9,11 @@ (def sketches {:color {:setup sketchbook.sketches.color/setup :draw sketchbook.sketches.color/draw - :title "Color Play"} + :title "Pretty Colors"} :foo {:setup sketchbook.sketches.color/setup :draw sketchbook.sketches.color/draw :title "Foo"}}) -(defn get-sketches [] - (map (fn [sketch] - (let [path (name (first sketch))] - {:path path - :title (:title (second sketch))})) - sketches)) - (defn sketch-bytes [& {:keys [name size setup draw seed]}] (let [channel (chan) nonce (str (System/nanoTime)) diff --git a/src_clj/sketchbook/views.clj b/src_clj/sketchbook/views.clj new file mode 100644 index 0000000..056114e --- /dev/null +++ b/src_clj/sketchbook/views.clj @@ -0,0 +1,36 @@ +(ns sketchbook.views + (:require [hiccup.core :refer [html]] + [sketchbook.sketches :refer [sketches]])) + +(defn sketch-img + "A sketch image" + [name width height] + (html + [:img {:src (str "/api/sketches/" name "?width=" width "&height=" height) + :width width + :height height}])) + +(defn sketch-thumbnail + "A thumbnail of a sketch" + [title name width height] + (html + [:h1 title] + [:a {:href (str "/" name)} + (sketch-img name width height)])) + +(defn index + "The index page" + [] + (html + (map #(sketch-thumbnail (:title (second %)) + (name (first %)) + 300 + 300) + sketches))) + +(defn sketch-page + "Page for an individual sketch" + [title name] + (html + [:h1 title] + (sketch-img name 768 768))) diff --git a/src_cljs/sketchbook/core.cljs b/src_cljs/sketchbook/core.cljs deleted file mode 100644 index 5a4eaf6..0000000 --- a/src_cljs/sketchbook/core.cljs +++ /dev/null @@ -1,35 +0,0 @@ -(ns sketchbook.core - (:require [reagent.core :as r] - [re-frame.core :refer [dispatch subscribe]] - [sketchbook.events] - [sketchbook.subs])) - -(defn sketch [sketch-item width height] - [:div {:key (:path sketch-item)} - [:h1 (:title sketch-item)] - [:img {:src (str "/api/sketches/" - (:path sketch-item) - "?width=" width - "&height=" height) - :width width - :height height}]]) - -(defn sketches [] - (let [sketch-list @(subscribe [:sketches]) - loading @(subscribe [:loading-sketches])] - (if loading - [:div "Loading..."] - (if (> (count sketch-list) 0) - [:div - (map #(sketch % 300 300) sketch-list)] - [:div "No sketches found"])))) - -(defn app [] - [:div - [sketches]]) - -(dispatch [:initialize]) -(dispatch [:fetch-sketches]) - -(r/render [app] - (.getElementById js/document "app")) diff --git a/src_cljs/sketchbook/db.cljs b/src_cljs/sketchbook/db.cljs deleted file mode 100644 index 9f82022..0000000 --- a/src_cljs/sketchbook/db.cljs +++ /dev/null @@ -1,5 +0,0 @@ -(ns sketchbook.db) - -(def initial-db - {:sketches [] - :loading-sketches false}) diff --git a/src_cljs/sketchbook/events.cljs b/src_cljs/sketchbook/events.cljs deleted file mode 100644 index c58ad7c..0000000 --- a/src_cljs/sketchbook/events.cljs +++ /dev/null @@ -1,27 +0,0 @@ -(ns sketchbook.events - (:require [re-frame.core :refer [reg-event-db reg-event-fx]] - [ajax.core :as ajax] - [day8.re-frame.http-fx] - [sketchbook.db :as db])) - -(reg-event-db - :initialize - (fn [_ _] - db/initial-db)) - -(reg-event-fx - :fetch-sketches - (fn [{db :db} _] - {:http-xhrio {:method :get - :uri "/api/sketches" - :format (ajax/json-request-format) - :response-format (ajax/json-response-format {:keywords? true}) - :on-success [:sketches-fetched]} - :db (assoc db :loading-sketches true)})) - -(reg-event-db - :sketches-fetched - (fn [db [_ response]] - (-> db - (assoc :loading-sketches false) - (assoc :sketches (js->clj response))))) diff --git a/src_cljs/sketchbook/subs.cljs b/src_cljs/sketchbook/subs.cljs deleted file mode 100644 index 5372be7..0000000 --- a/src_cljs/sketchbook/subs.cljs +++ /dev/null @@ -1,12 +0,0 @@ -(ns sketchbook.subs - (:require [re-frame.core :refer [reg-sub]])) - -(reg-sub - :sketches - (fn [db _] - (:sketches db))) - -(reg-sub - :loading-sketches - (fn [db _] - (:loading-sketches db)))