core.cljs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 update-display [c r display]
  40. (println (str "trying to update " c r " to " display))
  41. (swap! data-atom (fn [d] (map #(if (and (= r (:row %)) (= c (:col %))) (assoc % :display display) %) d))))
  42. (defn toggle-display [c r view-mode]
  43. (println (str "toggling " c r " to " view-mode))
  44. (swap! data-atom (fn [d] (map #(if (and (= r (:row %)) (= c (:col %))) (assoc % :view view-mode) %) d))))
  45. ; CALCULATION / FORMULA EVALUATION FUNCTIONS
  46. (defn temp1 [data]
  47. (let [first-order (group-by #(not= (first (:value %)) "=") data)
  48. evaluated-cells (map #(keyword (str (:col %) (:row %))) (first-order true))
  49. non-evaluated-cells (map #(keyword (str (:col %) (:row %))) (first-order false))
  50. evaluated-values (map :value (first-order true))
  51. non-evaluated-values (map #(subs (:value %) 1) (first-order false))]
  52. {:remaining (zipmap non-evaluated-cells non-evaluated-values)
  53. :evaluated (zipmap evaluated-cells evaluated-values)}))
  54. (def parse-variables (memoize (fn [expression]
  55. (js->clj (.getVariables mathjs expression)))))
  56. (defn temp3 [[k v]]
  57. (let [w (name k)
  58. c (re-find #"^[A-Z]+" w)
  59. r (.parseInt js/window (re-find #"[0-9]+$" w))]
  60. (do
  61. (update-display c r v)
  62. )
  63. )
  64. )
  65. (defn temp2 [evaluated remaining]
  66. (if (empty? remaining)
  67. evaluated
  68. (let [vars (map #(parse-variables (val %)) remaining)
  69. remaining-vars (zipmap (keys remaining) vars)
  70. evaluated-vars (map (fn [x] (map #(let [item (keyword %)]
  71. (cond
  72. (contains? evaluated item) [% (evaluated item)]
  73. (contains? remaining item) [% :not-yet]
  74. :else [% 0]
  75. )) x)) vars)
  76. parsed (zipmap (keys remaining) evaluated-vars)
  77. ready-or-not (group-by (fn [[ k v ]] (some #(not= :not-yet (last %)) v)) parsed)
  78. ;TODO: detect circular references
  79. prepared (map (fn [[k v]] [k (.evaluate mathjs (remaining k) (apply js-obj (flatten v)))]) (ready-or-not true))
  80. new-evaluated (reduce conj evaluated prepared)
  81. still-remaining (apply hash-map (flatten (map #(list (key %) (remaining (key %))) (ready-or-not nil))))
  82. ;merged (map temp3 prepared) ;TODO: only merge after recursion is complete (outside this function)
  83. ]
  84. ;remaining ; {:B8 B7 * 2, :C7 D1, :B7 C5 + D6}
  85. ;vars ; ([B7] [D1] [C5 D6])
  86. ;remaining-vars ; {:B8 [B7], :C7 [D1], :B7 [C5 D6]}
  87. ;parsed ; {:B8 ([B7 :not-yet]), :C7 ([D1 0]), :B7 ([C5 269] [D6 4065])}
  88. ;ready-or-not ; {nil [[:B8 ([B7 :not-yet])]], true [[:C7 ([D1 0])] [:B7 ([C5 269] [D6 4065])]]}
  89. ;prepared ; ([:C7 0] [:B7 4334])
  90. ;still-remaining ; {:B8 B7 * 2}
  91. ;evaluated; TODO: change name, consider putting "data" directly in here
  92. (recur new-evaluated still-remaining)
  93. ;new-evaluated
  94. )
  95. )
  96. )
  97. ;; -------------------------
  98. ;; Views
  99. (defn cell [c r data]
  100. (let [datum (some #(if (and (= c (:col %)) (= r (:row %))) %) data)]
  101. ^{:key (str c r)} [:td [:input {:value (if (= (get datum :view nil) :value) (get datum :value "") (get datum :display (get datum :value "")))
  102. :on-change #(update-value c r datum (.. % -target -value))
  103. :on-blur (fn [e] (println (str "blur! " c r)) (toggle-display c r :display))
  104. :on-focus (fn [e] (println (str "focus! " c r)) (toggle-display c r :value))}]]
  105. )
  106. )
  107. (defn row [r cols data]
  108. ^{:key (str "row-" r)} [:tr
  109. (cons
  110. ^{:key (str "row-head-" r)} [:th (str r)]
  111. (map #(cell % r data) cols))])
  112. (defn header-row [cols]
  113. ^{:key "header"} [:tr
  114. (cons
  115. ^{:key "corner"} [:th]
  116. (map (fn [c] ^{:key (str "col-head-" c)} [:th c]) cols))])
  117. (defn sheet [data]
  118. [:table [:tbody
  119. (let [maxrow (highest :row data)
  120. cols (take-while (partial not= (next-letter (highest :col data))) col-letters)]
  121. (cons
  122. (header-row cols)
  123. (map #(row % cols data) (range 1 (inc maxrow)))))
  124. ]])
  125. (defn app []
  126. [:div
  127. [:h3 "Microtables"]
  128. #_(do
  129. (println (.stringify js/JSON (.parse mathjs "3 + 4x")))
  130. (println (js->clj (.parse js/JSON (.stringify js/JSON (.parse mathjs "3 + 4x")))))
  131. (println (type (js->clj (.stringify js/JSON (.parse mathjs "3 + 4x")))))
  132. (println (js-obj "a" 2 "b" "one"))
  133. (println (type (js-obj "a" 2 "b" "one")))
  134. (println (type (.parse mathjs "3 + 4x")))
  135. (println (.log js/console (.parse mathjs "3 + 4x")))
  136. (println (.keys js/Object (.parse mathjs "3 + 4x")))
  137. (println (js->clj (.parse mathjs "3 + 4x")))
  138. (println (js->clj (.parse mathjs "3 + 4x") :keywordize-keys true))
  139. (println (.-op (.parse mathjs "3 + 4x")))
  140. (println (.-fn (.parse mathjs "3 + 4x")))
  141. (println (js->clj (.-args (.parse mathjs "3 + 4x"))))
  142. (println (.-comment (.parse mathjs "3 + 4x")))
  143. (println (.-implicit (.parse mathjs "3 + 4x")))
  144. (println (.-value (.parse mathjs "3 + 4x")))
  145. (println (second (.-args (.parse mathjs "3 + 4x"))))
  146. (println (.-implicit (second (.-args (.parse mathjs "3 + 4x")))))
  147. (println (.-op (second (.-args (.parse mathjs "3 + 4x")))))
  148. (println (.-fn (second (.-args (.parse mathjs "3 + 4x")))))
  149. (println (.evaluate mathjs "3 + 4x" (js-obj "x" 10)))
  150. (println (filter #(not= (first (:value %)) "=") @data-atom))
  151. (let [rt (temp1 @data-atom)]
  152. (do
  153. (println rt)
  154. (println (temp2 (:evaluated rt) (:remaining rt)))))
  155. (println "data-atom")
  156. (println @data-atom)
  157. "hi")
  158. [sheet @data-atom]])
  159. ;; -------------------------
  160. ;; Initialize app
  161. (defn mount-root []
  162. (r/render [app] (.getElementById js/document "app")))
  163. (defn init! []
  164. (mount-root))