Skip to content

Instantly share code, notes, and snippets.

@micha
Last active March 8, 2016 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micha/3b441cfa6a83b45f8459 to your computer and use it in GitHub Desktop.
Save micha/3b441cfa6a83b45f8459 to your computer and use it in GitHub Desktop.
(page "index.html")
(let [dragging (cell nil)
reorder (fn [items from to]
(let [item (get items from)
[head tail] (split-at from items)
[head tail] (split-at to (into (vec head) (rest tail)))]
(into (vec head) (cons item tail))))]
(defmethod do! :reorderable
[elem k [items i]]
(with-timeout 0
(doto (js/jQuery elem)
(.on "mousedown" #(reset! dragging [@items @i]))
(.on "mousemove" #(when @dragging
(let [[items* from] @dragging]
(reset! items (reorder items* from @i))))))))
(with-init!
(-> (js/jQuery "body")
(.on "mouseup" #(reset! dragging nil)))))
(def indexed (partial map-indexed list))
(defc items ["a" "b" "c" "d" "e"])
(html
(head
(link :href "app.css" :rel "stylesheet"))
(body
(h1 "Hello, Hoplon!")
(ul
(loop-tpl
:bindings [[i item] (cell= (indexed items))]
(li :reorderable [items i] :text item)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment