Skip to content
Permalink
4e925a5b65
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
116 lines (99 sloc) 3.33 KB
(ns ulysses.utils
(:require [clojure.string :as string]
[ulysses.config :as config]))
;; general ----------------------
(defn map-subels
"reagent util: apply f to each item in collection,
associating :key metadata (first tries :id of each el;
defaults to default-key-i where i is position in coll)"
[f coll]
(map-indexed
(fn [i el]
^{:key (or (:id el) (str "default-key-" i))}
[f el])
coll))
;; classes ----------------------
(defn classes-vector
"same as classes, but a vector instead of a space-sep string"
[& args]
(reduce
(fn [final h]
(cond
(nil? h) final
(map? h) (into final
(apply classes-vector
(->> h
(vec)
(remove (comp false? second))
(map first))))
(coll? h) (into final (apply classes-vector h))
(keyword? h) (conj final (name h))
(string? h) (conj final h)
:else (conj final (str h))))
[]
args))
(defn classes
"make a string of classes intelligently.
takes: string/keyword args or [recursive] seqs
or maps (for switching).
ex. (classes 'btn' :brown) => 'btn brown'
ex. (classes ['btn' 'brown']) => 'btn brown'
ex. (classes {['btn' 'blue'] true 'brown' false}) => 'btn blue'"
[& args]
(string/join " " (apply classes-vector args)))
(defn classes-attr
"same as classes, but places in {:class CLASSES}"
[& args]
{:class (apply classes args)})
(defn btn-classes
"make string of bootstrap button classes
ex. (btn-classes \"sm\" \"block\") => \"btn btn-sm btn-block\""
[& classes]
(->> classes
(apply classes-vector)
(map (partial str "btn-"))
(cons "btn")
(string/join " ")))
;; dom event handling -----------------
(defn d-p
"default-prevent a click handler (and forward event)"
[f]
(fn [e] (.preventDefault e) (f e)))
;; dom info ---------------------------
(defn window-size []
"get the :width and :height of the current window"
{:width (.-innerWidth js/window)
:height (.-innerHeight js/window)})
(defn document-size
"get the :width and :height of the current document"
[]
(let [d js/document
body (.-body d)
de (.-documentElement d)]
{:width (max
(.-scrollWidth body) (.-offsetWidth body)
(.-clientWidth de) (.-scrollWidth de) (.-offsetWidth de))
:height (max
(.-scrollHeight body) (.-offsetHeight body)
(.-clientHeight de) (.-scrollHeight de) (.-offsetHeight de))}))
(defn window-scroll-position
"get the :y (scrollTop) and :x (scrollLeft) of the current window"
[]
(let [w js/window
de (.-documentElement js/document)]
{:x (or (.-pageXOffset w) (.-scrollLeft de) 0)
:y (or (.-pageYOffset w) (.-scrollTop de) 0)}))
;; misc helpers ------------------------
(defn mechanism-to-contextual
"mechanism to contextual (as keyword) helper
ex. :nsf => :info"
[mechanism]
(mechanism config/mechanism-contextuals))
;; data filtering ----------------------
(defn filter-grant-ops
[grant-ops filters]
(let [{:keys [search mechanisms]} filters
search-lower (string/lower-case search)]
(->> grant-ops
(filter #(get mechanisms (-> % :funding-mechanism :name keyword)))
(filter #(-> % :company string/lower-case (string/includes? search-lower))))))