Last active
December 15, 2018 04:20
-
-
Save Conaws/0e8f6860c57be50f5edf7c0c636d478d 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
(ns cards.sortable | |
(:require [reagent.core :as reagent]) | |
(:require-macros | |
[cljs.test :refer [is testing]] | |
[devcards.core :refer [defcard-rg deftest]])) | |
(defonce app-state (reagent/atom {:list (vec (take 11 "abcdefghijkl")) | |
:over -1 | |
:dragging false})) | |
(defn splice [x vctr pstn] | |
(let [vctr (vec (filter #(not (= x %)) vctr)) | |
start (subvec vctr 0 pstn) | |
end (subvec vctr pstn)] | |
(vec (concat (conj start x) end)))) | |
(defn placeholder [i v] | |
[:li {:style {:background "rgb(255,240,120)"}} | |
v] | |
) | |
(defn without [x v] | |
(filter #(not (= x %)) v) | |
) | |
(defn replace-v [v outgoing incoming] | |
(let [xpos (.indexOf v outgoing)] | |
(assoc v xpos incoming)) | |
) | |
(defn swap-in-vector [v a b] | |
(-> (without b v) | |
vec | |
(replace-v a b) | |
)) | |
(defn listitem [i v s] | |
(fn [i v s] | |
(if (= :placeholder v) | |
[placeholder (:dragging @s) v] | |
[:li {:data-id i | |
:draggable true | |
:style {:display | |
(if (= v (:dragging @s)) | |
"none" | |
) | |
:background-color "green" | |
:border "2px solid white" | |
:margin "5px" | |
:opacity | |
(if (:dragging @s) | |
"0.9" | |
"1")} | |
:on-drag-enter (fn [e] | |
(swap! s update :list (fn [l] | |
(splice :placeholder | |
l | |
i)))) | |
:on-drag-start (fn [e] | |
(swap! s assoc :dragging v | |
:oldlist (:list @s))) | |
} | |
v]))) | |
(defn list-render [s] | |
(fn [s] | |
[:ol {:style {:border "2px solid blue"} | |
:on-drag-end (fn [e] | |
(do | |
(swap! s update :list (fn [l] | |
(swap-in-vector | |
l | |
:placeholder | |
(:dragging @s)))) | |
(swap! s assoc | |
:over false | |
:dragging false)))} | |
(for [[i v] (map-indexed vector (:list @s))] | |
^{:key i}[listitem i v s])])) | |
(defcard-rg listcard | |
[list-render app-state] | |
app-state | |
{:inspect-data true | |
:history true}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment