Skip to content

Instantly share code, notes, and snippets.

@Conaws
Created February 14, 2017 01:30
Show Gist options
  • Save Conaws/b2b9410d45b3f977d2603d56841282b3 to your computer and use it in GitHub Desktop.
Save Conaws/b2b9410d45b3f977d2603d56841282b3 to your computer and use it in GitHub Desktop.
(ns devcards.re-kindle.dagtree
(:require [cljs-time.core :as time :refer [now]]
[clojure.set :as set]
[datascript.core :as d]
[posh.reagent :as posh :refer [posh!]]
[re-com.core :as rc]
[re-kindle.multi :as multi]
[re-kindle.util :refer [deref-or-value icon]]
[reagent.core :as r])
(:require-macros
[cljs.test :refer [is testing]]
[devcards.core :as dc :refer [defcard-rg deftest]]
[re-kindle.util :refer [deftrack]]))
(def schema {:node/title {:db/unique :db.unique/identity}
:node/prereqs {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
})
(def conn (d/create-conn schema))
(posh! conn)
(d/transact! conn [{:node/title "Goal"
:node/prereqs [{:node/title "AA"
:node/prereqs [{:node/title "A"}]}
{:node/title "AB"
:node/prereqs [{:node/title "A"}]}]}
{:node/title "Simple Goal"}
{:node/title "A1"
:node/prereqs [{:node/title "A"}]}])
(defn pull-tree [conn root]
(posh/pull conn '[:node/title {:node/_prereqs ...
:node/prereqs ...}]
[:node/title root]))
(deftest pull-deep
(testing "Pull"
(is (= "" @(pull-tree conn "Goal")))
))
(defn leaf [conn name]
(let [t (pull-tree conn name)
tabs (r/atom :x)]
(fn [conn name]
[rc/v-box
:style {:margin-left "40px"}
:children [
[rc/h-box
:align :start
:justify :between
:children [[rc/title :label (:node/title @t)
:level :level1]
[rc/horizontal-pill-tabs
:model tabs
:tabs [{:id :node/_prereqs :label
[:div
[icon "chevron-up"](count (:node/_prereqs @t))]
}
{:id :node/prereqs :label
[:div
[icon "chevron-down"](count (:node/prereqs @t))]
}
{:id :x :label
[:div
[icon "close"]]
}]
:on-change #(reset! tabs %)]
]]
(for [child (get @t @tabs)]
(if-let [name (:node/title child)]
[leaf conn name]
[:button]
)
)
]
]))
)
(defn tree [conn root]
(let [t (pull-tree conn root)]
(fn [conn root]
[rc/v-box
:children [
[rc/h-box
:justify :between
:children [[rc/title :label (:node/title @t)
:level :level1]
[:div [icon "chevron-up"](count (:node/_prereqs @t))]
[:div [icon "chevron-down"](count (:node/prereqs @t))]
]]
(for [child (:node/prereqs @t)]
[:div {:key
(str (:db/id @t)
(:db/id child))}
(if-let [name (:node/title child)]
[leaf conn name]
[:button]
)
]
)
]
])
)
)
(defcard-rg tree-card
[leaf conn "Goal"])
(defn fold1 [parent-open i]
(let [open (r/atom false)]
(fn [parent-open i]
[rc/v-box
:class "indent"
:children [[:div [:div.btn-tiny.btn-outline
{:on-click #(swap! open not)}
[icon
(if (or @open @parent-open)
"chevron-down"
"chevron-right"
)]
]
i
]
(when (or @open @parent-open)
[:div :open]
)
]]))
)
(defn Fold []
(let [all-open (r/atom false)
open (r/atom false)]
(fn []
[rc/v-box
:children
[[:div [:div.btn-tiny.btn-outline
{:on-click #(swap! all-open not)}
[icon
(if @all-open
"chevron-down"
"chevron-right"
)]]]
[:div "Me"]
[:div [:div.btn-tiny.btn-outline
{:on-click #(swap! open not)}
[icon
(if @open
"chevron-down"
"chevron-right"
)]]]
(when @open
(for [i (range 20)]
[fold1 all-open i]
))
]])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment