|
|
@@ -14,10 +14,11 @@
|
|
|
[clojure.set :as set]
|
|
|
[cljs-bean.core :as bean]
|
|
|
["pixi-graph-fork" :as Pixi-Graph]
|
|
|
+ ["@pixi/utils" :as pixi-utils]
|
|
|
["graphology" :as graphology]
|
|
|
["d3-force" :refer [forceSimulation forceManyBody forceCenter forceLink forceCollide forceRadial forceX forceY SimulationLinkDatum SimulationNodeDatum] :as force]))
|
|
|
|
|
|
-(def graph (gobj/get graphology "Graph"))
|
|
|
+(def Graph (gobj/get graphology "Graph"))
|
|
|
|
|
|
(defonce colors
|
|
|
["#1f77b4"
|
|
|
@@ -81,37 +82,48 @@
|
|
|
(.tick 3)
|
|
|
(.stop))))
|
|
|
|
|
|
+(defonce *graph-instance (atom nil))
|
|
|
(defn render!
|
|
|
[state]
|
|
|
- (when-let [graph (:graph state)]
|
|
|
- (.destroy graph))
|
|
|
- (let [{:keys [nodes links style hover-style height register-handlers-fn dark?]} (first (:rum/args state))
|
|
|
- style (or style (default-style dark?))
|
|
|
- hover-style (or hover-style (default-hover-style dark?))
|
|
|
- graph (graph.)
|
|
|
- nodes-set (set (map :id nodes))
|
|
|
- links (->> (filter (fn [link]
|
|
|
- (and (nodes-set (:source link)) (nodes-set (:target link)))) links)
|
|
|
- (distinct))
|
|
|
- nodes-js (bean/->js nodes)
|
|
|
- links-js (bean/->js links)]
|
|
|
- (layout! nodes-js links-js)
|
|
|
- (doseq [node nodes-js]
|
|
|
- (.addNode graph (.-id node) node))
|
|
|
- (doseq [link links-js]
|
|
|
- (.addEdge graph (.-id (.-source link)) (.-id (.-target link)) link))
|
|
|
-
|
|
|
- (if-let [container-ref (:ref state)]
|
|
|
- (let [graph (new (.-PixiGraph Pixi-Graph)
|
|
|
- (bean/->js
|
|
|
- {:container @container-ref
|
|
|
- :graph graph
|
|
|
- :style style
|
|
|
- :hoverStyle hover-style
|
|
|
- :height height}))]
|
|
|
- (when register-handlers-fn
|
|
|
- (register-handlers-fn graph))
|
|
|
-
|
|
|
- ;; (.addEventListener container-ref)
|
|
|
- (assoc state :graph graph))
|
|
|
- state)))
|
|
|
+ (try
|
|
|
+ (let [old-instance @*graph-instance
|
|
|
+ {:keys [graph pixi]} old-instance
|
|
|
+ _ (when (and graph pixi)
|
|
|
+ (.forEachNode graph (fn [node]
|
|
|
+ (.dropNode graph node))))]
|
|
|
+ (let [{:keys [nodes links style hover-style height register-handlers-fn dark?]} (first (:rum/args state))
|
|
|
+ style (or style (default-style dark?))
|
|
|
+ hover-style (or hover-style (default-hover-style dark?))
|
|
|
+ graph (or graph (Graph.))
|
|
|
+ nodes-set (set (map :id nodes))
|
|
|
+ links (->> (filter (fn [link]
|
|
|
+ (and (nodes-set (:source link)) (nodes-set (:target link)))) links)
|
|
|
+ (distinct))
|
|
|
+ nodes (remove nil? nodes)
|
|
|
+ links (remove (fn [{:keys [source target]}] (or (nil? source) (nil? target))) links)
|
|
|
+ nodes-js (bean/->js nodes)
|
|
|
+ links-js (bean/->js links)]
|
|
|
+ (layout! nodes-js links-js)
|
|
|
+ (doseq [node nodes-js]
|
|
|
+ (.addNode graph (.-id node) node))
|
|
|
+ (doseq [link links-js]
|
|
|
+ (let [source (.-id (.-source link))
|
|
|
+ target (.-id (.-target link))]
|
|
|
+ (.addEdge graph source target link)))
|
|
|
+ (if-let [{:keys [pixi]} @*graph-instance]
|
|
|
+ (.resetView pixi)
|
|
|
+ (when-let [container-ref (:ref state)]
|
|
|
+ (let [pixi-graph (new (.-PixiGraph Pixi-Graph)
|
|
|
+ (bean/->js
|
|
|
+ {:container @container-ref
|
|
|
+ :graph graph
|
|
|
+ :style style
|
|
|
+ :hoverStyle hover-style
|
|
|
+ :height height}))]
|
|
|
+ (reset! *graph-instance {:graph graph
|
|
|
+ :pixi pixi-graph})
|
|
|
+ (when register-handlers-fn
|
|
|
+ (register-handlers-fn pixi-graph)))))))
|
|
|
+ (catch js/Error e
|
|
|
+ (js/console.error e)))
|
|
|
+ state)
|