ui.cljs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. (ns frontend.ui
  2. (:require [rum.core :as rum]
  3. [frontend.rum :as r]
  4. ["react-transition-group" :refer [TransitionGroup CSSTransition]]
  5. ["react-textarea-autosize" :as TextareaAutosize]
  6. [frontend.util :as util]
  7. [frontend.mixins :as mixins]
  8. [frontend.handler.notification :as notification-handler]
  9. [frontend.state :as state]
  10. [frontend.components.svg :as svg]
  11. [clojure.string :as string]
  12. [goog.object :as gobj]
  13. [goog.dom :as gdom]
  14. [medley.core :as medley]
  15. [frontend.ui.date-picker]))
  16. (defonce transition-group (r/adapt-class TransitionGroup))
  17. (defonce css-transition (r/adapt-class CSSTransition))
  18. (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default")))
  19. (rum/defc ls-textarea [{:keys [on-change] :as -props}]
  20. (let [composition? (atom false)
  21. set-composition? #(reset! composition? %)
  22. on-composition (fn [e]
  23. (case e.type
  24. "compositionend" (do (set-composition? false))
  25. (set-composition? true)))
  26. props (assoc -props
  27. :on-change (fn [e] (when (not @composition?)
  28. (on-change e)))
  29. :on-composition-start on-composition
  30. :on-composition-update on-composition
  31. :on-composition-end on-composition)]
  32. (textarea props)))
  33. (rum/defc dropdown-content-wrapper [state content class]
  34. (let [class (or class
  35. (util/hiccup->class "origin-top-right.absolute.right-0.mt-2.rounded-md.shadow-lg"))]
  36. [:div.dropdown-wrapper
  37. {:class (str class " "
  38. (case state
  39. "entering" "transition ease-out duration-100 transform opacity-0 scale-95"
  40. "entered" "transition ease-out duration-100 transform opacity-100 scale-100"
  41. "exiting" "transition ease-in duration-75 transform opacity-100 scale-100"
  42. "exited" "transition ease-in duration-75 transform opacity-0 scale-95"))}
  43. content]))
  44. ;; public exports
  45. (rum/defcs dropdown < (mixins/modal :open?)
  46. [state content-fn modal-content-fn
  47. & [{:keys [modal-class z-index]
  48. :or {z-index 999}
  49. :as opts}]]
  50. (let [{:keys [open? toggle-fn]} state
  51. modal-content (modal-content-fn state)]
  52. [:div.ml-1.relative {:style {:z-index z-index}}
  53. (content-fn state)
  54. (css-transition
  55. {:in @open? :timeout 0}
  56. (fn [dropdown-state]
  57. (when @open?
  58. (dropdown-content-wrapper dropdown-state modal-content modal-class))))]))
  59. (rum/defc menu-link
  60. [options child]
  61. [:a.block.px-4.py-2.text-sm.text-gray-700.transition.ease-in-out.duration-150.cursor.menu-link.overflow-hidden
  62. options
  63. child])
  64. (rum/defc dropdown-with-links
  65. [content-fn links {:keys [modal-class links-header z-index] :as opts}]
  66. (dropdown
  67. content-fn
  68. (fn [{:keys [close-fn] :as state}]
  69. [:div.py-1.rounded-md.shadow-xs
  70. (when links-header links-header)
  71. (for [{:keys [options title icon]} links]
  72. (let [new-options
  73. (assoc options
  74. :on-click (fn [e]
  75. (when-let [on-click-fn (:on-click options)]
  76. (on-click-fn e))
  77. (close-fn)))
  78. child [:div
  79. {:style {:display "flex" :flex-direction "row"}}
  80. [:div {:style {:margin-right "8px"}} title]
  81. ;; [:div {:style {:position "absolute" :right "8px"}}
  82. ;; icon]
  83. ]]
  84. (rum/with-key
  85. (menu-link new-options child)
  86. title)))])
  87. opts))
  88. (defn button
  89. [text & {:keys [background on-click href]
  90. :as option}]
  91. (let [class "inline-flex.items-center.px-3.py-2.border.border-transparent.text-sm.leading-4.font-medium.rounded-md.text-white.bg-indigo-600.hover:bg-indigo-700.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.active:bg-indigo-700.transition.ease-in-out.duration-150.mt-1"
  92. class (if background (string/replace class "indigo" background) class)]
  93. (if href
  94. [:a.button (merge
  95. {:type "button"
  96. :class (util/hiccup->class class)}
  97. (dissoc option :background))
  98. text]
  99. [:button
  100. (merge
  101. {:type "button"
  102. :class (util/hiccup->class class)}
  103. (dissoc option :background))
  104. text])))
  105. (rum/defc notification-content
  106. [state content status uid]
  107. (when (and content status)
  108. (let [[color-class svg]
  109. (case status
  110. :success
  111. ["text-gray-900"
  112. [:svg.h-6.w-6.text-green-400
  113. {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"}
  114. [:path
  115. {:d "M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
  116. :stroke-width "2"
  117. :stroke-linejoin "round"
  118. :stroke-linecap "round"}]]]
  119. :warning
  120. ["text-gray-900"
  121. [:svg.h-6.w-6.text-yellow-500
  122. {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"}
  123. [:path
  124. {:d "M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
  125. :stroke-width "2"
  126. :stroke-linejoin "round"
  127. :stroke-linecap "round"}]]]
  128. ["text-red-500"
  129. [:svg.h-6.w-6.text-red-500
  130. {:view-box "0 0 20 20", :fill "currentColor"}
  131. [:path
  132. {:clip-rule "evenodd"
  133. :d
  134. "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
  135. :fill-rule "evenodd"}]]])]
  136. [:div.inset-0.flex.items-end.justify-center.px-4.py-3.pointer-events-none.sm:px-6.sm:py-3.sm:items-start.sm:justify-end
  137. {:style {:z-index (if (or (= state "exiting")
  138. (= state "exited"))
  139. -1
  140. 99)
  141. :top "3.2em"}}
  142. [:div.max-w-sm.w-full.shadow-lg.rounded-lg.pointer-events-auto.notification-area
  143. {:class (case state
  144. "entering" "transition ease-out duration-300 transform opacity-0 translate-y-2 sm:translate-x-0"
  145. "entered" "transition ease-out duration-300 transform translate-y-0 opacity-100 sm:translate-x-0"
  146. "exiting" "transition ease-in duration-100 opacity-100"
  147. "exited" "transition ease-in duration-100 opacity-0")}
  148. [:div.rounded-lg.shadow-xs.overflow-hidden
  149. [:div.p-4
  150. [:div.flex.items-start
  151. [:div.flex-shrink-0
  152. svg]
  153. [:div.ml-3.w-0.flex-1
  154. [:div.text-sm.leading-5.font-medium {:style {:margin 0}
  155. :class color-class}
  156. content]]
  157. [:div.ml-4.flex-shrink-0.flex
  158. [:button.inline-flex.text-gray-400.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150
  159. {:on-click (fn []
  160. (notification-handler/clear! uid))}
  161. [:svg.h-5.w-5
  162. {:fill "currentColor", :view-Box "0 0 20 20"}
  163. [:path
  164. {:clip-rule "evenodd"
  165. :d
  166. "M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
  167. :fill-rule "evenodd"}]]]]]]]]])))
  168. (rum/defc notification < rum/reactive
  169. []
  170. (let [contents (state/sub :notification/contents)]
  171. (transition-group
  172. {:class-name "notifications"}
  173. (doall (map (fn [el]
  174. (let [k (first el)
  175. v (second el)]
  176. (css-transition
  177. {:timeout 100
  178. :key (name k)}
  179. (fn [state]
  180. (notification-content state (:content v) (:status v) k)))))
  181. contents)))))
  182. (defn checkbox
  183. [option]
  184. [:input.form-checkbox.h-4.w-4.transition.duration-150.ease-in-out
  185. (merge {:type "checkbox"} option)])
  186. (defn badge
  187. [text option]
  188. [:span.inline-flex.items-center.px-2.5.py-0.5.rounded-full.text-xs.font-medium.leading-4.bg-purple-100.text-purple-800
  189. option
  190. text])
  191. ;; scroll
  192. (defn main-node
  193. []
  194. (gdom/getElement "main-content"))
  195. (defn get-scroll-top []
  196. (.-scrollTop (main-node)))
  197. (defn get-dynamic-style-node
  198. []
  199. (js/document.getElementById "dynamic-style-scope"))
  200. (defn inject-document-devices-envs!
  201. []
  202. (let [cl (.-classList js/document.documentElement)]
  203. (if (util/ios?) (.add cl "is-ios"))
  204. (if (util/safari?) (.add cl "is-safari"))))
  205. (defn inject-dynamic-style-node!
  206. []
  207. (let [style (get-dynamic-style-node)]
  208. (if (nil? style)
  209. (let [node (js/document.createElement "style")]
  210. (set! (.-id node) "dynamic-style-scope")
  211. (.appendChild js/document.head node))
  212. style)))
  213. (defn setup-patch-ios-fixed-bottom-position!
  214. "fix a common issue about ios webpage viewport
  215. when soft keyboard setup"
  216. []
  217. (if (and
  218. (util/ios?)
  219. (not (nil? js/window.visualViewport)))
  220. (let [viewport js/visualViewport
  221. style (get-dynamic-style-node)
  222. sheet (.-sheet style)
  223. raf-pending? (atom false)
  224. set-raf-pending! #(reset! raf-pending? %)
  225. handler
  226. (fn []
  227. (if-not @raf-pending?
  228. (let [f (fn []
  229. (set-raf-pending! false)
  230. (let [vh (+ (.-offsetTop viewport) (.-height viewport))
  231. rule (.. sheet -rules (item 0))
  232. set-top #(set! (.. rule -style -top) (str % "px"))]
  233. (set-top vh)))]
  234. (set-raf-pending! true)
  235. (js/window.requestAnimationFrame f))))]
  236. (.insertRule sheet ".fix-ios-fixed-bottom {bottom:unset !important; transform: translateY(-100%); top: 100vh;}")
  237. (.addEventListener viewport "resize" handler)
  238. (.addEventListener viewport "scroll" handler)
  239. (fn []
  240. (.removeEventListener viewport "resize" handler)
  241. (.removeEventListener viewport "scroll" handler)))))
  242. ;; FIXME: compute the right scroll position when scrolling back to the top
  243. (defn on-scroll
  244. [on-load on-top-reached]
  245. (let [node (main-node)
  246. full-height (gobj/get node "scrollHeight")
  247. scroll-top (gobj/get node "scrollTop")
  248. client-height (gobj/get node "clientHeight")
  249. bottom-reached? (<= (- full-height scroll-top client-height) 100)
  250. top-reached? (= scroll-top 0)]
  251. (when (and bottom-reached? on-load)
  252. (on-load))
  253. (when (and top-reached? on-top-reached)
  254. (on-top-reached))))
  255. (defn attach-listeners
  256. "Attach scroll and resize listeners."
  257. [state]
  258. (let [opts (-> state :rum/args second)
  259. debounced-on-scroll (util/debounce 500 #(on-scroll
  260. (:on-load opts) ; bottom reached
  261. (:on-top-reached opts)))]
  262. (mixins/listen state (main-node) :scroll debounced-on-scroll)))
  263. (rum/defcs infinite-list <
  264. (mixins/event-mixin attach-listeners)
  265. "Render an infinite list."
  266. [state body {:keys [on-load on-top-reached]
  267. :as opts}]
  268. body)
  269. (rum/defcs auto-complete <
  270. (rum/local 0 ::current-idx)
  271. (mixins/event-mixin
  272. (fn [state]
  273. (mixins/on-key-down
  274. state
  275. {;; up
  276. 38 (fn [_ e]
  277. (let [current-idx (get state ::current-idx)
  278. matched (first (:rum/args state))]
  279. (util/stop e)
  280. (cond
  281. (>= @current-idx 1)
  282. (swap! current-idx dec)
  283. (= @current-idx 0)
  284. (reset! current-idx (dec (count matched)))
  285. :else
  286. nil)
  287. (when-let [element (gdom/getElement (str "ac-" @current-idx))]
  288. (let [ac-inner (gdom/getElement "ui__ac-inner")
  289. element-top (gobj/get element "offsetTop")
  290. scroll-top (- (gobj/get element "offsetTop") 360)]
  291. (set! (.-scrollTop ac-inner) scroll-top)))))
  292. ;; down
  293. 40 (fn [state e]
  294. (let [current-idx (get state ::current-idx)
  295. matched (first (:rum/args state))]
  296. (util/stop e)
  297. (let [total (count matched)]
  298. (if (>= @current-idx (dec total))
  299. (reset! current-idx 0)
  300. (swap! current-idx inc)))
  301. (when-let [element (gdom/getElement (str "ac-" @current-idx))]
  302. (let [ac-inner (gdom/getElement "ui__ac-inner")
  303. element-top (gobj/get element "offsetTop")
  304. scroll-top (- (gobj/get element "offsetTop") 360)]
  305. (set! (.-scrollTop ac-inner) scroll-top)))))
  306. ;; enter
  307. 13 (fn [state e]
  308. (util/stop e)
  309. (let [[matched {:keys [on-chosen on-enter]}] (:rum/args state)]
  310. (let [current-idx (get state ::current-idx)]
  311. (if (and (seq matched)
  312. (> (count matched)
  313. @current-idx))
  314. (on-chosen (nth matched @current-idx) false)
  315. (and on-enter (on-enter state))))))}
  316. nil)))
  317. [state matched {:keys [on-chosen
  318. on-shift-chosen
  319. on-enter
  320. empty-div
  321. item-render
  322. class]}]
  323. (let [current-idx (get state ::current-idx)]
  324. [:div#ui__ac {:class class}
  325. (if (seq matched)
  326. [:div#ui__ac-inner
  327. (for [[idx item] (medley/indexed matched)]
  328. (rum/with-key
  329. (menu-link
  330. {:id (str "ac-" idx)
  331. :class (when (= @current-idx idx)
  332. "chosen")
  333. ;; :tab-index -1
  334. :on-click (fn [e]
  335. (util/stop e)
  336. (if (and (gobj/get e "shiftKey") on-shift-chosen)
  337. (on-shift-chosen item)
  338. (on-chosen item)))}
  339. (if item-render (item-render item) item))
  340. idx))]
  341. (when empty-div
  342. empty-div))]))
  343. (def datepicker frontend.ui.date-picker/date-picker)
  344. (defn toggle
  345. [on? on-click]
  346. [:a {:on-click on-click}
  347. [:span.relative.inline-block.flex-shrink-0.h-6.w-11.border-2.border-transparent.rounded-full.cursor-pointer.transition-colors.ease-in-out.duration-200.focus:outline-none.focus:shadow-outline
  348. {:aria-checked "false", :tabindex "0", :role "checkbox"
  349. :class (if on? "bg-indigo-600" "bg-gray-200")}
  350. [:span.inline-block.h-5.w-5.rounded-full.bg-white.shadow.transform.transition.ease-in-out.duration-200
  351. {:class (if on? "translate-x-5" "translate-x-0")
  352. :aria-hidden "true"}]]])
  353. (defn tooltip
  354. [label children]
  355. [:div.Tooltip {:style {:display "inline"}}
  356. [:div {:class "Tooltip__label"}
  357. label]
  358. children])
  359. (defonce modal-show? (atom false))
  360. (rum/defc modal-overlay
  361. [state]
  362. [:div.fixed.inset-0.transition-opacity
  363. {:class (case state
  364. "entering" "ease-out duration-300 opacity-0"
  365. "entered" "ease-out duration-300 opacity-100"
  366. "exiting" "ease-in duration-200 opacity-100"
  367. "exited" "ease-in duration-200 opacity-0")}
  368. [:div.absolute.inset-0.bg-gray-500.opacity-75]])
  369. (rum/defc modal-panel
  370. [panel-content state close-fn]
  371. [:div.relative.bg-white.rounded-lg.px-4.pt-5.pb-4.overflow-hidden.shadow-xl.transform.transition-all.sm:max-w-lg.sm:w-full.sm:p-6
  372. {:class (case state
  373. "entering" "ease-out duration-300 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  374. "entered" "ease-out duration-300 opacity-100 translate-y-0 sm:scale-100"
  375. "exiting" "ease-in duration-200 opacity-100 translate-y-0 sm:scale-100"
  376. "exited" "ease-in duration-200 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95")}
  377. [:div.absolute.top-0.right-0.pt-4.pr-4
  378. [:button.text-gray-400.hover:text-gray-500.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150
  379. {:aria-label "Close"
  380. :type "button"
  381. :on-click close-fn}
  382. [:svg.h-6.w-6
  383. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  384. [:path
  385. {:d "M6 18L18 6M6 6l12 12"
  386. :stroke-width "2"
  387. :stroke-linejoin "round"
  388. :stroke-linecap "round"}]]]]
  389. (panel-content close-fn)])
  390. (rum/defc modal < rum/reactive
  391. []
  392. (let [modal-panel-content (state/sub :modal/panel-content)
  393. show? (boolean modal-panel-content)
  394. close-fn #(state/close-modal!)
  395. modal-panel-content (or modal-panel-content (fn [close] [:div]))]
  396. [:div.fixed.bottom-0.inset-x-0.px-4.pb-4.sm:inset-0.sm:flex.sm:items-center.sm:justify-center
  397. {:style {:z-index (if show? 10 -1)}}
  398. (css-transition
  399. {:in show? :timeout 0}
  400. (fn [state]
  401. (modal-overlay state)))
  402. (css-transition
  403. {:in show? :timeout 0}
  404. (fn [state]
  405. (modal-panel modal-panel-content state close-fn)))]))
  406. (defn loading
  407. [content]
  408. [:div.flex.flex-row.align-center
  409. [:span.lds-dual-ring.mr-2]
  410. [:span {:style {:margin-top 2}}
  411. content]])
  412. (rum/defcs foldable <
  413. (rum/local false ::control?)
  414. (rum/local false ::collapsed?)
  415. {:will-mount (fn [state]
  416. (let [args (:rum/args state)]
  417. (when (true? (last args))
  418. (reset! (get state ::collapsed?) true)))
  419. state)}
  420. [state header content default-collapsed?]
  421. (let [control? (get state ::control?)
  422. collapsed? (get state ::collapsed?)]
  423. [:div.flex.flex-col
  424. [:div.content
  425. [:div.flex-1.flex-row.foldable-title {:on-mouse-over #(reset! control? true)
  426. :on-mouse-out #(reset! control? false)}
  427. [:div.flex.flex-row.items-center
  428. [:a.block-control.opacity-50.hover:opacity-100.mr-2
  429. {:style {:width 14
  430. :height 16
  431. :margin-left -24}
  432. :on-click (fn [e]
  433. (util/stop e)
  434. (swap! collapsed? not))}
  435. (cond
  436. @collapsed?
  437. (svg/caret-right)
  438. @control?
  439. (svg/caret-down)
  440. :else
  441. [:span ""])]
  442. (if (fn? header)
  443. (header @collapsed?)
  444. header)]]]
  445. [:div {:class (if @collapsed?
  446. "hidden"
  447. "initial")}
  448. (cond
  449. (and (fn? content) (not @collapsed?))
  450. (content)
  451. (fn? content)
  452. nil
  453. :else
  454. content)]]))
  455. (defn admonition
  456. [type content]
  457. (let [type (name type)]
  458. (when-let [icon (case (string/lower-case type)
  459. "note" svg/note
  460. "tip" svg/tip
  461. "important" svg/important
  462. "caution" svg/caution
  463. "warning" svg/warning
  464. nil)]
  465. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  466. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  467. {:title (string/upper-case type)} (icon)]
  468. [:div.ml-4.text-lg
  469. content]])))
  470. (rum/defcs catch-error
  471. < {:did-catch
  472. (fn [state error info]
  473. (js/console.dir error)
  474. (assoc state ::error error))}
  475. [{error ::error, c :rum/react-component} error-view view]
  476. (if (some? error)
  477. error-view
  478. view))
  479. (rum/defc select
  480. [options on-change]
  481. [:select.mt-1.form-select.block.w-full.px-3.text-base.leading-6.border-gray-300.focus:outline-none.focus:shadow-outline-blue.focus:border-blue-300.sm:text-sm.sm:leading-5.ml-4
  482. {:style {:padding "0 0 0 12px"}
  483. :on-change (fn [e]
  484. (let [value (util/evalue e)]
  485. (on-change value)))}
  486. (for [{:keys [label value selected]} options]
  487. [:option (cond->
  488. {:key label
  489. :value (or value label)}
  490. selected
  491. (assoc :selected selected))
  492. label])])