From 0ef064f075b8bc75daaa557dcb41a2205f80c832 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 15 Dec 2015 23:54:50 +0200 Subject: [PATCH] WIP: improve page edition. --- frontend/uxbox/data/projects.cljs | 13 ++++- frontend/uxbox/ui/workspace.cljs | 89 ++++++++++++++++++------------- 2 files changed, 63 insertions(+), 39 deletions(-) diff --git a/frontend/uxbox/data/projects.cljs b/frontend/uxbox/data/projects.cljs index 10fc514fd..71fe2cce3 100644 --- a/frontend/uxbox/data/projects.cljs +++ b/frontend/uxbox/data/projects.cljs @@ -71,6 +71,17 @@ (-pr-writer [mv writer _] (-write writer "#")))) +(defn update-page-name + [pageid name] + (reify + rs/UpdateEvent + (-apply-update [_ state] + (update-in state [:pages-by-id pageid] assoc :name name)) + + IPrintWithWriter + (-pr-writer [mv writer _] + (-write writer "#")))) + (defn delete-page [pageid] (reify @@ -80,7 +91,7 @@ IPrintWithWriter (-pr-writer [mv writer _] - (-write writer "#")))) + (-write writer "#")))) (defn create-project [{:keys [name width height layout] :as data}] diff --git a/frontend/uxbox/ui/workspace.cljs b/frontend/uxbox/ui/workspace.cljs index 679e90d8a..78204eeb6 100644 --- a/frontend/uxbox/ui/workspace.cljs +++ b/frontend/uxbox/ui/workspace.cljs @@ -11,6 +11,7 @@ [uxbox.ui.icons.dashboard :as icons] [uxbox.ui.icons :as i] [uxbox.ui.lightbox :as lightbox] + [uxbox.ui.keyboard :as k] [uxbox.ui.users :as ui.u] [uxbox.ui.navigation :as nav] [uxbox.ui.dom :as dom])) @@ -143,19 +144,20 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn- project-sidebar-pageitem-render - [own page numpages] - (let [pageid (:id page)] + [own parent page numpages] + (letfn [(on-edit [e] + (let [data {:edit true :form page}] + (reset! parent data)))] (html [:li.single-page {:class "current" - :key pageid :on-click #(dp/go-to-project (:project page) (:id page))} [:div.tree-icon i/page] [:span (:name page)] [:div.options - [:div {:on-click (constantly nil)} i/pencil] + [:div {:on-click on-edit} i/pencil] [:div {:class (when (= 1 (count @pages-state)) "hide") - :on-click #(rs/emit! (dp/delete-page pageid))} + :on-click #(rs/emit! (dp/delete-page (:id page)))} i/trash]]]))) (def project-sidebar-pageitem @@ -173,45 +175,56 @@ [:div.project-bar-inside [:span.project-name name] [:ul.tree-view - (for [page pages] (project-sidebar-pageitem page (count pages)))] + (for [page pages] + (let [component (project-sidebar-pageitem local page (count pages))] + (rum/with-key component (str (:id page)))))] [:button.btn-primary.btn-small {:on-click #(reset! local {:edit true :form {}})} "+ Add new page"]]))) (defn- project-sidebar-form-render [own parent] - (letfn [(on-change [e] - (let [value (str/trim (dom/event->value e))] - (swap! parent assoc-in [:form :name] value))) - (on-save [e] - (let [project @project-state - name (get-in @parent [:form :name]) - data {:project (:id project) - :width (:width project) - :height (:height project) - :name name}] - (rs/emit! (dp/create-page data)) - (reset! parent {:edit false}))) - (on-cancel [e] - (reset! parent {:edit false}))] - (html - [:div.project-bar-inside - [:input.input-text - { - :name "test" - :auto-focus true - :placeholder "Page title" - :type "text" - :value (get-in @parent [:form :name] "") - :on-change on-change - }] - [:button.btn-primary.btn-small - {:disabled (str/empty? (get-in @parent [:form :name] "")) - :on-click on-save} - "Save"] - [:button.btn-primary.btn-small - {:on-click on-cancel} - "Cancel"]]))) + (let [form (:form @parent) + project @project-state] + (letfn [(on-change [e] + (let [value (dom/event->value e)] + (swap! parent assoc-in [:form :name] value))) + (persist [] + (if (nil? (:id form)) + (let [data {:project (:id project) + :width (:width project) + :height (:height project) + :name (:name form)}] + (rs/emit! (dp/create-page data)) + (reset! parent {:edit false})) + (do + (rs/emit! (dp/update-page-name (:id form) (:name form))) + (reset! parent {:edit false})))) + (on-save [e] + (persist)) + (on-key-up [e] + (when (k/enter? e) + (persist))) + (on-cancel [e] + (reset! parent {:edit false}))] + (html + [:div.project-bar-inside + [:input.input-text + {:name "test" + :auto-focus true + :placeholder "Page title" + :type "text" + :value (get-in @parent [:form :name] "") + :on-change on-change + :on-key-up on-key-up + }] + [:button.btn-primary.btn-small + {:disabled (str/empty? (str/trim (get-in @parent [:form :name] ""))) + :on-click on-save} + "Save"] + [:button.btn-primary.btn-small + {:on-click on-cancel} + "Cancel"]])))) (def project-sidebar-form (util/component