typkov / src / typkov / templates.clj

(ns typkov.templates
  (:require [noir.validation :as valid])
  (:use [noir.core :only [defpartial]]
        [hiccup.form-helpers :only [hidden-field]]
        [ :only [include-css html5 include-js link-to]]))

; Utils -----------------------------------------------------------------------
(def gauges "
  <script type='text/javascript'>
    var _gauges = _gauges || [];
    (function() {
      var t   = document.createElement('script');
      t.type  = 'text/javascript';
      t.async = true;    = 'gauges-tracker';
      t.setAttribute('data-site-id', '4f1b29ce613f5d7d9b000001');
      t.src = '//';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(t, s);

(defpartial select [id name options selected]
  [:select {:name name :id id}
   (map (fn [o]
          [:option (merge {:value o}
                          (when (= selected o) {:selected "selected"}))

(defpartial error-item [[first-error]]
  [:div.alert-message.error first-error])

; Base ------------------------------------------------------------------------
(defpartial base [title & content]
     [:title title]
     (include-css "/css/bootstrap.css")
     [:link {:rel "stylesheet/less" :type "text/css" :href "/css/style.less"}]
     (include-js "/js/less.js")]
       [:h1 (link-to "/" "Typkov")]
        "Create typing lessons from your own writing!"]]
        "Created by "
        (link-to "" "Steve Losh")
        "It's open source "
        (link-to "" "on BitBucket")
        " and "
        (link-to "" "on GitHub")

; Home ------------------------------------------------------------------------
(defpartial main-copy []
     "Typing practice is great, but wouldn't it be wonderful to "
     "practice with the kind of words you usually use in your own writing?"]
     "Enter some text and we'll use it to create a "
     (link-to "" "gtypist")
     " or "
     (link-to "" "ktouch")
     " lesson file for you."]))

(defpartial home-form [text num format]
    [:form {:method "post" :action "" :class "form-stacked"}
      [:label {:for "text"}
       "Paste in some text you've written:"]
      (valid/on-error :text error-item)
      [:textarea#text {:name "text"} text]]
      [:label {:for "format"}
       "Which typing program do you use?"]
      (valid/on-error :format error-item)
      (select "format" "format" ["gtypist" "ktouch"] format)]
      [:label {:for "num"}
       "How many drills do you want?"]
      (valid/on-error :num error-item)
      (select "num" "num" (range 10 51 10) num)]
     [:button {:type "submit" :class "btn primary"} "Get a Lesson"]]))

(defpartial usage-gtypist []
   "Copy all of the following into a "
   [:code "whatever.typ"]
   " file and run "
   [:code "gtypist whatever.typ"]
   " to use it!"])

(defpartial usage-ktouch []
   "Copy all of the following into a "
   [:code "whatever.ktouch"]
   " file and load it up in ktouch to use it!"])

(defpartial home-lesson [text num format lesson]
    (case format
      "gtypist" (usage-gtypist)
      "ktouch" (usage-ktouch))
     [:textarea.lesson lesson]]
    [:form {:method "post" :action ""}
     [:button.btn {:type "submit"} "Start Over"]
     (hidden-field "restart" "1")
     (hidden-field "text" text)
     (hidden-field "num" num)
     (hidden-field "format" format)]))

(defpartial home [text num format lesson]
  (base "Typkov"
        (if lesson
          (home-lesson text num format lesson)
          (home-form text num format))))