(ns microtables-frontend.events (:require [re-frame.core :as re-frame] [microtables-frontend.db :as db] [microtables-frontend.utils :as utils])) (re-frame/reg-event-db ::initialize-db (fn [_ _] (println "initializing db") (-> db/default-db (update-in [:table-data] (partial map utils/add-parsed-variables)) (update-in [:table-data] utils/re-evaluate)))) (re-frame/reg-event-db ::movement-enter-cell (fn [db [_ c r]] (println "::movement-enter-cell" c r) (assoc-in db [:position :cursor] {:col c :row r}))) (defn re-evaluate-if-dirty [db dirty] (if dirty (update-in db [:table-data] utils/re-evaluate) db)) (re-frame/reg-event-db ::movement-leave-cell (fn [db [_ c r]] (let [datum (utils/get-datum (:table-data db) c r)] (println "::movement-leave-cell" c r (if (:dirty datum) "- dirty" "")) (-> db (assoc-in [:position :cursor] nil) (assoc-in [:position :selection] nil) (update-in [:table-data] (partial utils/add-parsed-variables-to-specific-datum c r)) (re-evaluate-if-dirty (:dirty datum)))))) (re-frame/reg-event-db ::edit-cell-value (fn [db [_ c r existing-datum value]] (println "::edit-cell-value" c r value) (if (nil? existing-datum) (assoc db :table-data (conj (:table-data db) {:row r :col c :value value :dirty true})) (assoc db :table-data (map #(if (and (= r (:row %)) (= c (:col %))) (assoc (assoc % :dirty true) :value value) %) (:table-data db))))))