/examples/e01_loom_formatters_and_actions.clj

https://github.com/vlaaad/reveal · Clojure · 61 lines · 37 code · 11 blank · 13 comment · 1 complexity · 045a5aa8eac96b31c0baf2d89b646406 MD5 · raw file

  1. (ns e01-loom-formatters-and-actions
  2. (:require [vlaaad.reveal.ext :as rx]
  3. [loom.graph :as g])
  4. (:import [loom.graph BasicEditableGraph]))
  5. ;; This example adds formatting and navigation support for loom: a clojure
  6. ;; library for building graph data structures.
  7. ;; see https://github.com/aysylu/loom
  8. ;; by default we only show graph nodes when displaying the graph:
  9. (rx/defstream BasicEditableGraph [{:keys [nodeset] :as graph}]
  10. (rx/horizontal
  11. (rx/raw-string "#loom/graph[" {:fill :object})
  12. (rx/items nodeset {::graph graph})
  13. (rx/raw-string "]" {:fill :object})))
  14. ;; define an action on a selected node when it has access to graph:
  15. (rx/defaction ::loom:successors [v ann]
  16. (when-let [g (::graph ann)]
  17. (fn []
  18. ;; we want to stream this custom formatting as is, not as a function value
  19. (rx/stream-as-is
  20. ;; forward the graph to every successor
  21. (rx/vertically (g/successors g v) {::graph g})))))
  22. ;; define an action on a node to see its graph:
  23. (rx/defaction ::loom:graph [v ann]
  24. (when-let [g (::graph ann)]
  25. (constantly g)))
  26. ;; define an action on a graph to see its edges:
  27. (rx/defaction ::loom:edges [v]
  28. (when (satisfies? g/Graph v)
  29. (fn []
  30. (rx/stream-as-is
  31. (rx/vertically
  32. (map (fn [[from to]]
  33. ;; since vertically works on values, not sfs, we want to make these sfs
  34. ;; streamed as is too
  35. (rx/stream-as-is
  36. (rx/horizontal
  37. ;; forward the graph
  38. (rx/stream from {::graph v})
  39. rx/separator
  40. (rx/raw-string "->" {:fill :util :selectable false})
  41. rx/separator
  42. (rx/stream to {::graph v}))))
  43. (g/edges v)))))))
  44. ;; graph of associations to explore:
  45. (g/graph [:food :takoyaki]
  46. [:food :drinks]
  47. [:takoyaki :japan]
  48. [:drinks :coca-cola]
  49. [:drinks :genmaicha]
  50. [:genmaicha :japan])