This commit is contained in:
jem 2021-11-05 14:40:40 +01:00
parent bb7757338c
commit c4e1539958

View file

@ -6,12 +6,14 @@
[orchestra.core :refer-macros [defn-spec]] [orchestra.core :refer-macros [defn-spec]]
[hickory.render :as hr])) [hickory.render :as hr]))
(def js-object? any?)
(defn-spec print-debug string? (defn-spec print-debug string?
[sth string?] [sth string?]
(print "debug " sth) (print "debug " sth)
sth) sth)
(defn-spec get-element-by-id any? (defn-spec get-element-by-id js-object?
[name string?] [name string?]
(-> js/document (-> js/document
(.getElementById name))) (.getElementById name)))
@ -19,7 +21,7 @@
(s/def ::deserializer fn?) (s/def ::deserializer fn?)
(s/def ::optional boolean?) (s/def ::optional boolean?)
(def dom-function-parameter (s/keys :opt-un [::deserializer ::optional])) (def dom-function-parameter (s/keys :opt-un [::deserializer ::optional]))
(defn-spec get-content-from-element any? (defn-spec get-content-from-element js-object?
[name string? [name string?
& {:keys [deserializer optional] & {:keys [deserializer optional]
:or {deserializer nil optional false}} dom-function-parameter] :or {deserializer nil optional false}} dom-function-parameter]
@ -34,9 +36,9 @@
:else :else
content))) content)))
(defn-spec set-validation-result! any? (defn-spec set-validation-result! js-object?
[name string? [name string?
validation-result any?] validation-result js-object?]
(-> (get-element-by-id (str name "-validation")) (-> (get-element-by-id (str name "-validation"))
(.-innerHTML) (.-innerHTML)
(set! validation-result)) (set! validation-result))
@ -44,9 +46,9 @@
(.setCustomValidity validation-result)) (.setCustomValidity validation-result))
validation-result) validation-result)
(defn-spec validate! any? (defn-spec validate! js-object?
[name string? [name string?
spec any? spec js-object?
& {:keys [deserializer optional] & {:keys [deserializer optional]
:or {deserializer nil optional false}} dom-function-parameter] :or {deserializer nil optional false}} dom-function-parameter]
(let [content (get-content-from-element name :optional optional :deserializer deserializer)] (let [content (get-content-from-element name :optional optional :deserializer deserializer)]
@ -56,86 +58,116 @@
(set-validation-result! name (set-validation-result! name
(expound/expound-str spec content {:print-specs? false}))))) (expound/expound-str spec content {:print-specs? false})))))
(defn-spec set-output! any? (defn-spec set-output! js-object?
[input string?] [input string?]
(-> js/document (-> js/document
(.getElementById "output") (.getElementById "output")
(.-value) (.-value)
(set! input))) (set! input)))
(defn-spec set-validated! any? (defn-spec set-form-validated! js-object?
[] []
(-> (get-element-by-id "form") (-> (get-element-by-id "form")
(.-classList) (.-classList)
(.add "was-validated"))) (.add "was-validated")))
(defn-spec ^{:deprecated "0.4"} set-validated! js-object?
[]
(set-form-validated!))
(defn create-js-obj-from-html (defn-spec create-js-obj-from-html js-object?
[html-string] [html-string string?]
(-> js/document (-> js/document
.createRange .createRange
(.createContextualFragment html-string))) (.createContextualFragment html-string)))
(defn append-to-c4k-content (defn-spec append-to-c4k-content js-object?
[js-obj] [js-obj js-object?]
(-> (get-element-by-id "c4k-content") (-> (get-element-by-id "c4k-content")
(.appendChild js-obj))) (.appendChild js-obj)))
(defn append-hickory (defn-spec append-hickory js-object?
[hickory-obj] [hickory-obj map?]
(-> hickory-obj (-> hickory-obj
(hr/hickory-to-html) (hr/hickory-to-html)
(create-js-obj-from-html) (create-js-obj-from-html)
(append-to-c4k-content))) (append-to-c4k-content)))
(defn generate-feedback-tag (defn-spec generate-feedback-tag map?
[id] [id string?]
{:type :element :attrs {:class "invalid-feedback"} :tag :div :content [{:type :element :attrs {:id (str id "-validation")} :tag :pre :content nil}]}) {:type :element
:attrs {:class "invalid-feedback"}
:tag :div
:content [{:type :element
:attrs {:id (str id "-validation")}
:tag :pre
:content nil}]})
(defn generate-label (defn-spec generate-label map?
[id-for [id-for string?
label] label string?]
{:type :element :attrs {:for id-for :class "form-label"} :tag :label :content [label]}) {:type :element
:attrs {:for id-for :class "form-label"}
:tag :label
:content [label]})
(defn generate-br (defn-spec generate-br map?
[] []
{:type :element, :attrs nil, :tag :br, :content nil}) {:type :element, :attrs nil, :tag :br, :content nil})
(defn generate-input-field (defn-spec generate-input-field map?
[id [id string?
label label string?
default-value] default-value string?]
[(generate-label id label) [(generate-label id label)
{:type :element :attrs {:class "form-control" :type "text" :name id :id id :value default-value} :tag :input :content nil} {:type :element
:attrs {:class "form-control" :type "text" :name id :id id :value default-value}
:tag :input
:content nil}
(generate-feedback-tag id) (generate-feedback-tag id)
(generate-br)]) (generate-br)])
(defn generate-text-area (defn-spec generate-text-area map?
[id [id string?
label label string?
default-value default-value string?
rows] rows pos-int?]
[(generate-label id label) [(generate-label id label)
{:type :element :attrs {:name id :id id :class "form-control" :rows rows} :tag :textarea :content [default-value]} {:type :element
:attrs {:name id :id id :class "form-control" :rows rows}
:tag :textarea
:content [default-value]}
(generate-feedback-tag id) (generate-feedback-tag id)
(generate-br)]) (generate-br)])
(defn generate-button (defn-spec generate-button map?
[id [id string?
label] label string?]
[{:type :element [{:type :element
:attrs {:type "button", :id id, :class "btn btn-primary"} :attrs {:type "button", :id id, :class "btn btn-primary"}
:tag :button :tag :button
:content [label]} :content [label]}
(generate-br)]) (generate-br)])
(defn generate-output (defn-spec generate-output vector?
[id [id string?
label label string?
rows] rows pos-int?]
[{:type :element, :attrs {:id id}, :tag :div, :content [{:type :element, :attrs {:for "output", :class "form-label"}, :tag :label, :content [label]} [{:type :element,
{:type :element, :attrs {:name "output", :id "output", :class "form-control", :rows rows}, :tag :textarea, :content []}]} :attrs {:id id},
:tag :div,
:content [{:type :element
:attrs {:for "output", :class "form-label"}
:tag :label, :content [label]}
{:type :element, :attrs {:name "output",
:id "output",
:class "form-control",
:rows rows},
:tag :textarea, :content []}]}
(generate-br)]) (generate-br)])
(defn generate-needs-validation (defn-spec generate-needs-validation map?
[] []
{:type :element, :attrs {:class "needs-validation", :id "form"}, :tag :form, :content []}) {:type :element,
:attrs {:class "needs-validation", :id "form"},
:tag :form,
:content []})