forked from lift/framework
/
LiftScreen.scala
1651 lines (1319 loc) · 51.1 KB
/
LiftScreen.scala
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
/*
* Copyright 2010-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package http
import xml._
import reflect.Manifest
import common._
import util._
import Helpers._
import js._
import JsCmds._
/**
* The trait that forms the basis for LiftScreen and the
* Screen instances in Wizard
*/
trait AbstractScreen extends Factory {
override def toString = screenName
protected type Errors = List[FieldError]
@volatile private[this] var _fieldList: List[() => FieldContainer] = Nil
/**
* any additional parameters that need to be put in the on the form (e.g., mime type)
*/
def additionalAttributes: MetaData =
if (hasUploadField) new UnprefixedAttribute("enctype", Text("multipart/form-data"), Null) else Null
/**
* Add a FieldContainer to the Screen. A FieldContainer
* can contain either a single field (a BaseField) or
* a collection of BaseFields. The key take-away is that
* if the LiftScreen or Wizard is a singleton, you can still
* display variable number of fields by returning a variable
* number of BaseField instances from the FieldContainer.
* <br/>
* WARNING -- this method is public so it can be called
* from a Wizard. This method should only be called from within
* the Screen or Wizard that owns the Screen and not
* from external code.
*/
def addFields(fields: () => FieldContainer) {
_fieldList = _fieldList ::: List(fields)
}
/**
* Use addFields
*
* @deprecated
*/
@deprecated("use addFields()")
protected def _register(field: () => FieldContainer) = addFields(field)
protected def hasUploadField: Boolean = screenFields.foldLeft(false)(_ | _.uploadField_?)
/**
* A list of fields in this screen
*/
def screenFields: List[BaseField] = _fieldList.flatMap(_.apply().allFields)
/**
* Override this method to do any setup of this screen
*/
protected def localSetup() {
}
def screenTop: Box[Elem] = Empty
def screenBottom: Box[Elem] = Empty
// an implicit coversion so we don't have to put Full around every Elem
protected implicit def elemInABox(in: Elem): Box[Elem] = Box !! in
/**
* The name of the screen. Override this to change the screen name
*/
def screenName: String = "Screen"
def screenNameAsHtml: NodeSeq = Text(screenName)
def screenTitle: NodeSeq = screenNameAsHtml
def cancelButton: Elem = <button>
{S.??("Cancel")}
</button>
def finishButton: Elem = <button>
{S.??("Finish")}
</button>
implicit def boxOfScreen[T <: AbstractScreen](in: T): Box[T] = Box !! in
def validate: List[FieldError] = screenFields.filter(_.shouldDisplay_?).
filter(_.show_?).flatMap(_.validate) ++ screenValidate
def validations: List[() => List[FieldError]] = Nil
def screenValidate: List[FieldError] = validations.flatMap(_())
protected def vendForm[T](implicit man: Manifest[T]): Box[(T, T => Any) => NodeSeq] = Empty
protected def vendAVar[T](dflt: => T): NonCleanAnyVar[T]
object Field {
implicit def fToType[T](field: Field {type ValueType = T}): T = field.is
}
protected sealed trait OtherValueInitializer[T]
protected case object NothingOtherValueInitializer extends OtherValueInitializer[Nothing]
protected final case class OtherValueInitializerImpl[T](f: () => T) extends OtherValueInitializer[T]
/**
* By default, are all the fields on this screen on the confirm screen?
*/
def onConfirm_? : Boolean
/**
* A field that's part of a Screen
*/
trait Field extends ConfirmField {
type OtherValueType
// >: Nothing
AbstractScreen.this.addFields(() => this)
private val _currentValue: NonCleanAnyVar[ValueType] =
vendAVar[ValueType](setFilter.foldLeft(default)((nv, f) => f(nv)))
private val _otherValue: NonCleanAnyVar[OtherValueType] =
vendAVar[OtherValueType](otherValueDefault)
/**
* Is this field on the confirm screen
*/
def onConfirm_? : Boolean = AbstractScreen.this.onConfirm_?
protected def otherValueDefault: OtherValueType = null.asInstanceOf[OtherValueType]
/**
* A field my have an "otherValue" which can be used
* to store a list of options or other information that
* the fields needs on a Screen/Wizard by Screen/Wizard
* basis
*/
def otherValue: OtherValueType = _otherValue.get
def setOtherValue(v: OtherValueType) = _otherValue.set(v)
def default: ValueType
def is = _currentValue.is
/**
* Set to true if this field is part of a multi-part mime upload
*/
override def uploadField_? = false
def get = is
def set(v: ValueType) = _currentValue.set(setFilter.foldLeft(v)((nv, f) => f(nv)))
implicit def manifest: Manifest[ValueType]
protected def buildIt[T](implicit man: Manifest[T]): Manifest[T] = man
override def helpAsHtml: Box[NodeSeq] = Empty
/**
* What form elements are we going to add to this field?
*/
def formElemAttrs: Seq[SHtml.ElemAttr] = Nil
/**
* Is the field editable
*/
def editable_? = true
def toForm: Box[NodeSeq] = {
val func: Box[(ValueType, ValueType => Any) => NodeSeq] =
AbstractScreen.this.vendForm(manifest) or otherFuncVendors(manifest) or
LiftRules.vendForm(manifest)
func.map(f => f(is, set _)).filter(x => editable_?).
map(ns => SHtml.ElemAttr.applyToAllElems(ns, formElemAttrs))
}
protected def otherFuncVendors(what: Manifest[ValueType]): Box[(ValueType, ValueType => Any) => NodeSeq] = Empty
def validate: List[FieldError] = currentField.doWith(this) {
validations.flatMap(_.apply(is))
}
def validations: List[ValueType => List[FieldError]] = Nil
def setFilter: List[ValueType => ValueType] = Nil
override def uniqueFieldId: Box[String] = Full(_theFieldId.get)
private lazy val _theFieldId: NonCleanAnyVar[String] =
vendAVar(Helpers.nextFuncName)
override def toString = is.toString
def binding: Box[FieldBinding] = Empty
def transform: Box[() => CssSel] = Empty
}
protected object currentField extends ThreadGlobal[FieldIdentifier]
protected class FieldBuilder[T](name: => String,
default: => T,
manifest: Manifest[T],
help: Box[NodeSeq],
validations: List[T => List[FieldError]],
filters: List[T => T],
stuff: Seq[FilterOrValidate[T]]) {
/**
* Set the Help HTML
*/
def help(h: NodeSeq): FieldBuilder[T] = new FieldBuilder[T](name, default,
manifest, Full(h), validations, filters, stuff)
/**
* Add a filter field (the wacky symbols are supposed to look like a filter symbol)
*/
def <*>(f: T => T): FieldBuilder[T] =
new FieldBuilder[T](name, default,
manifest, help, validations, filters ::: List(f),
stuff)
/**
* Add a validation (the wacky symbols are supposed to look like a check mark)
*/
def ^/(f: T => List[FieldError]): FieldBuilder[T] =
new FieldBuilder[T](name, default,
manifest, help, validations ::: List(f), filters, stuff)
/**
* Convert the field builder into a field
*/
def make: Field {type ValueType = T} = {
val paramFieldId: Box[String] = (stuff.collect {
case FormFieldId(id) => id
}).headOption
val confirmInfo = stuff.collect {
case NotOnConfirmScreen => false
}.headOption orElse
stuff.collect {
case OnConfirmScreen => true
}.headOption
val newBinding: Box[FieldBinding] = (stuff.collect {
case AFieldBinding(i) => i
}).headOption
val newHelp: Box[NodeSeq] = help or (stuff.collect {
case Help(ns) => ns
}).headOption
val newTransform: Box[() => CssSel] = (stuff.collect {
case FieldTransform(func) => func
}).headOption
new Field {
type ValueType = T
/**
* Is this field on the confirm screen
*/
override def onConfirm_? : Boolean = confirmInfo getOrElse super.onConfirm_?
override def name: String = FieldBuilder.this.name
override def default = FieldBuilder.this.default
/**
* What form elements are we going to add to this field?
*/
override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
override implicit def manifest: Manifest[ValueType] = FieldBuilder.this.manifest
override def helpAsHtml = newHelp
override def validations = FieldBuilder.this.validations
override def setFilter = FieldBuilder.this.filters
override def uniqueFieldId: Box[String] =
paramFieldId or Full(_theFieldId.get)
override def binding = newBinding
private lazy val _theFieldId: NonCleanAnyVar[String] =
vendAVar(Helpers.nextFuncName)
override def transform = newTransform
}
}
}
implicit def strToListFieldError(msg: String): List[FieldError] =
List(FieldError(currentField.box openOr new FieldIdentifier {}, Text(msg)))
implicit def xmlToListFieldError(msg: NodeSeq): List[FieldError] =
List(FieldError(currentField.box openOr new FieldIdentifier {}, msg))
implicit def boxStrToListFieldError(msg: Box[String]): List[FieldError] =
msg.toList.map(msg =>
FieldError(currentField.box openOr new FieldIdentifier {}, Text(msg)))
implicit def boxXmlToListFieldError(msg: Box[NodeSeq]): List[FieldError] =
msg.toList.map(msg => FieldError(currentField.box openOr new FieldIdentifier {}, msg))
/**
* Create a FieldBuilder so you can add help screens, validations and filters. Remember to invoke "make" on
* the returned FieldBuilder to convert it into a field
*
* @param name - the name of the field. This is a call-by-name parameter, so you can dynamically calculate
* the name of the fiels (e.g., localize its name)
* @param default - the default value of the field
* @param validate - any validation functions
*/
protected def builder[T](name: => String, default: => T, stuff: FilterOrValidate[T]*)(implicit man: Manifest[T]): FieldBuilder[T] =
new FieldBuilder[T](name, default, man, Empty,
stuff.toList.collect {
case AVal(v) => v
},
stuff.toList.collect {
case AFilter(v) => v
},
stuff)
protected object FilterOrValidate {
implicit def promoteFilter[T](f: T => T): FilterOrValidate[T] = AFilter(f)
implicit def promoteValidate[T](v: T => List[FieldError]): FilterOrValidate[T] = AVal(v)
implicit def promoteToFormParam(a: SHtml.ElemAttr): FilterOrValidate[Nothing] = FormParam(a)
implicit def promoteToFormParam(a: (String, String)): FilterOrValidate[Nothing] = FormParam(a)
implicit def promoteFieldBinding(binding: FieldBinding): FilterOrValidate[Nothing] = AFieldBinding(binding)
}
sealed protected trait FilterOrValidate[+T]
/**
* Override the screen default for fields appearing on the confirm
* screen and force this field to appear on the confirm screen
*/
protected case object OnConfirmScreen extends FilterOrValidate[Nothing]
/**
* Override the screen default for fields appearing on the confirm
* screen and force this field not to appear on the confirm screen
*/
protected case object NotOnConfirmScreen extends FilterOrValidate[Nothing]
protected final case class FormParam(fp: SHtml.ElemAttr) extends FilterOrValidate[Nothing]
protected final case class FormFieldId(id: String) extends FilterOrValidate[Nothing]
protected final case class AFilter[T](f: T => T) extends FilterOrValidate[T]
protected final case class AVal[T](v: T => List[FieldError]) extends FilterOrValidate[T]
protected final case class AFieldBinding(binding: FieldBinding) extends FilterOrValidate[Nothing]
protected final case class Help(ns: NodeSeq) extends FilterOrValidate[Nothing]
protected final case class FieldTransform(func: () => CssSel) extends FilterOrValidate[Nothing]
protected def field[T](underlying: => BaseField {type ValueType = T},
stuff: FilterOrValidate[T]*)(implicit man: Manifest[T]): Field {type ValueType = T} = {
val paramFieldId: Box[String] = (stuff.collect {
case FormFieldId(id) => id
}).headOption
val confirmInfo = stuff.collect {
case NotOnConfirmScreen => false
}.headOption orElse
stuff.collect {
case OnConfirmScreen => true
}.headOption
val newBinding: Box[FieldBinding] = (stuff.collect {
case AFieldBinding(i) => i
}).headOption
val newHelp: Box[NodeSeq] = (stuff.collect {
case Help(ns) => ns
}).headOption
val newTransform: Box[() => CssSel] = (stuff.collect {
case FieldTransform(func) => func
}).headOption
new Field {
type ValueType = T
/**
* Is this field on the confirm screen
*/
override def onConfirm_? : Boolean = confirmInfo getOrElse super.onConfirm_?
override def toForm: Box[NodeSeq] = underlying.toForm
/**
* Give the current state of things, should the this field be shown
*/
override def show_? = underlying.show_?
/**
* What form elements are we going to add to this field?
*/
override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
/**
* Given the current context, should this field be displayed
*/
override def shouldDisplay_? = underlying.shouldDisplay_?
override def displayName = underlying.displayName
override def displayNameHtml: Box[NodeSeq] = underlying.displayNameHtml
override def asHtml = underlying.asHtml
override def name: String = underlying.name
override def default = underlying.get
override implicit def manifest: Manifest[ValueType] = man
override def helpAsHtml = newHelp or underlying.helpAsHtml
override def validate: List[FieldError] = underlying.validate
/*
override def validations = stuff.collect {
case AVal(f) => f
}.toList ::: underlying.validations
*/
override def setFilter = stuff.collect {
case AFilter(f) => f
}.toList
override def is = underlying.get
override def get = underlying.get
override def set(v: T) = underlying.set(setFilter.foldLeft(v)((v, f) => f(v)))
override def uniqueFieldId: Box[String] = paramFieldId or underlying.uniqueFieldId or super.uniqueFieldId
override def binding = newBinding or super.binding
override def transform = newTransform or super.transform
}
}
sealed protected trait BoxMarker
/**
* A little hack because => BaseField and => Box[BaseField]
* have the same method signature
*/
protected implicit object BoxMarkerObj extends BoxMarker
protected def field[T](underlying: => Box[BaseField {type ValueType = T}],
stuff: FilterOrValidate[T]*)(implicit man: Manifest[T], marker: BoxMarker): Field {type ValueType = T} = {
val paramFieldId: Box[String] = (stuff.collect {
case FormFieldId(id) => id
}).headOption
val newBinding: Box[FieldBinding] = (stuff.collect {
case AFieldBinding(i) => i
}).headOption
val newHelp: Box[NodeSeq] = (stuff.collect {
case Help(ns) => ns
}).headOption
val newTransform: Box[() => CssSel] = (stuff.collect {
case FieldTransform(func) => func
}).headOption
val confirmInfo = stuff.collect {
case NotOnConfirmScreen => false
}.headOption orElse
stuff.collect {
case OnConfirmScreen => true
}.headOption
new Field {
type ValueType = T
/**
* Is this field on the confirm screen
*/
override def onConfirm_? : Boolean = confirmInfo getOrElse super.onConfirm_?
override def toForm: Box[NodeSeq] = underlying.flatMap(_.toForm)
/**
* Give the current state of things, should the this field be shown
*/
override def show_? = underlying.map(_.show_?) openOr false
/**
* What form elements are we going to add to this field?
*/
override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
/**
* Given the current context, should this field be displayed
*/
override def shouldDisplay_? = underlying.map(_.shouldDisplay_?) openOr false
override def displayName = underlying.map(_.displayName) openOr "N/A"
override def displayNameHtml: Box[NodeSeq] = underlying.flatMap(_.displayNameHtml)
override def asHtml = underlying.map(_.asHtml) openOr NodeSeq.Empty
override def name: String = underlying.map(_.name) openOr "N/A"
override def default = underlying.open_!.get
override implicit def manifest: Manifest[ValueType] = man
override def helpAsHtml = newHelp or underlying.flatMap(_.helpAsHtml)
override def validate: List[FieldError] = underlying.toList.flatMap(_.validate)
override def setFilter = stuff.collect {
case AFilter(f) => f
}.toList
override def is = underlying.open_!.get
override def get = underlying.open_!.get
override def set(v: T) = underlying.open_!.set(setFilter.foldLeft(v)((v, f) => f(v)))
override def uniqueFieldId: Box[String] = paramFieldId or underlying.flatMap(_.uniqueFieldId) or super.uniqueFieldId
override def binding = newBinding or super.binding
override def transform = newTransform or super.transform
}
}
/**
* Create a field with a name, default value, and
*
* @param name - the name of the field. This is a call-by-name parameter, so you can dynamically calculate
* the name of the fiels (e.g., localize its name)
* @param default - the default value of the field
* @param validate - any validation functions
*/
protected def field[T](name: => String, default: => T, stuff: FilterOrValidate[T]*)(implicit man: Manifest[T]): Field {type ValueType = T} =
new FieldBuilder[T](name, default, man, Empty, stuff.toList.flatMap {
case AVal(v) => List(v)
case _ => Nil
}, stuff.toList.flatMap {
case AFilter(v) => List(v)
case _ => Nil
}, stuff).make
protected def removeRegExChars(regEx: String): String => String =
s => s match {
case null => null
case s => s.replaceAll(regEx, "")
}
protected def toLower: String => String =
s => s match {
case null => null
case s => s.toLowerCase
}
protected def toUpper: String => String =
s => s match {
case null => null
case s => s.toUpperCase
}
protected def trim: String => String =
s => s match {
case null => null
case s => s.trim
}
protected def notNull: String => String =
s => s match {
case null => ""
case x => x
}
/**
* A validation helper. Make sure the string is at least a particular
* length and generate a validation issue if not
*/
protected def valMinLen(len: => Int, msg: => String): String => List[FieldError] =
s => s match {
case str if (null ne str) && str.length >= len => Nil
case _ => List(FieldError(currentField.box openOr new FieldIdentifier {}, Text(msg)))
}
/**
* A validation helper. Make sure the string is no more than a particular
* length and generate a validation issue if not
*/
protected def valMaxLen(len: => Int, msg: => String): String => List[FieldError] =
s => s match {
case str if (null eq str) || str.length <= len => Nil
case _ => List(FieldError(currentField.box openOr new FieldIdentifier {}, Text(msg)))
}
/**
* Make sure the field matches a regular expression
*/
protected def valRegex(pat: => java.util.regex.Pattern, msg: => String): String => List[FieldError] =
s => s match {
case str if (null ne str) && pat.matcher(str).matches => Nil
case _ => List(FieldError(currentField.box openOr new FieldIdentifier {}, Text(msg)))
}
protected def minVal[T](len: => T, msg: => String)(implicit f: T => Number): T => List[FieldError] =
s => if (f(s).doubleValue < f(len).doubleValue) msg else Nil
protected def maxVal[T](len: => T, msg: => String)(implicit f: T => Number): T => List[FieldError] =
s => if (f(s).doubleValue > f(len).doubleValue) msg else Nil
def noticeTypeToAttr(screen: AbstractScreen): Box[NoticeType.Value => MetaData] =
inject[NoticeType.Value => MetaData] or LiftScreenRules.inject[NoticeType.Value => MetaData]
/**
* Create a field that's added to the Screen
*
* @param theName - the name of the field. This is call-by-name, so you
* can do things like S.?("Dog's Name") such that the string will be
* localized
*
* @param defaultValue - the starting value for the field. This is
* also call-by-name which is handy for constructs like:
* <code class="scala">SomeExternalRequestVarOrSessionVar.get</code>
*
* @theToForm - a function to convert the field into a form
*
* @otherValue - a handy way include other values in the field. The other value is
* calcualted when the field is initialized. You can, for example, put
* a list of valid options in the field.
*
* @stuff - a list of filters and validations for the field
*
* @return a newly minted Field
*/
protected def makeField[T, OV](theName: => String, defaultValue: => T,
theToForm: (Field {type OtherValueType = OV
type ValueType = T} =>
Box[NodeSeq]),
otherValue: OtherValueInitializer[OV],
stuff: FilterOrValidate[T]*):
Field {type ValueType = T; type OtherValueType = OV} = {
val newBinding: Box[FieldBinding] = (stuff.collect {
case AFieldBinding(i) => i
}).headOption
val newHelp: Box[NodeSeq] = (stuff.collect {
case Help(ns) => ns
}).headOption
val newTransform: Box[() => CssSel] = (stuff.collect {
case FieldTransform(func) => func
}).headOption
otherValue match {
case OtherValueInitializerImpl(otherValueInitFunc) => {
new Field {
type OtherValueType = OV
type ValueType = T
override protected def otherValueDefault: OtherValueType = otherValueInitFunc()
override def name: String = theName
override implicit def manifest = buildIt[T]
override def default: T = defaultValue
/**
* What form elements are we going to add to this field?
*/
override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
override val setFilter = stuff.flatMap {
case AFilter(f) => List(f)
case _ => Nil
}.toList
override val validations = stuff.flatMap {
case AVal(v) => List(v)
case _ => Nil
}.toList
override def binding = newBinding
override def helpAsHtml = newHelp
override def toForm: Box[NodeSeq] = theToForm(this)
override def transform = newTransform
}
}
case _ => {
new Field {
type ValueType = T
type OtherValueType = OV
override def name: String = theName
override implicit def manifest = buildIt[T]
override def default: T = defaultValue
/**
* What form elements are we going to add to this field?
*/
override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
override val setFilter = stuff.flatMap {
case AFilter(f) => List(f)
case _ => Nil
}.toList
override val validations = stuff.flatMap {
case AVal(v) => List(v)
case _ => Nil
}.toList
override def binding = newBinding
override def helpAsHtml = newHelp
override def toForm: Box[NodeSeq] = theToForm(this)
override def transform = newTransform
}
}
}
}
/**
* Create a password field
*
* @param name the name of the field (call-by-name)
* @param defaultValue the starting value of the field (call-by-name)
* @param stuff the filters, validators and attributes
*
* @returns a newly minted Field
*/
protected def password(name: => String, defaultValue: => String, stuff: FilterOrValidate[String]*): Field {type ValueType = String} = {
val eAttr = grabParams(stuff)
makeField[String, Nothing](name,
defaultValue,
field => SHtml.password(field.get, field.set(_), eAttr: _*),
NothingOtherValueInitializer,
stuff: _*)
}
/**
* Create a text field
*
* @param name the name of the field (call-by-name)
* @param defaultValue the starting value of the field (call-by-name)
* @param stuff the filters, validators and attributes
*
* @returns a newly minted Field
*/
protected def text(name: => String, defaultValue: => String, stuff: FilterOrValidate[String]*): Field {type ValueType = String} = {
val eAttr = grabParams(stuff)
makeField[String, Nothing](name,
defaultValue,
field => SHtml.text(field.get, field.set(_), eAttr: _*),
NothingOtherValueInitializer,
stuff: _*)
}
/**
* Create a textarea Field with 80 columns and 5 rows
*
* @param name the name of the field (call-by-name)
* @param defaultValue the starting value of the field (call-by-name)
*
* @param stuff - a list of filters and validations for the field
*
* @returns a newly minted Field{type ValueType = String}
*/
protected def textarea(name: => String, defaultValue: => String, stuff: FilterOrValidate[String]*): Field {type ValueType = String} =
textarea(name, defaultValue, 5, 80, stuff: _*)
/**
* Create a textarea Field
*
* @param name the name of the field (call-by-name)
* @param defaultValue the starting value of the field (call-by-name)
* @param rows the number of rows in the textarea
* @param cols the number of columns in the textarea
*
* @param stuff - a list of filters and validations for the field
*
* @returns a newly minted Field{type ValueType = String}
*/
protected def textarea(name: => String, defaultValue: => String, rows: Int, cols: Int, stuff: FilterOrValidate[String]*):
Field {type ValueType = String} = {
val eAttr: List[SHtml.ElemAttr] = (("rows" -> rows.toString): SHtml.ElemAttr) ::
(("cols" -> cols.toString): SHtml.ElemAttr) :: grabParams(stuff)
makeField[String, Nothing](name,
defaultValue,
field => SHtml.textarea(field.is,
field.set(_),
eAttr: _*),
NothingOtherValueInitializer,
stuff: _*)
}
/**
* Create a select HTML element
*
* @param name the name of the field (call-by-name)
* @param default the starting value of the field (call-by-name)
* @param choices the possible choices for the select
*
* @param stuff - a list of filters and validations for the field
* @param f a PairStringPromoter (a wrapper around a function) that converts T => display String
*
* @returns a newly minted Field{type ValueType = String}
*/
protected def select[T](name: => String, default: => T, choices: => Seq[T], stuff: FilterOrValidate[T]*)
(implicit f: SHtml.PairStringPromoter[T]): Field {type ValueType = T; type OtherValueType = Seq[T]}
= {
val eAttr = grabParams(stuff)
makeField[T, Seq[T]](name, default,
field =>
SHtml.selectElem(field.otherValue,
Full(field.is), eAttr: _*)(field.set(_)),
OtherValueInitializerImpl[Seq[T]](() => choices),
stuff: _*)
}
/**
* Create a multi select HTML element
*
* @param name the name of the field (call-by-name)
* @param default the starting value of the field (call-by-name)
* @param choices the possible choices for the select
*
* @param stuff - a list of filters and validations for the field
* @param f a PairStringPromoter (a wrapper around a function) that converts T => display String
*
* @returns a newly minted Field{type ValueType = String}
*/
protected def multiselect[T](name: => String, default: => Seq[T], choices: => Seq[T], stuff: FilterOrValidate[Seq[T]]*)
(implicit f: SHtml.PairStringPromoter[T]): Field {type ValueType = Seq[T]; type OtherValueType = Seq[T]}
= {
val eAttr = grabParams(stuff)
makeField[Seq[T], Seq[T]](name, default,
field =>
SHtml.multiSelectElem(field.otherValue,
field.is, eAttr: _*)(field.set(_)),
OtherValueInitializerImpl[Seq[T]](() => choices),
stuff: _*)
}
/**
* Grabs the FormFieldId and FormParam parameters
*/
protected def grabParams(in: Seq[FilterOrValidate[_]]):
List[SHtml.ElemAttr] = {
val sl = in.toList
in.collect {
case FormFieldId(id) => ("id" -> id): SHtml.ElemAttr
}.
headOption.toList :::
sl.collect {
case FormParam(fp) => fp
}
}
/**
* Create a radio HTML element
*
* @param name the name of the field (call-by-name)
* @param default the starting value of the field (call-by-name)
* @param choices the possible choices for the select
*
* @param stuff - a list of filters and validations for the field
* @param f a PairStringPromoter (a wrapper around a function) that converts T => display String
*
* @returns a newly minted Field{type ValueType = String}
*/
protected def radio(name: => String, default: => String, choices: => Seq[String], stuff: FilterOrValidate[String]*):
Field {type ValueType = String; type OtherValueType = Seq[String]} = {
val eAttr = grabParams(stuff)
makeField[String, Seq[String]](name, default,
field =>
Full(SHtml.radio(field.otherValue,
Full(field.is),
field.set _,
eAttr: _*).toForm),
OtherValueInitializerImpl[Seq[String]](() => choices),
stuff: _*)
}
}
trait ScreenWizardRendered {
protected def wrapInDiv(in: NodeSeq): Elem =
<div style="display: inline" id={FormGUID.get}>
{in}
</div>
protected def renderAll(currentScreenNumber: Box[NodeSeq],
screenCount: Box[NodeSeq],
wizardTop: Box[Elem],
screenTop: Box[Elem],
fields: List[ScreenFieldInfo],
prev: Box[Elem],
cancel: Box[Elem],
next: Box[Elem],
finish: Box[Elem],
screenBottom: Box[Elem],
wizardBottom: Box[Elem],
nextId: (String, () => JsCmd),
prevId: Box[(String, () => JsCmd)],
cancelId: (String, () => JsCmd),
theScreen: AbstractScreen,
ajax_? : Boolean): NodeSeq = {
val notices: List[(NoticeType.Value, NodeSeq, Box[String])] = S.getAllNotices
def bindFieldLine(xhtml: NodeSeq): NodeSeq = {
fields.flatMap {
f =>
val theFormEarly = f.input
val curId = theFormEarly.flatMap(Helpers.findId) or
f.field.uniqueFieldId openOr Helpers.nextFuncName
val theForm = theFormEarly.map {
fe => {
val f = Helpers.deepEnsureUniqueId(fe)
val id = Helpers.findBox(f)(_.attribute("id").
map(_.text).
filter(_ == curId))