views.cljs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. :on-keyPress #(when (= (.. % -which) 13)
  19. (re-frame/dispatch [::events/press-enter-in-cell c r]))}]]))
  20. (defn row [r cols data]
  21. ^{:key (str "row-" r)} [:tr
  22. (cons
  23. ^{:key (str "row-head-" r)} [:th (str r)]
  24. (map #(cell % r data) cols))])
  25. (defn header-row [cols]
  26. ^{:key "header"} [:tr
  27. (cons
  28. ^{:key "corner"} [:th]
  29. (map (fn [c] ^{:key (str "col-head-" c)} [:th c]) cols))])
  30. (defn sheet [data]
  31. [:table
  32. {:id "main-table"}
  33. [:tbody
  34. ;TODO: figure out appropriate starting values for maxrow and maxcol (maybe keep them intentionally small)
  35. ;TODO: figure out movement (maybe allow scroll overflow)
  36. (let [maxrow 50;(utils/highest-row data)
  37. maxcol "Z";(utils/highest-col data)
  38. cols (take-while (partial not= (utils/next-letter maxcol)) utils/col-letters)]
  39. (cons
  40. (header-row cols)
  41. (map #(row % cols data) (range 1 (inc maxrow)))))]])
  42. (defn extend-range
  43. []
  44. [:div {:class "control-group"}
  45. [:div {:class "control-label"}
  46. "Extend Range"]
  47. [:button "-cols"]
  48. [:input]
  49. [:button]])
  50. (defn controls []
  51. [:div
  52. ;[extend-range]
  53. ;extend range (number field, then shrink/left/up and expand/right/down buttons on either side)
  54. ;fill range (single tap, single column/row fill? number field and button?)
  55. ;move range (designate new "top-left corner" cell for range, figure out overwriting destination cells)
  56. ;empty range (delete contents, re-evaluate)
  57. ;delete range (figure out how to move cells over)
  58. ;copy range (figure out clipboard)
  59. [:div
  60. {:class "control-group"}
  61. [:a ;TODO: consider making the "About" info an overlay rather than a link
  62. {:href "/about.html"
  63. :target "_blank"}
  64. "About"]]])
  65. (defn control-panel [state]
  66. [:div
  67. {:id "controls"}
  68. [:div
  69. ;TODO: link left controls with position of the whole table
  70. {:id "controls-left"
  71. :class (if (= state :left) "open" "")}
  72. [controls]]
  73. [:div
  74. {:id "controls-bottom"
  75. :class (if (= state :bottom) "open" "")}
  76. [controls]]
  77. ;←↑→↓
  78. [:div
  79. {:id "left-controls-button"
  80. :on-click #(re-frame/dispatch [::events/set-controls-state (if (= state :left) nil :left)])}
  81. (if (= state :left) "←" "→")]
  82. [:div
  83. {:id "bottom-controls-button"
  84. :on-click #(re-frame/dispatch [::events/set-controls-state (if (= state :bottom) nil :bottom)])}
  85. (if (= state :bottom) "↓" "↑")]
  86. [:div
  87. {:id "main-logo"
  88. :title "Microtables"}
  89. [:img {:src "logo.svg"}]]])
  90. (defn main-panel []
  91. (let [data (re-frame/subscribe [::subs/table-data])
  92. controls-state (re-frame/subscribe [::subs/controls-state])]
  93. [:div
  94. [sheet @data]
  95. [control-panel @controls-state]]))