mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
@@ -0,0 +1,115 @@
|
||||
(function($) {
|
||||
var Confirm = function(element, options) {
|
||||
this.$element = $(element);
|
||||
this.options = $.extend({}, $.fn.confirm.defaults, options);
|
||||
this.$element.click($.proxy(this.show, this));
|
||||
};
|
||||
|
||||
Confirm.prototype = {
|
||||
constructor: Confirm,
|
||||
|
||||
show: function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
this.box = this.buildbox();
|
||||
this.box.modal();
|
||||
this.box.on('hidden', $.proxy(this.hidden_callback, this));
|
||||
},
|
||||
|
||||
buildcheckboxes: function() {
|
||||
var result = "";
|
||||
|
||||
if (!this.options.checkboxes) {
|
||||
return "";
|
||||
}
|
||||
result = '<form class="form-inline">';
|
||||
$.each(this.options.checkboxes, function(key, val) {
|
||||
result += "<label class='checkbox'><input type='checkbox' name='" + key + "' value='' />";
|
||||
result += val + "</label>";
|
||||
});
|
||||
result += '</form>';
|
||||
return result;
|
||||
},
|
||||
|
||||
buildbox: function() {
|
||||
var box = $('<div />', {
|
||||
id: "confirmbox",
|
||||
'class': "modal"
|
||||
});
|
||||
|
||||
box.append($('<div class="modal-body">'
|
||||
+ '<h4>' + this.options.question + '</h4>'
|
||||
+ '<p>' + this.buildcheckboxes() + '</p></div>'));
|
||||
|
||||
var footer = $('<div class="modal-footer" />');
|
||||
var cancel_btn = $('<a href="#" class="btn">' + gettext("Cancel") + "</a>");
|
||||
var ok_btn = $('<a href="#" class="btn btn-primary">Ok</a>');
|
||||
|
||||
ok_btn.click(function(evt) {
|
||||
box.data('result', true);
|
||||
box.modal('hide');
|
||||
});
|
||||
|
||||
cancel_btn.click(function(evt) {
|
||||
box.modal('hide');
|
||||
});
|
||||
|
||||
footer.append(ok_btn, cancel_btn);
|
||||
box.append(footer);
|
||||
return box;
|
||||
},
|
||||
|
||||
hidden_callback: function() {
|
||||
if (this.box.data('result') == undefined) {
|
||||
return;
|
||||
}
|
||||
var params = "";
|
||||
|
||||
$('input[type=checkbox]:checked').each(function() {
|
||||
if (params != "") {
|
||||
params += "&";
|
||||
}
|
||||
params += $(this).attr("name") + "=true";
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
cache: false,
|
||||
data: params,
|
||||
url: this.$element.attr('href'),
|
||||
complete: function(XMLHttpRequest, textStatus) {
|
||||
var data;
|
||||
try {
|
||||
data = $.parseJSON(XMLHttpRequest.responseText);
|
||||
} catch (e) {
|
||||
data = { status: 'ko', respmsg: gettext('Internal Error') };
|
||||
}
|
||||
if (data.status == "ok") {
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
});
|
||||
this.box.remove();
|
||||
}
|
||||
};
|
||||
|
||||
$.fn.confirm = function(method) {
|
||||
return this.each(function() {
|
||||
var $this = $(this),
|
||||
data = $this.data('confirm'),
|
||||
options = typeof method === "object" && method;
|
||||
|
||||
if (!data) {
|
||||
$this.data('confirm', new Confirm(this, options));
|
||||
}
|
||||
if (typeof method === "string") {
|
||||
data[method]();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$.fn.confirm.defaults = {
|
||||
question: "",
|
||||
checkboxes: null
|
||||
};
|
||||
|
||||
})(jQuery);
|
||||
Reference in New Issue
Block a user