/
easyadmin-extension.js
86 lines (74 loc) · 2.66 KB
/
easyadmin-extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function reloadEmbeddedList(identifier, toggleBaseUrl) {
var containerPrefix = '.embedded-list[for="'+identifier+'"]';
// Reload sorted/paginated list in the embedded-list container
$(containerPrefix)
.on('click', 'th a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
.on('click', '.list-pagination a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
;
const toggles = $(containerPrefix).find('table .checkbox-switch input[type="checkbox"]');
for (i = 0; i < toggles.length; i++) {
toggles[i].addEventListener('change', function () {
const toggle = this;
const newValue = this.checked;
const oldValue = !newValue;
const propertyName = this.closest('.checkbox-switch').dataset.propertyname;
const toggleUrl = toggleBaseUrl
+ "&id=" + this.closest('tr').dataset.id
+ "&property=" + propertyName
+ "&newValue=" + newValue.toString();
let toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
toggleRequest.done(function(result) {});
toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.checked = oldValue;
toggle.disabled = true;
toggle.closest('.checkbox-switch').classList.add('disabled');
});
});
}
}
window.addEventListener('load', function() {
$(function() {
$('[data-confirm]').on('click', function(e) {
e.preventDefault();
var message = $(this).data('confirm');
var content = $('#modal-confirm p.modal-body-content');
content.html(message);
var confirmButton = $('#modal-confirm #modal-confirm-button');
if (!confirmButton.find('i').length) { confirmButton.prepend('<i></i>'); }
confirmButton.find('i')
.removeClass()
.addClass($(this).find('i').attr('class'))
;
var href = $(this).data('href');
$('#modal-confirm #confirm-form').attr('action', href);
$('#modal-confirm').modal({ backdrop: true, keyboard: true });
});
});
});
function serializeForm(form) {
var formData = new FormData();
var params = form.serializeArray();
$.each(params, function (i, val) {
formData.append(val.name, val.value);
});
return formData;
};