|
1 | 1 | (ns options.editor.select)
|
2 | 2 |
|
3 |
| -(defn entry->option [current-val {:keys [id name]}] |
4 |
| - ;; works for spec like: |
5 |
| - ;; [{:id 1 :name "Batman"} |
6 |
| - ;; {:id 2 :name "Robin"} |
7 |
| - ;; {:id 3 :name "Harry Potter"}] |
8 |
| - (if (= current-val id) |
9 |
| - [:option {:value id :selected true} name] |
10 |
| - [:option {:value id} name])) |
| 3 | +; create-spec from vals |
11 | 4 |
|
12 |
| -(defn spec->map [spec1] |
| 5 | +(defn val->map [idx val] |
13 | 6 | ;; convert spec like
|
14 | 7 | ;; [:blue :green :red]
|
15 | 8 | ;; to a spec with :id and :name
|
16 |
| - {:id spec1 |
17 |
| - :name spec1}) |
| 9 | + {:id (str idx) |
| 10 | + :name (str val) |
| 11 | + :val val}) |
| 12 | + |
| 13 | +(defn vals->spec [vals] |
| 14 | + (map-indexed val->map vals)) |
| 15 | + |
| 16 | +; create-spec from spec |
18 | 17 |
|
19 |
| -(defn type-convert [val1 v] |
20 |
| - (cond |
21 |
| - (number? val1) |
22 |
| - (long v) |
| 18 | +(defn add-val [{:keys [id] :as opts}] |
| 19 | + (assoc opts :val id :id (str id))) |
23 | 20 |
|
24 |
| - (keyword? val1) |
25 |
| - (keyword v) |
| 21 | +(defn spec->spec [specs] |
| 22 | + (map add-val specs)) |
26 | 23 |
|
27 |
| - (string? val1) |
28 |
| - v |
| 24 | +; html helper |
| 25 | + |
| 26 | +(defn entry->option [current-val {:keys [id name val]}] |
| 27 | + ;; works for spec like: |
| 28 | + ;; [{:id 1 :name "Batman"} |
| 29 | + ;; {:id 2 :name "Robin"} |
| 30 | + ;; {:id 3 :name "Harry Potter"}] |
| 31 | + (if (= current-val val) |
| 32 | + [:option {:value id :selected true} name] |
| 33 | + [:option {:value id} name])) |
29 | 34 |
|
30 |
| - :else |
31 |
| - v)) |
| 35 | +(defn get-id [spec id] |
| 36 | + (println "get-id: " id " in spec: " spec) |
| 37 | + (-> (filter #(= id (:id %)) spec) |
| 38 | + first)) |
32 | 39 |
|
33 | 40 | (defn editor-select [{:keys [set-fn options]} current-val]
|
34 | 41 | (let [{:keys [class spec]
|
35 | 42 | :or {class ""
|
36 | 43 | spec []}} options
|
37 |
| - option1 (first spec) |
38 |
| - spec (if (map? option1) |
39 |
| - spec |
40 |
| - (map spec->map spec)) |
41 |
| - val1 (-> spec first :id) |
42 |
| - otype (type val1)] |
43 |
| - (println "select id type: " otype) |
| 44 | + mapped? (map? (first spec)) |
| 45 | + spec (if mapped? |
| 46 | + (spec->spec spec) |
| 47 | + (vals->spec spec))] |
44 | 48 | (into [:select {:class class
|
45 | 49 | :on-change (fn [e]
|
46 |
| - (let [v (-> e .-target .-value) |
47 |
| - v2 (type-convert val1 v)] |
48 |
| - (println "setting select to: " v "v2: " v2) |
49 |
| - (set-fn v2)))}] |
| 50 | + (let [id-str (-> e .-target .-value) |
| 51 | + entry (get-id spec id-str) |
| 52 | + v (:val entry)] |
| 53 | + ;(println "entry: " entry) |
| 54 | + ;(println "setting select to id: " id-str "val: " v) |
| 55 | + (set-fn v)))}] |
50 | 56 | (map #(entry->option current-val %) spec))))
|
51 | 57 |
|
52 | 58 | (defn select? [spec]
|
|
0 commit comments