views.cljs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (ns microtables-frontend.views
  2. (:require
  3. [re-frame.core :as re-frame]
  4. [microtables-frontend.subs :as subs]
  5. [microtables-frontend.events :as events]
  6. [microtables-frontend.utils :as utils]))
  7. ;; TABLE COMPONENTS
  8. (defn cell [c r data]
  9. (let [datum (get-in data [c r])]
  10. ^{:key (str c r)} [:td
  11. [:input {:id (str c r)
  12. :value (if (= (get datum :view nil) :value)
  13. (get datum :value "")
  14. (get datum :error (get datum :display (get datum :value ""))));TODO: add "highlight" display mode (possibly just a css class)
  15. :on-change #(re-frame/dispatch [::events/edit-cell-value c r (.. % -target -value)])
  16. :on-focus #(re-frame/dispatch [::events/movement-enter-cell c r])
  17. :on-blur #(re-frame/dispatch [::events/movement-leave-cell c r])}]]))
  18. (defn row [r cols data]
  19. ^{:key (str "row-" r)} [:tr
  20. (cons
  21. ^{:key (str "row-head-" r)} [:th (str r)]
  22. (map #(cell % r data) cols))])
  23. (defn header-row [cols]
  24. ^{:key "header"} [:tr
  25. (cons
  26. ^{:key "corner"} [:th]
  27. (map (fn [c] ^{:key (str "col-head-" c)} [:th c]) cols))])
  28. (defn sheet [data]
  29. [:table [:tbody
  30. (let [maxrow (utils/highest-row data)
  31. cols (take-while (partial not= (utils/next-letter (utils/highest-col data))) utils/col-letters)]
  32. (cons
  33. (header-row cols)
  34. (map #(row % cols data) (range 1 (inc maxrow)))))]])
  35. (defn main-panel []
  36. (let [data (re-frame/subscribe [::subs/table-data])]
  37. [:div
  38. [:h1 "Microtables"]
  39. [sheet @data]]))