Commit 4f31c740 by Kálmán Viktor

rework confirmation js part

parent 1450d8ee
...@@ -49,7 +49,12 @@ $(function () { ...@@ -49,7 +49,12 @@ $(function () {
var vm_pk = $(this).data('vm-pk'); var vm_pk = $(this).data('vm-pk');
text = "Are you sure you want to delete this VM?"; text = "Are you sure you want to delete this VM?";
var dir = window.location.pathname.indexOf('list') == -1; var dir = window.location.pathname.indexOf('list') == -1;
addModalConfirmation(text, vm_pk, deleteVm, dir); addModalConfirmation(deleteVm,
{ 'url': '/dashboard/vm/delete/' + vm_pk + '/',
'data': [],
'vm_pk': vm_pk,
'redirect': dir});
return false; return false;
}); });
}); });
...@@ -79,17 +84,17 @@ function refreshSliders() { ...@@ -79,17 +84,17 @@ function refreshSliders() {
/* deletes the VM with the pk /* deletes the VM with the pk
* if dir is true, then redirect to the dashboard landing page * if dir is true, then redirect to the dashboard landing page
* else it adds a success message */ * else it adds a success message */
function deleteVm(pk, dir) { function deleteVm(data) {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
data: {'redirect': dir}, data: {'redirect': data['redirect']},
url: '/dashboard/vm/delete/' + pk + '/', url: data['url'],
headers: {"X-CSRFToken": getCookie('csrftoken')}, headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(data, textStatus, xhr) { success: function(re, textStatus, xhr) {
if(!dir) { if(!data['redirect']) {
selected = []; selected = [];
addMessage(data['message'], 'success'); addMessage(re['message'], 'success');
$('a[data-vm-pk="' + pk + '"]').closest('tr').fadeOut(function() { $('a[data-vm-pk="' + data['vm_pk'] + '"]').closest('tr').fadeOut(function() {
$(this).remove(); $(this).remove();
}); });
} else { } else {
...@@ -102,22 +107,22 @@ function deleteVm(pk, dir) { ...@@ -102,22 +107,22 @@ function deleteVm(pk, dir) {
}); });
} }
function massDeleteVm() { function massDeleteVm(data) {
$.ajax({ $.ajax({
traditional: true, traditional: true,
url: '/dashboard/vm/mass-delete/', url: data['url'],
headers: {"X-CSRFToken": getCookie('csrftoken')}, headers: {"X-CSRFToken": getCookie('csrftoken')},
type: 'POST', type: 'POST',
data: {'vms': collectIds(selected)}, data: {'vms': data['data']['v']},
success: function(data, textStatus, xhr) { success: function(re, textStatus, xhr) {
for(var i=0; i< selected.length; i++) for(var i=0; i< selected.length; i++)
$('.vm-list-table tbody tr').eq(selected[i]).fadeOut(500, function() { $('.vm-list-table tbody tr').eq(data['data']['selected'][i]).fadeOut(500, function() {
// reset group buttons // reset group buttons
selected = [] selected = []
$('.vm-list-group-control a').attr('disabled', true); $('.vm-list-group-control a').attr('disabled', true);
$(this).remove(); $(this).remove();
}); });
addMessage(data['message'], 'success'); addMessage(re['message'], 'success');
}, },
error: function(xhr, textStatus, error) { error: function(xhr, textStatus, error) {
// TODO this // TODO this
...@@ -134,11 +139,11 @@ function addMessage(text, type) { ...@@ -134,11 +139,11 @@ function addMessage(text, type) {
} }
function addModalConfirmation(text, data, func, dir) { function addModalConfirmation(func, data) {
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: '/dashboard/vm/delete/' + data + '/', url: data['url'],
data: {'text': text}, data: jQuery.param(data['data']),
success: function(result) { success: function(result) {
$('body').append(result); $('body').append(result);
$('#confirmation-modal').modal('show'); $('#confirmation-modal').modal('show');
...@@ -146,7 +151,7 @@ function addModalConfirmation(text, data, func, dir) { ...@@ -146,7 +151,7 @@ function addModalConfirmation(text, data, func, dir) {
$('#confirmation-modal').remove(); $('#confirmation-modal').remove();
}); });
$('#confirmation-modal-button').click(function() { $('#confirmation-modal-button').click(function() {
func(data, dir); func(data);
$('#confirmation-modal').modal('hide'); $('#confirmation-modal').modal('hide');
}); });
} }
......
...@@ -103,9 +103,15 @@ $(function() { ...@@ -103,9 +103,15 @@ $(function() {
/* mass vm delete */ /* mass vm delete */
$('#vm-list-group-delete').click(function() { $('#vm-list-group-delete').click(function() {
text = "Are you sure you want to delete the selected VMs?"; addModalConfirmation(massDeleteVm,
random_vm_pk = $('.vm-delete').eq(0).data('vm-pk'); {
addModalConfirmation(text, random_vm_pk, massDeleteVm, false); 'url': '/dashboard/vm/mass-delete/',
'data': {
'selected': selected,
'v': collectIds(selected)
}
}
);
return false; return false;
}); });
}); });
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
<div class="modal-body"> <div class="modal-body">
{{ request.GET.text }} Are you sure you want to delete <strong>{{ object }}</strong>?
<br /> <br />
<div class="pull-right"> <div class="pull-right">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
......
<div class="modal fade" id="confirmation-modal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
Are you sure you want to delete these objects<br />
{% for o in objects %}
<strong>{{ o }}</strong>{% if not forloop.last %},{% endif %}
{% endfor %}
?
<div class="pull-right" style="margin-top: 40px;">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button id="confirmation-modal-button" type="button" class="btn btn-danger">Delete</button>
</div>
<div class="clearfix"></div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
...@@ -9,7 +9,7 @@ from django.core.exceptions import PermissionDenied ...@@ -9,7 +9,7 @@ from django.core.exceptions import PermissionDenied
from django.core import signing from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect from django.shortcuts import redirect, render
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
from django.views.generic import TemplateView, DetailView, View, DeleteView from django.views.generic import TemplateView, DetailView, View, DeleteView
...@@ -332,8 +332,13 @@ class VmDelete(DeleteView): ...@@ -332,8 +332,13 @@ class VmDelete(DeleteView):
return reverse_lazy('dashboard.index') return reverse_lazy('dashboard.index')
@require_POST
def mass_delete_vm(request, **kwargs): def mass_delete_vm(request, **kwargs):
if request.method == "GET":
vms = request.GET.getlist('v[]')
objects = Instance.objects.filter(pk__in=vms)
return render(request, "dashboard/confirm/mass-delete.html",
{'objects': objects})
vms = request.POST.getlist('vms') vms = request.POST.getlist('vms')
names = [] names = []
if vms is not None: if vms is not None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment