diff --git a/src/uxbox/main/data/history.cljs b/src/uxbox/main/data/history.cljs index 355ce3217..4350f7ba6 100644 --- a/src/uxbox/main/data/history.cljs +++ b/src/uxbox/main/data/history.cljs @@ -16,7 +16,6 @@ [uxbox.util.schema :as sc] [uxbox.main.data.pages :as udp] [uxbox.main.state :as st] - [uxbox.main.state.project :as stpr] [uxbox.util.datetime :as dt] [uxbox.util.data :refer (without-keys replace-by-id @@ -135,7 +134,7 @@ :history true :data (:data item))] (-> state - (stpr/unpack-page page) + (udp/unpack-page page) (assoc-in [:workspace :history :selected] version))))) (defn select-page-history @@ -165,7 +164,7 @@ rs/UpdateEvent (-apply-update [_ state] (let [packed (get-in state [:pagedata-by-id id])] - (-> (stpr/unpack-page state packed) + (-> (udp/unpack-page state packed) (assoc-in [:workspace :history :deselecting] true) (assoc-in [:workspace :history :selected] nil)))) diff --git a/src/uxbox/main/data/pages.cljs b/src/uxbox/main/data/pages.cljs index d760d019f..960b5857c 100644 --- a/src/uxbox/main/data/pages.cljs +++ b/src/uxbox/main/data/pages.cljs @@ -16,7 +16,6 @@ [uxbox.util.i18n :refer (tr)] [uxbox.util.schema :as sc] [uxbox.main.state :as st] - [uxbox.main.state.project :as stpr] [uxbox.util.datetime :as dt] [uxbox.util.data :refer (without-keys replace-by-id)])) @@ -24,14 +23,69 @@ "A marker protocol for mark events that alters the page and is subject to perform a backend synchronization.") +;; --- Page Pack/Unpack + +(defn dissoc-page-shapes + [state id] + (let [shapes (get-in state [:shapes-by-id])] + (assoc state :shapes-by-id (reduce-kv (fn [acc k v] + (if (= (:page v) id) + (dissoc acc k) + acc)) + shapes + shapes)))) + +(defn pack-page + "Return a packed version of page object ready + for send to remore storage service." + [state id] + (let [page (get-in state [:pages-by-id id]) + xf (filter #(= (:page (second %)) id)) + shapes (into {} xf (:shapes-by-id state))] + (-> page + (assoc-in [:data :shapes] (into [] (:shapes page))) + (assoc-in [:data :shapes-by-id] shapes) + (update-in [:data] dissoc :items) + (dissoc :shapes)))) + +(defn unpack-page + "Unpacks packed page object and assocs it to the + provided state." + [state page] + (let [data (:data page) + shapes (:shapes data) + shapes-by-id (:shapes-by-id data) + page (-> page + (dissoc page :data) + (assoc :shapes shapes))] + (-> state + (update :shapes-by-id merge shapes-by-id) + (update :pages-by-id assoc (:id page) page)))) + +(defn purge-page + "Remove page and all related stuff from the state." + [state id] + (-> state + (update :pages-by-id dissoc id) + (update :pagedata-by-id dissoc id) + (dissoc-page-shapes id))) + +(defn assoc-page + [state {:keys [id] :as page}] + (assoc-in state [:pagedata-by-id id] page)) + +(defn dissoc-page + [state id] + (update state :pagedata-by-id dissoc id)) + ;; --- Pages Fetched (defrecord PagesFetched [pages] rs/UpdateEvent (-apply-update [_ state] (as-> state $ - (reduce stpr/unpack-page $ pages) - (reduce stpr/assoc-page $ pages)))) + (reduce unpack-page $ pages) + (reduce assoc-page $ pages)))) (defn pages-fetched? [v] @@ -56,8 +110,8 @@ (-apply-watch [this state s] (letfn [(on-created [{page :payload}] (rx/of - #(stpr/unpack-page % page) - #(stpr/assoc-page % page)))] + #(unpack-page % page) + #(assoc-page % page)))] (let [params (-> (into {} this) (assoc :data {}))] (->> (rp/req :create/page params) @@ -82,7 +136,7 @@ (-apply-update [this state] (-> state (assoc-in [:pages-by-id (:id page) :version] (:version page)) - (stpr/assoc-page page)))) + (assoc-page page)))) (defn- page-synced? [event] @@ -93,7 +147,7 @@ (defrecord SyncPage [id] rs/WatchEvent (-apply-watch [this state s] - (let [page (stpr/pack-page state id)] + (let [page (pack-page state id)] (->> (rp/req :update/page page) (rx/map (comp ->PageSynced :payload)))))) @@ -188,7 +242,7 @@ rs/WatchEvent (-apply-watch [_ state s] (letfn [(on-success [_] - #(stpr/purge-page % id))] + #(purge-page % id))] (->> (rp/req :delete/page id) (rx/map on-success) (rx/tap callback) diff --git a/src/uxbox/main/data/projects.cljs b/src/uxbox/main/data/projects.cljs index 705db8a54..a2993313d 100644 --- a/src/uxbox/main/data/projects.cljs +++ b/src/uxbox/main/data/projects.cljs @@ -14,7 +14,6 @@ [uxbox.main.repo :as rp] [uxbox.util.i18n :refer (tr)] [uxbox.util.schema :as sc] - [uxbox.main.state.project :as stpr] [uxbox.main.data.pages :as udp])) ;; --- Initialize @@ -46,10 +45,12 @@ ;; --- Projects Fetched +(declare assoc-project) + (defrecord ProjectsFetched [projects] rs/UpdateEvent (-apply-update [_ state] - (reduce stpr/assoc-project state projects))) + (reduce assoc-project state projects))) (defn projects-fetched [projects] @@ -77,7 +78,7 @@ (defrecord ProjectCreated [project] rs/UpdateEvent (-apply-update [_ state] - (stpr/assoc-project state project))) + (assoc-project state project))) (defn project-created [data] @@ -113,11 +114,13 @@ ;; --- Delete Project (by id) +(declare dissoc-project) + (defrecord DeleteProject [id] rs/WatchEvent (-apply-watch [_ state s] (letfn [(on-success [_] - #(stpr/dissoc-project % id))] + #(dissoc-project % id))] (->> (rp/req :delete/project id) (rx/map on-success))))) @@ -163,24 +166,7 @@ ([projectid] (GoTo. projectid)) ([projectid pageid] (GoToPage. projectid pageid))) -;; --- Helpers - -(defn sort-projects-by - [ordering projs] - (case ordering - :name (sort-by :name projs) - :created (reverse (sort-by :created-at projs)) - projs)) - -(defn contains-term? - [phrase term] - (str/contains? (str/lower phrase) (str/trim (str/lower term)))) - -(defn filter-projects-by - [term projs] - (if (str/blank? term) - projs - (filter #(contains-term? (:name %) term) projs))) +;; --- UI related events (defn set-project-ordering [order] @@ -202,3 +188,36 @@ rs/UpdateEvent (-apply-update [_ state] (assoc-in state [:dashboard :project-filter] "")))) + +;; --- Helpers + +(defn sort-projects-by + [ordering projs] + (case ordering + :name (sort-by :name projs) + :created (reverse (sort-by :created-at projs)) + projs)) + +(defn contains-term? + [phrase term] + (str/contains? (str/lower phrase) (str/trim (str/lower term)))) + +(defn filter-projects-by + [term projs] + (if (str/blank? term) + projs + (filter #(contains-term? (:name %) term) projs))) + +(defn assoc-project + "A reduce function for assoc the project + to the state map." + [state proj] + (let [id (:id proj)] + (update-in state [:projects-by-id id] merge proj))) + +(defn dissoc-project + "A reduce function for dissoc the project + from the state map." + [state id] + (update-in state [:projects-by-id] dissoc id)) + diff --git a/src/uxbox/main/state/project.cljs b/src/uxbox/main/state/project.cljs deleted file mode 100644 index 56b1b39da..000000000 --- a/src/uxbox/main/state/project.cljs +++ /dev/null @@ -1,76 +0,0 @@ -(ns uxbox.main.state.project - "A collection of functions for manage shapes insinde the state.") - -(defn assoc-project - "A reduce function for assoc the project - to the state map." - [state proj] - (let [id (:id proj)] - (update-in state [:projects-by-id id] merge proj))) - -(defn dissoc-project - "A reduce function for dissoc the project - from the state map." - [state id] - (update-in state [:projects-by-id] dissoc id)) - -(defn dissoc-page-shapes - [state id] - (let [shapes (get-in state [:shapes-by-id])] - (assoc state :shapes-by-id (reduce-kv (fn [acc k v] - (if (= (:page v) id) - (dissoc acc k) - acc)) - shapes - shapes)))) -(defn assoc-page - [state {:keys [id] :as page}] - (assoc-in state [:pagedata-by-id id] page)) - -(defn dissoc-page - [state id] - (update state :pagedata-by-id dissoc id)) - -(defn pack-page - "Return a packed version of page object ready - for send to remore storage service." - [state id] - (let [page (get-in state [:pages-by-id id]) - xf (filter #(= (:page (second %)) id)) - shapes (into {} xf (:shapes-by-id state))] - (-> page - (assoc-in [:data :shapes] (into [] (:shapes page))) - (assoc-in [:data :shapes-by-id] shapes) - (update-in [:data] dissoc :items) - (dissoc :shapes)))) - -(defn unpack-page - "Unpacks packed page object and assocs it to the - provided state." - [state page] - (let [data (:data page) - shapes (:shapes data) - shapes-by-id (:shapes-by-id data) - page (-> page - (dissoc page :data) - (assoc :shapes shapes))] - (-> state - (update :shapes-by-id merge shapes-by-id) - (update :pages-by-id assoc (:id page) page)))) - -(defn purge-page - "Remove page and all related stuff from the state." - [state id] - (-> state - (update :pages-by-id dissoc id) - (update :pagedata-by-id dissoc id) - (dissoc-page-shapes id))) - -(defn project-pages - "Get a ordered list of pages that - belongs to a specified project." - [state projectid] - (->> (vals (:pages-by-id state)) - (filter #(= projectid (:project %))) - (sort-by :created-at))) - diff --git a/src/uxbox/main/ui/workspace/base.cljs b/src/uxbox/main/ui/workspace/base.cljs index 4e474ebf3..6b13aadb8 100644 --- a/src/uxbox/main/ui/workspace/base.cljs +++ b/src/uxbox/main/ui/workspace/base.cljs @@ -10,7 +10,6 @@ [lentes.core :as l] [uxbox.util.rstore :as rs] [uxbox.main.state :as st] - [uxbox.main.state.project :as stpr] [uxbox.main.data.workspace :as dw] [uxbox.main.data.shapes :as uds] [uxbox.main.geom.point :as gpt] diff --git a/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs b/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs index a1de4fb64..7c2a1a6d9 100644 --- a/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs +++ b/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs @@ -14,7 +14,6 @@ [uxbox.util.router :as r] [uxbox.util.rstore :as rs] [uxbox.main.state :as st] - [uxbox.main.state.project :as stpr] [uxbox.main.library :as library] [uxbox.main.data.projects :as dp] [uxbox.main.data.pages :as udp] diff --git a/src/uxbox/view/data/viewer.cljs b/src/uxbox/view/data/viewer.cljs index 7abc02d3d..73be74a6d 100644 --- a/src/uxbox/view/data/viewer.cljs +++ b/src/uxbox/view/data/viewer.cljs @@ -10,7 +10,6 @@ [uxbox.util.router :as rt] [uxbox.util.schema :as sc] [uxbox.util.data :refer (parse-int)] - [uxbox.main.state.project :as stpr] [uxbox.main.repo :as rp] [uxbox.main.data.pages :as udpg] [uxbox.main.data.projects :as udpj])) @@ -29,30 +28,31 @@ [token] (Initialize. token)) -;; (defrecord Initialize [id token] -;; rs/UpdateEvent -;; (-apply-update [_ state] -;; (assoc state -;; :token token -;; :index id))) - -;; (defn initialize -;; "Initialize the viewer state." -;; [id token] -;; (let [id (parse-int id 0)] -;; (Initialize. id token))) - ;; --- Data Loaded +(defn- unpack-page + "Unpacks packed page object and assocs it to the + provided state." + [state page] + (let [data (:data page) + shapes (:shapes data) + shapes-by-id (:shapes-by-id data) + page (-> page + (dissoc page :data) + (assoc :shapes shapes))] + (-> state + (update :shapes-by-id merge shapes-by-id) + (update :pages conj page)))) + (defrecord DataLoaded [data] rs/UpdateEvent (-apply-update [_ state] (let [project (dissoc data :pages) - pages (vec (:pages data))] + pages (sort-by :created-at (:pages data))] (as-> state $ (assoc $ :project project) - (assoc $ :pages pages) - (reduce stpr/unpack-page $ pages))))) + (assoc $ :pages []) + (reduce unpack-page $ pages))))) (defn data-loaded [data] @@ -97,7 +97,6 @@ "Toggle the enabled flag of the specified tool." [key] {:pre [(keyword? key)]} - (println "toggle-flag" key) (ToggleFlag. key))