Last active
December 19, 2015 22:48
-
-
Save swannodette/6029694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn selector | |
"Creates a selection process on list-el, a HTML list element. | |
Parameters | |
in: | |
a channel that should produce a number for the item to | |
highlight, :out to remove all highlighting, and :select to | |
signal user selection. | |
list-el: | |
a HTML list element. | |
data: | |
The values represented by the individual items in the HTML | |
list element. When in signals user selection, the index of | |
of highlighted list item will be used to select an item out | |
of data which will then be written to the output channel. | |
Returns | |
A map of {:chan c0 :control c1}, where c0 is the output channel | |
where user selections will be written and c1 is the control | |
channel to kill the selector process." | |
([in list-el data] | |
(selector (chan) in list-el data)) | |
([c in list-el data] | |
(let [control (chan)] | |
(go | |
(loop [selected ::none] | |
(let [[v sc] (alts! [in control]) | |
items (h/by-tag-name list-el "li")] | |
(cond | |
(= control sc) :done | |
(= v :select) (do (>! c (nth data selected)) | |
(recur selected)) | |
:else (do (when (number? selected) | |
(h/clear-class (nth items selected))) | |
(if (= v :out) | |
(recur ::none) | |
(let [n (if (number? v) v (select items selected v))] | |
(h/set-class (nth items n) "selected") | |
(recur n)))))))) | |
{:chan c | |
:control control}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment