Fork me on GitHub
#hoplon
<
2018-01-17
>
thedavidmeister07:01:26

> Avoids some of the code-walking problems of the Hoplon macros.

thedavidmeister07:01:32

which problems and what macros?

thedavidmeister09:01:10

@flyboarder i think you might have missed some places that when-dom is wrapping on! >.<

thedavidmeister09:01:56

yay, this passes!

thedavidmeister09:01:00

(defn expandable
 [item]
 (let [expand? (j/cell false)]
  (h/div
   :data-expanded expand?
   :click #(swap! expand? not)
   (j/cell= (name item)))))

(deftest ??for-tpl--not-sortable
 (let [items (j/cell [:a :b :c])
       el (h/div (h/for-tpl [i items] (expandable i)))
       first-child (first (hoplon.test-util/find el "div"))]
  (is (not (.querySelector el "[data-expanded]")))

  (hoplon.test-util/trigger! first-child "click")
  (is (= "a" (hoplon.test-util/text first-child)))
  (is (hoplon.test-util/matches first-child "[data-expanded]"))

  ; c should be expanded (it is positional in for-tpl)
  ; first-child should still reference the first child (nothing moves)
  ; the item text should be in reverse order (it is re-derived in expandable)
  ; event handlers should not break
  (swap! items reverse)
  (is (= "c" (hoplon.test-util/text first-child)))
  (is (hoplon.test-util/matches first-child "[data-expanded]"))

  (hoplon.test-util/trigger! first-child "click")
  (is (not (hoplon.test-util/matches first-child "[data-expanded]")))))

(deftest ??keyed-for-tpl--sortable
 (let [items (j/cell [:a :b :c])
       el (h/div (h/keyed-for-tpl identity [i items] (expandable i)))
       first-child (first (hoplon.test-util/find el "div"))]
  (is (not (.querySelector el "[data-expanded]")))

  (hoplon.test-util/trigger! first-child "click")
  (is (= "a" (hoplon.test-util/text first-child)))
  (is (hoplon.test-util/matches first-child "[data-expanded]"))

  ; a should be expanded
  ; first-child should be a reference to the last child now (because it moved)
  ; the items should be in reverse order
  ; event handlers should not break
  (swap! items reverse)
  (is (= ["c" "b" "a"] (map hoplon.test-util/text (hoplon.test-util/find el "div"))))
  (is (= "a" (hoplon.test-util/text first-child)))
  (is (hoplon.test-util/matches first-child "[data-expanded]"))

  (hoplon.test-util/trigger! first-child "click")
  (is (not (hoplon.test-util/matches first-child "[data-expanded]")))))

thedavidmeister09:01:27

keyed-for-tpl seems awesome

thedavidmeister09:01:08

could do with a slight refactor, but it does indeed seem to make DOM elements track the position of list items by keyfn

thedavidmeister10:01:56

@flyboarder reckon we could get a WIP from https://github.com/hoplon/hoplon/pull/231/files into a snapshot so that i could play around with it in context of an app?