views.cljs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. (defn cell [c r data]
  8. (let [datum (some #(if (and (= c (:col %)) (= r (:row %))) %) data)]
  9. ^{:key (str c r)} [:td
  10. [:input {:id (str c r)
  11. :value (if (= (get datum :view nil) :value)
  12. (get datum :value "")
  13. (get datum :error (get datum :display (get datum :value ""))))
  14. :on-change #(re-frame/dispatch [::events/edit-cell-value c r datum (.. % -target -value)])}]]))
  15. (defn row [r cols data]
  16. ^{:key (str "row-" r)} [:tr
  17. (cons
  18. ^{:key (str "row-head-" r)} [:th (str r)]
  19. (map #(cell % r data) cols))])
  20. (defn header-row [cols]
  21. ^{:key "header"} [:tr
  22. (cons
  23. ^{:key "corner"} [:th]
  24. (map (fn [c] ^{:key (str "col-head-" c)} [:th c]) cols))])
  25. (defn sheet [data]
  26. [:table [:tbody
  27. (let [maxrow (utils/highest :row data)
  28. cols (take-while (partial not= (utils/next-letter (utils/highest :col data))) utils/col-letters)]
  29. (cons
  30. (header-row cols)
  31. (map #(row % cols data) (range 1 (inc maxrow)))))]])
  32. (defn main-panel []
  33. (let [name (re-frame/subscribe [::subs/name])
  34. data (re-frame/subscribe [::subs/table-data])]
  35. [:div
  36. [:h1 "Hello from " @name]
  37. [:div
  38. [:h1 "Microtables"]
  39. [sheet @data]]]))