(ns microtables-frontend.views (:require [re-frame.core :as re-frame] [microtables-frontend.subs :as subs] [microtables-frontend.events :as events] [microtables-frontend.utils :as utils])) (defn cell [c r data] (let [datum (some #(if (and (= c (:col %)) (= r (:row %))) %) data)] ^{:key (str c r)} [:td [:input {:id (str c r) :value (if (= (get datum :view nil) :value) (get datum :value "") (get datum :error (get datum :display (get datum :value "")))) :on-change #(re-frame/dispatch [::events/edit-cell-value c r datum (.. % -target -value)])}]])) (defn row [r cols data] ^{:key (str "row-" r)} [:tr (cons ^{:key (str "row-head-" r)} [:th (str r)] (map #(cell % r data) cols))]) (defn header-row [cols] ^{:key "header"} [:tr (cons ^{:key "corner"} [:th] (map (fn [c] ^{:key (str "col-head-" c)} [:th c]) cols))]) (defn sheet [data] [:table [:tbody (let [maxrow (utils/highest :row data) cols (take-while (partial not= (utils/next-letter (utils/highest :col data))) utils/col-letters)] (cons (header-row cols) (map #(row % cols data) (range 1 (inc maxrow)))))]]) (defn main-panel [] (let [name (re-frame/subscribe [::subs/name]) data (re-frame/subscribe [::subs/table-data])] [:div [:h1 "Hello from " @name] [:div [:h1 "Microtables"] [sheet @data]]]))