/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
- (ns e01-loom-formatters-and-actions
- (:require [vlaaad.reveal.ext :as rx]
- [loom.graph :as g])
- (:import [loom.graph BasicEditableGraph]))
- ;; This example adds formatting and navigation support for loom: a clojure
- ;; library for building graph data structures.
- ;; see https://github.com/aysylu/loom
- ;; by default we only show graph nodes when displaying the graph:
- (rx/defstream BasicEditableGraph [{:keys [nodeset] :as graph}]
- (rx/horizontal
- (rx/raw-string "#loom/graph[" {:fill :object})
- (rx/items nodeset {::graph graph})
- (rx/raw-string "]" {:fill :object})))
- ;; define an action on a selected node when it has access to graph:
- (rx/defaction ::loom:successors [v ann]
- (when-let [g (::graph ann)]
- (fn []
- ;; we want to stream this custom formatting as is, not as a function value
- (rx/stream-as-is
- ;; forward the graph to every successor
- (rx/vertically (g/successors g v) {::graph g})))))
- ;; define an action on a node to see its graph:
- (rx/defaction ::loom:graph [v ann]
- (when-let [g (::graph ann)]
- (constantly g)))
- ;; define an action on a graph to see its edges:
- (rx/defaction ::loom:edges [v]
- (when (satisfies? g/Graph v)
- (fn []
- (rx/stream-as-is
- (rx/vertically
- (map (fn [[from to]]
- ;; since vertically works on values, not sfs, we want to make these sfs
- ;; streamed as is too
- (rx/stream-as-is
- (rx/horizontal
- ;; forward the graph
- (rx/stream from {::graph v})
- rx/separator
- (rx/raw-string "->" {:fill :util :selectable false})
- rx/separator
- (rx/stream to {::graph v}))))
- (g/edges v)))))))
- ;; graph of associations to explore:
- (g/graph [:food :takoyaki]
- [:food :drinks]
- [:takoyaki :japan]
- [:drinks :coca-cola]
- [:drinks :genmaicha]
- [:genmaicha :japan])