Permalink
Browse files

Update timeago for years, nil-guard dom functions

  • Loading branch information...
1 parent 050c4b1 commit dc457558bd6ed834d9a33056a1b7226adedcdab7 @zk committed Nov 11, 2013
Showing with 44 additions and 27 deletions.
  1. +22 −19 src/clj/nsfw/util.clj
  2. +22 −8 src/cljs/nsfw/dom.cljs
View
@@ -43,6 +43,8 @@
(let [d (Date. ms)]
(.format (SimpleDateFormat. format) d)))
+(defn day-suffix [])
+
(defn web-stacktrace [e req]
(str "<html><body>"
"<h1>500 - " (.getMessage e) "</h1>"
@@ -216,24 +218,25 @@
(defn ms [date]
(cond
- (= java.util.Date (class date)) (.getTime date)
- :else (.getMillis date)))
+ (= java.util.Date (class date)) (.getTime date)
+ (= org.joda.time.DateTime (class date)) (.getTime date)))
(defn timeago [date-or-ms]
- (let [ms (if (number? date-or-ms)
- (- (now) date-or-ms)
- (- (now) (ms date-or-ms)))
- s (/ ms 1000)
- m (/ s 60)
- h (/ m 60)
- d (/ h 24)
- y (/ d 365)]
- (cond
- (< s 60) "less than a minute"
- (< m 2) "1 minute"
- (< h 1) (str (int m) " minutes")
- (< h 2) "1 hour"
- (< d 1) (str (int h) " hours")
- (< d 2) "1 day"
- (< y 1) (str (int d) " days")
- :else "over a year")))
+ (when date-or-ms
+ (let [ms (if (number? date-or-ms)
+ (- (now) date-or-ms)
+ (- (now) (ms date-or-ms)))
+ s (/ ms 1000)
+ m (/ s 60)
+ h (/ m 60)
+ d (/ h 24)
+ y (/ d 365.0)]
+ (cond
+ (< s 60) "less than a minute"
+ (< m 2) "1 minute"
+ (< h 1) (str (int m) " minutes")
+ (< h 2) "1 hour"
+ (< d 1) (str (int h) " hours")
+ (< d 2) "1 day"
+ (< y 1) (str (int d) " days")
+ :else (str (format "%.1f" y) " years")))))
View
@@ -113,8 +113,9 @@
(defn val
([el]
- (let [res (forms/getValue (unwrap el))]
- (when-not (empty? res) res)))
+ (when el
+ (let [res (forms/getValue (unwrap el))]
+ (when-not (empty? res) res))))
([el new-value]
(doseq [el (ensure-coll el)]
(forms/setValue el new-value))
@@ -239,12 +240,14 @@
(defn add-class [els cls]
(doseq [el (ensure-coll els)]
- (classes/add el (name cls)))
+ (when el
+ (classes/add el (name cls))))
els)
(defn rem-class [els cls]
(doseq [el (ensure-coll els)]
- (classes/remove el (name cls)))
+ (when el
+ (classes/remove el (name cls))))
els)
(defn tog-class [els cls]
@@ -264,10 +267,22 @@
(defn onload [f]
(set! (.-onload js/window) f))
+(def listen-error-handler (atom nil))
+
+(defn set-listen-error-handler [f]
+ (reset! listen-error-handler f))
+
(defn listen [els evt f]
- (doseq [el (ensure-coll els)]
- (events/listen el (name evt) #(f (ge->map %) el)))
- els)
+ (when els
+ (doseq [el (ensure-coll els)]
+ (events/listen el (name evt)
+ #(try
+ (f (ge->map %) el)
+ (catch js/Error e
+ (if @listen-error-handler
+ (@listen-error-handler e)
+ (throw e))))))
+ els))
(defn handler [evt]
(fn
@@ -697,7 +712,6 @@
(re-find #"application/edn" content-type)
(reader/read-string resp))))]
(if (.isSuccess req)
- ;; maybe pull js->clj
(success data req)
(error data req)))
(catch js/Object e

0 comments on commit dc45755

Please sign in to comment.