-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
bootstrap3.cljc
1211 lines (1044 loc) · 55.7 KB
/
bootstrap3.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns fulcro.ui.bootstrap3
(:require
#?(:cljs [fulcro.client.dom :as dom]
:clj [fulcro.client.dom-server :as dom])
[fulcro.client.primitives :as prim :refer [defui defsc]]
[fulcro.ui.elements :as ele]
[fulcro.events :as evt]
[fulcro.ui.html-entities :as ent]
[fulcro.i18n :refer [tr tr-unsafe]]
#?(:cljs [cljsjs.react.dom])
[fulcro.client.mutations :as m :refer [defmutation]]
#?(:clj
[clojure.future :refer :all])
[clojure.string :as str]
[clojure.set :as set]
[fulcro.client :as fc]
[fulcro.logging :as log]
[fulcro.client.util :as util]))
#?(:clj (defn- clj->js [m] m))
;; Bootstrap CSS and Components for Fulcro
(defn- dom-with-class [dom-factory cls attrs children]
(let [attrs (-> attrs
#_(update :key (fnil identity "placeholder-key"))
(update :className #(str cls " " %)))]
(apply dom-factory (clj->js attrs) children)))
(defn- div-with-class [cls attrs children] (dom-with-class dom/div cls attrs children))
(defn- p-with-class [cls attrs children] (dom-with-class dom/p cls attrs children))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BASE CSS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn container
"Top-level container for bootstrap grid content. This is a responsive fixed-width container. See also container-fluid."
[attrs & children] (div-with-class "container" attrs children))
(defn container-fluid
"Top-level container for bootstrap grid content. This is a responsive full-width container. See also container."
[attrs & children] (div-with-class "container-fluid" attrs children))
(defn row
"Generate a layout row. This is a div container for a row in a 12-wide grid responsive layout.
Rows should contain layout columns generated with the `col` function of this namespace.
The properties are normal DOM attributes as a cljs map and can include standard React DOM properties.
"
[props & children]
(div-with-class "row" props children))
(defn col
"Output a div that represents a column in the 12-column responsive grid.
Any React props are allowed. The following special one pertain to the column:
xs The width of the column on xs screens
sm The width of the column on sm screens
md The width of the column on md screens
lg The width of the column on lg screens
xs-offset The offset of the column on xs screens
sm-offset The offset of the column on sm screens
md-offset The offset of the column on md screens
lg-offset The offset of the column on lg screens
"
[{:keys [className xs sm md lg xs-offset sm-offset md-offset lg-offset] :as props} & children]
(let [classes (cond-> (:className props)
xs (str " col-xs-" xs)
sm (str " col-sm-" sm)
md (str " col-md-" md)
lg (str " col-lg-" lg)
xs-offset (str " col-xs-offset-" xs-offset)
sm-offset (str " col-sm-offset-" sm-offset)
md-offset (str " col-md-offset-" md-offset)
lg-offset (str " col-lg-offset-" lg-offset))
attrs (-> props
(dissoc :xs :sm :md :lg :xs-offset :sm-offset :md-offset :lg-offset)
(assoc :className classes)
clj->js)]
(apply dom/div attrs children)))
(defn lead
"Generates a lead paragraph with an increased font size."
[attrs & children]
(p-with-class "lead" attrs children))
(defn address
"Formats an address"
[name & {:keys [street street2 city-state phone email]}]
(let [brs (repeatedly #(dom/br nil))
children (cond-> (vec (keep identity (list street street2 city-state)))
phone (conj (dom/span nil (dom/abbr {:title "Phone"} "P:") phone)))]
(apply dom/address nil
(dom/strong nil name) (dom/br nil)
(vec (interleave children brs)))))
(defn quotation
"Render a block quotation with optional citation and source.
align - :right or :left (default)
credit - The name of the person.
source - The place where it was said/written, you supply the joining preposition.
children - The DOM element(s) (typicaly a p) that represent the body of the quotation
"
[{:keys [align credit source] :as attrs} & children]
(let [attrs (cond-> (dissoc attrs :align :credit :source)
(= :right align) (assoc :className (str "blockquote-reverse " (:className attrs))))
content (concat children [(dom/footer nil credit " " (dom/cite nil source))])]
(apply dom/blockquote (clj->js attrs) content)))
(defn plain-ul
"Render an unstyled unordered list"
[attrs & children]
(let [attrs (update attrs :className #(str " list-unstyled"))]
(apply dom/ul (clj->js attrs) children)))
(defn inline-ul
"Render an inline unordered list"
[attrs & children]
(let [attrs (update attrs :className #(str " list-inline"))]
(apply dom/ul (clj->js attrs) children)))
(def table-styles #{:striped :bordered :condensed :hover})
(defn table
"Renders a table. attrs can contain any normal react attributes, including :className.
Extended attributes that you can include are:
styles - A set of one or more of: #{:striped :bordered :condensed :hover}
"
[{:keys [className styles] :as attrs} & children]
(let [style-classes (str/join " " (map #(str "table-" (name %)) (set/intersection table-styles styles)))
classes (str (when className (str className " ")) "table " style-classes)
attrs (-> attrs
(dissoc :styles)
(assoc :className classes)
clj->js)]
(apply dom/table attrs children)))
(defn form-horizontal
"Set up a container for labeled inputs (which should have :split) to render the labels beside the fields."
[attrs & children]
(div-with-class "form-horizontal" attrs children))
(defn labeled-input
"An input with a label. All of the attrs apply to the input itself. You must supply a type and id for the
field to work correctly. DO NOT USE for checkbox or radio.
The additional attributes are supported:
:split - A number from 1 to 11. The number of `sm` columns to allocate to the field label. If not specified, the
label will be above the input. The parent must use the `.form-horizontal` class.
:help - A string. If supplied, displayed as the help text for the field. NOT shown if warning, error, or success are set.
:input-generator An (optional) function of `props -> input` to be called to generate the DOM input. It will receive a clj map
of properties and should return an input that has those minimum properties. It can, of course, augment those.
You should only ever supply zero or one of the following:
:warning - A boolean or string. If set as a string, that content replaces the help text.
:error - A boolean or string. If set as a string, that content replaces the help text.
:success - A boolean or string. If set as a string, that content replaces the help text.
"
[{:keys [id type className placeholder split help warning error success
input-generator] :as attrs} label]
(let [state-class (cond
error " has-error"
warning " has-warning"
success " has-success"
:else "")
form-group-classes (str "form-group" state-class)
help (first (keep identity [error warning success help]))
split-right (- 12 split)
help-id (str id "-help")
attrs (cond-> (dissoc attrs :split :help :warning :error :success :input-generator)
help (assoc :aria-describedby help-id)
:always (update :className #(str % " form-control")))]
(cond
(int? split) (dom/div {:className form-group-classes}
(dom/label {:className (str "control-label col-sm-" split) :htmlFor id} label)
(dom/div {:className (str "col-sm-" split-right)}
(if input-generator
(input-generator attrs)
(dom/input (clj->js attrs)))
(when help (dom/span {:id help-id :className "help-block"} help))))
:else (dom/div {:className form-group-classes}
(dom/label {:className "control-label" :htmlFor id} label)
(if input-generator
(input-generator attrs)
(dom/input (clj->js attrs)))
(when help (dom/span {:id help-id :className "help-block"} help))))))
(defn button
"Render a button with optional styling
kind - Optional. One of :primary, :success, :info, :warning, or :danger. Defaults to none (default).
size - Optional. One of :xs, :sm, or :lg. Defaults to a normal size.
as-block - Optional. Boolean. When true makes the button a block element.
"
[{:keys [kind size as-block] :as attrs} & children]
{:pre [(or (nil? kind) (contains? #{:primary :success :info :warning :danger} kind))
(or (nil? size) (contains? #{:xs :sm :lg} size))]}
(let [incoming-classes (:className attrs)
button-classes (cond-> "btn"
kind (str " btn-" (name kind))
size (str " btn-" (name size))
(not kind) (str " btn-default")
as-block (str " btn-block")
incoming-classes (str " " incoming-classes))
attrs (-> attrs
(dissoc :kind :size :as-block)
(assoc :className button-classes)
clj->js)]
(apply dom/button attrs children)))
(defn close-button [attrs]
(let [addl-classes (:className attrs)
classes (cond-> "close"
addl-classes (str " " addl-classes))
attrs (assoc attrs :type "button" :aria-label "Close" :className classes)]
(dom/button (clj->js attrs)
(dom/span {:aria-hidden true} "\u00D7"))))
(defn img
"Render an img tag with bootstrap classes.
is-responsive - Boolean. Marks the image so that it scales to its container.
shape - Optional. One of :rounded, :circle, or :thumbnail
All other normal react attributes (including className) are allowed."
[{:keys [is-responsive shape] :as attrs}]
{:pre [(or (nil? shape) (contains? #{:rounded :circle :thumbnail} shape))]}
(let [incoming-classes (:className attrs)
button-classes (cond-> ""
shape (str " " "img-" (name shape))
is-responsive (str " img-responsive")
incoming-classes (str " " incoming-classes))
attrs (-> attrs
(dissoc :is-responsive :shape)
(assoc :className button-classes)
clj->js)]
(dom/img attrs)))
;; raw classes (for docstrings and autocomplete
(def text-left "A CSS class for Left aligned text." "text-left")
(def text-center "A CSS class for Center aligned text." "text-center")
(def text-right "A CSS class for Right aligned text." "text-right")
(def text-justify "A CSS class for Justified text." "text-justify")
(def text-nowrap "A CSS class for No wrap text." "text-nowrap")
(def text-lowercase "A css transform that will change encosed text to lowercased text." "text-lowercase")
(def text-uppercase "A css transform that will change encosed text to uppercased text." "text-uppercase")
(def text-capitalize "A css transform that will change encosed text to capitalized text." "text-capitalize")
(def text-muted "CSS class for a muted text color" "text-muted")
(def text-primary "A CSS classname for a primary text color" "text-primary")
(def text-success "A CSS classname for a success text color" "text-success")
(def text-info "A CSS classname for a info text color" "text-info")
(def text-warning "A CSS classname for a warning text color" "text-warning")
(def text-danger "A CSS classname for a danger text color" "text-danger")
(def bg-primary "A CSS classname for a contextual primary background color" "bg-primary")
(def bg-success "A CSS classname for a contextual success background color" "bg-success")
(def bg-info "A CSS classname for a contextual info background color" "bg-info")
(def bg-warning "A CSS classname for a contextual warning background color" "bg-warning")
(def bg-danger "A CSS classname for a contextual danger background color" "bg-danger")
(def pull-left "A CSS class for forcing a float" "pull-left")
(def pull-right "A CSS class for forcing a float" "pull-right")
(def center-block "A CSS class for centering a block element" "center-block")
(def clearfix "A CSS class used on the PARENT to clear floats within that parent." "clearfix")
(def show "A CSS class for BLOCK-level elements. Element affects flow and is visible." "show")
(def hidden "A CSS class for BLOCK-level elements. Element is not in flow." "hidden")
(def invisible "A CSS class for BLOCK-level elements. Element still affects flow." "invisible")
(def visible-xs-block "A responsive CSS class to show element according to screen size" "visible-xs-block")
(def visible-xs-inline "A responsive CSS class to show element according to screen size" "visible-xs-inline")
(def visible-xs-inline-block "A responsive CSS class to show element according to screen size" "visible-xs-inline-block")
(def visible-sm-block "A responsive CSS class to show element according to screen size" "visible-sm-block")
(def visible-sm-inline "A responsive CSS class to show element according to screen size" "visible-sm-inline")
(def visible-sm-inline-block "A responsive CSS class to show element according to screen size" "visible-sm-inline-block")
(def visible-md-block "A responsive CSS class to show element according to screen size" "visible-md-block")
(def visible-md-inline "A responsive CSS class to show element according to screen size" "visible-md-inline")
(def visible-md-inline-block "A responsive CSS class to show element according to screen size" "visible-md-inline-block")
(def visible-lg-block "A responsive CSS class to show element according to screen size" "visible-lg-block")
(def visible-lg-inline "A responsive CSS class to show element according to screen size" "visible-lg-inline")
(def visible-lg-inline-block "A responsive CSS class to show element according to screen size" "visible-lg-inline-block")
(def hidden-xs "A CSS class to hide the element according to screen size" "hidden-xs")
(def hidden-sm "A CSS class to hide the element according to screen size" "hidden-sm")
(def hidden-md "A CSS class to hide the element according to screen size" "hidden-md")
(def hidden-lg "A CSS class to hide the element according to screen size" "hidden-lg")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Bootstrap Components
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def glyph-icons #{:asterisk :plus :euro :eur :minus :cloud :envelope :pencil :glass :music :search :heart
:star :star-empty :user :film :th-large :th :th-list :ok :remove :zoom-in :zoom-out :off :signal
:cog :trash :home :file :time :road :download-alt :download :upload :inbox :play-circle
:repeat :refresh :list-alt :lock :flag :headphones :volume-off :volume-down :volume-up :qrcode
:barcode :tag :tags :book :bookmark :print :camera :font :bold :italic :text-height
:text-width :align-left :align-center :align-right :align-justify :list :indent-left :indent-right
:facetime-video :picture :map-marker :adjust :tint :edit :share :check :move :step-backward
:fast-backward :backward :play :pause :stop :forward :fast-forward :step-forward :eject
:chevron-left :chevron-right :plus-sign :minus-sign :remove-sign :ok-sign :question-sign
:info-sign :screenshot :remove-circle :ok-circle :ban-circle :arrow-left :arrow-right :arrow-up
:arrow-down :share-alt :resize-full :resize-small :exclamation-sign :gift :leaf :fire :eye-open
:eye-close :warning-sign :plane :calendar :random :comment :magnet :chevron-up :chevron-down
:retweet :shopping-cart :folder-close :folder-open :resize-vertical :resize-horizontal :hdd :bullhorn :bell
:certificate :thumbs-up :thumbs-down :hand-right :hand-left :hand-up :hand-down :circle-arrow-right :circle-arrow-left
:circle-arrow-up :circle-arrow-down :globe :wrench :tasks :filter :briefcase :fullscreen :dashboard
:paperclip :heart-empty :link :phone :pushpin :usd :gbp :sort :sort-by-alphabet
:sort-by-alphabet-alt :sort-by-order :sort-by-order-alt :sort-by-attributes :sort-by-attributes-alt :unchecked
:expand :collapse-down :collapse-up :log-in :flash :log-out :new-window :record :save :open :saved :import
:export :send :floppy-disk :floppy-saved :floppy-remove :floppy-save :floppy-open :credit-card :transfer
:cutlery :header :compressed :earphone :phone-alt :tower :stats :sd-video :hd-video
:subtitles :sound-stereo :sound-dolby :sound-5-1 :sound-6-1 :sound-7-1 :copyright-mark :registration-mark :cloud-download
:cloud-upload :tree-conifer :tree-deciduous :cd :save-file :open-file :level-up :copy :paste
:alert :equalizer :king :queen :pawn :bishop :knight :baby-formula :tent
:blackboard :bed :apple :erase :hourglass :lamp :duplicate :piggy-bank :scissors
:bitcoin :btc :xbt :yen :jpy :ruble :rub :scale :ice-lolly
:ice-lolly-tasted :education :option-horizontal :option-vertical :menu-hamburger :modal-window :oil :grain :sunglasses
:text-size :text-color :text-background :object-align-top :object-align-bottom :object-align-horizontal :object-align-left
:object-align-vertical :object-align-right :triangle-right :triangle-left :triangle-bottom :triangle-top
:console :superscript :subscript :menu-left :menu-right :menu-down :menu-up})
(defn glyphicon
"Render a glyphicon in a span. Legal icon names are in b/glyph-icons.
attrs will be added to the span's attributes.
size - The size of the icon font. Defaults to 10pt.
"
[{:keys [size] :or {size "10pt"} :as attrs} icon]
{:pre [(contains? glyph-icons icon)]}
(let [attrs (-> attrs
(dissoc :size)
(assoc :aria-hidden true)
(assoc :style {:fontSize size})
(update :className #(str % " glyphicon glyphicon-" (name icon)))
clj->js)]
(dom/span attrs "")))
(defn button-group
"Groups nested buttons together in a horizontal row.
`size` - (optional) can be :xs, :sm, or :lg.
`kind` - (optional) can be :vertical or :justified"
[{:keys [size kind] :as attrs} & children]
(let [justified? (= kind :justified)
vertical? (= kind :vertical)
cls (cond-> "btn-group"
justified? (str " btn-group-justified")
vertical? (str "-vertical")
size (str " btn-group-" (name size)))
wrap-button (fn [ele] (if (ele/react-instance? "button" ele) (button-group {} ele) ele))
attrs (-> attrs (dissoc :size :kind) (assoc :role "group"))
children (if justified? (map #(wrap-button %) children) children)]
(div-with-class cls attrs children)))
(defn button-toolbar
"Groups button groups together as a toolbar, and a bit of space between each group"
[attrs & children]
(div-with-class "btn-toolbar" (assoc attrs :role "toolbar") children))
(defn breadcrumb-item
"Define a breadcrumb.
label - The label to show. You should internationalize this yourself.
onClick - A function. What to do when the item is clicked. Not needed for the last item."
([label] {:label label :onClick identity})
([label onClick] {:label label :onClick onClick}))
(defn breadcrumbs
"props - Properties to place on the top-level `ol`.
items - a list of breadcrumb-item"
[props & items]
(let [attrs (update props :className #(str " breadcrumb"))]
(dom/ol (clj->js attrs)
(conj
(mapv (fn [item] (dom/li {:key (:label item)} (dom/a {:onClick (:onClick item)} (:label item)))) (butlast items))
(dom/li {:key (:label (last items)) :className "active"} (:label (last items)))))))
(defn pagination
"Render a pagination control.
props - A map of properties.
size - One of :sm or :lg
pagination-entries - One or more `pagination-entry`"
[{:keys [size] :as props} & pagination-entries]
(let [classes (cond-> (get props :className "")
:always (str "pagination")
size (str " pagination-" (name size)))
attrs (assoc props :className classes)]
(dom/nav {:aria-label "Page Navigation"}
(apply dom/ul (clj->js attrs) pagination-entries))))
(defn pagination-entry
"Create an entry in a pagination control. Forward and back buttons can be rendered at either end with any label, but
the fulcro.ui.html-entities/raqao and laqao defs give a nicely sized font-based arrow."
[{:keys [label disabled active onClick] :as props}]
(let [onClick (if (and onClick (not disabled)) onClick identity)
classes (cond-> (:className props)
disabled (str " disabled")
active (str " active"))
attrs (-> props
(assoc :className classes)
(dissoc :active :label :disabled :onClick))]
(dom/li (clj->js attrs)
(if (or (= label ent/raqao) (= label ent/laqao))
(dom/a {:onClick onClick :aria-label (if (= ent/raqao label) "Next" "Previous")}
(dom/span {:aria-hidden "true"} label))
(dom/a {:onClick onClick}
label (when active (dom/span {:className "sr-only"} " (current)")))))))
(defn pager
"A light next/previous pair of controls. Use `pager-next` and `pager-previous` as the children of this."
[props & children]
(let [attrs (-> props
(update :className #(str " pager"))
clj->js)]
(dom/nav {:aria-label "Page Navigation"}
(apply dom/ul attrs children))))
(defn pager-next
"Render a next button in a pager"
[{:keys [onClick disabled]} & label-children]
(dom/li {:key "next" :className (str "next" (when disabled " disabled"))} (apply dom/a {:onClick (or onClick identity)} label-children)))
(defn pager-previous
"Render a previous button in a pager"
[{:keys [onClick disabled]} & label-children]
(dom/li {:key "prior" :className (str "previous" (when disabled " disabled"))} (apply dom/a {:onClick (or onClick identity)} label-children)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dropdowns
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def dropdown-table :bootstrap.dropdown/by-id)
(def dropdown-item-table :bootstrap.dropdown-item/by-id)
(defn dropdown-item
"Define the state for an item of a dropdown."
[id label] {::id id ::label label})
(defn dropdown-divider
"Creates a divider between items. Must have a unique ID"
[id] {::id id ::label ::divider})
(defn dropdown
"Creates a dropdown's state. Create items with dropdown-item or dropdown-divider."
[id label items] {::id id ::active-item nil ::label label ::items items ::open? false :type dropdown-table})
(defn dropdown-ident [id-or-props]
(if (map? id-or-props)
[(:type id-or-props) (::id id-or-props)]
[dropdown-table id-or-props]))
(defn dropdown-item-ident [id-or-props]
(if (map? id-or-props)
(dropdown-item-ident (::id id-or-props))
[dropdown-item-table id-or-props]))
(m/defmutation set-dropdown-open
"Mutation. Set the open flag to true/false to open/close the dropdown."
[{:keys [id open?]}]
(action [{:keys [state]}]
(let [kpath (conj (dropdown-ident id) ::open?)]
(swap! state assoc-in kpath open?))))
(defn set-dropdown-item-active* [state-map dropdown-id item-id]
(assoc-in state-map (conj (dropdown-ident dropdown-id) ::active-item) item-id))
(m/defmutation set-dropdown-item-active
"Mutation. Set one of the items in a dropdown to active.
id - the ID of the dropdown
item-id - the ID of the dropdown item
"
[{:keys [id item-id]}]
(action [{:keys [state]}]
(swap! state set-dropdown-item-active* id item-id)))
(defn- close-all-dropdowns-impl [dropdown-map]
(reduce (fn [m id] (assoc-in m [id ::open?] false)) dropdown-map (keys dropdown-map)))
(m/defmutation close-all-dropdowns
"Mutations: Close all dropdowns (globally)"
[ignored]
(action [{:keys [state]}]
(swap! state update dropdown-table close-all-dropdowns-impl)))
(defui ^:once DropdownItem
static prim/IQuery
(query [this] [::id ::label ::active? ::disabled? :type])
static prim/Ident
(ident [this props] (dropdown-item-ident props))
Object
(render [this]
(let [{:keys [::label ::id ::disabled?]} (prim/props this)
active? (prim/get-computed this :active?)
onSelect (or (prim/get-computed this :onSelect) identity)]
(if (= ::divider label)
(dom/li {:key id :role "separator" :className "divider"})
(dom/li {:key id :className (cond-> ""
disabled? (str " disabled")
active? (str " active"))}
(dom/a {:onClick (fn [evt]
(.stopPropagation evt)
(onSelect id)
false)} (tr-unsafe label)))))))
(let [ui-dropdown-item-factory (prim/factory DropdownItem {:keyfn ::id})]
(defn ui-dropdown-item
"Render a dropdown item. The props are the state props of the dropdown item. The additional by-name
arguments:
onSelect - The function to call when a menu item is selected
active? - render this item as active
"
[props & {:keys [onSelect active?]}]
(ui-dropdown-item-factory (prim/computed props {:onSelect onSelect :active? active?}))))
(defui ^:once Dropdown
static prim/IQuery
(query [this] [::id ::active-item ::label ::open? {::items (prim/get-query DropdownItem)} :type])
static prim/Ident
(ident [this props] (dropdown-ident props))
Object
(render [this]
(let [{:keys [::id ::label ::active-item ::items ::open?]} (prim/props this)
{:keys [onSelect kind stateful? value]} (prim/get-computed this)
active-item-label (->> items
(some #(and (= active-item (::id %)) %))
::label)
value-label (->> items
(some #(and (= value (::id %)) %))
::label)
label (cond
(and value value-label) value-label
(and active-item-label stateful?) active-item-label
:otherwise label)
onSelect (fn [item-id]
(prim/transact! this `[(close-all-dropdowns {}) (set-dropdown-item-active ~{:id id :item-id item-id})])
(when onSelect (onSelect item-id)))
open-menu (fn [evt]
(.stopPropagation evt)
(prim/transact! this `[(close-all-dropdowns {}) (set-dropdown-open ~{:id id :open? (not open?)})])
false)]
(button-group {:className (if open? "open" "")}
(button {:className (cond-> "dropdown-toggle"
kind (str " btn-" (name kind))) :aria-haspopup true :aria-expanded open? :onClick open-menu}
(tr-unsafe label) " " (dom/span {:className "caret"}))
(dom/ul {:className "dropdown-menu"}
(map #(ui-dropdown-item % :onSelect onSelect :active? (cond
stateful? (= (::id %) active-item)
value (= value (::id %)))) items))))))
(let [ui-dropdown-factory (prim/factory Dropdown {:keyfn ::id})]
(defn ui-dropdown
"Render a dropdown. The props are the state props of the dropdown. The additional by-name
arguments:
onSelect - The function to call when a menu item is selected
stateful? - If set to true, the dropdown will remember the selection and show it.
kind - The kind of dropdown. See `button`."
[props & {:keys [onSelect kind value stateful?] :as attrs}]
(ui-dropdown-factory (prim/computed props attrs))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NAV (tabs/pills)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def nav-table :bootstrap.nav/by-id)
(def nav-link-table :bootstrap.navitem/by-id)
(defn nav-link
"Creates a navigation link. ID must be globally unique. The label will be run through `tr-unsafe`, so it can be
internationalized. "
[id label disabled?]
{::id id ::label label ::disbled? disabled? :type nav-link-table})
(defn nav-link-ident [id-or-props]
(if (map? id-or-props)
[(:type id-or-props) (::id id-or-props)]
[nav-link-table id-or-props]))
(defn nav-ident [id-or-props]
(if (map? id-or-props)
[nav-table (::id id-or-props)]
[nav-table id-or-props]))
(defn nav
"Creates a navigation control.
- kind - One of :tabs or :pills
- layout - One of :normal, :stacked or :justified
- active-link-id - Which of the nested links is the active one
- links - A vector of `nav-link` or `dropdown` instances."
[id kind layout active-link-id links]
{::id id ::kind kind ::layout layout ::active-link-id active-link-id ::links links})
(defui ^:once NavLink
static prim/IQuery
(query [this] [::id ::label ::disabled? :type])
static prim/Ident
(ident [this props] (nav-link-ident props))
Object
(render [this]
(let [{:keys [::id ::label ::disabled?]} (prim/props this)
{:keys [onSelect active?]} (prim/get-computed this)]
(dom/li {:role "presentation" :className (cond-> ""
active? (str " active")
disabled? (str " disabled"))}
(dom/a {:onClick #(when onSelect (onSelect id))} (tr-unsafe label))))))
(def ui-nav-link (prim/factory NavLink {:keyfn ::id}))
(defui ^:once NavItemUnion
static prim/Ident
(ident [this {:keys [::id type]}] [type id])
static prim/IQuery
(query [this] {dropdown-table (prim/get-query Dropdown) nav-link-table (prim/get-query NavLink)})
Object
(render [this]
(let [{:keys [type ::items] :as child} (prim/props this)
{:keys [onSelect active-id active?] :as computed} (prim/get-computed this)
stateful? (some #(= active-id (::id %)) items)]
(case type
:bootstrap.navitem/by-id (ui-nav-link (prim/computed child computed))
:bootstrap.dropdown/by-id (ui-dropdown child :onSelect onSelect :stateful? stateful?)
(dom/p nil "Unknown link type!")))))
(def ui-nav-item (prim/factory NavItemUnion {:keyfn ::id}))
(defn set-active-nav-link*
[state-map nav-id link-id]
(update-in state-map (nav-ident nav-id) assoc ::active-link-id link-id))
(m/defmutation set-active-nav-link
"Mutation: Set the active navigation link"
[{:keys [id target]}]
(action [{:keys [state]}]
(swap! state set-active-nav-link* id target)))
(defui ^:once Nav
static prim/Ident
(ident [this props] (nav-ident props))
static prim/IQuery
(query [this] [::id ::kind ::layout ::active-link-id {::links (prim/get-query NavItemUnion)}])
Object
(render [this]
(let [{:keys [::id ::kind ::layout ::active-link-id ::links]} (prim/props this)
{:keys [onSelect]} (prim/get-computed this)
onSelect (fn [nav-id] (when onSelect (onSelect nav-id))
(prim/transact! this `[(set-active-nav-link ~{:id id :target nav-id})]))]
(dom/ul {:className (str "nav nav-" (name kind) (case layout :justified " nav-justified" :stacked " nav-stacked" ""))}
(map #(ui-nav-item (prim/computed % {:onSelect onSelect :active-id active-link-id :active? (= (::id %) active-link-id)})) links)))))
(let [nav-factory (prim/factory Nav {:keyfn ::id})]
(defn ui-nav
"Render a nav, which should have state declared with `nav`.
props - a cljs map of the data props
onSelect - an optional named parameter to supply a function that is called when navigation is done.
"
[props & {:keys [onSelect]}]
(nav-factory (prim/computed props {:onSelect onSelect}))))
(defn label
"Wraps children in an (inline) bootstrap label.
props are standard DOM props, with support for the additional:
kind - One of :primary, :success, :warning, :info, :danger. Defaults to :default."
[{:keys [kind] :as props :or {kind :default}} & children]
(let [classes (str "label label-" (name kind) " " (get props :className ""))
attrs (-> (dissoc props :kind)
(assoc :className classes))]
(apply dom/span (clj->js attrs) children)))
(defn badge
"Wraps children in an (inline) bootstrap badge.
props are standard DOM props."
[props & children]
(let [classes (str "badge " (get props :className ""))
attrs (assoc props :className classes)]
(apply dom/span (clj->js attrs) children)))
(defn jumbotron
"Wraps children in a jumbotron"
[props & children]
(div-with-class "jumbotron" props children))
(defn alert
"Renders an alert.
Props can contain normal DOM props, and additionally:
kind - The kind of alert: :info, :success, :warning, or :danger. Defaults to `:danger`.
onClose - What to do when the close button is pressed. If nil, close will not be rendered."
[{:keys [kind onClose] :as props} & children]
(let [classes (str (:className props) " alert alert-dismissable alert-" (if kind (name kind) "danger"))
attrs (-> props
(dissoc :kind :onClose)
(assoc :role "alert" :className classes)
clj->js)]
(apply dom/div attrs (if onClose
(close-button {:onClick onClose})
"") children)))
(defn caption
"Renders content that has padding and a lightened color for the font."
[props & children]
(div-with-class "caption" props children))
(defn thumbnail
"Renders a box around content. Typically used to double-box and image or generate Pinterest-style blocks."
[props & children]
(div-with-class "thumbnail" props children))
(defn progress-bar
"Render's a progress bar from an input of the current progress (a number from 0 to 100).
:current - The current value of progress (0 to 100)
:animated? - Should the bar have a striped animation?
:kind - One of :success, :warning, :danger, or :info
Any classname or other properties included will be placed on the top-level div of the progress bar.
"
[{:keys [current kind animated?] :or {kind :info} :as props}]
(let [attrs (dissoc props :current :kind :animated?)]
(div-with-class "progress" attrs
[(dom/div {:className (str "progress-bar progress-bar-" (name kind)
(when animated? " progress-bar-striped active")) :role "progressbar" :aria-valuenow current
:aria-valuemin 0 :aria-valuemax 100 :style {:width (str current "%")}})])))
(defn panel
"Render a panel. Use `panel-heading`, `panel-title`, `panel-body`, and `panel-footer` for elements of the panel.
:kind is one of :primary, :success, :info, :warning, or :danger"
[{:keys [kind] :or {kind :default} :as props} & children]
(div-with-class (str "panel panel-" (name kind)) props children))
(defn panel-group
"A wrapper for panels that visually groups them together."
[props & children]
(div-with-class "panel-group" props children))
(defn panel-heading
"Render a heading area in a panel. Must be first. Optional."
[props & children]
(div-with-class "panel-heading" props children))
(defn panel-title
"Render a title in a panel. Must be in a `panel-heading`."
[props & children]
(div-with-class "panel-title" props children))
(defn panel-body
"Render children in the body of a panel. Not needed for tables or list groups. Should come after (optional) panel-heading."
[props & children]
(div-with-class "panel-body" props children))
(defn panel-footer
"Render children in a footer of a panel."
[props & children]
(div-with-class "panel-footer" props children))
(defn well
"Inset content.
size - Optional to increase or decrease size. Can be :sm or :lg"
[{:keys [size] :as props} & children]
(div-with-class (str "well " (when size (str "well-" (name size)))) (dissoc props :size) children))
(defn get-abs-position
"Get a map (with the keys :left and :top) that has the absolute position of the given DOM element."
[ele]
#?(:clj {}
:cljs (let [doc (.-ownerDocument ele)
win (or (.-defaultView doc) js/window)
box (.getBoundingClientRect ele)]
{:width (.-width box)
:height (.-height box)
:left (+ (.-left box) (.-scrollX win))
:top (+ (.-top box) (.-scrollY win))})))
; we're using owner document to make sure this works in iframes, where js/document would be wrong
(defui RenderInBody
Object
(renderLayer [this]
#?(:cljs (let [child (first (prim/children this))
popup (.-popup this)]
(.render js/ReactDOM child popup))))
(componentDidMount [this]
#?(:cljs (let [doc (some-> (.-doc-element this) .-ownerDocument)
popup (when doc (.createElement doc "div"))]
(set! (.-popup this) popup)
(when doc (.appendChild (.-body doc) popup))
(.renderLayer this))))
(componentDidUpdate [this np ns] (.renderLayer this))
(shouldComponentUpdate [this np ns] true)
(componentWillUnmount [this]
#?(:cljs (let [doc (some-> (.-doc-element this) .-ownerDocument)
popup (.-popup this)]
(.unmountComponentAtNode js/ReactDOM (.-popup this))
(when doc
(.removeChild (.-body doc) (.-popup this))))))
(render [this] (dom/div {:key "placeholder" :ref (fn [r] (set! (.-doc-element this) r))})))
(def ui-render-in-body (prim/factory RenderInBody {:keyfn :key}))
(defsc PopOverContent [this props] (dom-with-class dom/div "popover-content" props (prim/children this)))
(def ui-popover-content (prim/factory PopOverContent))
(defsc PopOverTitle [this props] (dom-with-class dom/h3 "popover-title" props (prim/children this)))
(def ui-popover-title (prim/factory PopOverTitle))
(defsc PopOverTarget [this props] (dom-with-class dom/span "" props (prim/children this)))
(def ui-popover-target (prim/factory PopOverTarget))
(defsc PopOver [this {:keys [active orientation] :or {orientation :top}}]
{:componentWillUpdate (fn [new-props new-state]
(let [old-props (prim/props this)
becoming-active? (and (:active new-props) (not (:active old-props)))]
(when becoming-active? (prim/update-state! this update :render-for-size inc))))}
(let [target (.-target-ref this)
popup (.-popup-ref this)
popup-box (if popup (get-abs-position popup) {})
target-box (if target (get-abs-position target) {})
deltaY (case orientation
:bottom (:height target-box)
:left (-> (:height target-box)
(- (:height popup-box))
(/ 2))
:right (-> (:height target-box)
(- (:height popup-box))
(/ 2))
(- (:height popup-box)))
deltaX (case orientation
:left (- (:width popup-box))
:right (:width target-box)
(/ (- (:width target-box) (:width popup-box)) 2))
popupTop (if active (+ (:top target-box) deltaY) -1000)
popupLeft (if active (+ (:left target-box) deltaX) -1000)
children (prim/children this)
content (util/first-node PopOverContent children)
title (util/first-node PopOverTitle children)
target (util/first-node PopOverTarget children)]
(dom/span {:style {:display "inline-block"} :ref (fn [r] (set! (.-target-ref this) r))}
(ui-render-in-body {}
(dom/div {:className (str "popover fade " (name orientation) (when active " in"))
:ref (fn [r] (set! (.-popup-ref this) r))
:style {:position "absolute"
:top (str popupTop "px")
:left (str popupLeft "px")
:display "block"}}
(dom/div {:className "arrow" :style {:left (case orientation
:left "100%"
:right "-11px"
"50%")}})
(when title) title
(when content) content))
target)))
(def ui-popover (prim/factory PopOver))
(defn modal-ident
"Get the ident for a modal with the given props or ID"
[props-or-id]
(if (map? props-or-id)
[:fulcro.ui.boostrap3.modal/by-id (:db/id props-or-id)]
[:fulcro.ui.boostrap3.modal/by-id props-or-id]))
(defn- show-modal* [modal tf]
(assoc modal :modal/visible tf))
(defn- activate-modal* [modal tf]
(assoc modal :modal/active tf))
#?(:cljs
(defmutation show-modal
"Mutation: Show a modal by ID."
[{:keys [id]}]
(action [{:keys [state]}]
(swap! state update-in (modal-ident id) show-modal* true)
(js/setTimeout (fn [] (swap! state update-in (modal-ident id) activate-modal* true)) 10))))
#?(:cljs
(defmutation hide-modal
"mutation: Hide a modal by ID."
[{:keys [id]}]
(action [{:keys [state]}]
(swap! state update-in (modal-ident id) activate-modal* false)
(js/setTimeout (fn [] (swap! state update-in (modal-ident id) show-modal* false)) 300))))
(defui ^:once ModalTitle
Object
(render [this]
(apply dom/div (clj->js (prim/props this)) (prim/children this))))
(def ui-modal-title (prim/factory ModalTitle {:keyfn (fn [props] "modal-title")}))
(defui ^:once ModalBody
Object
(render [this]
(div-with-class "modal-body" (prim/props this) (prim/children this))))
(def ui-modal-body (prim/factory ModalBody {:keyfn (fn [props] "modal-body")}))
(defui ^:once ModalFooter
Object
(render [this]
(div-with-class "modal-footer" (prim/props this) (prim/children this))))
(def ui-modal-footer (prim/factory ModalFooter {:keyfn (fn [props] "modal-footer")}))
(defui ^:once Modal
static prim/InitialAppState
(initial-state [c {:keys [id sz backdrop keyboard] :or {backdrop true keyboard true}}]
{:db/id id :modal/active false :modal/visible false :modal/keyboard keyboard
:modal/size sz :modal/backdrop (boolean backdrop)})
static prim/Ident
(ident [this props] (modal-ident props))
static prim/IQuery
(query [this] [:db/id :modal/active :modal/visible :modal/size :modal/backdrop :modal/keyboard])
Object
(componentDidMount [this] (.focus (.-the-dialog this)))
(componentDidUpdate [this pp _] (when (and (:modal/visible (prim/props this)) (not (:modal/visible pp))) (.focus (.-the-dialog this))))
(render [this]
(let [{:keys [db/id modal/active modal/visible modal/size modal/keyboard modal/backdrop]} (prim/props this)
{:keys [onClose]} (prim/get-computed this)
onClose (fn []
(prim/transact! this `[(hide-modal {:id ~id})])
(when onClose (onClose id)))
children (prim/children this)
label-id (str "modal-label-" id)
title (util/first-node ModalTitle children)
body (util/first-node ModalBody children)
footer (util/first-node ModalFooter children)]
(dom/div {:role "dialog" :aria-labelledby label-id
:ref (fn [r] (set! (.-the-dialog this) r))
:onKeyDown (fn [evt] (when (and keyboard (evt/escape-key? evt)) (onClose)))
:style {:display (if visible "block" "none")}
:className (str "modal fade" (when active " in")) :tabIndex "-1"}
(dom/div {:role "document" :className (str "modal-dialog" (when size (str " modal-" (name size))))}
(dom/div {:className "modal-content"}
(dom/div {:key "modal-header" :className "modal-header"}
(dom/button {:type "button" :onClick onClose :aria-label "Close" :className "close"}
(dom/span {:aria-hidden "true"} ent/times))
(when title
(dom/h4 {:key label-id :id label-id :className "modal-title"} title)))
(when body body)
(when footer footer)))
(when (and backdrop visible)
(ui-render-in-body {}
(dom/div {:key "backdrop" :className (str "modal-backdrop fade" (when active " in"))})))))))
(let [modal-factory (prim/factory Modal {:keyfn (fn [props] (str "modal-" (:db/id props)))})]
(defn ui-modal
"Render a modal.
Modals are stateful. You must compose in initial state and a query. Modals also have IDs.
Modal content should include a ui-modal-title, ui-modal-body, and ui-modal-footer as children. The footer usually contains
one or more buttons.
Use the `prim/get-initial-state` function to pull a valid initial state for this component. The arguments are:
`(prim/get-initial-state Modal {:id ID :sz SZ :backdrop BOOLEAN})`
where the id is required (and must be unique among modals, and `:sz` is optional and must be `:sm` or `:lg`. The
:backdrop option is boolean, and indicates you want a backdrop that blocks the UI. The `:keyboard` option
defaults to true and enables removal of the dialog with `ESC`.
When rendering the modal, it typically looks something like this:
````
(b/ui-modal modal
(b/ui-modal-title nil
(dom/b nil \"WARNING!\"))
(b/ui-modal-body nil
(dom/p {:className b/text-danger} \"Stuff went sideways.\"))
(b/ui-modal-footer nil
(b/button {:onClick #(prim/transact! this `[(b/hide-modal {:id :warning-modal})])} \"Bummer!\"))))))
````
NOTE: The grid (`row` and `col`) can be used within the modal body *without* a `container`.
See the developer's guide for an example in the N15-Twitter-Bootstrap-Components section.
Available mudations: `b/show-modal` and `b/hide-modal`."
[props & children]
(apply modal-factory props children)))
(defn collapse-ident [id-or-props]