Skip to content

Instantly share code, notes, and snippets.

@stuartstein777
Created March 12, 2022 14:42
Show Gist options
  • Save stuartstein777/ce6f6162eff3b4cd23cbb8c63ca42e24 to your computer and use it in GitHub Desktop.
Save stuartstein777/ce6f6162eff3b4cd23cbb8c63ca42e24 to your computer and use it in GitHub Desktop.
Can I not hot reload the graph?
(ns exfn.app
(:require [reagent.dom :as dom]
[re-frame.core :as rf]
[goog.string.format]
["cytoscape" :as cytoscape]))
(defn cytoscape-clj [graph-options elements container-id]
(cytoscape
(clj->js (merge graph-options
{:container (js/document.getElementById container-id)
:elements elements})))
nil)
;;-- Events and Effects --------------------------------------------------------------------------
(rf/reg-event-db
:initialize
(fn [_ _]
{:graph-options {:style [{:selector "node"
:style {:background-color "#666"
:label "data(label)"}}
{:selector "edge"
:style {"width" 3
:line-color "#000"
:target-arrow-color "#000"
:curve-style "bezier"
:target-arrow-shape "triangle"
:label "data(label)"}}]
:layout {:name "circle"}
:userZoomingEnabled false
:userPanningEnabled false
:boxSelectionEnabled false}
:elements [{:data {:id "a" :label "a"}}
{:data {:id "b" :label "b"}}
{:data {:id "c" :label "c"}}
{:data {:id "d" :label "d"}}
{:data {:id "e" :label "e"}}
{:data {:id "ab" :source "a" :target "b"}}
{:data {:id "ad" :source "a" :target "d"}}
{:data {:id "be" :source "b" :target "e"}}
{:data {:id "cb" :source "c" :target "b"}}
{:data {:id "de" :source "d" :target "e"}}]
:foo 1}))
(rf/reg-fx
:render-graph
(fn [[graph-options elements]]
(cytoscape-clj graph-options elements "cy")))
(rf/reg-event-fx
:render
(fn [{:keys [db]} _]
{:db db
:render-graph [(:graph-options db) (:elements db)]}))
(rf/reg-event-fx
:add-f
(fn [{:keys [db]} _]
(let [elements-with-f (conj (:elements db) {:data {:id "f" :label "f"}})
db (assoc db :elements elements-with-f)]
{:db db
:render-graph [(:graph-options db) (:elements db)]})))
;; event behind as no subcription!
;; -- Subs ------------------------------------------------------------------------
(rf/reg-sub
:graph-options
(fn [db _]
(:graph-options db)))
;; -- App -------------------------------------------------------------------------
(defn app []
(let [graph-options @(rf/subscribe [:graph-options])]
[:div.container
[:h1 "Cytoscape Testing"]
[:button
{:on-click #(rf/dispatch [:render graph-options])}
"Render graph"]
[:button
{:on-click #(rf/dispatch [:add-f])}
"Add F"]
[:div#cy]]))
;; -- After-Load --------------------------------------------------------------------
;; Do this after the page has loaded.
;; Initialize the initial db state.
(defn ^:dev/after-load start
[]
(dom/render [app]
(.getElementById js/document "app")))
(defn ^:export init []
(start))
; dispatch the event which will create the initial state.
(defonce initialize (rf/dispatch-sync [:initialize]))
(comment
(rf/dispatch-sync [:initialize])
)
(let [graph-options @(rf/subscribe [:graph-options])]
(rf/dispatch [:render graph-options]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment