From 81c75d953b0b20de18287f20ab70f1c064a8cca0 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 23 Oct 2016 17:21:46 +0200 Subject: [PATCH] Add copy and move tooltips to images page. --- src/uxbox/main/data/images.cljs | 76 +++++++++++++++++++++++++ src/uxbox/main/repo/images.cljs | 7 +++ src/uxbox/main/ui/dashboard/images.cljs | 69 +++++++++++++++++++--- 3 files changed, 144 insertions(+), 8 deletions(-) diff --git a/src/uxbox/main/data/images.cljs b/src/uxbox/main/data/images.cljs index 4cb850ef1..537376472 100644 --- a/src/uxbox/main/data/images.cljs +++ b/src/uxbox/main/data/images.cljs @@ -219,6 +219,33 @@ {:pre [(or (uuid? id) (nil? id))]} (CreateImages. id files)) +;; --- Image Updated + +(defrecord ImageUpdated [id data] + rs/UpdateEvent + (-apply-update [_ state] + (assoc-in state [:images-by-id id] data))) + +(defn image-updated + [{:keys [id] :as data}] + {:pre [(map? data)]} + (ImageUpdated. id data)) + +;; --- Update Image + +(defrecord UpdateImage [id] + rs/WatchEvent + (-apply-watch [_ state stream] + (let [image (get-in state [:images-by-id id])] + (->> (rp/req :update/image image) + (rx/map :payload) + (rx/map image-updated))))) + +(defn update-image + [id] + {:pre [(uuid? id)]} + (UpdateImage. id)) + ;; --- Images Fetched (defrecord ImagesFetched [items] @@ -323,10 +350,59 @@ (DeselectImage. id) (SelectImage. id)))))) +(defn deselect-image + [id] + {:pre [(uuid? id)]} + (DeselectImage. id)) + (defn toggle-image-selection [id] (ToggleImageSelection. id)) +;; --- Copy Selected Image + +(defrecord CopySelected [id] + rs/WatchEvent + (-apply-watch [_ state stream] + (let [selected (get-in state [:dashboard :images :selected]) + selected (map #(get-in state [:images-by-id %]) selected)] + (->> (rx/from-coll selected) + (rx/map #(dissoc % :id)) + (rx/map #(assoc % :collection id)) + (rx/flat-map #(rp/req :create/image %)) + (rx/map :payload) + (rx/map image-created))))) + +(defn copy-selected + [id] + {:pre [(or (uuid? id) (nil? id))]} + (CopySelected. id)) + +;; --- Move Selected Image + +(defrecord MoveSelected [id] + rs/UpdateEvent + (-apply-update [_ state] + (let [selected (get-in state [:dashboard :images :selected])] + (reduce (fn [state image] + (assoc-in state [:images-by-id image :collection] id)) + state + selected))) + + rs/WatchEvent + (-apply-watch [_ state stream] + (let [selected (get-in state [:dashboard :images :selected])] + (rx/merge + (->> (rx/from-coll selected) + (rx/map update-image)) + (->> (rx/from-coll selected) + (rx/map deselect-image)))))) + +(defn move-selected + [id] + {:pre [(or (uuid? id) (nil? id))]} + (MoveSelected. id)) + ;; --- Delete Selected (defrecord DeleteSelected [] diff --git a/src/uxbox/main/repo/images.cljs b/src/uxbox/main/repo/images.cljs index 9fa0f7192..95bf60c39 100644 --- a/src/uxbox/main/repo/images.cljs +++ b/src/uxbox/main/repo/images.cljs @@ -70,3 +70,10 @@ [_ id] (let [url (str url "/library/images/" id)] (send! {:url url :method :delete}))) + +(defmethod request :update/image + [_ {:keys [id collection] :as body}] + (let [params {:url (str url "/library/images/" id) + :method :put + :body body}] + (send! params))) diff --git a/src/uxbox/main/ui/dashboard/images.cljs b/src/uxbox/main/ui/dashboard/images.cljs index 8e5f6a8ca..52b02ef4f 100644 --- a/src/uxbox/main/ui/dashboard/images.cljs +++ b/src/uxbox/main/ui/dashboard/images.cljs @@ -202,27 +202,80 @@ :type "file" :on-change on-file-selected}]])) -(mx/defc grid-options - [{:keys [type] :as coll}] - (let [editable? (or (= type :own) (nil? coll))] +(mx/defc grid-options-copy + {:mixins [mx/reactive mx/static]} + [current-coll] + {:pre [(uuid? current-coll)]} + (let [colls (mx/react collections-ref) + colls (->> (vals colls) + (filter #(= :own (:type %))) + (remove #(= current-coll (:id %))) + (sort-by :name colls)) + on-select (fn [event id] + (dom/prevent-default event) + (rs/emit! (di/copy-selected id)))] + [:ul.move-list + [:li.title "Copy to library"] + [:li [:a {:href "#" :on-click #(on-select % nil)} "Storage"]] + (for [coll colls + :let [id (:id coll) + name (:name coll)]] + [:li {:key (str id)} + [:a {:on-click #(on-select % id)} name]])])) + +(mx/defc grid-options-move + {:mixins [mx/reactive mx/static]} + [current-coll] + {:pre [(uuid? current-coll)]} + (let [colls (mx/react collections-ref) + colls (->> (vals colls) + (filter #(= :own (:type %))) + (remove #(= current-coll (:id %))) + (sort-by :name colls)) + on-select (fn [event id] + (println "on-select" event id) + (dom/prevent-default event) + (rs/emit! (di/move-selected id)))] + [:ul.move-list + [:li.title "Move to library"] + [:li [:a {:href "#" :on-click #(on-select % nil)} "Storage"]] + (for [coll colls + :let [id (:id coll) + name (:name coll)]] + [:li {:key (str id)} + [:a {:on-click #(on-select % id)} name]])])) + +(mx/defcs grid-options + {:mixins [(mx/local) mx/static]} + [own {:keys [type id] :as coll}] + (let [editable? (or (= type :own) (nil? coll)) + local (:rum/local own)] (letfn [(delete [] (rs/emit! (di/delete-selected))) (on-delete [event] - (udl/open! :confirm {:on-accept delete}))] + (udl/open! :confirm {:on-accept delete})) + (on-toggle-copy [event] + (swap! local update :show-copy-tooltip not)) + (on-toggle-move [event] + (swap! local update :show-move-tooltip not))] ;; MULTISELECT OPTIONS BAR [:div.multiselect-bar (if editable? [:div.multiselect-nav - #_[:span.move-item.tooltip.tooltip-top - {:alt "Move to"} - i/organize] + [:span.move-item.tooltip.tooltip-top + {:on-click on-toggle-move} + (when (:show-move-tooltip @local) + (grid-options-move id)) + i/organize] [:span.delete.tooltip.tooltip-top {:alt "Delete" :on-click on-delete} i/trash]] [:div.multiselect-nav [:span.move-item.tooltip.tooltip-top - {:alt "Copy to"} + {:on-click on-toggle-copy} + (when (:show-copy-tooltip @local) + (grid-options-copy id)) i/organize]])]))) (mx/defc grid-item