events.cljs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. (ns microtables-frontend.events
  2. (:require
  3. [re-frame.core :as re-frame]
  4. [microtables-frontend.db :as db]
  5. [microtables-frontend.utils :as utils]))
  6. (re-frame/reg-event-db
  7. ::initialize-db
  8. (fn [_ _]
  9. (println "initializing db")
  10. (-> db/default-db
  11. (update-in [:table-data] (partial map utils/add-parsed-variables))
  12. (update-in [:table-data] utils/re-evaluate))))
  13. (re-frame/reg-event-db
  14. ::movement-enter-cell
  15. (fn [db [_ c r]]
  16. (println "::movement-enter-cell" c r)
  17. (assoc-in db [:position :cursor] {:col c :row r})))
  18. (defn re-evaluate-if-dirty [db dirty]
  19. (if dirty
  20. (update-in db [:table-data] utils/re-evaluate)
  21. db))
  22. (re-frame/reg-event-db
  23. ::movement-leave-cell
  24. (fn [db [_ c r]]
  25. (let [datum (utils/get-datum (:table-data db) c r)]
  26. (println "::movement-leave-cell" c r (if (:dirty datum) "- dirty" ""))
  27. (-> db
  28. (assoc-in [:position :cursor] nil)
  29. (assoc-in [:position :selection] nil)
  30. (update-in [:table-data] (partial utils/add-parsed-variables-to-specific-datum c r))
  31. (re-evaluate-if-dirty (:dirty datum))))))
  32. (re-frame/reg-event-db
  33. ::edit-cell-value
  34. (fn [db [_ c r existing-datum value]]
  35. (println "::edit-cell-value" c r value)
  36. (if (nil? existing-datum)
  37. (assoc db :table-data (conj (:table-data db) {:row r :col c :value value :dirty true}))
  38. (assoc db :table-data (map #(if (and (= r (:row %)) (= c (:col %))) (assoc (assoc % :dirty true) :value value) %) (:table-data db))))))