Advertisement
Guest User

Untitled

a guest
May 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defmacro suggestions-comp
  2.   ([comp-name label]
  3.    `(rum/defc ~comp-name ~(quote <) rum/reactive []
  4.       [:div.suggestion-input-comp
  5.        [:div.suggestions-input.flex
  6.         [:input {:type "text" :placeholder ~label}]
  7.         [:div.showhide-icon.show]]]))
  8.   ([comp-name label atom-name atom-ns url]
  9.    `(let [hidden?#       ~(symbol atom-ns (str atom-name "-suggestions-hidden?"))
  10.           suggestions#   ~(symbol atom-ns (str atom-name "-suggestions"))
  11.           current-suggs# ~(symbol atom-ns (str atom-name "-suggestions-cur"))
  12.           input-field#   ~(symbol atom-ns (str atom-name "-input-field"))
  13.           ajax-post#     ~(symbol "ajax.core" "GET")
  14.           swap-chained!# ~(symbol atom-ns (str atom-name "-swap-chained!"))]
  15.       (rum/defcs ~comp-name ~(quote <)
  16.         rum/reactive
  17.         (rum/local "0px" :width)
  18.         (rum/local -1 :active-id)
  19.         {:did-update  (fn [~(quote state)]
  20.                         (assoc ~(quote state) :width (.-width (.getBoundingClientRect (rum/dom-node ~(quote state))))))}
  21.         {:will-update (fn [~(quote state)]
  22.                         (assoc ~(quote state) :width (.-width (.getBoundingClientRect (rum/dom-node ~(quote state))))))}
  23.         {:did-mount   (fn [~(quote state)]
  24.                         (assoc ~(quote state) :width (.-width (.getBoundingClientRect (rum/dom-node ~(quote state))))))}
  25.         [~(quote state)]
  26.         (let [_# (rum/react ~(symbol "project.app-state.core" "resize-sync"))
  27.               width# (:width ~(quote state))
  28.               active-id# (:active-id ~(quote state))
  29.               current-suggs-derefed# (rum/react current-suggs#)]
  30.           [:div.suggestion-input-comp
  31.            [:div.suggestions-input.flex
  32.             [:input
  33.              {:type "text"
  34.               :placeholder ~label
  35.               :value (let [v# (:value (rum/react input-field#))]
  36.                        (cond->> v# (vector? v#) (clojure.string/join " ")))
  37.               :on-change (fn [~(quote ev)]
  38.                            (let [input-val# (-> ~(quote ev) .-target .-value)]
  39.                              (do (swap! input-field# assoc :value input-val#)
  40.                                  (if-let [suggs# (get (deref suggestions#) input-val#)]
  41.                                    (do (reset! hidden?# false)
  42.                                        (reset! current-suggs# suggs#))
  43.                                    (ajax-post# ~url {:handler #(do (swap! suggestions# assoc input-val# (vec %))
  44.                                                                    (reset! hidden?# false)
  45.                                                                    (reset! current-suggs# %))})))))
  46.               :onKeyDown (fn [~(quote ev)]
  47.                            (let [code# (.-keyCode ~(quote ev))]
  48.                              (case code#
  49.                                40 (swap! active-id# #(mod (inc %) (count @current-suggs#)))
  50.                                38 (swap! active-id# #(mod (dec %) (count @current-suggs#)))
  51.                                13 (do (let [choise-result# (nth @current-suggs# @active-id#)]
  52.                                         (swap! input-field# #(-> %
  53.                                                                  (assoc :value (:sugg choise-result#))
  54.                                                                  (assoc :id    (:id choise-result#)))))
  55.                                       (swap-chained!#)
  56.                                       (reset! active-id# -1)
  57.                                       (reset! hidden?# true))
  58.                                27 (reset! hidden?# true)
  59.                                :do-nothing)))}]
  60.             [(keyword (str "div.showhide-icon." (if @hidden?# "show" "hide")))
  61.              {:on-click #(swap! hidden?# not)}]]
  62.            (when-not (rum/react hidden?#)
  63.              (when current-suggs-derefed#
  64.                [:div.suggestions-list
  65.                 {:style {:width (str (- (js/parseInt width#) 14) "px")}}
  66.                 (let [act-id# @active-id#]
  67.                   (for [[idx# el#] (map-indexed vector current-suggs-derefed#)]
  68.                     [(keyword (str "div" (when (= act-id# idx#) ".active")))
  69.                      {:on-click (fn [~(quote ev)]
  70.                                   (do (swap! input-field# #(-> % (assoc :id (:id el#)) (assoc :value (:sugg el#))))
  71.                                       (swap-chained!#)
  72.                                       (reset! hidden?# true)))}
  73.                      (:sugg el#)]))]))])))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement