Skip to content

Commit

Permalink
Move much Sodium code to new library, Iron
Browse files Browse the repository at this point in the history
  • Loading branch information
deg committed Nov 25, 2017
1 parent 4e211f6 commit 28a6534
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 267 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Sodium

## ALERT

Sodium 0.9.0-SNAPSHOT introduces incompatible changes.

I have divided Sodium into two libraries. Sodium retains the Soda-ash/Semantic-UI
code. But, all the more general utilities have been moved to a new library,
[Iron](https://github.com/deg/iron). There will be teething pains, so I recommend
avoiding these snapshot version unless you want to help me stabilize. I hope to have a
new version up and stable within a few days.

## Introduction

Sodium is a wrapper around [soda-ash](https://github.com/gadfly361/soda-ash) which,
Expand Down
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[org.clojure/clojurescript "1.9.946"]
[re-frame "0.10.2"]
[reagent "0.7.0"]
[soda-ash "0.76.0"]]
[soda-ash "0.76.0"]
[com.degel/iron "0.1.0-SNAPSHOT"]]
:plugins [[lein-cljsbuild "1.1.7"]
[lein-doo "0.1.8"]]
:cljsbuild
Expand Down
15 changes: 0 additions & 15 deletions src/sodium/chars.cljs

This file was deleted.

14 changes: 0 additions & 14 deletions src/sodium/chrome_utils.cljs

This file was deleted.

113 changes: 33 additions & 80 deletions src/sodium/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,37 @@
[clojure.spec.alpha :as s]
[re-frame.loggers :refer [console]]
[soda-ash.core :as sa]
[sodium.re-utils :refer [<sub >evt]]
[iron.re-utils :refer [<sub >evt]]
[iron.utils :refer [validate camelize-map-keys negligible?]]
[sodium.utils :as utils]))

(s/def :sodium/size #{:tiny :small :medium :large :huge})

(defn- negligible?
[x]
(if (seqable? x) (empty? x) (not x)))

(defn >event
"Return a function suitable for an on-* handler, to deliver the value
into into a re-frame event. See also >atom.
The first argument is a re-frame event vector, into which the value
will be conjed as the final element.
It is followed by two optional arguments: a default value that will
be used when the value is empty, and a 'coercer' function to convert
the value into a suitable form for the event.
Note that the default value is _not_ passed through the coercer."
;; [TODO] The line about the default value is obviously wrong. Check users and fix!
([event]
(>event event ""))
([event default]
(>event event default identity))
([event default coercer]
#(>evt (let [value (or (.-value %2) (.-checked %2))]
(conj event
(coercer (if (negligible? value)
default
value)))))))

(defn >events
"Utility function to dispatch multiple events from an on-* hander.
The syntax is a bit opaque, because we have to wrap both the event
parameters and the parameters to >event (default and coercer).
So, a usage looks like:
(na/>events [[[:update-age :in-minutes] 42]
[[:set-color] :cyan nearest-color]])
"
[events]
(fn [dom-event param-map]
(run! (fn [event]
((apply >event event) dom-event param-map))
events)))

(defn >atom
"Return a function suitable for an on-* handler, to deliver the value
into into an atom. This would typically be used to store a result into
a local reagent atom. See also >event.
The first argument is an atom, which the value will be reset! into.
It is followed by two optional arguments: a default value that will
be used when the value is empty, and a 'coercer' function to convert
the value into a suitable form for the event.
Note that the default value is _not_ passed through the coercer."
;; [TODO] Default wrong here too
([atom]
(>atom atom ""))
([atom default]
(>atom atom default identity))
([atom default coercer]
#(->> (or (.-value %2) (.-checked %2))
js->clj
coercer
(reset! atom))))

(defn value
"A bit hackish, but I think this is enough to get the useful value
from any Semantic-UI-React element."
[_dom_event data]
(js->clj
(or (.-value data) (.-checked data))))

(defn value->event-fn
"Return a function that will collect the value from a
react-semantic-ui dom event and pass it to a re-frame event"
([event] (value->event-fn event {}))
([event {:keys [default coercer] :as >evt-params}]
(fn [dom_event data]
(>evt event (value dom_event data) >evt-params))))

(defn value->atom-fn
"Return a function that will collect the value from a
react-semantic-ui dom event and pass it into an atom"
([atom] (value->atom-fn atom {}))
([atom {:keys [default coercer] :or {coercer identity}}]
(fn [dom_event data]
(let [value (value dom_event data)]
(reset! atom (coercer (if (negligible? value)
default
value)))))))

(defn list-option
"Convert value and text info format suitable for a React list element
Expand All @@ -90,18 +55,6 @@
(list-option (value-fn item) (text-fn item)))
items))

(defn <atom
"Get a value from an atom. Suitable to use, e.g., as the :value or
:default-value parameter to a component.
Atom is the atom to be dereferenced. It will be translated by access-fn.
If null, default will be supplied instead."
([atom]
(<atom atom nil))
([atom default]
(<atom atom default identity))
([atom default access-fn]
(or (access-fn @atom) default)))


