forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.scala
More file actions
229 lines (217 loc) · 7.6 KB
/
Copy pathevent.scala
File metadata and controls
229 lines (217 loc) · 7.6 KB
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
package views.html
import controllers.routes
import play.api.data.Form
import lila.app.templating.Environment.{ given, * }
import lila.app.ui.ScalatagsTemplate.{ *, given }
import lila.common.{ Markdown, MarkdownRender }
import lila.event.{ Event, EventForm }
import lila.i18n.LangList
object event:
private val dataSeconds = attr("data-seconds")
def create(form: Form[?])(using PageContext) =
layout(title = "New event", css = "mod.form") {
div(cls := "crud page-menu__content box box-pad")(
h1(cls := "box__top")("New event"),
postForm(cls := "content_box_content form3", action := routes.Event.create)(inForm(form))
)
}
def edit(event: Event, form: Form[?])(using PageContext) =
layout(title = event.title, css = "mod.form") {
div(cls := "crud edit page-menu__content box box-pad")(
boxTop(
h1(
a(href := routes.Event.show(event.id))(event.title),
span("Created by ", titleNameOrId(event.createdBy), " ", momentFromNow(event.createdAt)),
event.updatedBy map { updatedBy =>
span("Updated by ", titleNameOrId(updatedBy), " ", event.updatedAt.map(momentFromNow(_)))
}
),
st.form(cls := "box__top__actions", action := routes.Event.cloneE(event.id), method := "get")(
form3.submit("Clone", licon.Mic.some)(cls := "button-green button-empty")
)
),
standardFlash,
postForm(cls := "content_box_content form3", action := routes.Event.update(event.id))(inForm(form))
)
}
def iconOf(e: Event) =
e.icon match
case None => i(cls := "img", dataIcon := licon.Mic)
case Some(c) if c == EventForm.icon.broadcast => i(cls := "img", dataIcon := licon.RadioTower)
case Some(c) => img(cls := "img", src := assetUrl(s"images/$c"))
def show(e: Event)(using PageContext) =
views.html.base.layout(
title = e.title,
moreCss = cssTag("event"),
moreJs = iifeModule("javascripts/event-countdown.js")
) {
main(cls := "page-small event box box-pad")(
boxTop(
iconOf(e),
div(
h1(e.title),
strong(cls := "headline")(e.headline)
)
),
e.description.map { d =>
div(cls := "desc")(markdown(e, d))
},
if e.isFinished then p(cls := "desc")("The event is finished.")
else if e.isNow then a(href := e.url, cls := "button button-fat")(trans.eventInProgress())
else
ul(cls := "countdown", dataSeconds := (~e.secondsToStart + 1))(
List("Days", "Hours", "Minutes", "Seconds") map { t =>
li(span(cls := t.toLowerCase), t)
}
)
)
}
private object markdown:
private val renderer = new MarkdownRender(table = true, list = true)
// hashcode caching is safe for official events
private val cache = lila.memo.CacheApi.scaffeineNoScheduler
.expireAfterAccess(10 minutes)
.maximumSize(64)
.build[Int, Html]()
def apply(e: Event, text: Markdown): Frag =
rawHtml(cache.get(text.hashCode, _ => renderer(s"event:${e.id}")(text)))
def manager(events: List[Event])(using PageContext) =
val title = "Event manager"
layout(title = title) {
div(cls := "crud page-menu__content box")(
boxTop(
h1(title),
div(cls := "box__top__actions")(
a(cls := "button button-green", href := routes.Event.form, dataIcon := licon.PlusButton)
)
),
table(cls := "slist slist-pad")(
thead(
tr(
th,
th(utcLink, " start"),
th(utcLink, " end"),
th
)
),
tbody(
events.map { e =>
tr(
td(
a(href := routes.Event.edit(e.id))(
strong(e.title),
em(e.headline)
)
),
td(
showInstantUTC(e.startsAt),
momentFromNow(e.startsAt)
),
td(
showInstantUTC(e.finishesAt),
momentFromNow(e.finishesAt)
),
td(a(cls := "text", href := routes.Event.show(e.id), dataIcon := licon.Eye))
)
}
)
)
)
}
private def inForm(form: Form[?])(using PageContext) =
frag(
form3.split(
form3.group(form("startsAt"), frag("Start date ", strong(utcLink)), half = true)(
form3.flatpickr(_, utc = true)
),
form3.group(form("finishesAt"), frag("End date ", strong(utcLink)), half = true)(
form3.flatpickr(_, utc = true)
)
),
form3.split(
form3.group(
form("title"),
raw("Short title"),
help = raw("Keep it VERY short, so it fits on homepage").some,
half = true
)(form3.input(_)),
form3.group(
form("icon"),
frag("Icon"),
half = true,
help = frag("Displayed on the homepage button").some
)(form3.select(_, EventForm.icon.choices))
),
form3.group(
form("headline"),
raw("Short headline"),
help = raw("Keep it VERY short, so it fits on homepage").some
)(form3.input(_)),
form3
.group(form("description"), raw("Possibly long description"), help = raw("Markdown enabled").some)(
form3.textarea(_)(rows := 15)
),
form3.split(
form3.group(
form("url"),
raw("External URL"),
help = raw("What to redirect to when the event starts").some,
half = true
)(form3.input(_)),
form3.checkbox(
form("countdown"),
frag("Show countdown"),
help = frag(
"Show a countdown on the event page before start. Unselect to redirect to the event URL immediately, even before the event has started."
).some,
half = true
)
),
form3.split(
form3.group(form("lang"), raw("Language"), half = true)(
form3.select(
_,
lila.i18n.LangList.popularNoRegion.map { l =>
l.code -> s"${l.language.toUpperCase} ${LangList name l}"
}
)
),
form3.group(
form("hostedBy"),
raw("Hosted by Lichess user"),
help = raw("Username that must not be featured while the event is ongoing").some,
half = true
) { f =>
div(cls := "complete-parent")(
input(
cls := "form-control user-autocomplete",
name := f.name,
id := form3.id(f),
value := f.value,
dataTag := "span"
)
)
}
),
form3.split(
form3.checkbox(form("enabled"), raw("Enabled"), help = raw("Display the event").some, half = true),
form3.group(
form("homepageHours"),
raw("Hours on homepage before the start (0 to 24)"),
half = true,
help = raw("Go easy on this. The event will also remain on homepage while ongoing.").some
)(form3.input(_, typ = "number")(step := ".01"))
),
form3.action(form3.submit(trans.apply()))
)
private def layout(title: String, css: String = "mod.misc")(body: Frag)(using PageContext) =
views.html.base.layout(
title = title,
moreCss = cssTag(css),
moreJs = jsModule("flatpickr")
) {
main(cls := "page-menu")(
mod.menu("event"),
body
)
}