(ns dmr.views (:require [re-frame.core :as re-frame] [clojure.string :as string] [dmr.styles :as styles] [dmr.subs :as subs] [dmr.events :as events])) (defn prompt [] (let [cmdline (re-frame/subscribe [::subs/cmdline]) update-cmdline #(re-frame/dispatch [::events/update-cmdline (-> % .-target .-value)]) submit-cmd #((.preventDefault %) (re-frame/dispatch [::events/submit-cmd]))] [:form {:class (styles/prompt-style) :on-submit submit-cmd} [:input {:value @cmdline :on-change update-cmdline :type "text"}]])) (defn title [] (let [name (re-frame/subscribe [::subs/name])] [:h1 "Welcome to " @name])) ; TODO: move this functionto a separate 'utils' module (defn ordinal [n] (let [final-digit (mod n 10) suffix (case final-digit 0 "th" 1 "st" 2 "nd" 3 "rd" "th")] (str n suffix))) (defn- join-lines [coll] (string/join \newline coll)) (defn- comma-separated [coll] (string/join ", " coll)) (defn- parenthesize [s] (when s (str "(" s ")"))) (defn spell-panel [spell] [:article {:class (styles/spell-panel)} [:h1 (:name spell)] [:div [:span.level (-> spell :level ordinal) " level"] [:span.school "school of " (-> spell (get-in [:school :name]))]] [:ul {:class (styles/property-list)} ; Refactor these so that we get the "name" from the keyword by replacing ; '-' and '_' and capitalizing [:li [:span "Casting Time"] [:span (-> spell :casting_time)]] [:li [:span "Range"] [:span (-> spell :range)]] [:li [:span "Components"] [:span (comma-separated (-> spell :components))] [:span (parenthesize (-> spell :material))]] [:li [:span "Duration"] [:span (-> spell :duration)]]] [:section.description (-> spell :desc join-lines)] [:section.higher-level [:span "At higher levels"] [:span (-> spell :higher_level join-lines)]]]) (defn history [] (let [history (re-frame/subscribe [::subs/cmd-history])] (into [:ul {:class (styles/history-style)}] (map #(vector :li [:div.input (:input %)] [:div.output (-> % :output spell-panel)]) @history)))) (defn main-panel [] [:div {:class (styles/screen)} [:div {:class (styles/main-panel)} (title) (history) (prompt)]])