newseasons / src / newseasons / templates / main.clj

(ns newseasons.templates.main
  (:require [noir.session :as sess])
  (:use noir.core

(def ga "
  <script type=\"text/javascript\">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-15328874-7']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);


; Utils -----------------------------------------------------------------------
(defn include-less [href]
  [:link {:rel "stylesheet/less" :type "text/css" :href href}])

(defn field [fieldfn field-name label]
  (list [:label {:for (str "id_" field-name)} label]
    (fieldfn field-name)))

(defn pluralize
  ([coll] (pluralize coll "s"))
  ([coll plural-suffix] (if (= 1 (count coll))

; Layout ----------------------------------------------------------------------
(defpartial base [body-class & content]
     (map include-css ["/css/base.css"
     (include-less "/css/style.less")
     (include-js "/js/less.js")
     [:title "New Seasons"]]
    [:body {:class body-class}
      [:header.sixteen.columns [:h1 (link-to "/" "New Seasons")]]
      (when-let [message (sess/flash-get)]
         [:p message]])
        "Made by "
        (link-to "" "Steve Losh")
        " with "
        (link-to "" "Noir")
        ". "
        "It&apos;s "
        (link-to "" "open source")

(defpartial inner [title body-class & content]
  (base body-class
      (form-to [:post "/logout"]
        (submit-button "Log Out"))
      (form-to [:get "/password"]
        (submit-button "Change Password"))]
    (when-not (= body-class "user")
      [:p.sixteen.columns (link-to "/" "&larr; Back to Dashboard")]) 
     "Don't want to receive any more notifications? "
     "You can "
     (link-to "/delete-account" "delete your account")

; Pages -----------------------------------------------------------------------
(defpartial home []
  (base "home"
         [:form {:action "" :method "POST"}
          (field text-field "email" "Email Address")
          (field password-field "password" "Password")
          (submit-button "Log In or Create Account")]]
         [:p "New Seasons will notify you when your favorite TV "
          "shows have new seasons on iTunes."]]
         [:p "Add some shows to your list, we'll email you when new seasons come out.  That's it."]]))

(defpartial user-show [show]
   (form-to [:post "/rem"]
     [:input {:type "hidden" :name "artist-id" :value (show :id)}]
     (submit-button "Remove"))
   [:img {:src (show :image)}]
   [:h3 (link-to (show :url) (show :title))]
   [:p.latest "Latest: " (show :latest)]])

(defpartial user [user]
  (inner (str "Hello, " (:email user)) "user"
     [:form {:action "/search"}
      (field text-field
        "Which show do you want to keep track of?")
      (submit-button "Search")]]
     (let [shows (:shows user)]
       (if (empty? shows)
         [:p "You're not currently watching any shows."]
            "You're watching "
            (count shows)
            " show"
            (pluralize shows)
           [:ul.shows (map user-show shows)])))]))

(defpartial result [r]
   [:img {:src (r "artworkUrl100")}]
   [:h3 (link-to (r "artistViewUrl") (r "artistName"))]
   [:p.latest "Latest: " (r "collectionName")]
   (form-to [:post "/add"]
     [:input {:type "hidden" :name "artist-id" :value (r "artistId")}]
     (submit-button "Add Show to List"))])

(defpartial search [query results]
  (inner (str (if (empty? results)
                (count results))
           " Result" (pluralize results)
           " for &ldquo;" query "&rdquo;")
     (map result results)]))

(defpartial password []
  (inner "Change Your Password" "change-password"
     (form-to [:post ""]
       (field password-field "password" "New Password")
       (submit-button "Change Password"))]))

(defpartial delete-account []
  (inner "Delete Your Account" "delete-account"
     [:p "If you're sure about this you can delete your account here."]
     (form-to [:post ""]
       (submit-button "Delete Account"))]))