Commit 4c08b886 authored by Joachim Schunk's avatar Joachim Schunk

modify units-builder to use Bulma & identify units

parent 872158a2
Pipeline #45388 passed with stages
in 1 minute and 35 seconds
......@@ -2,6 +2,7 @@
(:require [garden.def :refer [defstyles]]
[garden.selectors :as selectors]))
(def color-info "#3298DC")
(def color-blue "#2196F3")
(def color-green "#4CAF50")
(def color-grey "#9E9E9E")
......@@ -15,9 +16,16 @@
[:html {:line-height 1.2}]
[:input {:color "inherit"}]
;; [:.shifttotop {:margin-top "-1rem"}]
[:.expo-container {:display "grid"
:grid-template-columns "14.0rem 3.0rem"
:grid-column-gap "0.3em"}
[:.value-builder-container {:display "grid"
:grid-template-columns "14.0rem 3.0rem"
:grid-column-gap "0.3em"}
[:.base {:grid-column "1 / 2"
:grid-row "2 / 4"}]
[:.expo {:grid-column "2 / 3"
:grid-row "1 / 3"}]]
[:.unit-builder-container {:display "grid"
:grid-template-columns "9.0rem 3.0rem"
:grid-column-gap "0.3em"}
[:.base {:grid-column "1 / 2"
:grid-row "2 / 4"}]
[:.expo {:grid-column "2 / 3"
......@@ -181,13 +189,11 @@
[:div.var-select {:cursor "pointer"}
[:&:hover {:color color-blue}]]
[:div.selectable-used-unit {:cursor "pointer"
:margin "1px"
:padding "4px"
:border-style "solid"
:border-width "1px"
:border-radius "4px"}
[:&:hover {:border-width "2px"
:border-color "green"}]]
:border-radius "2px"
:border-color color-info}
[:&:hover {:background-color color-info}]]
[:.tooltip
{:padding "10px 8px"
:font-size "1rem"
......
......@@ -21,15 +21,17 @@
[answer-scheme-version migrate-answer]]
[lernmeister.components.helper :refer [math-abs]])]))
(def prefix-mapping {"Y" 24
"Z" 21
"E" 18
"P" 15
"T" 12
"G" 9
"M" 6
"k" 3
"h" 2
(def prefix-mapping {"Y" 24
"Z" 21
"E" 18
"P" 15
"T" 12
"G" 9
"M" 6
"k" 3
"h" 2
"da" 1
"" 0
"d" -1
"c" -2
"m" -3
......@@ -39,9 +41,9 @@
"f" -15
"a" -18
"z" -21
"y" -24
"" 0
nil 0})
"y" -24})
(def prefix-set (set (keys prefix-mapping)))
(def si-unit-mapping {"m" [{:factor 1 :oom 0 :unit "m" :expo 1}]
"g" [{:factor 1 :oom -3 :unit "kg" :expo 1}]
......@@ -108,27 +110,33 @@
"d" [{:factor 8.64 :oom 4 :unit "s" :expo 1}]
"l" [{:factor 1 :oom -1 :unit "m" :expo 3}]})
(def additional-unit-mappings {"B" [{:factor 1 :oom 1 :unit "dB" :expo 1}]
(def additional-unit-mappings {"B" [{:factor 1 :oom 0 :unit "B" :expo 1}]
"°" [{:factor 1 :oom 0 :unit "°" :expo 1}]})
(def base-unit-set #{"m" "kg" "s" "A" "K" "mol" "cd" "dB" "°"})
(def unit-replace-map {"kg" {:unit "g" :prefix "k"}
"dB" {:unit "B" :prefix "d"}})
(def number-obj {:significand nil :order-of-magnitude "0"})
(def unit-mapping (merge si-unit-mapping additional-unit-mappings))
(def regex-split-char "§§")
(def unit-set (set (keys unit-mapping)))
(def unit-replace-set (set (keys unit-replace-map)))
(def base-unit-set #{"m" "kg" "s" "A" "K" "mol" "cd" "B" "°"})
(def base-unit-map (reduce (fn [res-map base-unit] (assoc res-map base-unit 0)) {} base-unit-set))
(def unit-mapping (merge si-unit-mapping additional-unit-mappings))
(def p-u-c (delay (reduce (fn [res m]
(let [k (first m)
v (second m)]
(if (contains? res k)
(assoc res k (conj (get res k) v))
(assoc res k [v]))))
{} (for [prefix prefix-set
unit unit-set]
[(str prefix unit) {:prefix prefix
:unit unit}]))))
(def prefix-set (set (keys prefix-mapping)))
(def regex-split-char "§§")
(def unit-set (set (keys unit-mapping)))
(def number-obj {:significand nil :order-of-magnitude "0"})
(defn prefix-unit-combinations [] (force p-u-c))
(defn add-to-baseunits-map [outer-multiplier result base-unit]
(let [multiplier (* outer-multiplier (:expo base-unit))
......
......@@ -3,11 +3,7 @@
[clojure.string :refer [join replace]]
[lernmeister.components.exercise-types.multistep-calculation.check
:refer
[prefix-set
unit-replace-map
unit-replace-set
unit-set
valid-significand?]]
[prefix-unit-combinations valid-significand?]]
[lernmeister.components.helper :refer [vec-remove]]))
(defn update-units [{:keys [units prefix unit expo]}]
......@@ -16,52 +12,40 @@
(vec-remove units-new index)
units-new)))
(defn check-and-replace-expo [unit-map]
(defn replace-expo [unit-map]
(let [expo (:expo unit-map)]
(when (not (#{"-" "0" "-0"} expo))
(if (contains? #{nil ""} expo) (assoc unit-map :expo "1") unit-map))))
(defn check-and-replace-unit [unit-map]
(when (not (and (unit-replace-set (:unit unit-map)) (not-empty (:prefix unit-map))))
(let [replaced-map (if-let [unit-mapping (get unit-replace-map (:unit unit-map))]
(-> unit-map
(assoc :unit (:unit unit-mapping))
(assoc :prefix (:prefix unit-mapping)))
unit-map)]
(when (unit-set (:unit replaced-map)) replaced-map))))
(defn check-and-replace-prefix [unit-map]
(let [replaced-map (assoc unit-map :prefix (or (:prefix unit-map) ""))]
(when (prefix-set (:prefix replaced-map)) replaced-map)))
(defn check-unit [unit-map]
(if (contains? #{nil "" "-"} expo)
(if (= "-" expo)
(assoc unit-map :expo "-1")
(assoc unit-map :expo "1"))
unit-map)))
(defn resolve-unit [{:keys [unit expo]}]
(when-let [possible-prefix-unit-combinations (get (prefix-unit-combinations) unit)]
(for [m possible-prefix-unit-combinations]
(assoc m :expo expo))))
(defn check-and-resolve-unit [unit-map]
(some-> unit-map
check-and-replace-expo
check-and-replace-unit
check-and-replace-prefix))
replace-expo
resolve-unit))
(defn parse-unit [string]
(when (string? string)
(when-let [matched-string (re-matches #"^[a-zA-ZΩ°]*" string)]
(replace (subs matched-string 0 3) #"Ohm|ohm" "Ω"))))
(defn parse-prefix [string]
(when (string? string)
(when-let [replaced-string (replace string "u" "µ")]
(let [last-char (or (last replaced-string) "")]
(when (contains? prefix-set last-char) last-char)))))
(when-let [matched-string (re-matches #"^[µa-zA-ZΩ°]*" string)]
(replace matched-string #"Ohm|ohm" "Ω"))))
(defn parse-int [string]
(when (string? string)
(when-let [[match sign int-part] (re-matches #"^(\-?)(\d*)$" string)]
(if-let [[match stripped-int-part] (re-matches #"^0*(\d+)$" int-part)]
(when-let [[_ sign int-part] (re-matches #"^(\-?)(\d*)$" string)]
(if-let [[_ stripped-int-part] (re-matches #"^0*(\d+)$" int-part)]
(str sign stripped-int-part)
(str sign)))))
(defn parse-float [string]
(when (string? string)
(when-let [[match sign int-part float-part] (re-matches #"^(\-?)(\d*)([\.|,]?\d*)$" string)]
(if-let [[match stripped-int-part] (re-matches #"^0*(\d+)$" int-part)]
(when-let [[_ sign int-part float-part] (re-matches #"^(\-?)(\d*)([\.|,]?\d*)$" string)]
(if-let [[_ stripped-int-part] (re-matches #"^0*(\d+)$" int-part)]
(str sign stripped-int-part (replace float-part "," "."))
(if (= float-part "") sign (str sign "0" (replace float-part "," ".")))))))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment