0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 15:39:50 -05:00
penpot/frontend/uxbox/ui/dashboard.cljs

280 lines
8.8 KiB
Text
Raw Normal View History

2015-12-13 13:04:34 +02:00
(ns uxbox.ui.dashboard
(:require [sablono.core :as html :refer-macros [html]]
[rum.core :as rum]
2015-12-14 14:17:18 +02:00
[cats.labs.lens :as l]
2015-12-14 10:48:50 +02:00
[cuerdas.core :as str]
2015-12-13 13:04:34 +02:00
[uxbox.util :as util]
[uxbox.router :as r]
2015-12-14 14:17:18 +02:00
[uxbox.rstore :as rs]
[uxbox.state :as s]
[uxbox.data.projects :as dp]
[uxbox.ui.icons.dashboard :as icons]
2015-12-13 13:04:34 +02:00
[uxbox.ui.icons :as i]
2015-12-14 09:50:19 +02:00
[uxbox.ui.dom :as dom]
[uxbox.ui.header :as ui.h]
[uxbox.ui.lightbox :as lightbox]
2015-12-13 13:04:34 +02:00
[uxbox.time :refer [ago]]))
2015-12-14 14:17:18 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers & Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FIXME rename
(def +ordering-options+ {:name "name"
:last-updated "date updated"
:created "date created"})
2015-12-13 13:04:34 +02:00
2015-12-14 14:17:18 +02:00
(def +layouts+ {:mobile {:name "Mobile"
:id "mobile"
:width 320
:height 480}
:tablet {:name "Tablet"
:id "tablet"
:width 1024
:height 768}
:notebook {:name "Notebook"
:id "notebook"
:width 1366
:height 768}
:desktop {:name "Desktop"
:id "desktop"
:width 1920
:height 1080}})
2015-12-13 13:04:34 +02:00
2015-12-14 14:17:18 +02:00
(def ^:static ^:private
+project-defaults+ {:name ""
:width 1920
:height 1080
:layout :desktop})
2015-12-13 13:04:34 +02:00
2015-12-14 14:17:18 +02:00
;; (def name->order (into {} (for [[k v] project-orderings] [v k])))
2015-12-13 13:04:34 +02:00
;; Views
2015-12-14 09:50:19 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lightbox
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-13 13:04:34 +02:00
(defn layout-input
2015-12-14 10:48:50 +02:00
[local layout-id]
2015-12-14 14:17:18 +02:00
(let [layout (get-in +layouts+ [layout-id])
2015-12-14 10:48:50 +02:00
id (:id layout)
name (:name layout)
width (:width layout)
height (:height layout)]
(html
2015-12-14 14:17:18 +02:00
[:div
[:input
{:type "radio"
:key id
:id id
:name "project-layout"
:value name
:checked (= layout-id (:layout @local))
:on-change #(swap! local merge {:layout layout-id :width width :height height})}]
[:label {:value (:name @local) :for id} name]])))
2015-12-14 09:50:19 +02:00
(defn- layout-selector
[local]
(html
[:div.input-radio.radio-primary
2015-12-14 14:17:18 +02:00
(layout-input local :mobile)
(layout-input local :tablet)
(layout-input local :notebook)
(layout-input local :desktop)]))
2015-12-14 09:50:19 +02:00
(defn- new-project-lightbox-render
[own]
(let [local (:rum/local own)
2015-12-14 10:48:50 +02:00
name (:name @local)
2015-12-14 09:50:19 +02:00
width (:width @local)
height (:height @local)]
(html
2015-12-13 13:04:34 +02:00
[:div.lightbox-body
[:h3 "New project"]
2015-12-14 09:50:19 +02:00
[:form {:on-submit (constantly nil)}
2015-12-13 13:04:34 +02:00
[:input#project-name.input-text
2015-12-14 09:50:19 +02:00
{:placeholder "New project name"
:type "text"
:value name
:auto-focus true
:on-change #(swap! local assoc :name (.-value (.-target %)))}]
2015-12-13 13:04:34 +02:00
[:div.project-size
[:input#project-witdh.input-text
{:placeholder "Width"
:type "number"
:min 0 ;;TODO check this value
:max 666666 ;;TODO check this value
:value width
2015-12-14 09:50:19 +02:00
:on-change #(swap! local assoc :width (.-value (.-target %)))}]
2015-12-13 13:04:34 +02:00
[:a.toggle-layout
{:href "#"
2015-12-14 09:50:19 +02:00
:on-click #(swap! local assoc :width width :height height)}
2015-12-13 13:04:34 +02:00
i/toggle]
[:input#project-height.input-text
{:placeholder "Height"
:type "number"
:min 0 ;;TODO check this value
:max 666666 ;;TODO check this value
:value height
2015-12-14 09:50:19 +02:00
:on-change #(swap! local assoc :height (.-value (.-target %)))}]]
2015-12-13 13:04:34 +02:00
;; Layout selector
2015-12-14 09:50:19 +02:00
(layout-selector local)
2015-12-13 13:04:34 +02:00
;; Submit
2015-12-14 10:48:50 +02:00
(when-not (empty? (str/trim name))
2015-12-13 13:04:34 +02:00
[:input#project-btn.btn-primary
{:value "Go go go!"
2015-12-14 14:17:18 +02:00
:on-click #(do
(dom/prevent-default %)
(rs/emit! (dp/create-project @local))
(lightbox/close!))
2015-12-13 13:04:34 +02:00
:type "submit"}])]
2015-12-14 14:17:18 +02:00
[:a.close {:href "#"
:on-click #(do (dom/prevent-default %)
(lightbox/close!))}
2015-12-14 09:50:19 +02:00
i/close]])))
2015-12-13 13:04:34 +02:00
2015-12-14 09:50:19 +02:00
;; (.preventDefault e)
;; (let [new-project-attributes {:name (trim name)
;; :width (int width)
;; :height (int height)
;; :layout layout}]
;; ;; (actions/create-project conn new-project-attributes)
;; (close-lightbox!)))}
2015-12-13 13:04:34 +02:00
2015-12-14 09:50:19 +02:00
(def new-project-lightbox
(util/component
{:render new-project-lightbox-render
:name "new-project-lightbox"
2015-12-14 14:17:18 +02:00
:mixins [(rum/local +project-defaults+)]}))
2015-12-13 13:04:34 +02:00
2015-12-14 09:50:19 +02:00
(defmethod lightbox/render-lightbox :new-project
[_]
(new-project-lightbox))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Menu
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-13 13:04:34 +02:00
2015-12-14 14:17:18 +02:00
(def ^:static menu-state
(as-> (l/select-keys [:projects]) $
(l/focus-atom $ s/state)))
2015-12-13 13:04:34 +02:00
(rum/defc project-sort-selector < rum/reactive
[sort-order]
2015-12-14 14:17:18 +02:00
nil)
;; (let [sort-name (get project-orderings (rum/react sort-order))]
;; [:select.input-select
;; {:on-change #(reset! sort-order (name->order (.-value (.-target %))))
;; :value sort-name}
;; (for [order (keys project-orderings)
;; :let [name (get project-orderings order)]]
;; [:option {:key name} name])]))
2015-12-14 09:50:19 +02:00
(defn menu-render
2015-12-13 13:04:34 +02:00
[]
2015-12-14 14:17:18 +02:00
(let [state (rum/react menu-state)
pcount (count (:projects state))]
(html
[:section#dashboard-bar.dashboard-bar
[:div.dashboard-info
[:span.dashboard-projects pcount " projects"]
[:span "Sort by"]
#_(project-sort-selector (atom :name))]
[:div.dashboard-search
icons/search]])))
2015-12-14 09:50:19 +02:00
(def menu
(util/component
{:render menu-render
2015-12-14 14:17:18 +02:00
:name "dashboard-menu"
:mixins [rum/reactive]}))
2015-12-14 09:50:19 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Project Item
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn project-render
[own project]
2015-12-14 20:31:21 +02:00
(let [on-click #(rs/emit! (dp/go-to-project (:id project)))]
(html
[:div.grid-item.project-th {:on-click on-click
:key (:id project)}
[:h3 (:name project)]
[:span.project-th-update
(str "Updated " (ago (:last-update project)))]
[:div.project-th-actions
[:div.project-th-icon.pages
icons/page
[:span 0]]
[:div.project-th-icon.comments
i/chat
[:span 0]]
[:div.project-th-icon.delete
{:on-click #(do
(dom/stop-propagation %)
;; (actions/delete-project conn uuid)
%)}
icons/trash]]])))
2015-12-14 09:50:19 +02:00
2015-12-14 14:17:18 +02:00
(def project-item
2015-12-14 09:50:19 +02:00
(util/component
{:render project-render
:name "project"
:mixins [rum/static]}))
2015-12-13 13:04:34 +02:00
;; (defn sorted-projects
;; [projects sort-order]
;; (let [project-cards (map (partial project-card conn) (sort-by sort-order projects))]
;; (if (= sort-order :project/name)
;; project-cards
;; (reverse project-cards))))
2015-12-14 09:50:19 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Grid
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-14 14:17:18 +02:00
(def ^:static grid-state
2015-12-14 23:57:47 +02:00
(as-> (l/select-keys [:projects-by-id]) $
2015-12-14 14:17:18 +02:00
(l/focus-atom $ s/state)))
2015-12-14 09:50:19 +02:00
(defn grid-render
[own]
(letfn [(on-click [e]
(dom/prevent-default e)
(lightbox/set! :new-project))]
2015-12-14 14:17:18 +02:00
(let [state (rum/react grid-state)]
(html
[:section.dashboard-grid
[:h2 "Your projects"]
[:div.dashboard-grid-content
[:div.grid-item.add-project {:on-click on-click}
[:span "+ New project"]]
2015-12-14 23:57:47 +02:00
(for [item (vals (:projects-by-id state))]
2015-12-15 12:58:49 +01:00
(rum/with-key (project-item item) (:id item)))]]))))
2015-12-14 09:50:19 +02:00
(def grid
(util/component
{:render grid-render
:name "grid"
:mixins [rum/reactive]}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-13 13:04:34 +02:00
(defn dashboard-render
[own]
(html
[:main.dashboard-main
2015-12-14 09:50:19 +02:00
(ui.h/header)
2015-12-13 13:04:34 +02:00
[:section.dashboard-content
2015-12-14 09:50:19 +02:00
(menu)
2015-12-14 14:17:18 +02:00
(grid)]]))
2015-12-13 13:04:34 +02:00
(def dashboard
(util/component {:render dashboard-render
2015-12-14 14:17:18 +02:00
:mixins [rum/static]
2015-12-13 13:04:34 +02:00
:name "dashboard"}))