Skip to content

Instantly share code, notes, and snippets.

@ThomasDeutsch
Created November 25, 2015 16:46
Show Gist options
  • Save ThomasDeutsch/6ab70d6146439afaec28 to your computer and use it in GitHub Desktop.
Save ThomasDeutsch/6ab70d6146439afaec28 to your computer and use it in GitHub Desktop.
(ns scheduler.main.core
(:require ;; global
[goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[datascript.core :as d]
;[sablono.core :as html :refer-macros [html]]
;; scheduler
;[scheduler.devcards.testdata :as testdata]
;[scheduler.main.db :refer [conn mutate read]]
;[scheduler.main.placement :refer [placement Placement]]
;[scheduler.main.settings :refer [settings Settings]]
;[scheduler.main.q :as q]
[om.dom :as dom]))
(enable-console-print!)
(def norm-tree-data
{:tree {:id 0
:node-value 1
:children [{:id 1
:node-value 2
:children [{:id 2
:node-value 3
:children []}]}
{:id 3
:node-value 4
:children []}]}})
(declare norm-node)
(defmulti norm-tree-read om/dispatch)
(defmethod norm-tree-read :tree
[{:keys [state query] :as env} _ _]
(let [st @state]
{:value (om/db->tree query (:tree st) st)}))
(defmethod norm-tree-read :node/by-id
[{:keys [state query query-root]} _ _]
(println "node/by-id: " query)
(om/db->tree query query-root @state))
(defmethod norm-tree-read :search/results
[{:keys [state query query-root]} _ _]
(println "search-results reader called"))
(defmulti norm-tree-mutate om/dispatch)
(defmethod norm-tree-mutate 'tree/increment
[{:keys [state]} _ {:keys [id]}]
{:action
(fn []
(swap! state update-in [:node/by-id id :node-value] inc))})
(defmethod norm-tree-mutate 'tree/decrement
[{:keys [state]} _ {:keys [id]}]
{:action
(fn []
(swap! state update-in [:node/by-id id :node-value]
(fn [n] (max 0 (dec n)))))})
(defn increment! [c id]
(fn [e]
(om/set-query! c {:params {:query "test"}})
(om/transact! c `[(tree/increment {:id ~id})])
))
(defn decrement! [c id]
(fn [e]
(om/transact! c `[(tree/decrement {:id ~id})])))
(defui NormNode
static om/IQueryParams
(params [_]
{:query ""})
static om/Ident
(ident [this {:keys [id]}]
[:node/by-id id])
static om/IQuery
(query [this]
'[:id :node-value {:children ...} (:search/results {:query ?query})])
Object
(render [this]
(let [{:keys [id node-value children]} (om/props this)]
(dom/li nil
(dom/div nil
(dom/label nil (str "Node value:" node-value))
(dom/button #js {:onClick (increment! this id)} "+")
(dom/button #js {:onClick (decrement! this id)} "-"))
(dom/ul nil
(map norm-node children))))))
(def norm-node (om/factory NormNode))
(defui NormTree
static om/IQuery
(query [this]
[{:tree (om/get-query NormNode)}])
Object
(render [this]
(let [{:keys [tree]} (om/props this)]
(dom/ul nil
(norm-node tree)))))
(def norm-tree-parser
(om/parser {:read norm-tree-read
:mutate norm-tree-mutate}))
(def norm-tree-reconciler
(om/reconciler
{:state norm-tree-data
:parser norm-tree-parser
:pathopt true}))
(om/add-root! norm-tree-reconciler
NormTree (gdom/getElement "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment