001  (ns ^{:doc "Routes which serve the main pages of the application."
002        :author "Simon Brooke"}
003    mw-ui.routes.home
004    (:require [clojure.java.io :refer [file]]
005              [clojure.walk :refer [keywordize-keys]]
006              [compojure.core :refer [defroutes GET POST]]
007              [hiccup.core :refer [html]]
008              [mw-engine.utils :as engine-utils]
009              [mw-ui.layout :as layout]
010              [mw-ui.render-world :as world]
011              [mw-ui.routes.load :as load]
012              [mw-ui.routes.rules :as rules]
013              [mw-ui.routes.params :as params]
014              [mw-ui.routes.save :as save]
015              [mw-ui.util :as util]
016              [noir.session :as session]
017              [ring.util.response :as response]))
018  
019  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
020  ;;;;
021  ;;;; mw-ui: a servlet user/visualisation interface for MicroWorld.
022  ;;;;
023  ;;;; This program is free software; you can redistribute it and/or
024  ;;;; modify it under the terms of the GNU General Public License
025  ;;;; as published by the Free Software Foundation; either version 2
026  ;;;; of the License, or (at your option) any later version.
027  ;;;;
028  ;;;; This program is distributed in the hope that it will be useful,
029  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
030  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031  ;;;; GNU General Public License for more details.
032  ;;;;
033  ;;;; You should have received a copy of the GNU General Public License
034  ;;;; along with this program; if not, write to the Free Software
035  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
036  ;;;; USA.
037  ;;;;
038  ;;;; Copyright (C) 2014 Simon Brooke
039  ;;;;
040  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
041  
042  
043  (defn list-states []
044    (sort
045      (filter #(not (nil? %))
046              (map #(first (rest (re-matches #"([0-9a-z-]+).png" (.getName %))))
047                   ;; TODO: this will not work when running from jar; see utils.clj
048                   (file-seq (file "resources/public/img/tiles"))))))
049  
050  
051  (defn about-page []
052    (layout/render "trusted-content.html"
053                   {:title "About MicroWorld"
054                    :about-selected "active"
055                    :content (util/md->html "/md/about.md")
056                    :version (System/getProperty "mw-ui.version")}))
057  
058  (defn docs-page []
059    (layout/render "docs.html" {:title "Documentation"
060                                :parser (util/md->html "/md/mw-parser.md" )
061                                :states (util/list-resources "/img/tiles" #"([0-9a-z-_]+).png")
062                                :lessons (util/list-resources "/md/lesson-plans"  #"([0-9a-z-_]+).md")
063                                :components ["mw-engine" "mw-parser" "mw-ui"]
064                                :version (System/getProperty "mw-ui.version")}))
065  
066  (defn home-page 
067    "Render the home page."
068    []
069    (layout/render "trusted-content.html" {:title "Welcome to MicroWorld"
070                                :content (util/md->html "/md/mw-ui.md")
071                                :version (System/getProperty "mw-ui.version")}))
072  
073  (defn inspect-page 
074    "Open an inspector on the cell at the co-ordinates specified in this request"
075    [request]
076    (let [params (keywordize-keys (:params request))
077          xs (:x params)
078          ys (:y params)
079          x (if (seq xs) (read-string xs) 0)
080          y (if (seq ys) (read-string ys) 0)
081          world (session/get :world)
082          cell (engine-utils/get-cell world x y)
083          state (:state params)]
084      (cond state
085        (do
086          (session/put! :world (engine-utils/set-property world cell :state (keyword state)))
087          (response/redirect "world"))
088        true
089        (layout/render "inspector.html"
090                       {:title (format "Inspect cell at %d, %d" x y)
091                        :content (html (world/render-inspector cell))
092                        :cell cell
093                        :x (:x cell)
094                        :y (:y cell)
095                        :states (util/list-resources
096                                  "/img/tiles" #"([0-9a-z-_]+).png")}))))
097  
098  (defn md-page
099    "Render the markdown page specified in this request, if any. Probably undesirable,
100     should be removed."
101    [request]
102    (let [params (keywordize-keys (:params request))
103          content (or (:content params) "missing.md")]
104      (layout/render "trusted-content.html"
105                     {:title "Welcome to MicroWorld"
106                      :content (util/md->html (str "/md/" content))})))
107  
108  (defn world-page []
109    "Render the world in the current session (or a default one if none)."
110    (layout/render "trusted-content.html"
111                   {:title "Watch your world grow"
112                   :world-selected "active"
113                   :content (html (world/render-world-table))
114                   :pause (or (session/get :pause) 5)
115                   :maybe-refresh "refresh"}))
116  
117  
118  (defroutes home-routes
119    (GET  "/" [] (home-page))
120    (GET  "/about" [] (about-page))
121    (GET  "/docs"  [] (docs-page))
122    (GET  "/inspect" request (inspect-page request))
123    (POST "/inspect" request (inspect-page request))
124    (GET  "/load" [] (load/load-page))
125    (POST "/load" request (load/load-page request))
126    (GET  "/md" request (md-page request))
127    (GET  "/params" [] (params/params-page))
128    (POST "/params" request (params/params-page request))
129    (GET  "/rules" request (rules/rules-page request))
130    (POST "/rules" request (rules/rules-page request))
131    (GET  "/saved-map.mwm" [] (save/save-page))
132    (GET  "/world"  [] (world-page))
133    )