Browse Source

removed old list-based data table and associated functions

Brandon Wong 3 years ago
parent
commit
8c5a4e369c

+ 14 - 30
frontend/src/cljs/microtables_frontend/db.cljs

@@ -5,34 +5,18 @@
    ;TODO: add "start" and "end" corners as selection
    :position {:cursor nil
               :selection nil}
-   :table-data [{:row 1 :col "A" :value "59"}
-                {:row 5 :col "C" :value "269"}
-                {:row 4 :col "B" :value "7893"}
-                {:row 2 :col "F" :value "8650"}
-                {:row 6 :col "D" :value "4065"}
-                {:row 7 :col "F" :value "5316"}
-                {:row 12 :col "A" :value "2405"}
-                {:row 5 :col "B" :value "7863"}
-                {:row 9 :col "E" :value "3144"}
-                {:row 10 :col "D" :value "8272"}
-                {:row 11 :col "D" :value "2495"}
-                {:row 15 :col "E" :value "8968"}
-                {:row 7 :col "B" :value "=C5 + D6"}
-                {:row 8 :col "B" :value "=B7 * 2"}
-                {:row 7 :col "C" :value "=D1"}
-                {:row 12 :col "B" :value "=C12"}]
-   :alt-table-data {"A" {1 {:value "59"}
-                         12 {:value "2405"}}
-                    "B" {4 {:value "7893"}
-                         5 {:value "7863"}
-                         7 {:value "=C5 + D6"}
-                         8 {:value "=B7 * 2"}
-                         12 {:value "=C12"}}
-                    "C" {7 {:value "=D1"}
-                         5 {:value "269"}}
-                    "D" {6 {:value "4065"}
-                         10 {:value "8272"}
-                         11 {:value "2495"}}
-                    "F" {2 {:value "8650"}
-                         7 {:value "5316"}}}})
+   :table-data {"A" {1 {:value "59"}
+                     12 {:value "2405"}}
+                "B" {4 {:value "7893"}
+                     5 {:value "7863"}
+                     7 {:value "=C5 + D6"}
+                     8 {:value "=B7 * 2"}
+                     12 {:value "=C12"}}
+                "C" {7 {:value "=D1"}
+                     5 {:value "269"}}
+                "D" {6 {:value "4065"}
+                     10 {:value "8272"}
+                     11 {:value "2495"}}
+                "F" {2 {:value "8650"}
+                     7 {:value "5316"}}}})
 

+ 12 - 29
frontend/src/cljs/microtables_frontend/events.cljs

@@ -12,12 +12,10 @@
  (fn [_ _]
    (println "initializing db")
    (-> db/default-db
-       (update-in [:table-data] (partial map utils/add-parsed-variables))
-       (update-in [:table-data] utils/re-evaluate)
-       (update-in [:alt-table-data] #(utils/walk-modify-data % (fn [c r datum] (if (= (first (:value datum)) "=") (assoc datum :dirty true) datum))))
-       (update-in [:alt-table-data] utils/create-all-references)
-       (update-in [:alt-table-data] utils/create-all-back-references)
-       (update-in [:alt-table-data] utils/evaluate-all))))
+       (update-in [:table-data] #(utils/walk-modify-data % (fn [c r datum] (if (= (first (:value datum)) "=") (assoc datum :dirty true) datum))))
+       (update-in [:table-data] utils/create-all-references)
+       (update-in [:table-data] utils/create-all-back-references)
+       (update-in [:table-data] utils/evaluate-all))))
 
 
 (re-frame/reg-event-db
@@ -27,36 +25,21 @@
     (assoc-in db [:position :cursor] {:col c :row r})))
 
 
