-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.cljs
105 lines (85 loc) · 3.08 KB
/
ui.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(ns snow.files.ui
(:require [re-frame.core :as rf]
[goog.object :as gobj]))
(def <sub (comp deref re-frame.core/subscribe))
(def >evt re-frame.core/dispatch)
;;;;;;;;;;;;;;;
;; icon-view ;;
;;;;;;;;;;;;;;;
(defn dissoc-in
"https://stackoverflow.com/a/14488425"
[m [k & ks :as keys]]
(if ks
(if-let [nextmap (get m k)]
(let [newmap (dissoc-in nextmap ks)]
(assoc m k newmap))
m)
(dissoc m k)))
(rf/reg-event-db
::delete
(fn [db [_ {:keys [filename id type]}]]
(dissoc-in db [id type filename])))
(defn file-view [{:keys [filename id type] :as opts}]
(println "opts is " opts)
[:div
[:div>span.icon.is-large
[:i.fab.fa-css3-alt.fa-3x]]
[:div (cond-> filename
(keyword? filename) name)
[:span.icon.is-medium {:on-click #(rf/dispatch [::delete opts])}
[:i.fas.fa-trash-alt]]]])
(rf/reg-sub ::files
(fn [db [_ id type]] (get-in db [id type])))
(defn file-list [{:keys [id type]}]
(println "id type " id type)
[:div.column>div.columns
(cond->> @(rf/subscribe [::files id type])
some? (map (fn [[n _]]
[:div.column {:key n} [file-view {:filename n
:id id
:type type}]])))])
;;;;;;;;;;;;;;;;
;; input-view ;;
;;;;;;;;;;;;;;;;
(rf/reg-event-fx
::new
(fn [{:keys [db]} [_ [type id {:keys [name file-content dispatch single?] :as m}]]]
(cond-> {:db (if single?
(assoc-in db [id type] [name file-content])
(assoc-in db [id type name] file-content))}
(vector? dispatch) (merge {:dispatch [(first dispatch)
(assoc (second dispatch) :file-data file-content)]}))))
(defn get-files [e]
(array-seq (.. e -target -files)))
(defn read-file
"read file and dispatch an event of [:file-content {:id :content}],
will apply a process function if provided"
[file {:keys [id type process dispatch binary] :as m}]
(println "Trying to read file name " (.-name file) "and will dispatch" dispatch)
(let [reader (js/FileReader.)]
(gobj/set reader
"onload"
(fn [e]
(println "event called ")
(rf/dispatch [::new [type id (merge m {:file-content (cond-> (.. e -target -result)
(fn? process) process)
:name (.-name file)})]])))
(if (true? binary)
(.readAsDataURL reader file)
(.readAsText reader file))))
(defn file-input [m]
[:div.field.file.is-boxed>label.file-label
[:div.field>div.control
[:input.input.file-input
{:type "file"
:on-change (fn [e] (-> e get-files first (read-file m)))}]
[:span.file-cta
[:span.file-icon>i.fas.fa-upload]
[:span.file-label (:placeholder m)]]]])
;;;;;;;;;;;;;;;;
;; combo view ;;
;;;;;;;;;;;;;;;;
(defn view [{:keys [id placeholder process type dispatch] :as m}]
[:div.columns
[:div.column [file-input m]]
[:div.column [file-list m]]])