(ns microtables-frontend.views.sheet (:require [microtables-frontend.events :as events] [microtables-frontend.utils :as utils] [re-frame.core :as re-frame])) (defn cell [c r data] (let [datum (get-in data [c r])] ^{:key (str c r)} [:td [:input {:id (str c r) :class (if (= (:view datum) :highlighted) "highlighted" "") :value (if (= (get datum :view nil) :value) (get datum :value "") (get datum :error (get datum :display (get datum :value ""))));TODO: add "highlight" display mode (possibly just a css class) :on-change #(re-frame/dispatch [::events/edit-cell-value c r (.. % -target -value)]) :on-focus #(re-frame/dispatch [::events/movement-enter-cell c r]) :on-blur #(re-frame/dispatch [::events/movement-leave-cell c r]) :on-keyPress #(when (= (.. % -which) 13) (re-frame/dispatch [::events/press-enter-in-cell c r]))}]])) (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 {:id "main-table"} [:tbody ;TODO: figure out appropriate starting values for maxrow and maxcol (maybe keep them intentionally small) ;TODO: figure out movement (maybe allow scroll overflow) (let [maxrow 50;(utils/highest-row data) maxcol "Z";(utils/highest-col data) cols (take-while (partial not= (utils/next-letter maxcol)) utils/col-letters)] (cons (header-row cols) (map #(row % cols data) (range 1 (inc maxrow)))))]])