-
Notifications
You must be signed in to change notification settings - Fork 16
/
chap-widgets.lyx
2283 lines (1555 loc) · 38.6 KB
/
chap-widgets.lyx
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
#LyX 1.6.1 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\use_default_options false
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Chapter
Lift Widgets
\begin_inset CommandInset label
LatexCommand label
name "cha:Lift-Widgets"
\end_inset
\end_layout
\begin_layout Standard
In this chapter we're going to discuss widgets in Lift.
A widget is essentially a library of Scala and JavaScript code that together
provide packaged XHTML fragments for display on the client browser.
In other web frameworks (JSF, Struts, etc) these are sometimes called component
s.
An example of a widget would be small library that automatically embeds
a Calendar instance (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Calendar-widgets"
\end_inset
), or a helper library to sort HTML tables (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:TableSorter-widget"
\end_inset
).
Typically widgets embody dynamic behavior on the client side, which is
what makes them so attractive; static client-side content is already dead
simple to generate in Lift with snippets, so the extra sauce of JavaScript
binding and Ajax callbacks really makes advanced functionality easy.
\end_layout
\begin_layout Standard
Lift's widgets are intended to minimize effort on your part.
Unlike some other frameworks where widgets/components require the use of
specific traits or special XML binding, Lift (and Scala's) inherent flexibility
with XML, JavaScript abstraction, and snippet generators make using widgets
as simple as dropping in a few lines of code to your existing snippets
or views.
\end_layout
\begin_layout Section
Current Lift Widgets
\end_layout
\begin_layout Standard
To start, we'll cover the current set of widgets included in Lift at the
time of writing this book.
These widgets are contained in the lift-widgets module, which means you'll
need to add the dependency to your pom.xml if you want to use them (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Adding-a-Dependency"
\end_inset
).
While this list will likely grow over time, remember that widgets are based
on the fundamentals of Scala's XML functionality as well as Lift's JavaScript
support (chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Lift-and-Javascript"
\end_inset
), so the same general rules apply to all of them.
At the end of the chapter we'll cover writing your own widgets (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:How-to-build-widgets"
\end_inset
).
\end_layout
\begin_layout Subsection
TableSorter widget
\begin_inset CommandInset label
LatexCommand label
name "sub:TableSorter-widget"
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\align center
\begin_inset Graphics
filename images/tablesorter.png
width 5in
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
TableSorter widget
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The TableSorter widget is based on the TableSorter jQuery plugin
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "http://tablesorter.com/docs/"
\end_inset
\end_layout
\end_inset
.
Basically, the TableSorter widget allows you to take an existing HTML table
(THEAD and TBODY tags are required) and add sorting to columns in the table.
By default, the widget handles sorting of numeric, currency, and other
value types automatically.
The full capabilities of the plugin are beyond the scope of the widget,
however; if you need more features you'll have to set up the JavaScript
yourself instead of using the widget.
\end_layout
\begin_layout Standard
The first step in using the widget is to call the
\family typewriter
TableSorter.init
\family default
function in your Boot class to make Lift aware of the resources used by
this widget.
Then, you need to set up a table in your page (either statically in the
template or via a snippet):
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
TableSorter Template
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<lift:surround with="default" at="content">
\end_layout
\begin_layout Plain Layout
<lift:TableSorterDemo/>
\end_layout
\begin_layout Plain Layout
<table id=
"
table-id
"
class=
"
tablesorter
"
> ...
</table>
\end_layout
\begin_layout Plain Layout
</lift:surround>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Note that you need to have an
\family typewriter
id
\family default
attribute on the table and add the
\family typewriter
tablesorter
\family default
class to the table element.
Next you simply call the TableSorter widget from a snippet:
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
TableSorter Snippet
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class TableSorterDemo {
\end_layout
\begin_layout Plain Layout
def render(xhtml: NodeSeq): NodeSeq = TableSorter("table-id")
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The argument to TableSorter is the HTML element
\family typewriter
id
\family default
of the table you want sorted.
The TableSorter code relies on head merge (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Head-Merge"
\end_inset
) to put the appropriate JavaScript and jQuery functions into the returned
page.
\end_layout
\begin_layout Subsection
Calendar widgets
\begin_inset CommandInset label
LatexCommand label
name "sub:Calendar-widgets"
\end_inset
\end_layout
\begin_layout Standard
There are three calendar widgets corresponding to month, week and day views.
These widgets display calendars with a similar look and feel to Microsoft
Outlook or Google Calendar.They provide basic functionality for display,
but you can easily customize CSS and JavaScript hooks for calendar items
to fit your application requirements.
\end_layout
\begin_layout Paragraph
Calendar Month-View
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\align center
\begin_inset Graphics
filename images/month-view.png
width 5in
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Calendar Month-View
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
This widget allows you to create month view calendars in your web page,
manage your calendar events etc.
The first thing you need to do is call the
\family typewriter
CalendarMonthView.init
\family default
function in your Boot class; this performs initialization by telling Lift's
\family typewriter
ResourceServer
\family default
about the paths to JavaScripts and stylesheets needed by this widget since
these dependencies are embedded in the same jar file (we'll cover this
topic more in section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:How-to-build-widgets"
\end_inset
).
\end_layout
\begin_layout Standard
The template for our widget example is relatively straightforward, as shown
in listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Month-view-template"
\end_inset
.
Basically, we provide a binding element where the calendar will be rendered.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Month view template
\begin_inset CommandInset label
LatexCommand label
name "lst:Month-view-template"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<lift:surround with="default" at="content">
\end_layout
\begin_layout Plain Layout
<h2>Calendar Month View Demo</h2>
\end_layout
\begin_layout Plain Layout
<lift:CalendarMonthViewDemo>
\end_layout
\begin_layout Plain Layout
<cal:widget/>
\end_layout
\begin_layout Plain Layout
</lift:CalendarMonthViewDemo>
\end_layout
\begin_layout Plain Layout
</lift:surround>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
In our snippet code, listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Month-view-snippet"
\end_inset
, we first perform some setup of the widget.
The Calendar widget takes a
\family typewriter
java.util.Calendar
\family default
instance telling it which month to display.
Additionally, it takes a Seq[CalendarItem] of items to be displayed on
the calendar.
Finally, it takes three arguments containing optional JavaScript functions
to be called when an item, day, or week is clicked, respectively.
In our example we're not showing any events or setting up any callbacks.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Month view snippet
\begin_inset CommandInset label
LatexCommand label
name "lst:Month-view-snippet"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class CalendarMonthViewDemo {
\end_layout
\begin_layout Plain Layout
def render(html: Group) : NodeSeq = {
\end_layout
\begin_layout Plain Layout
val c = Calendar.getInstance;
\end_layout
\begin_layout Plain Layout
c.set(MONTH, 0)
\end_layout
\begin_layout Plain Layout
bind("cal", html,
\end_layout
\begin_layout Plain Layout
"widget" -> CalendarMonthView(c, Nil, Empty, Empty, Empty)
\end_layout
\begin_layout Plain Layout
)
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
In addition, CalendarMonthView can also take a MonthViewMeta instance as
the second argument so that you can control the first day of the week and
the locale used for formatting dates and times.
For instance, we could set the calendar to use Monday as the first day
of the week:
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
"widget" -> CalendarMonthView(c,
\end_layout
\begin_layout Plain Layout
MonthViewMeta(Calendar.MONDAY, Locale.getDefault),
\end_layout
\begin_layout Plain Layout
Nil, Empty, Empty, Empty)
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Of course, without anything to display or do this isn't very useful, so
let's look at how you create CalendarItems.
\end_layout
\begin_layout Standard
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:CalendarItem-example"
\end_inset
shows how we can create a calendar item for a meeting on June 5th at 2:30
pm.
We have to set up another Calendar instance to hold the time of the meeting,
then we use the CalendarItem helper object to set up the actual item instance.
The first parameter is the id of the div that will be created for the item.
This can be used from other scripts if needed.
The second argument is the time of the event.
The third argument is the CalendarType of the event, in this case, a meeting.
The optional method on CalendarItem allows you to set optional attributes
essentially via a sequence of
\begin_inset Formula $(CalendarItem)\Rightarrow CalendarItem$
\end_inset
functions.
This technique is used since CalendarItems are immutable and modifying
them returns new instances.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
CalendarItem example
\begin_inset CommandInset label
LatexCommand label
name "lst:CalendarItem-example"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
val time = Calendar.getInstance
\end_layout
\begin_layout Plain Layout
time.setTime(DateFormat.pars("2009-06-05 2:30pm"))
\end_layout
\begin_layout Plain Layout
val meeting = CalendarItem("4", time, CalendarType.MEETING) optional (
\end_layout
\begin_layout Plain Layout
_ end(time),
\end_layout
\begin_layout Plain Layout
_ subject("Important Meeting!"))
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The widget renders not only the XHTML to display the calendar, but it generates
the
\family typewriter
<script>
\family default
and CSS tags using head merge to control display.
One common customization of the widget would be to override the CSS used;
to do this, provide your own
\family typewriter
style.css
\family default
file under the WEB-INF/classes/calendars/monthview directory in your project.
Because Lift uses the classpath to load resources, your style.css file will
be
\begin_inset Quotes eld
\end_inset
found
\begin_inset Quotes erd
\end_inset
before the default one bundled in the lift-widgets jar file.
You can use the default
\family typewriter
style.css
\family default
as a starting point
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/dpp/liftweb/tree/master/lift-widgets/src/main/resources/toserve/calendars/monthview/style.css"
\end_inset
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
The final thing we'd like to cover for the Month view is the JavaScript
callbacks.
These callbacks are constructed using the AnonFunc JavaScript artifact,
which essentially constructs an anonymous function on the client side.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Calendar-callback-example"
\end_inset
shows an example of using the callbacks to redirect to an event view page
for the given event when the item is clicked.
In this example we assume that the id of each calendar item is its unique
id in the ORM (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Object-Relationships"
\end_inset
) and that we have a rewrite rule set up to handle item viewing (section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:URL-Rewriting"
\end_inset
).
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Calendar callback example
\begin_inset CommandInset label
LatexCommand label
name "lst:Calendar-callback-example"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
import JsCmds._
\end_layout
\begin_layout Plain Layout
val itemClick = Full(
\end_layout
\begin_layout Plain Layout
AnonFunc("elem, param", JsRaw("alert(elem);")
\end_layout
\end_inset
\end_layout
\begin_layout Paragraph
Calendar Week-View
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\align center
\begin_inset Graphics
filename images/week-view.png
width 5in
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Calendar Week-View
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The CalendarWeekView widget provides a weekly view of the calendar.
The same general principles apply as for month view.
Again, you need to initialize the CalendarWeekView by calling the
\family typewriter
CalendarWeekView.init
\family default
function in your Boot class.
\end_layout
\begin_layout Standard
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Week-View-example"
\end_inset
shows a snippet returning a week view.
As you can see, we still use a Calendar instance to set the time, and we
also provide a WeekViewMeta in this example to set the first day of the
week and the locale.
The
\family typewriter
list
\family default
argument is a Seq[CalendarItem], constructed exactly the same as for a
month view.
Finally, we provide a JavaScript item callback.
Note that there aren't day or week callbacks available.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Week view example
\begin_inset CommandInset label
LatexCommand label
name "lst:Week-View-example"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class CalendarWeekViewDemo {
\end_layout
\begin_layout Plain Layout
def render(html: Group) : NodeSeq = {
\end_layout
\begin_layout Plain Layout
val c = Calendar.getInstance
\end_layout
\begin_layout Plain Layout
c.set(DAY_OF_MONTH, 17)
\end_layout
\begin_layout Plain Layout
c.set(MONTH, 4)
\end_layout
\begin_layout Plain Layout
bind("cal", html,
\end_layout
\begin_layout Plain Layout
"widget" -> CalendarWeekView(c,
\end_layout
\begin_layout Plain Layout
WeekViewMeta(MONDAY, Locale.getDefault()),
\end_layout
\begin_layout Plain Layout
list,
\end_layout
\begin_layout Plain Layout
itemClick))
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Paragraph