-
Notifications
You must be signed in to change notification settings - Fork 275
/
JsCommands.scala
executable file
·1000 lines (808 loc) · 29 KB
/
JsCommands.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
/*
* Copyright 2007-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
package js
import scala.xml.{NodeSeq, Group, Unparsed, Elem}
import net.liftweb.util.Helpers._
import net.liftweb.common._
import net.liftweb.util._
import scala.xml.Node
object JsCommands {
def create = new JsCommands(Nil)
def apply(in: Seq[JsCmd]) = new JsCommands(in.toList.reverse)
def apply(in: JsExp) = new JsCommands(List(in.cmd))
}
/**
* A container for accumulating `[[JsCmd]]`s that need to be sent to the client.
* When `[[toResponse]]` is called to finalize the response, in addition to the
* JS passed directly to this instance, the commands in `[[S.jsToAppend]]` are
* also read and included in the response. Also in this process, all of the
* `JsCmd` instances have their `toJsCmd` methods called to convert them to a
* string.
*
* @note The contents of `jsToAppend` are cleared in this process!
*/
class JsCommands(val reverseList: List[JsCmd]) {
def &(in: JsCmd) = new JsCommands(in :: reverseList)
def &(in: List[JsCmd]) = new JsCommands(in.reverse ::: reverseList)
def toResponse = {
// Evaluate all toJsCmds, which may in turn call S.append[Global]Js.
val containedJs = reverseList.reverse.map(_.toJsCmd)
val toAppend = S.jsToAppend(clearAfterReading = true).map(_.toJsCmd)
val data = (containedJs ++ toAppend).mkString("\n").getBytes("UTF-8")
InMemoryResponse(data, List("Content-Length" -> data.length.toString, "Content-Type" -> "text/javascript; charset=utf-8"), S.responseCookies, 200)
}
}
case class JsonCall(funcId: String) {
def exp(exp: JsExp): JsCmd = JsCmds.Run(funcId + "(" + exp.toJsCmd + ");")
def apply(command: String): JsCmd = apply(JE.Str(command))
def apply(command: JsExp): JsCmd =
JsCmds.Run(funcId + "({'command': " + command.toJsCmd + ", 'params': false});")
def apply(command: String, params: JsExp) =
JsCmds.Run(funcId + "({'command': " + command.encJs + ", 'params':" +
params.toJsCmd + "});")
def apply(command: String, target: String, params: JsExp) =
JsCmds.Run(funcId + "({'command': " + command.encJs + ", 'target': " +
target.encJs +
", 'params':" +
params.toJsCmd + "});")
def apply(command: JsExp, params: JsExp) =
JsCmds.Run(funcId + "({'command': " + command.toJsCmd + ", 'params':" +
params.toJsCmd + "});")
def apply(command: JsExp, target: JsExp, params: JsExp) =
JsCmds.Run(funcId + "({'command': " + command.toJsCmd + ", 'target': " +
target.toJsCmd +
", 'params':" +
params.toJsCmd + "});")
}
trait JsObj extends JsExp {
def props: List[(String, JsExp)]
def toJsCmd = props.map {case (n, v) => n.encJs + ": " + v.toJsCmd}.mkString("{", ", ", "}")
override def toString(): String = toJsCmd
override def equals(other: Any): Boolean = {
other match {
case jsObj: JsObj => {
import scala.annotation.tailrec
@tailrec def test(me: Map[String, JsExp], them: List[(String, JsExp)]): Boolean = {
them match {
case Nil => me.isEmpty
case _ if me.isEmpty => false
case (k, v) :: xs =>
me.get(k) match {
case None => false
case Some(mv) if mv != v => false
case _ => test(me - k, xs)
}
}
}
test(Map(props :_*), jsObj.props)
}
case x => super.equals(x)
}
}
def +*(other: JsObj) = {
val np = props ::: other.props
new JsObj {
def props = np
}
}
/**
* Overwrites any existing keys and adds the rest.
*/
def extend(other: JsObj) = {
// existing, non-existing props
val (ep, nep) = other.props.partition { case (key, exp) => props.exists { case (k, e) => k == key }}
// replaced props
val rp = props.map { case (key, exp) =>
ep.find { case (k, e) => k == key }.getOrElse(key -> exp)
}
new JsObj {
def props = rp ::: nep
}
}
}
/**
* The companion object to JsExp that has some
* helpful conversions to/from Lift's JSON library
*/
object JsExp {
import json._
implicit def jValueToJsExp(jv: JValue): JsExp = new JsExp {
lazy val toJsCmd = compactRender(jv)
}
implicit def strToJsExp(str: String): JE.Str = JE.Str(str)
implicit def boolToJsExp(b: Boolean): JsExp = JE.boolToJsExp(b)
implicit def intToJsExp(in: Int): JE.Num = JE.Num(in)
implicit def longToJsExp(in: Long): JE.Num = JE.Num(in)
implicit def doubleToJsExp(in: Double): JE.Num = JE.Num(in)
implicit def floatToJsExp(in: Float): JE.Num = JE.Num(in)
implicit def numToJValue(in: JE.Num): JValue = in match {
case JE.Num(n) => JDouble(n.doubleValue())
}
implicit def strToJValue(in: JE.Str): JValue = JString(in.str)
}
/**
* The basic JavaScript expression
*/
trait JsExp extends HtmlFixer with ToJsCmd {
def toJsCmd: String
override def equals(other: Any): Boolean = {
other match {
case jx: JsExp => this.toJsCmd == jx.toJsCmd
case _ => super.equals(other)
}
}
override def toString = "JsExp("+toJsCmd+")"
def appendToParent(parentName: String): JsCmd = {
val ran = "v" + Helpers.nextFuncName
JsCmds.JsCrVar(ran, this) &
JE.JsRaw("if (" + ran + ".parentNode) " + ran + " = " + ran + ".cloneNode(true)").cmd &
JE.JsRaw("if (" + ran + ".nodeType) {" + parentName + ".appendChild(" + ran + ");} else {" +
parentName + ".appendChild(document.createTextNode(" + ran + "));}").cmd
}
/**
* ~> accesses a property in the current JsExp
*/
def ~>(right: JsMember): JsExp = new JsExp {
def toJsCmd = JsExp.this.toJsCmd + "." + right.toJsCmd
}
def ~>(right: Box[JsMember]): JsExp = right.dmap(this)(r => ~>(r))
def cmd: JsCmd = JsCmds.Run(toJsCmd + ";")
def +(right: JsExp): JsExp = new JsExp {
def toJsCmd = JsExp.this.toJsCmd + " + " + right.toJsCmd
}
def ===(right: JsExp): JsExp = new JsExp {
def toJsCmd = JsExp.this.toJsCmd + " = " + right.toJsCmd
}
}
trait JsMember {
def toJsCmd: String
}
/**
* JavaScript Expressions. To see these in action, check out
* sites/example/src/webapp/json.html
*/
object JE {
def boolToJsExp(in: Boolean): JsExp = if (in) JsTrue else JsFalse
/**
* The companion object to Num which has some helpful
* constructors
*/
object Num {
def apply(i: Int): Num = new Num(i)
def apply(lng: Long): Num = new Num(lng)
def apply(d: Double): Num = new Num(d)
def apply(f: Float): Num = new Num(f)
}
case class Num(n: Number) extends JsExp {
def toJsCmd = n.toString
}
case class Stringify(in: JsExp) extends JsExp {
def toJsCmd = "JSON.stringify(" + in.toJsCmd + ")"
}
case class JsArray(in: JsExp*) extends JsExp {
def toJsCmd = new JsExp {
def toJsCmd = in.map(_.toJsCmd).mkString("[", ", ", "]\n")
}.toJsCmd
def this(in: List[JsExp]) = this (in: _*)
}
object JsArray {
def apply(in: List[JsExp]) = new JsArray(in: _*)
}
case class ValById(id: String) extends JsExp {
def toJsCmd = "(function() {if (document.getElementById(" + id.encJs + ")) {return document.getElementById(" + id.encJs + ").value;} else {return null;}})()"
}
/**
* Given the id of a checkbox, see if it's checked
*/
case class CheckedById(id: String) extends JsExp {
def toJsCmd = "(function() {if (document.getElementById(" + id.encJs + ")) {return document.getElementById(" + id.encJs + ").checked} else {return false;}})()"
}
/**
* gets the element by ID
*/
case class ElemById(id: String, thenStr: String*) extends JsExp {
override def toJsCmd = "document.getElementById(" + id.encJs + ")" + (
if (thenStr.isEmpty) "" else thenStr.mkString(".", ".", "")
)
}
/**
* Gives the parent node of the node denominated by the id
*
* @param id - the id of the node
*/
case class ParentOf(id: String) extends JsExp {
def toJsCmd = (ElemById(id) ~> Parent).toJsCmd
}
object LjSwappable {
def apply(visible: JsExp, hidden: JsExp): JxBase = {
new JxNodeBase {
def child = Nil
def appendToParent(name: String): JsCmd =
JsRaw(name + ".appendChild(lift$.swappable(" + visible.toJsCmd
+ ", " + hidden.toJsCmd + "))").cmd
}
}
def apply(visible: NodeSeq, hidden: NodeSeq): JxBase = {
new JxNodeBase {
def child = Nil
def appendToParent(name: String): JsCmd =
JsRaw(name + ".appendChild(lift$.swappable(" + AnonFunc(
JsCmds.JsCrVar("df", JsRaw("document.createDocumentFragment()")) &
addToDocFrag("df", visible.toList) &
JE.JsRaw("return df").cmd
).toJsCmd
+ "(), " + AnonFunc(JsCmds.JsCrVar("df", JsRaw("document.createDocumentFragment()")) &
addToDocFrag("df", hidden.toList) &
JE.JsRaw("return df").cmd).toJsCmd + "()))").cmd
}
}
}
object LjBuildIndex {
def apply(obj: String,
indexName: String, tables: (String, String)*): JsExp = new JsExp {
def toJsCmd = "lift$.buildIndex(" + obj + ", " + indexName.encJs +
(if (tables.isEmpty) "" else ", " +
tables.map {case (l, r) => "[" + l.encJs + ", " + r.encJs + "]"}.mkString(", ")) +
")"
}
def apply(obj: JsExp,
indexName: String, tables: (String, String)*): JsExp = new JsExp {
def toJsCmd = "lift$.buildIndex(" + obj.toJsCmd + ", " + indexName.encJs +
(if (tables.isEmpty) "" else ", " +
tables.map {case (l, r) => "[" + l.encJs + ", " + r.encJs + "]"}.mkString(", ")) +
")"
}
}
protected trait MostLjFuncs {
def funcName: String
def apply(obj: String, func: String): JsExp = new JsExp {
def toJsCmd = "lift$." + funcName + "(" + obj + ", " + func.encJs + ")"
}
def apply(obj: JsExp, func: JsExp): JsExp = new JsExp {
def toJsCmd = "lift$." + funcName + "(" + obj.toJsCmd + ", " + func.toJsCmd + ")"
}
}
object LjAlt {
def apply(obj: String, func: String, alt: String): JsExp = new JsExp {
def toJsCmd = "lift$.alt(" + obj + ", " + func.encJs + ", " + alt.encJs + ")"
}
def apply(obj: JsExp, func: JsExp, alt: String): JsExp = new JsExp {
def toJsCmd = "lift$.alt(" + obj.toJsCmd + ", " + func.toJsCmd + ", " + alt.encJs + ")"
}
def apply(obj: JsExp, func: JsExp, alt: JsExp): JsExp = new JsExp {
def toJsCmd = "lift$.alt(" + obj.toJsCmd + ", " + func.toJsCmd + ", " + alt.toJsCmd + ")"
}
}
object LjMagicUpdate {
def apply(obj: String, field: String, idField: String, toUpdate: JsExp): JsExp = new JsExp {
def toJsCmd = "lift$.magicUpdate(" + obj + ", " + field.encJs + ", " + idField.encJs + ", " + toUpdate.toJsCmd + ")"
}
def apply(obj: JsExp, field: String, idField: String, toUpdate: JsExp): JsExp = new JsExp {
def toJsCmd = "lift$.magicUpdate(" + obj.toJsCmd + ", " + field.encJs + ", " + idField.encJs + ", " + toUpdate.toJsCmd + ")"
}
}
object LjForeach extends MostLjFuncs {
def funcName: String = "foreach"
}
object LjFilter extends MostLjFuncs {
def funcName: String = "filter"
}
object LjMap extends MostLjFuncs {
def funcName: String = "map"
}
object LjFold {
def apply(what: JsExp, init1: JsExp, func: String): JsExp = new JsExp {
def toJsCmd = "lift$.fold(" + what.toJsCmd + ", " + init1.toJsCmd + ", " + func.encJs + ")"
}
def apply(what: JsExp, init1: JsExp, func: AnonFunc): JsExp = new JsExp {
def toJsCmd = "lift$.fold(" + what.toJsCmd + ", " + init1.toJsCmd + ", " + func.toJsCmd + ")"
}
}
object LjFlatMap extends MostLjFuncs {
def funcName: String = "flatMap"
}
object LjSort extends MostLjFuncs {
def funcName: String = "sort"
def apply(obj: String): JsExp = new JsExp {
def toJsCmd = "lift$." + funcName + "(" + obj + ")"
}
def apply(obj: JsExp): JsExp = new JsExp {
def toJsCmd = "lift$." + funcName + "(" + obj.toJsCmd + ")"
}
}
object FormToJSON {
def apply(formId: String) = new JsExp {
def toJsCmd = LiftRules.jsArtifacts.formToJSON(formId).toJsCmd;
}
}
/**
* A String (JavaScript encoded)
*/
case class Str(str: String) extends JsExp {
def toJsCmd = str.encJs
}
/**
* A JavaScript method that takes parameters
*
* JsFunc is very similar to Call but only the latter will be implicitly converted to a JsCmd.
* @see Call
*/
case class JsFunc(method: String, params: JsExp*) extends JsMember {
def toJsCmd = params.map(_.toJsCmd).mkString(method + "(", ", ", ")")
def cmd: JsCmd = JsCmds.Run(toJsCmd + ";")
}
/**
* Put any JavaScript expression you want in here and the result will be
* evaluated.
*/
case class JsRaw(rawJsCmd: String) extends JsExp {
def toJsCmd = rawJsCmd
}
case class JsVar(varName: String, andThen: String*) extends JsExp {
def toJsCmd = varName + (if (andThen.isEmpty) ""
else andThen.mkString(".", ".", ""))
}
/**
* A value that can be retrieved from an expression
*/
case class JsVal(valueName: String) extends JsMember {
def toJsCmd = valueName
}
case object Id extends JsMember {
def toJsCmd = "id"
}
case object Parent extends JsMember {
def toJsCmd = "parentNode"
}
case object Style extends JsMember {
def toJsCmd = "style"
}
case object Value extends JsMember {
def toJsCmd = "value"
}
case object JsFalse extends JsExp {
def toJsCmd = "false"
}
case object JsNull extends JsExp {
def toJsCmd = "null"
}
case object JsTrue extends JsExp {
def toJsCmd = "true"
}
/**
* A JavaScript method that takes parameters
*
* Call is very similar to JsFunc but only the former will be implicitly converted to a JsCmd.
* @see JsFunc
*/
case class Call(function: String, params: JsExp*) extends JsExp {
def toJsCmd = function + "(" + params.map(_.toJsCmd).mkString(",") + ")"
}
trait AnonFunc extends JsExp {
def applied: JsExp = new JsExp {
def toJsCmd = "(" + AnonFunc.this.toJsCmd + ")" + "()"
}
def applied(params: JsExp*): JsExp = new JsExp {
def toJsCmd = "(" + AnonFunc.this.toJsCmd + ")" +
params.map(_.toJsCmd).mkString("(", ",", ")")
}
}
object AnonFunc {
def apply(in: JsCmd): AnonFunc = new JsExp with AnonFunc {
def toJsCmd = "function() {" + in.toJsCmd + "}"
}
def apply(params: String, in: JsCmd): AnonFunc = new JsExp with AnonFunc {
def toJsCmd = "function(" + params + ") {" + in.toJsCmd + "}"
}
}
object JsObj {
def apply(members: (String, JsExp)*): JsObj =
new JsObj {
def props = members.toList
}
}
case class JsLt(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " < " + right.toJsCmd
}
case class JsGt(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " > " + right.toJsCmd
}
case class JsEq(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " == " + right.toJsCmd
}
case class JsNotEq(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " != " + right.toJsCmd
}
case class JsLtEq(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " <= " + right.toJsCmd
}
case class JsGtEq(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " >= " + right.toJsCmd
}
case class JsOr(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " || " + right.toJsCmd
}
case class JsAnd(left: JsExp, right: JsExp) extends JsExp {
def toJsCmd = left.toJsCmd + " && " + right.toJsCmd
}
case class JsNot(exp: JsExp) extends JsExp {
def toJsCmd = "!" + exp.toJsCmd
}
}
trait HtmlFixer {
/**
* Calls fixHtmlAndJs and if there's embedded script tags,
* construct a function that executes the contents of the scripts
* then evaluations to Expression. For use when converting
* a JsExp that contains HTML.
*/
def fixHtmlFunc(uid: String, content: NodeSeq)(f: String => String) =
fixHtmlAndJs(uid, content) match {
case (str, Nil) => f(str)
case (str, cmds) => "((function() {"+cmds.reduceLeft{_ & _}.toJsCmd+" return "+f(str)+";})())"
}
/**
* Calls fixHtmlAndJs and if there's embedded script tags,
* append the JsCmds to the String returned from applying
* the function to the enclosed HTML.
* For use when converting
* a JsCmd that contains HTML.
*/
def fixHtmlCmdFunc(uid: String, content: NodeSeq)(f: String => String) =
fixHtmlAndJs(uid, content) match {
case (str, Nil) => f(str)
case (str, cmds) => f(str)+"; "+cmds.reduceLeft(_ & _).toJsCmd
}
/**
* Super important... call fixHtml at instance creation time and only once
* This method must be run in the context of the thing creating the XHTML
* to capture the bound functions
*/
protected def fixHtmlAndJs(uid: String, content: NodeSeq): (String, List[JsCmd]) = {
import Helpers._
val w = new java.io.StringWriter
val NodesAndEventJs(xhtml, eventJs) =
S.session.map { session =>
session.normalizeHtmlAndEventHandlers(
session.processSurroundAndInclude(
s"JS SetHTML id: $uid",
content
)
)
} openOr {
NodesAndEventJs(content, JsCmds.Noop)
}
import scala.collection.mutable.ListBuffer
val lb =
if (eventJs == JsCmds.Noop) {
ListBuffer[JsCmd]()
} else {
ListBuffer[JsCmd](eventJs)
}
val revised = ("script" #> nsFunc(ns => {
ns match {
case FindScript(e) => {
lb += JE.JsRaw(ns.text).cmd
NodeSeq.Empty
}
case x => x
}
})).apply(xhtml)
S.htmlProperties.htmlWriter(Group(revised), w)
(w.toString.encJs, lb.toList)
}
private object FindScript {
def unapply(in: NodeSeq): Option[Elem] = in match {
case e: Elem => {
e.attribute("type").map(_.text).filter(_ == "text/javascript").flatMap {
a =>
if (e.attribute("src").isEmpty) Some(e) else None
}
}
case _ => None
}
}
}
trait JsCmd extends HtmlFixer with ToJsCmd {
def &(other: JsCmd): JsCmd = {
if (other == JsCmds.Noop) {
this
} else {
JsCmds.CmdPair(this, other)
}
}
def toJsCmd: String
override def toString() = "JsCmd("+toJsCmd+")"
}
object JsCmd {
/**
* If you've got Unit and need a JsCmd, return a Noop
*/
implicit def unitToJsCmd(in: Unit): JsCmd = JsCmds.Noop
}
object JsCmds {
implicit def seqJsToJs(in: Seq[JsCmd]): JsCmd = in.foldLeft[JsCmd](Noop)(_ & _)
object Script {
def apply(script: JsCmd): Node = <script type="text/javascript">{Unparsed("""
// <![CDATA[
""" + fixEndScriptTag(script.toJsCmd) + """
// ]]>
""")}</script>
private def fixEndScriptTag(in: String): String =
"""\<\/script\>""".r.replaceAllIn(in, """<\\/script>""")
}
def JsHideId(what: String): JsCmd = LiftRules.jsArtifacts.hide(what).cmd
def JsShowId(what: String): JsCmd = LiftRules.jsArtifacts.show(what).cmd
/**
* Replaces the node having the provided id with the markup given by node
*
* @param id - the id of the node that will be replaced
* @param node - the new node
*/
case class Replace(id: String, content: NodeSeq) extends JsCmd {
val toJsCmd = LiftRules.jsArtifacts.replace(id, Helpers.stripHead(content)).toJsCmd
}
/**
* Replaces the content of the node with the provided id with the markup given by content
*
* This is analogous to assigning a new value to a DOM object's innerHtml property in Javascript.
*
* @param id - the id of the node whose content will be replaced
* @param content - the new content
*/
case class SetHtml(uid: String, content: NodeSeq) extends JsCmd {
// we want eager evaluation of the snippets so they get evaluated in context
val toJsCmd = LiftRules.jsArtifacts.setHtml(uid, Helpers.stripHead(content)).toJsCmd
}
/**
* Makes the parameter the selected HTML element on load of the page
*
* @param in the element that should have focus
*
* @return the element and a script that will give the element focus
*/
@deprecated("Use S.appendJs(Focus(id))","3.0.0")
object FocusOnLoad {
def apply(in: Elem): NodeSeq = {
val (elem, id) = findOrAddId(in)
elem ++ Script(LiftRules.jsArtifacts.onLoad(Run("if (document.getElementById(" + id.encJs + ")) {document.getElementById(" + id.encJs + ").focus();};")))
}
}
/**
* Sets the value of an element and sets the focus
*/
case class SetValueAndFocus(id: String, value: String) extends JsCmd {
def toJsCmd = "if (document.getElementById(" + id.encJs + ")) {document.getElementById(" + id.encJs + ").value = " +
value.encJs +
"; document.getElementById(" + id.encJs + ").focus();};"
}
/**
* Sets the focus on the element denominated by the id
*/
case class Focus(id: String) extends JsCmd {
def toJsCmd = "if (document.getElementById(" + id.encJs + ")) {document.getElementById(" + id.encJs + ").focus();};"
}
/**
* Creates a JavaScript function with a name, a parameters list and
* a function body
*/
object Function {
def apply(name: String, params: List[String], body: JsCmd): JsCmd =
new JsCmd {
def toJsCmd = "function " + name + "(" +
params.mkString(", ") + """) {
""" + body.toJsCmd + """
}
"""
}
}
/**
* Execute the 'what' code when the page is ready for use
*/
object OnLoad {
def apply(what: JsCmd): JsCmd = LiftRules.jsArtifacts.onLoad(what)
}
/**
* Sets the value to the element having the 'id' attribute with
* the result of the 'right' expression
*/
case class SetValById(id: String, right: JsExp) extends JsCmd {
def toJsCmd = "if (document.getElementById(" + id.encJs + ")) {document.getElementById(" + id.encJs + ").value = " +
right.toJsCmd + ";};"
}
/**
* Assigns the value computed by the 'right' expression to the
* 'left' expression.
*/
case class SetExp(left: JsExp, right: JsExp) extends JsCmd {
def toJsCmd = left.toJsCmd + " = " + right.toJsCmd + ";"
}
/**
* Creates a JavaScript var named by 'name' and assigns it the
* value of 'right' expression.
*/
case class JsCrVar(name: String, right: JsExp) extends JsCmd {
def toJsCmd = "var " + name + " = " + right.toJsCmd + ";"
}
/**
* Assigns the value of 'right' to the members of the element
* having this 'id', chained by 'then' sequences
*/
case class SetElemById(id: String, right: JsExp, thenStr: String*) extends JsCmd {
def toJsCmd = "if (document.getElementById(" + id.encJs + ")) {document.getElementById(" + id.encJs + ")" + (
if (thenStr.isEmpty) "" else thenStr.mkString(".", ".", "")
) + " = " + right.toJsCmd + ";};"
}
implicit def jsExpToJsCmd(in: JsExp) = in.cmd
case class CmdPair(left: JsCmd, right: JsCmd) extends JsCmd {
import scala.collection.mutable.ListBuffer
def toJsCmd: String = {
val acc = new ListBuffer[JsCmd]()
appendDo(acc, left :: right :: Nil)
acc.map(_.toJsCmd).mkString("\n")
}
@scala.annotation.tailrec
private def appendDo(acc: ListBuffer[JsCmd], cmds: List[JsCmd]) {
cmds match {
case Nil =>
case CmdPair(l, r) :: rest => appendDo(acc, l :: r :: rest)
case `_Noop` :: rest => appendDo(acc, rest)
case a :: rest => acc.append(a); appendDo(acc, rest)
}
}
}
trait HasTime {
def time: Box[TimeSpan]
def timeStr = time.map(_.millis.toString) openOr ""
}
case class After(time: TimeSpan, toDo: JsCmd) extends JsCmd {
def toJsCmd = "setTimeout(function() {" + toDo.toJsCmd + "}, " + time.millis + ");"
}
case class Alert(text: String) extends JsCmd {
def toJsCmd = "alert(" + text.encJs + ");"
}
case class Prompt(text: String, default: String = "") extends JsExp {
def toJsCmd = "prompt(" + text.encJs + "," + default.encJs + ")"
}
case class Confirm(text: String, yes: JsCmd) extends JsCmd {
def toJsCmd = "if (confirm(" + text.encJs + ")) {" + yes.toJsCmd + "}"
}
case class Run(text: String) extends JsCmd {
def toJsCmd = text
}
case object _Noop extends JsCmd {
def toJsCmd = ""
}
implicit def cmdToString(in: JsCmd): String = in.toJsCmd
def Noop: JsCmd = _Noop
case class JsTry(what: JsCmd, alert: Boolean) extends JsCmd {
def toJsCmd = "try { " + what.toJsCmd + " } catch (e) {" + (if (alert) "alert(e);" else "") + "}"
}
/**
* JsSchedule the execution of the JsCmd using setTimeout()
* @param what the code to execute
*/
case class JsSchedule(what: JsCmd) extends JsCmd {
def toJsCmd = s"""setTimeout(function()
{
${what.toJsCmd}
} , 0);"""
}
/**
* A companion object with a helpful alternative constructor
*/
object RedirectTo {
/**
* Redirect to a page and execute the function
* when the page is loaded (only if the page is on the
* same server, not going to some other server on the internet)
*/
def apply(where: String, func: () => Unit): RedirectTo =
S.session match {
case Full(liftSession) =>
new RedirectTo(liftSession.attachRedirectFunc(where, Full(func)))
case _ => new RedirectTo(where)
}
}
case class RedirectTo(where: String) extends JsCmd {
private val where2 = // issue 176
if (where.startsWith("/") &&
!LiftRules.excludePathFromContextPathRewriting.vend(where)) (S.contextPath + where) else where
def toJsCmd = "window.location = " + S.encodeURL(where2).encJs + ";"
}
/**
* Reload the current page
*/
case object Reload extends JsCmd {
def toJsCmd = "window.location.reload();"
}
/**
* Update a Select with new Options
*/
case class ReplaceOptions(select: String, opts: List[(String, String)], dflt: Box[String]) extends JsCmd {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" +
opts.map {
case (value, text) =>
"y=document.createElement('option'); " +
"y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}.mkString("\n")+"};"
}
case object JsIf {
def apply(condition: JsExp, body: JsCmd): JsCmd = JE.JsRaw("if ( " + condition.toJsCmd + " ) { " + body.toJsCmd + " }")
def apply(condition: JsExp, bodyTrue: JsCmd, bodyFalse: JsCmd): JsCmd =
JE.JsRaw("if ( " + condition.toJsCmd + " ) { " + bodyTrue.toJsCmd + " } else { " + bodyFalse.toJsCmd + " }")
def apply(condition: JsExp, body: JsExp): JsCmd = JE.JsRaw("if ( " + condition.toJsCmd + " ) { " + body.toJsCmd + " }")
def apply(condition: JsExp, bodyTrue: JsExp, bodyFalse: JsExp): JsCmd =
JE.JsRaw("if ( " + condition.toJsCmd + " ) { " + bodyTrue.toJsCmd + " } else { " + bodyFalse.toJsCmd + " }")
}
case class JsWhile(condition: JsExp, body: JsExp) extends JsCmd {
def toJsCmd = "while ( " + condition.toJsCmd + " ) { " + body.toJsCmd + " }"
}
case class JsWith(reference: String, body: JsExp) extends JsCmd {
def toJsCmd = "with ( " + reference + " ) { " + body.toJsCmd + " }"
}
case class JsDoWhile(body: JsExp, condition: JsExp) extends JsCmd {
def toJsCmd = "do { " + body.toJsCmd + " } while ( " + condition.toJsCmd + " )"
}
case class JsFor(initialExp: JsExp, condition: JsExp, incrementExp: JsExp, body: JsExp) extends JsCmd {
def toJsCmd = "for ( " + initialExp.toJsCmd + "; " +
condition.toJsCmd + "; " +
incrementExp.toJsCmd + " ) { " + body.toJsCmd + " }"
}
case class JsForIn(initialExp: JsExp, reference: String, body: JsCmd) extends JsCmd {
def toJsCmd = "for ( " + initialExp.toJsCmd + " in " + reference + ") { " + body.toJsCmd + " }"
}
case object JsBreak extends JsCmd {
def toJsCmd = "break"
}
case object JsContinue extends JsCmd {
def toJsCmd = "continue"
}
object JsReturn {
def apply(in: JsExp): JsCmd = new JsCmd {
def toJsCmd = "return " + in.toJsCmd
}
def apply(): JsCmd = new JsCmd {
def toJsCmd = "return "
}
}
}
/**
* A collection of defaults for JavaScript related stuff
*/
object JsRules {
/**
* The default duration for displaying FadeOut and FadeIn
* messages.
*/
//@deprecated
@volatile var prefadeDuration: Helpers.TimeSpan = 5.seconds
/**
* The default fade time for fading FadeOut and FadeIn
* messages.
*/
//@deprecated
@volatile var fadeTime: Helpers.TimeSpan = 1.second
}