Commit 4f31c740 by Kálmán Viktor

rework confirmation js part

parent 1450d8ee
......@@ -49,7 +49,12 @@ $(function () {
var vm_pk = $(this).data('vm-pk');
text = "Are you sure you want to delete this VM?";
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;
});
});
......@@ -79,17 +84,17 @@ function refreshSliders() {
/* deletes the VM with the pk
* if dir is true, then redirect to the dashboard landing page
* else it adds a success message */
function deleteVm(pk, dir) {
function deleteVm(data) {
$.ajax({
type: 'POST',
data: {'redirect': dir},
url: '/dashboard/vm/delete/' + pk + '/',
data: {'redirect': data['redirect']},
url: data['url'],
headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(data, textStatus, xhr) {
if(!dir) {
success: function(re, textStatus, xhr) {
if(!data['redirect']) {
selected = [];
addMessage(data['message'], 'success');
$('a[data-vm-pk="' + pk + '"]').closest('tr').fadeOut(function() {
addMessage(re['message'], 'success');
$('a[data-vm-pk="' + data['vm_pk'] + '"]').closest('tr').fadeOut(function() {
$(this).remove();
});
} else {
......@@ -102,22 +107,22 @@ function deleteVm(pk, dir) {
});
}
function massDeleteVm() {
function massDeleteVm(data) {
$.ajax({
traditional: true,
url: '/dashboard/vm/mass-delete/',
url: data['url'],
headers: {"X-CSRFToken": getCookie('csrftoken')},
type: 'POST',
data: {'vms': collectIds(selected)},
success: function(data, textStatus, xhr) {
data: {'vms': data['data']['v']},
success: function(re, textStatus, xhr) {
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
selected = []
$('.vm-list-group-control a').attr('disabled', true);
$(this).remove();
});
addMessage(data['message'], 'success');
addMessage(re['message'], 'success');
},
error: function(xhr, textStatus, error) {
// TODO this
......@@ -134,11 +139,11 @@ function addMessage(text, type) {
}
function addModalConfirmation(text, data, func, dir) {
function addModalConfirmation(func, data) {
$.ajax({
type: 'GET',
url: '/dashboard/vm/delete/' + data + '/',
data: {'text': text},
url: data['url'],
data: jQuery.param(data['data']),
success: function(result) {
$('body').append(result);
$('#confirmation-modal').modal('show');
......@@ -146,7 +151,7 @@ function addModalConfirmation(text, data, func, dir) {
$('#confirmation-modal').remove();
});
$('#confirmation-modal-button').click(function() {
func(data, dir);
func(data);
$('#confirmation-modal').modal('hide');
});
}
......
......@@ -103,9 +103,15 @@ $(function() {
/* mass vm delete */
$('#vm-list-group-delete').click(function() {
text = "Are you sure you want to delete the selected VMs?";
random_vm_pk = $('.vm-delete').eq(0).data('vm-pk');
addModalConfirmation(text, random_vm_pk, massDeleteVm, false);
addModalConfirmation(massDeleteVm,
{
'url': '/dashboard/vm/mass-delete/',
'data': {
'selected': selected,
'v': collectIds(selected)
}
}
);
return false;
});
});
......
......@@ -2,7 +2,7 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
{{ request.GET.text }}
Are you sure you want to delete <strong>{{ object }}</strong>?
<br />
<div class="pull-right">
<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
from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy
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.generic.detail import SingleObjectMixin
from django.views.generic import TemplateView, DetailView, View, DeleteView
......@@ -332,8 +332,13 @@ class VmDelete(DeleteView):
return reverse_lazy('dashboard.index')
@require_POST
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')
names = []
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