Skip to content

Commit

Permalink
Merge pull request #89 from metafacture/77-exampleDirectories
Browse files Browse the repository at this point in the history
Add directories for examples
  • Loading branch information
katauber authored Mar 1, 2022
2 parents 3589cd5 + 0a4d0dc commit f3b5564
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 66 deletions.
53 changes: 29 additions & 24 deletions src/clj/metafacture_playground/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,45 @@
(catch Exception e
(exception-handler e uri))))

(defn- files-content [dir]
(let [files (->> (io/file dir)
.listFiles
(filter #(.isFile %)))]
(reduce
(fn [result file]
(assoc result (.getName file) (slurp file)))
{}
files)))
(defn- files->content [entries]
(reduce
(fn [result item]
(cond
(.isFile item) (assoc result (.getName item) (slurp item))
(.isDirectory item) (assoc result (.getName item) (files->content (.listFiles item)))
:else result))
{}
entries))

(defn- versions-from-files []
(let [dependencies (-> "project.clj" slurp read-string (nth 6))]
(reduce
(fn [result [file-name file-content]]
(if-let [matching-label (-> (filter #(= (-> % first name)
file-name)
dependencies)
first
second)]
(assoc result file-name {:version-label matching-label
:link file-content})
result))
{}
(files-content "resources/versions/"))))
(->> (io/file "resources/versions/")
.listFiles
(filter #(.isFile %))
files->content
(reduce
(fn [result [file-name file-content]]
(if-let [matching-label (-> (filter #(= (-> % first name)
file-name)
dependencies)
first
second)]
(assoc result file-name {:version-label matching-label
:link file-content})
result))
{}))))

(defroutes routes
(GET "/" [] (resource-response "index.html" {:root "public"}))
(GET "/process" [data flux fix morph uri]
(process-request data flux fix morph uri))
(GET "/examples" request
(try
(-> (files-content "resources/examples/")
json/write-str
response)
(->> (io/file "resources/examples/")
.listFiles
files->content
json/write-str
response)
(catch Exception e
(exception-handler e (:uri request)))))
(GET "/versions" request
Expand Down
4 changes: 2 additions & 2 deletions src/cljs/metafacture_playground/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
:type nil}
:ui {:height nil
:dropdown {:active-item nil
:open? false}}})
"main" {:open? false}}}})

(defn- parseBoolean [val]
(if (= val "true")
Expand Down Expand Up @@ -56,4 +56,4 @@
:message {:content str
:type keyword}
:ui {:height parseBoolean
:dropdown {:active-item str}}})
:dropdown {:active-item #(if (= "" %) nil (str %))}}})
42 changes: 23 additions & 19 deletions src/cljs/metafacture_playground/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
;;; Editing input fields

(defn edit-value
[{db :db} [_ field-name new-value]]
[{db :db} [_ field-name new-value & is-example-data?]]
(let [db-path [:input-fields field-name :content]
disable-editors (when (= field-name :flux)
(let [val (-> new-value
Expand All @@ -141,28 +141,30 @@
{[:input-fields :data :disabled?] (not data-used?)
[:input-fields :morph :disabled?] (not morph-used?)
[:input-fields :fix :disabled?] (not fix-used?)}))]
{:db (-> (reduce
(fn [db [path v]]
(assoc-in db path v))
db
disable-editors)
(assoc-in db-path new-value))
{:db (cond-> (reduce
(fn [db [path v]]
(assoc-in db path v))
db
disable-editors)
true (assoc-in db-path new-value)
(not is-example-data?) (assoc-in [:ui :dropdown :active-item] nil))
:storage/set {:session? true
:pairs (conj
(mapv
(fn [[db-path v]]
{:name (->storage-key db-path) :value v})
disable-editors)
{:name (->storage-key db-path) :value new-value})}
{:name (->storage-key db-path) :value new-value}
(when-not is-example-data? {:name (->storage-key [:ui :dropdown :active-item]) :value nil}))}
:dispatch [::update-width field-name new-value]}))

(re-frame/reg-event-fx
::edit-input-value
edit-value)

(defn open-dropdown
[{db :db} [_ status]]
{:db (assoc-in db [:ui :dropdown :open?] status)})
[{db :db} [_ folder status]]
{:db (assoc-in db [:ui :dropdown folder :open?] status)})

(re-frame/reg-event-fx
::open-dropdown
Expand All @@ -176,7 +178,7 @@
:fx (conj
(mapv
(fn [editor]
[:dispatch [::edit-input-value editor (get sample editor "")]])
[:dispatch [::edit-input-value editor (get sample editor "") true]])
[:data :flux :fix :morph])
[:dispatch [::switch-editor (:active-editor sample)]])
:storage/set {:session? true
Expand Down Expand Up @@ -205,7 +207,8 @@
[[:input-fields :switch :width] nil]]
storage-set [[[:input-fields :data :disabled?] true]
[[:input-fields :fix :disabled?] true]
[[:input-fields :morph :disabled?] true]]
[[:input-fields :morph :disabled?] true]
[[:ui :dropdown :active-item] nil]]
other [[[:result :content] nil]
[[:links :api-call] nil]
[[:links :workflow] nil]]]
Expand All @@ -224,13 +227,14 @@

