lila/app/views/event.scala

235 lines
7.6 KiB
Scala
Raw Permalink Normal View History

2019-04-20 20:50:03 -06:00
package views.html
2020-08-24 03:04:19 -06:00
import controllers.routes
2019-12-08 11:12:00 -07:00
import play.api.data.Form
2019-04-20 20:50:03 -06:00
import lila.api.Context
import lila.app.templating.Environment._
import lila.app.ui.ScalatagsTemplate._
import lila.event.{ Event, EventForm }
2021-04-20 02:23:51 -06:00
import lila.i18n.LangList
2019-04-20 20:50:03 -06:00
object event {
private val dataSeconds = attr("data-seconds")
def create(form: Form[_])(implicit ctx: Context) =
layout(title = "New event", css = "mod.form") {
div(cls := "crud page-menu__content box box-pad")(
h1("New event"),
postForm(cls := "content_box_content form3", action := routes.Event.create)(inForm(form))
2019-04-20 20:50:03 -06:00
)
}
def edit(event: Event, form: Form[_])(implicit ctx: Context) =
2019-04-20 20:50:03 -06:00
layout(title = event.title, css = "mod.form") {
div(cls := "crud edit page-menu__content box box-pad")(
2019-09-04 07:23:48 -06:00
div(cls := "box__top")(
h1(
2020-08-24 02:52:11 -06:00
a(href := routes.Event.show(event.id))(event.title),
2021-09-01 03:56:19 -06:00
span("Created by ", titleNameOrId(event.createdBy.value), " ", momentFromNow(event.createdAt)),
event.updatedBy map { updatedBy =>
2021-09-01 03:56:19 -06:00
span("Updated by ", titleNameOrId(updatedBy.value), " ", event.updatedAt.map(momentFromNow(_)))
}
2019-09-04 07:23:48 -06:00
),
2019-12-05 08:46:00 -07:00
st.form(cls := "box__top__actions", action := routes.Event.cloneE(event.id), method := "get")(
2021-06-21 11:40:09 -06:00
form3.submit("Clone", "".some)(cls := "button-green button-empty")
2019-09-04 07:23:48 -06:00
)
2019-04-20 20:50:03 -06:00
),
2020-01-17 13:05:42 -07:00
standardFlash(),
2019-08-02 01:54:15 -06:00
postForm(cls := "content_box_content form3", action := routes.Event.update(event.id))(inForm(form))
2019-04-20 20:50:03 -06:00
)
}
def iconOf(e: Event) =
e.icon match {
case None => i(cls := "img", dataIcon := "")
case Some(c) if c == EventForm.icon.broadcast => i(cls := "img", dataIcon := "")
case Some(c) => img(cls := "img", src := assetUrl(s"images/$c"))
}
def show(e: Event)(implicit ctx: Context) =
2019-12-13 07:30:20 -07:00
views.html.base.layout(
title = e.title,
moreCss = cssTag("event"),
moreJs = jsTag("event-countdown.js")
) {
2019-04-20 20:50:03 -06:00
main(cls := "page-small event box box-pad")(
2020-10-12 10:18:22 -06:00
div(cls := "box__top")(
iconOf(e),
2020-10-12 10:18:22 -06:00
div(
h1(e.title),
strong(cls := "headline")(e.headline)
)
),
2019-04-20 20:50:03 -06:00
e.description.map { d =>
div(cls := "desc")(markdown(e, d))
2019-04-20 20:50:03 -06:00
},
if (e.isFinished) p(cls := "desc")("The event is finished.")
2020-08-24 03:04:19 -06:00
else if (e.isNow) 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)
}
)
2019-04-20 20:50:03 -06:00
)
}
2021-06-27 01:23:42 -06:00
private object markdown {
import scala.concurrent.duration._
private val renderer = new lila.common.Markdown(table = true, list = true)
// hashcode caching is safe for official events
2021-09-02 03:24:22 -06:00
private val cache = lila.memo.CacheApi.scaffeineNoScheduler
.expireAfterAccess(10 minutes)
.maximumSize(64)
.build[Int, String]()
def apply(e: Event, text: String): Frag =
raw(cache.get(text.hashCode, _ => renderer(s"event:${e.id}")(text)))
2021-06-27 01:23:42 -06:00
}
def manager(events: List[Event])(implicit ctx: Context) = {
2019-04-20 20:50:03 -06:00
val title = "Event manager"
layout(title = title) {
div(cls := "crud page-menu__content box")(
div(cls := "box__top")(
h1(title),
div(cls := "box__top__actions")(
a(cls := "button button-green", href := routes.Event.form, dataIcon := "")
2019-04-20 20:50:03 -06:00
)
),
table(cls := "slist slist-pad")(
thead(
tr(
th,
2019-05-07 09:17:46 -06:00
th(utcLink, " start"),
th(utcLink, " end"),
2019-04-20 20:50:03 -06:00
th
)
),
tbody(
events.map { e =>
tr(
2019-12-13 07:30:20 -07:00
td(
a(href := routes.Event.edit(e.id))(
strong(e.title),
em(e.headline)
)
),
2019-04-20 20:50:03 -06:00
td(
showDateTimeUTC(e.startsAt),
momentFromNow(e.startsAt)
),
td(
showDateTimeUTC(e.finishesAt),
momentFromNow(e.finishesAt)
),
td(a(cls := "text", href := routes.Event.show(e.id), dataIcon := ""))
2019-04-20 20:50:03 -06:00
)
}
)
)
)
}
}
2020-05-05 22:11:15 -06:00
private def inForm(form: Form[_])(implicit ctx: Context) =
frag(
form3.split(
2020-09-12 04:30:40 -06:00
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)
)
2020-05-05 22:11:15 -06:00
),
2020-10-19 02:47:16 -06:00
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))
2020-10-19 02:47:16 -06:00
),
2020-05-05 22:11:15 -06:00
form3.group(
form("headline"),
raw("Short headline"),
help = raw("Keep it VERY short, so it fits on homepage").some
)(form3.input(_)),
form3
2020-10-12 10:08:51 -06:00
.group(form("description"), raw("Possibly long description"), help = raw("Markdown enabled").some)(
form3.textarea(_)(rows := 15)
2020-05-05 22:11:15 -06:00
),
2020-10-19 02:56:40 -06:00
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
)
),
2020-05-05 22:11:15 -06:00
form3.split(
2021-04-14 00:40:52 -06:00
form3.group(form("lang"), raw("Language"), half = true)(
2021-04-20 02:23:51 -06:00
form3.select(
_,
lila.i18n.LangList.popularNoRegion.map { l =>
l.code -> s"${l.language.toUpperCase} ${LangList name l}"
}
)
2021-04-14 00:40:52 -06:00
),
2020-05-05 22:11:15 -06:00
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 =>
2020-09-11 07:06:04 -06:00
div(cls := "complete-parent")(
input(
cls := "form-control user-autocomplete",
name := f.name,
id := form3.id(f),
value := f.value,
dataTag := "span"
)
2020-05-05 22:11:15 -06:00
)
}
),
form3.split(
form3.checkbox(form("enabled"), raw("Enabled"), help = raw("Display the event").some, half = true),
form3.group(
form("homepageHours"),
2020-10-19 02:56:40 -06:00
raw("Hours on homepage before the start (0 to 24)"),
2020-05-05 22:11:15 -06:00
half = true,
2020-10-19 02:56:40 -06:00
help = raw("Go easy on this. The event will also remain on homepage while ongoing.").some
)(form3.input(_, typ = "number")(step := ".01"))
2020-05-05 22:11:15 -06:00
),
form3.action(form3.submit(trans.apply()))
)
2019-04-20 20:50:03 -06:00
private def layout(title: String, css: String = "mod.misc")(body: Frag)(implicit ctx: Context) =
views.html.base.layout(
title = title,
2019-04-21 08:33:50 -06:00
moreCss = cssTag(css),
2020-09-12 04:30:40 -06:00
moreJs = jsModule("flatpickr")
2019-04-20 20:50:03 -06:00
) {
2019-12-13 07:30:20 -07:00
main(cls := "page-menu")(
mod.menu("event"),
body
)
}
2019-04-20 20:50:03 -06:00
}