core.cljs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. (ns microtables-frontend.core
  2. (:require
  3. [reagent.core :as r]
  4. ["mathjs" :as mathjs]))
  5. ; to generate random values
  6. ;for(let i = 0, s = new Set(); i < 10; i++){ let r = Math.floor(Math.random() * 15)+1, c = a[Math.floor(Math.random() * a.length)], k = `${c}${r}`; if(s.has(k)){ i--; continue; } s.add(k); v.push(`{:row ${r} :col "${c}" :value "${Math.floor(Math.random() * 10000)}"}`); }
  7. (def sample-data [{:row 1 :col "A" :value "59" :view :display}
  8. {:row 5 :col "C" :value "269" :view :display}
  9. {:row 4 :col "B" :value "7893" :view :display}
  10. {:row 2 :col "F" :value "8650" :view :display}
  11. {:row 6 :col "D" :value "4065" :view :display}
  12. {:row 7 :col "F" :value "5316" :view :display}
  13. {:row 12 :col "A" :value "2405" :view :display}
  14. {:row 5 :col "B" :value "7863" :view :display}
  15. {:row 9 :col "E" :value "3144" :view :display}
  16. {:row 10 :col "D" :value "8272" :view :display}
  17. {:row 11 :col "D" :value "2495" :view :display}
  18. {:row 15 :col "E" :value "8968" :view :display}
  19. {:row 7 :col "B" :value "=C5 + D6" :view :display}
  20. {:row 8 :col "B" :value "=B7 * 2" :view :display}
  21. {:row 7 :col "C" :value "=D1" :view :display}])
  22. (defonce data-atom (r/atom sample-data))
  23. (defn highest [dir data] (apply max (map dir data)))
  24. ; COLUMN NAMES
  25. (defn upgrade-letter-code [s]
  26. (let [l (last s)]
  27. (cond
  28. (empty? s) [65]
  29. (= l 90) (conj (upgrade-letter-code (subvec s 0 (dec (count s)))) 65)
  30. :else (conj (subvec s 0 (dec (count s))) (inc l)))))
  31. (defn next-letter [lc]
  32. (apply str (map char (upgrade-letter-code (mapv #(.charCodeAt % 0) lc)))))
  33. (def col-letters (iterate next-letter "A"))
  34. ; CHANGE VALUE FUNCTIONS
  35. (defn update-value [c r datum value]
  36. (if (nil? datum)
  37. (swap! data-atom conj {:row r :col c :value value})
  38. (swap! data-atom (fn [d] (map #(if (and (= r (:row %)) (= c (:col %))) (assoc % :value value) %) d)))))
  39. (defn toggle-display [c r view-mode]
  40. (println (str " toggling " c r " to " view-mode))
  41. (swap! data-atom (fn [d] (map #(if (and (= r (:row %)) (= c (:col %))) (assoc % :view view-mode) %) d))))
  42. ; CALCULATION / FORMULA EVALUATION FUNCTIONS
  43. (def parse-variables (memoize (fn [expression]
  44. (js->clj (.getVariables mathjs expression)))))
  45. (def evaluate-expression (memoize (fn [expression variables]
  46. (.evaluate mathjs expression (clj->js variables)))))
  47. (def str->rc (memoize (fn [s]
  48. (let [c (re-find #"^[A-Z]+" s)
  49. r (.parseInt js/window (re-find #"[0-9]+$" s))]
  50. {:row r :col c}))))
  51. (defn find-cell [data c r]
  52. (some #(if (and (= (:col %) c) (= (:row %) r)) %) data))
  53. (defn find-val [data c r]
  54. (let [l (find-cell data c r)
  55. v (get l :display (get l :value))]
  56. (println "found?" c r v l (get l :value) (get l :display))
  57. (cond
  58. (nil? v) 0
  59. (and (string? v) (= (first v) "=")) :not-yet
  60. :else v)))
  61. (defn copy-display-values [data display-values]
  62. (let [removed (map #(-> % (dissoc :vars) (dissoc :refs) (dissoc :found) (dissoc :inputs)) display-values)]
  63. (into data removed)))
  64. ;TODO: figure out how to re-evaluate only when the cell modified affects other cells
  65. (defn re-evaluate [data]
  66. (let [remove-old-displays (map #(dissoc % :display) data)
  67. {has-formula true original-values false} (group-by #(= (first (:value %)) "=") remove-old-displays)
  68. ;TODO: detect circular references
  69. variables (map #(assoc % :vars (parse-variables (subs (:value %) 1))) has-formula)
  70. first-mapped-cell-keys (map (fn [datum] (assoc datum :refs (map str->rc (:vars datum)))) variables)]
  71. (loop [values original-values mapped-cell-keys first-mapped-cell-keys]
  72. (let [search-values (map (fn [datum] (assoc datum :found (map #(find-val (concat values mapped-cell-keys) (:col %) (:row %)) (:refs datum)))) mapped-cell-keys)
  73. {not-ready true ready nil} (group-by (fn [datum] (some #(= :not-yet %) (:found datum))) search-values)
  74. prepped-for-eval (map (fn [datum] (assoc datum :inputs (apply hash-map (interleave (:vars datum) (:found datum))))) ready)
  75. evaluated (map (fn [datum] (assoc datum :display (evaluate-expression (subs (:value datum) 1) (:inputs datum)))) prepped-for-eval)
  76. updated-values (copy-display-values values evaluated)]
  77. (if (nil? not-ready)
  78. updated-values
  79. (recur updated-values not-ready))))))
  80. (defn on-enter-cell [c r e]
  81. (println (str "entering cell " c r))
  82. (toggle-display c r :value))
  83. (defn on-leave-cell [c r e]
  84. (println (str "leaving cell " c r))
  85. (toggle-display c r :display)
  86. (swap! data-atom re-evaluate))
  87. ;; -------------------------
  88. ;; Views
  89. (defn cell [c r data]
  90. (let [datum (some #(if (and (= c (:col %)) (= r (:row %))) %) data)]
  91. ^{:key (str c r)} [:td [:input {:value (if (= (get datum :view nil) :value) (get datum :value "") (get datum :display (get datum :value "")))
  92. :on-change #(update-value c r datum (.. % -target -value))
  93. :on-blur (partial on-leave-cell c r)
  94. :on-focus (partial on-enter-cell c r)}]]))
  95. (defn row [r cols data]
  96. ^{:key (str "row-" r)} [:tr
  97. (cons
  98. ^{:key (str "row-head-" r)} [:th (str r)]
  99. (map #(cell % r data) cols))])
  100. (defn header-row [cols]
  101. ^{:key "header"} [:tr
  102. (cons
  103. ^{:key "corner"} [:th]
  104. (map (fn [c] ^{:key (str "col-head-" c)} [:th c]) cols))])
  105. (defn sheet [data]
  106. [:table [:tbody
  107. (let [maxrow (highest :row data)
  108. cols (take-while (partial not= (next-letter (highest :col data))) col-letters)]
  109. (cons
  110. (header-row cols)
  111. (map #(row % cols data) (range 1 (inc maxrow)))))]])
  112. (defn app []
  113. [:div
  114. [:h3 "Microtables"]
  115. [sheet @data-atom]])
  116. ;; -------------------------
  117. ;; Initialize app
  118. (swap! data-atom re-evaluate) ; evalutate any formulas the first time
  119. (defn mount-root []
  120. (r/render [app] (.getElementById js/document "app")))
  121. (defn init! []
  122. (mount-root))