(defn switch-editor
[{db :db} [_ editor]]
(merge
{:db (assoc-in db [:input-fields :switch :active] editor)
:storage/set {:session? true
:name (->storage-key [:input-fields :switch :active])
:value (when editor (name editor))}}
(when editor
{:dispatch [::update-width editor (get-in db [:input-fields editor :content])]})))
(let [editor (or editor :fix)]
(merge
{:db (assoc-in db [:input-fields :switch :active] editor)
:storage/set {:session? true
:name (->storage-key [:input-fields :switch :active])
:value (when editor (name editor))}}
(when editor
{:dispatch [::update-width editor (get-in db [:input-fields editor :content])]}))))

(re-frame/reg-event-fx
::switch-editor
Expand Down
18 changes: 13 additions & 5 deletions src/cljs/metafacture_playground/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,27 @@

(re-frame/reg-sub
::dropdown-open?
(fn [db _]
(get-in db [:ui :dropdown :open?])))
(fn [db [_ folder]]
(get-in db [:ui :dropdown folder :open?])))

(defn- display-name [str]
(clj-str/replace str "_" " "))

(defn- examples->dropdown-entries []
(map
(fn [[k v]]
(if (map? v)
{k (into (sorted-map)
(examples->dropdown-entries)
v)}
{k {:display-name (display-name k)
:value (utils/parse-url v)}}))))

(re-frame/reg-sub
::examples
(fn [db _]
(into (sorted-map)
(map (fn [[k v]]
{k {:display-name (display-name k)
:value (utils/parse-url v)}}))
(examples->dropdown-entries)
(get db :examples))))

(re-frame/reg-sub
Expand Down
53 changes: 38 additions & 15 deletions src/cljs/metafacture_playground/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
(def menu (component "Menu"))
(def menu-item (component "Menu" "Item"))
(def dropdown (component "Dropdown"))
(def dropdown-menu (component "Dropdown" "Menu"))
(def dropdown-item (component "Dropdown" "Item"))

;;; Using monaco editor react component

Expand Down Expand Up @@ -196,30 +198,51 @@

;;; Control Panel

(defn is-group? [entry]
(try
(when (-> entry val :display-name) false)
(catch :default _
true)))

(defn dropdown-entries [entries]
[:> dropdown-menu
(doall
(for [entry entries]
(if (is-group? entry)
(let [dropdown-open? (re-frame/subscribe [::subs/dropdown-open? (key entry)])]
^{:key (key entry)}
[:> dropdown {:text (key entry)
:open @dropdown-open?
:item true
:on-click #(re-frame/dispatch [::events/open-dropdown (key entry) (not @dropdown-open?)])}
(dropdown-entries (val entry))])
(let [[k v] entry
display-name (:display-name v)
value (:value v)
dropdown-value (re-frame/subscribe [::subs/dropdown-active-item])]
^{:key k}
[:> dropdown-item
{:key k
:text display-name
:value display-name
:active (= display-name @dropdown-value)
:selected (= display-name @dropdown-value)
:onClick #(re-frame/dispatch [::events/load-sample display-name value])}]))))])

(defn examples-dropdown []
[:> button-group {:color color
:basic true}
(let [dropdown-value (re-frame/subscribe [::subs/dropdown-active-item])
dropdown-open? (re-frame/subscribe [::subs/dropdown-open?])]
dropdown-open? (re-frame/subscribe [::subs/dropdown-open? "main"])]
[:> dropdown
{:className "icon"
:button true
:labeled true
:search true
:placeholder "Load example"
:value (or @dropdown-value "")
:text (or @dropdown-value "Load Examples")
:open @dropdown-open?
:options (mapv
(fn [[k {:keys [display-name value]}]]
{:key k
:text display-name
:value display-name
:active (= display-name @dropdown-value)
:selected (= display-name @dropdown-value)
:onClick #(re-frame/dispatch [::events/load-sample display-name value])})
@(re-frame/subscribe [::subs/examples]))
:on-blur #(re-frame/dispatch [::events/open-dropdown false])
:on-click #(re-frame/dispatch [::events/open-dropdown (not @dropdown-open?)])}])])
:on-blur #(re-frame/dispatch [::events/open-dropdown "main" false])
:on-click #(re-frame/dispatch [::events/open-dropdown "main" (not @dropdown-open?)])}
(dropdown-entries @(re-frame/subscribe [::subs/examples]))])])

(defn process-button []
(let [data (re-frame/subscribe [::subs/field-value :data])
Expand Down
2 changes: 1 addition & 1 deletion test/cljs/metafacture_playground/event_handler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
(:flux sample-data)))
(is (= @(re-frame/subscribe [::subs/field-value :fix])
(:fix sample-data)))
(is (not @(re-frame/subscribe [::subs/dropdown-open?])))
(is (not @(re-frame/subscribe [::subs/dropdown-open? "main"])))
(is (= @(re-frame/subscribe [::subs/dropdown-active-item])
"sample data"))))))

Expand Down

0 comments on commit f3b5564

Please sign in to comment.