;;; Components that we supply so far. More coming soon.
;;; The final argument here is the descriptor groups of the acceptable parameters.
Expand Down Expand Up @@ -149,19 +102,19 @@
(defcomponent form-button [params]
{::key-sets [:basic :form-field :button]
::keys [data-tooltip]
:pre [(utils/validate (s/nilable ifn?) (:on-click params))
(utils/validate (s/nilable string?) (:data-tooltip params))]}
:pre [(validate (s/nilable ifn?) (:on-click params))
(validate (s/nilable string?) (:data-tooltip params))]}
[sa/FormButton (-> params
(update :type #(or % "button"))
(utils/camelize-map-keys :exclude [:data-tooltip]))])
(camelize-map-keys :exclude [:data-tooltip]))])

(defcomponent button [params]
{::key-sets [:basic :button]
::keys [data-tooltip]
:pre [(utils/validate (s/nilable ifn?) (:on-click params))
(utils/validate (s/nilable string?) (:data-tooltip params))]}
:pre [(validate (s/nilable ifn?) (:on-click params))
(validate (s/nilable string?) (:data-tooltip params))]}
[sa/Button (-> params
(update :type #(or % "button"))
(utils/camelize-map-keys :exclude [:data-tooltip]))])
(camelize-map-keys :exclude [:data-tooltip]))])


23 changes: 12 additions & 11 deletions src/sodium/extensions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
[reagent.core :as reagent]
[re-frame.core :as re-frame]
[re-frame.loggers :refer [console]]
[iron.re-utils :refer [<sub >evt]]
[iron.utils :refer [ci-sort validate]]
[sodium.core :as na]
[sodium.re-utils :refer [<sub >evt]]
[sodium.utils :as utils]))


Expand All @@ -17,9 +18,9 @@
;;; Various page and section headers/dividers

(defn- header-maker [title size dividing? sub?]
{:pre [(utils/validate (s/or :string string? :event vector?) title)
(utils/validate boolean? dividing?)
(utils/validate :sodium/size size)]}
{:pre [(validate (s/or :string string? :event vector?) title)
(validate boolean? dividing?)
(validate :sodium/size size)]}
(na/header {:content (if (vector? title)
(str (<sub title))
title)
Expand Down Expand Up @@ -152,7 +153,7 @@
:selected-class selected-class
:unselected-class unselected-class})
(if sort?
(utils/ci-sort tags)
(ci-sort tags)
tags)))])


Expand All @@ -176,7 +177,7 @@
(fn []
(let [all-tags (<sub all-tags-sub)
selected-tags (<sub selected-tags-sub #{})
available-tags (utils/ci-sort (clojure.set/difference all-tags selected-tags))
available-tags (ci-sort (clojure.set/difference all-tags selected-tags))
list-id (str (gensym "tags-"))
input-id (str (gensym "tags-input-"))]
[na/grid {:container? true}
Expand All @@ -194,8 +195,8 @@
;; [???] Setting :value fails subtly, updating datalist options for
;; the previous character entered as each character is entered.
;; So, :default-value and pay the piper below
:default-value (na/<atom partial-tag-text "")
:on-change (na/>atom partial-tag-text)
:default-value (or @partial-tag-text "")
:on-change (na/value->atom-fn partial-tag-text)
:action (when-not (empty? @partial-tag-text)
{:icon "add"
:on-click #(let [tag (conj selected-tags @partial-tag-text)]
Expand All @@ -218,12 +219,12 @@
:or {all-tags-sub [::all-tags]
selected-tags-sub [::selected-tags]
set-selected-tags-event [::selected-tags]}}]
(let [available-tags (utils/ci-sort (<sub all-tags-sub))
chosen-tags (utils/ci-sort (<sub selected-tags-sub #{}))]
(let [available-tags (ci-sort (<sub all-tags-sub))
chosen-tags (ci-sort (<sub selected-tags-sub #{}))]
[na/dropdown {:multiple? true
:button? true
:value chosen-tags
:on-change (na/>event set-selected-tags-event #{} set)
:on-change (na/value->event-fn set-selected-tags-event {:default #{} :coercer set})
:options (na/dropdown-list available-tags identity identity)}]))


Expand Down
16 changes: 9 additions & 7 deletions src/sodium/macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(ns sodium.macros
(:require
[clojure.spec.alpha :as s]
[iron.utils :refer [camelize-map-keys validate vconcat]]
[sodium.utils :as utils]
[sodium.keys :as keys]))

Expand Down Expand Up @@ -32,18 +33,19 @@
[name [params-var & other-params]
{:keys [pre post :sodium.core/keys :sodium.core/key-sets]}
& component-body]
{:pre [(utils/validate symbol? name)
(utils/validate symbol? params-var)]}
{:pre [(validate symbol? name)
(validate symbol? params-var)]}
(let [all-keys (merge-keys keys key-sets)]
`(defn ~name [{:keys ~all-keys :as ~params-var} ~@other-params]
{:pre ~(utils/vconcat pre
`[(utils/all-keys-valid? (keys ~params-var)
~(set (map keyword all-keys)))])
{:pre ~(vconcat pre
`[(utils/all-keys-valid?
(keys ~params-var)
~(set (map keyword all-keys)))])
:post ~post}
~@component-body)))

(defmacro def-simple-component [name parent key-sets]
`(defcomponent ~name [params# & body#]
{:sodium.core/key-sets ~key-sets}
(utils/vconcat [~parent (utils/camelize-map-keys params#)]
body#)))
(vconcat [~parent (camelize-map-keys params#)]
body#)))
59 changes: 0 additions & 59 deletions src/sodium/re_utils.cljs

This file was deleted.

Loading

0 comments on commit 28a6534

Please sign in to comment.