-(defn re-evaluate-if-dirty [db dirty]
-  (if dirty
-    (update-in db [:table-data] utils/re-evaluate)
-    db))
 
 (re-frame/reg-event-db
   ::movement-leave-cell
   (fn [db [_ c r]]
-    (let [datum (utils/get-datum (:table-data db) c r)
-          alt-datum (get-in (:alt-table-data db) [c r])]
-      (println "::movement-leave-cell" c r (if (:dirty datum) "- dirty" ""))
-      (-> db
-          (assoc-in [:position :cursor] nil)
-          (assoc-in [:position :selection] nil)
-          (update-in [:table-data] (partial utils/add-parsed-variables-to-specific-datum c r))
-          ;(update-in [:alt-table-data c r] utils/add-references)
-          (update-in [:alt-table-data] #(utils/reset-references % c r))
-          (update-in [:alt-table-data] #(utils/evaluate-from-cell % c r))
-          (re-evaluate-if-dirty (:dirty datum))))))
+    (println "::movement-leave-cell" c r)
+    (-> db
+        (assoc-in [:position :cursor] nil)
+        (assoc-in [:position :selection] nil)
+        (update-in [:table-data] #(utils/reset-references % c r))
+        (update-in [:table-data] #(utils/evaluate-from-cell % c r)))))
 
 
 (re-frame/reg-event-db
   ::edit-cell-value
-  (fn [db [_ c r existing-datum value]]
+  (fn [db [_ c r value]]
     (println "::edit-cell-value" c r value)
-    (if (nil? existing-datum);TODO: when removing list-based data, remove this conditional, and all references to "existing-datum" (not needed in map-based data)
-      (-> db
-          (assoc :table-data (conj (:table-data db) {:row r :col c :value value :dirty true}))
-          (update-in [:alt-table-data] #(utils/change-datum-value % c r value)))
-      (-> db
-          (assoc :table-data (map #(if (and (= r (:row %)) (= c (:col %))) (assoc (assoc % :dirty true) :value value) %) (:table-data db)))
-          (update-in [:alt-table-data] #(utils/change-datum-value % c r value))))))
+    (update-in db [:table-data] #(utils/change-datum-value % c r value))))
 

+ 0 - 14
frontend/src/cljs/microtables_frontend/subs.cljs

@@ -10,20 +10,6 @@
     (println "returning table data")
     (let [data (:table-data db)
           cursor (get-in db [:position :cursor])]
-      (if cursor
-        (map #(if (and
-                    (= (:row cursor) (:row %))
-                    (= (:col cursor) (:col %)))
-                (assoc % :view :value)
-                %) data)
-        data))))
-
-(re-frame/reg-sub
-  ::alt-table-data
-  (fn [db]
-    (println "returning alternative table data")
-    (let [data (:alt-table-data db)
-          cursor (get-in db [:position :cursor])]
       (if cursor
         (assoc-in data [(:col cursor) (:row cursor) :view] :value)
         data))))

+ 3 - 86
frontend/src/cljs/microtables_frontend/utils.cljs

@@ -6,8 +6,6 @@
 ; https://clojureverse.org/t/guide-on-how-to-use-import-npm-modules-packages-in-clojurescript/2298
 ; https://shadow-cljs.github.io/docs/UsersGuide.html#npm
 
-(defn highest [dir data] (apply max (map dir data)))
-
 
 (defn highest-col
   "Return the highest column (letter) for which there is a non-empty cell"
@@ -76,13 +74,6 @@
         (dissoc :display)
         (dissoc :error))))
 
-(defn add-parsed-variables-to-specific-datum
-  "Parse variables from the value of a datum and add in :vars and :refs (for swap! data-atom).
-  If the value does not contain a fomula, remove any :vars and :refs that may have been there."
-  [c r data] (map #(if (and (= (:col %) c) (= (:row %) r))
-                     (add-parsed-variables %)
-                     %) data))
-
 ; the references in the data are a set of disconnected, doubly-linked trees
 ;TODO: rather than denotify all, then re-notify all, maybe use a diff? maybe on small scales it's not worth it?
 (defn denotify-references
@@ -197,32 +188,12 @@
                  :calc-error)))))
 
 ;TODO: deal with lowercase cell references
-(defn find-cell [data c r]
-  (some #(if (and (= (:col %) c) (= (:row %) r)) %) data))
-(defn find-ref [data cell-ref]
-  (some (fn [{:keys [row col] :as datum}] (if (and (= row (:row cell-ref)) (= col (:col cell-ref))) datum)) data))
-(defn copy-display-values [data display-values]
-  (let [original (map #(dissoc % :dirty) data)
-        removed (map #(-> % (dissoc :found) (dissoc :inputs) (dissoc :dirty)) display-values)]
-    (into original removed)))
 
 ;TODO: memoize dynamically? probably not worth memoizing directly, and could take up too much memory over time
 ;      https://stackoverflow.com/a/13123571/8172807
 (defn find-cycle
-  ([data datum] (find-cycle data datum #{}))
-  ([data datum ances]
-   (let [cur {:row (:row datum) :col (:col datum)}
-         this-and-above (conj ances cur)
-         refs (:refs datum)
-         found (not (empty? (clojure.set/intersection this-and-above (set refs))))]
-     (if found
-       :cycle-error
-       (some (fn [cell]
-               (find-cycle data (find-ref data cell) this-and-above)) refs)))))
-
-(defn alt-find-cycle
   "Accepts the data and a datum, and peforms a depth-first search to find reference cycles, following back-references."
-  ([data c r] (alt-find-cycle data c r #{}))
+  ([data c r] (find-cycle data c r #{}))
   ([data c r ancest]
    (let [datum (get-in data [c r])
          current {:col c :row r}
@@ -231,7 +202,7 @@
          found-repeat (not (empty? (clojure.set/intersection this-and-above (set inbound))))]
      (if found-repeat
        :cycle-error
-       (some #(alt-find-cycle data (:col %) (:row %) this-and-above) inbound)))))
+       (some #(find-cycle data (:col %) (:row %) this-and-above) inbound)))))
 
 
 (defn gather-variables-and-evaluate-cell
@@ -278,7 +249,7 @@
 (defn evaluate-from-cell
   "Evaluate the final value of a cell, and recursively re-evaluate all the cells that reference it."
   [data c r]
-  (let [cycles? (alt-find-cycle data c r)
+  (let [cycles? (find-cycle data c r)
         new-data (if cycles?
                    (-> data                                           ; if there are cycles, mark :cycle-error and remove :dirty (rathan than evaluate) - still need to recurse up the tree to mark dependents with :cycle-error
                        (update-in [c r] dissoc :dirty)
@@ -323,58 +294,4 @@
              (recur evaluated (rest queue))))
          (recur data (rest queue)))))))
 
-(evaluate-all (walk-modify-data (:alt-table-data microtables-frontend.db/default-db) (fn [c r datum] (if (= (first (:value datum)) "=") (assoc datum :dirty true) datum))))
-
-#_(defn alt-re-evaluate
-    "Evaluate the values of cells that contain formulae, following reference chains if applicable."
-    [data]
-    (let [non-empty-cells (flatten (map (fn [[c v]] (map (fn [[r _]] {:col c :row r}) v)) data))
-          {has-formula true original-values false} (group-by #(= (first (get-in data [(:col %) (:row %) :value])) "=") non-empty-cells)
-          found-cycles (map #(let [found (alt-find-cycle data (:col %) (:row %))]
-                               (if found (assoc % :error found) %)) has-formula)]
-      non-empty-cells))
-
-
-(defn find-val [data c r]
-  (let [l (find-cell data c r)
-        v (get l :display (get l :value))
-        formula? (and (string? v) (= (first v) "="))]
-    (cond
-      (nil? v) 0
-      ;(contains? l :error) :ref-error
-      formula? :not-yet
-      :else v)))
-
-(defn re-evaluate [data]
-  (println "re-evaluating" data)
-  (let [{has-formula true original-values false} (group-by #(= (first (:value %)) "=") data)
-        found-cycles (map #(let [found (find-cycle data %)] (if found (assoc % :error found) %)) has-formula)
-        {eligible true ineligible false} (group-by #(not (contains? %  :error)) found-cycles)]
-    ; 
-    (loop [values (into original-values ineligible)
-           mapped-cell-keys eligible]
-      (let [search-values (map (fn [datum]
-                                 (assoc datum :found (map #(find-val
-                                                             (concat values mapped-cell-keys)
-                                                             (:col %)
-                                                             (:row %))
-                                                          (:refs datum))))
-                               mapped-cell-keys)
-            {not-ready true ready nil} (group-by (fn [datum]
-                                                   (some #(= :not-yet %) (:found datum)))
-                                                 search-values)
-            prepped-for-eval (map (fn [datum]
-                                    (let [hash-map-of-vars-to-vals (apply hash-map (interleave (:vars datum) (:found datum)))]
-                                      (assoc datum :inputs hash-map-of-vars-to-vals)))
-                                  ready)
-            evaluated (map (fn [datum]
-                             (assoc datum :display (evaluate-expression
-                                                     (subs (:value datum) 1)
-                                                     (:inputs datum))))
-                           prepped-for-eval)
-            updated-values (copy-display-values values evaluated)]
-        (println "EVALUATED" evaluated)
-        (if (nil? not-ready)
-          updated-values
-          (recur updated-values not-ready))))))
 

+ 2 - 7
frontend/src/cljs/microtables_frontend/views.cljs

@@ -15,9 +15,8 @@
                                 :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 datum (.. % -target -value)])
+                                :on-change #(re-frame/dispatch [::events/edit-cell-value c r (.. % -target -value)])
                                 :on-focus #(re-frame/dispatch [::events/movement-enter-cell c r])
-                                ;TODO: add "dirty" value to pass to on-blur (maybe not necessary??)
                                 :on-blur #(re-frame/dispatch [::events/movement-leave-cell c r])}]]))
 
 (defn row [r cols data]
@@ -41,12 +40,8 @@
                (map #(row % cols data) (range 1 (inc maxrow)))))]])
 
 
-
-
-
-
 (defn main-panel []
-  (let [data (re-frame/subscribe [::subs/alt-table-data])]
+  (let [data (re-frame/subscribe [::subs/table-data])]
     [:div
      [:h1 "Microtables"]
      [sheet @data]]))