-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page.scala
173 lines (131 loc) · 4.03 KB
/
Page.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
package swebdsl.ui
import collection.mutable.HashMap
import java.io._
import javax.servlet.http._
object PageMode extends Enumeration {
type PageMode = Value
val DataBind, Action, Render = Value
}
case class IgnoreMe()
trait Cache {
private var cache: HashMap[String, AnyRef] = new HashMap[String, AnyRef]
def cache[T](key: String, l: => T): T = {
if (!cache.contains(key)) {
cache(key) = l.asInstanceOf[AnyRef]
}
cache(key).asInstanceOf[T]
}
}
abstract case class Page(ign: IgnoreMe) extends Forms with Cache {
var sectionDepth = 1
var style = new Style
var out: PrintWriter = null
var request: HttpServletRequest = null
var response: HttpServletResponse = null
protected var mode = PageMode.DataBind
def ui
def title: String = productPrefix
def name: String = productPrefix.toLowerCase
def init(out: PrintWriter, req: HttpServletRequest, res: HttpServletResponse) {
this.out = out
this.request = req
this.response = res
postInit
}
def postInit {
}
def resetCounters {
actionCounter = 0
formCounter = 0
inputCounter = 0
}
def write(s: String) {
out.print(s)
}
def doDatabind {
mode = PageMode.DataBind
ui
}
def doAction {
mode = PageMode.Action
ui
}
def doRender {
mode = PageMode.Render
write("<html><head>")
write(style.toString)
write("<title>" + title + "</title>")
write("</head><body>")
ui
write("</body></html>")
}
def html(o: AnyRef) {
mode match {
case PageMode.Render => write(o.toString)
case _ =>
}
}
def header(content: => Unit) = renderSimple("<h" + sectionDepth + " class=\"header\">", "</h" + sectionDepth + ">", content)
def header(s: String) = renderSimple("<h" + sectionDepth + " class=\"header\">", "</h" + sectionDepth + ">", text(s))
def text(s: String) = html(s) // @TODO: escape this
def block(s: String)(content: => Unit) = renderSimple("<div class=\"" + s + "\">", "</div>", content)
def img(s: String) = html("<img src=\"" + s + "\" class=\"img\"/>")
def br = html("<br/>")
def hr = html("<hr/>")
def navigate(url: String)(content: => Unit) = renderSimple("<a href=\"" + url + "\" class=\"navigate\">", "</a>", content)
def navigate(p: Page)(content: => Unit) = renderSimple("<a href=\"" + buildPageUrl(p) + "\" class=\"navigate\">", "</a>", content)
def buildPageUrl(p: Page): String = {
val c = p.getClass
var queryStr = new StringBuilder
for (i <- 0 until p.productArity) {
val value = p.productElement(i)
queryStr.append("/" + value.toString)
}
request.getContextPath + "/" + p.name + queryStr.toString
}
def section(content: => Unit) {
mode match {
case PageMode.Render => {
write("<div class=\"section\">")
sectionDepth += 1
content
sectionDepth -= 1
write("</div>")
}
case _ => content
}
}
def list(content: => Unit) = renderSimple("<ul class=\"list\">", "</ul>", content)
def listitem(s: String) = renderSimple("<li class=\"listitem\">", "</li>", text(s))
def listitem(content: => Unit) = renderSimple("<li class=\"listitem\">", "</li>", content)
def table(content: => Unit) = renderSimple("<table>", "</table>", content)
def row(content: => Unit) = renderSimple("<tr>", "</tr>", content)
def col(content: => Unit) = renderSimple("<td>", "</td>", content)
protected def renderSimple(beginTag: String, endTag: String, content: => Unit) = {
mode match {
case PageMode.Render => {
write(beginTag)
content
write(endTag)
}
case _ => content
}
}
protected def html(html: String) {
mode match {
case PageMode.Render => {
write(html)
}
case _ =>
}
}
def goto(url: String) {
response.sendRedirect(url)
}
def goto(p: Page) {
response.sendRedirect(buildPageUrl(p))
}
def this() = this (new IgnoreMe)
if (productArity == 1 && (productElement(0) == IgnoreMe()))
throw new IllegalStateException("Pages must be defined as case classes")
}