mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
Fix Ajax processing.
This commit is contained in:
@@ -27,8 +27,50 @@ abstract class ControllerBase extends ScalatraFilter
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -32,7 +32,7 @@ trait LabelsControllerBase extends ControllerBase {
|
||||
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 @@ trait LabelsControllerBase extends ControllerBase {
|
||||
.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 @@ trait LabelsControllerBase extends ControllerBase {
|
||||
} 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 @@ trait LabelsControllerBase extends ControllerBase {
|
||||
} 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
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
@import view.helpers._
|
||||
@defining((if(label.isEmpty) ("new", 190, 4) else ("edit", 180, 8))){ case (mode, width, margin) =>
|
||||
<div id="@(mode)LabelArea">
|
||||
<form method="POST" id="edit-label-form"
|
||||
action="@url(repository)/issues/label/@{if(mode == "new") "new" else label.get.labelId + "/edit"}"
|
||||
validate="true" @if(mode == "edit"){ style="margin-bottom: 8px;"}>
|
||||
<form method="POST" id="edit-label-form" validate="true" style="margin-bottom: 8px;"
|
||||
action="@url(repository)/issues/label/@{if(mode == "new") "new" else label.get.labelId + "/edit"}">
|
||||
<span id="error-@(mode)LabelName" class="error"></span>
|
||||
<input type="text" name="@(mode)LabelName" id="@(mode)LabelName" style="width: @(width)px; margin-left: @(margin)px; margin-bottom: 0px;" value="@label.map(_.labelName)"@if(mode == "new"){ placeholder="New label name"}/>
|
||||
<span id="error-@(mode)Color" class="error"></span>
|
||||
@@ -26,21 +25,17 @@
|
||||
$('#editColor').colorpicker();
|
||||
|
||||
$('#edit-label-form').submit(function(e){
|
||||
var form = $(e.target);
|
||||
$.post(form.attr('action') + '/validate', $(form).serialize(), function(data){
|
||||
// clear all error messages
|
||||
$('.error').text('');
|
||||
|
||||
if($.isEmptyObject(data)){
|
||||
$.post(form.attr('action'), $(form).serialize(), function(data){
|
||||
$.ajax($(this).attr('action'), {
|
||||
type: 'POST',
|
||||
data: $(this).serialize()
|
||||
})
|
||||
.done(function(data){
|
||||
$('#label-edit').parent().empty().html(data);
|
||||
})
|
||||
.fail(function(data, status){
|
||||
displayErrors($.parseJSON(data.responseText));
|
||||
});
|
||||
} else {
|
||||
$.each(data, function(key, value){
|
||||
$('#error-' + key).text(value);
|
||||
});
|
||||
}
|
||||
}, 'json');
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user