diff --git a/src/main/scala/app/ControllerBase.scala b/src/main/scala/app/ControllerBase.scala index 7c6a754..74dbfe1 100644 --- a/src/main/scala/app/ControllerBase.scala +++ b/src/main/scala/app/ControllerBase.scala @@ -27,8 +27,50 @@ } } - protected def NotFound() = html.error("Not Found") - protected def Unauthorized() = redirect("/") + def ajaxGet(path : String)(action : => Any) : Route = { + super.get(path){ + request.setAttribute("AJAX", "true") + action + } + } + + override def ajaxGet[T](path : String, form : MappingValueType[T])(action : T => Any) : Route = { + super.ajaxGet(path, form){ form => + request.setAttribute("AJAX", "true") + action(form) + } + } + + def ajaxPost(path : String)(action : => Any) : Route = { + super.post(path){ + request.setAttribute("AJAX", "true") + action + } + } + + override def ajaxPost[T](path : String, form : MappingValueType[T])(action : T => Any) : Route = { + super.ajaxPost(path, form){ form => + request.setAttribute("AJAX", "true") + action(form) + } + } + + protected def NotFound() = { + if(request.getAttribute("AJAX") == null){ + org.scalatra.NotFound(html.error("Not Found")) + } else { + org.scalatra.NotFound() + } + } + + // TODO redirect to the sign-in page if not logged in? + protected def Unauthorized() = { + if(request.getAttribute("AJAX") == null){ + org.scalatra.Unauthorized(redirect("/")) + } else { + org.scalatra.Unauthorized() + } + } protected def baseUrl = { val url = request.getRequestURL.toString diff --git a/src/main/scala/app/LabelsController.scala b/src/main/scala/app/LabelsController.scala index 2767af9..daca78a 100644 --- a/src/main/scala/app/LabelsController.scala +++ b/src/main/scala/app/LabelsController.scala @@ -32,7 +32,7 @@ redirect("/%s/%s/issues".format(owner, repository)) }) - get("/:owner/:repository/issues/label/edit")(writableRepository { + ajaxGet("/:owner/:repository/issues/label/edit")(writableRepository { val owner = params("owner") val repository = params("repository") @@ -40,7 +40,7 @@ .map(issues.html.labeleditlist(getLabels(owner, repository), _)) getOrElse NotFound() }) - get("/:owner/:repository/issues/label/:labelId/edit")(writableRepository { + ajaxGet("/:owner/:repository/issues/label/:labelId/edit")(writableRepository { val owner = params("owner") val repository = params("repository") val labelId = params("labelId").toInt @@ -50,7 +50,7 @@ } getOrElse NotFound() }) - post("/:owner/:repository/issues/label/:labelId/edit", editForm)(writableRepository { form => + ajaxPost("/:owner/:repository/issues/label/:labelId/edit", editForm)(writableRepository { form => val owner = params("owner") val repository = params("repository") val labelId = params("labelId").toInt @@ -61,7 +61,7 @@ } getOrElse NotFound() }) - get("/:owner/:repository/issues/label/:labelId/delete")(writableRepository { + ajaxGet("/:owner/:repository/issues/label/:labelId/delete")(writableRepository { val owner = params("owner") val repository = params("repository") val labelId = params("labelId").toInt diff --git a/src/main/twirl/issues/labeledit.scala.html b/src/main/twirl/issues/labeledit.scala.html index 6fb61c5..649c093 100644 --- a/src/main/twirl/issues/labeledit.scala.html +++ b/src/main/twirl/issues/labeledit.scala.html @@ -3,9 +3,8 @@ @import view.helpers._ @defining((if(label.isEmpty) ("new", 190, 4) else ("edit", 180, 8))){ case (mode, width, margin) =>