events.cljs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. (update-in [:alt-table-data] utils/create-all-references)
  14. (update-in [:alt-table-data] utils/create-all-back-references))))
  15. (re-frame/reg-event-db
  16. ::movement-enter-cell
  17. (fn [db [_ c r]]
  18. (println "::movement-enter-cell" c r)
  19. (assoc-in db [:position :cursor] {:col c :row r})))
  20. (defn re-evaluate-if-dirty [db dirty]
  21. (if dirty
  22. (update-in db [:table-data] utils/re-evaluate)
  23. db))
  24. (re-frame/reg-event-db
  25. ::movement-leave-cell
  26. (fn [db [_ c r]]
  27. (let [datum (utils/get-datum (:table-data db) c r)
  28. alt-datum (get-in (:alt-table-data db) [c r])]
  29. (println "::movement-leave-cell" c r (if (:dirty datum) "- dirty" ""))
  30. (-> db
  31. (assoc-in [:position :cursor] nil)
  32. (assoc-in [:position :selection] nil)
  33. (update-in [:table-data] (partial utils/add-parsed-variables-to-specific-datum c r))
  34. ;(update-in [:alt-table-data c r] utils/add-references)
  35. (update-in [:alt-table-data] #(utils/reset-references % c r))
  36. (update-in [:alt-table-data] #(utils/evaluate-from-cell % c r))
  37. (re-evaluate-if-dirty (:dirty datum))))))
  38. (re-frame/reg-event-db
  39. ::edit-cell-value
  40. (fn [db [_ c r existing-datum value]]
  41. (println "::edit-cell-value" c r value)
  42. (if (nil? existing-datum);TODO: when removing list-based data, remove this conditional, and all references to "existing-datum" (not needed in map-based data)
  43. (-> db
  44. (assoc :table-data (conj (:table-data db) {:row r :col c :value value :dirty true}))
  45. (update-in [:alt-table-data] #(utils/change-datum-value % c r value)))
  46. (-> db
  47. (assoc :table-data (map #(if (and (= r (:row %)) (= c (:col %))) (assoc (assoc % :dirty true) :value value) %) (:table-data db)))
  48. (update-in [:alt-table-data] #(utils/change-datum-value % c r value))))))