forked from brexis/laravel-data-method
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-method.js
More file actions
192 lines (160 loc) · 5.78 KB
/
Copy pathdata-method.js
File metadata and controls
192 lines (160 loc) · 5.78 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* global $, Swal */
$(function() {
var modalTemplate = [
'<div class="modal fade laravel-data-method" tabindex="-1" role="dialog" aria-labelledby="dataMethodModalLabel">',
'<div class="modal-dialog modal-sm" role="document">',
'<div class="modal-content">',
'<div class="modal-header">',
'<h6 class="modal-title" id="dataMethodModalLabel"></h6>',
'<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>',
'</div>',
'<div class="modal-body"></div>',
'<div class="modal-footer">',
'<button type="button" class="btn btn-default btn-cancel" data-dismiss="modal">{{cancel}}</button>',
'<button type="button" class="btn btn-primary btn-confirm">{{ok}}</button>',
'</div>',
'</div>',
'</div>',
'</div>'
].join('');
function getCSRFToken(options) {
return options.token || $('meta[name=csrf-token]').attr('content');
}
function handleMethod(e) {
e.preventDefault();
var link = $(this);
dataMethod(link.attr('href'), link.data());
}
function dataMethod(url, options, cb) {
var method = options.method || 'POST';
method = method.toUpperCase();
cb = cb || function() {};
// If the data-method attribute is not PUT or DELETE,
// then we don't know what to do. Just ignore.
if (!['POST', 'PUT', 'DELETE'].includes(method)) {
return;
}
// Allow user to optionally provide data-confirm="Are you sure?"
if (options.confirm) {
confirm(options, function() {
cb(sendRequest(url, method, options));
});
} else {
cb(sendRequest(url, method, options));
}
}
function sendRequest(url, method, options) {
if (options.ajax) {
return sendAjax(url, method, options);
} else {
return createForm(url, method, options).submit();
}
}
function createForm(url, method, options) {
var form, params, fields, token;
form = $('<form>', {
'method': 'POST',
'action': url
});
params = options.params || {};
params._token = getCSRFToken(options);
params._method = method;
fields = createFields(params);
return form.append(fields)
.appendTo('body');
}
function sendAjax(url, method, options) {
var ajaxOptions = typeof options.ajax === 'object' ? options.ajax : {};
var params = options.params || {};
params._token = getCSRFToken(options);
ajaxOptions.url = url;
ajaxOptions.method = method;
ajaxOptions.data = params
return $.ajax(ajaxOptions);
}
function createFields(params) {
var fields = [];
for (var name in params) {
var input =
$('<input>', {
'name': name,
'type': 'hidden',
'value': params[name]
});
fields.push(input);
}
return fields;
}
function confirm(options, success, cancel) {
var title = options.title;
var message = options.confirm;
var theme = options.theme || 'default';
var okText = options.ok || 'Ok';
var cancelText = options.cancel || 'Cancel';
if (!cancel) {
cancel = function() {}
}
switch (theme) {
case 'default':
window.confirm(message) ? success() : cancel();
break;
case 'bootstrap':
var template = modalTemplate.replace('{{ok}}', okText)
.replace('{{cancel}}', cancelText)
var $modal = $(template);
$modal.find('.modal-title').html(title);
$modal.find('.modal-body').html(message);
$modal.appendTo('body');
$modal.modal('show');
$modal.find('.btn-confirm').on('click', function() {
$modal.modal('hide');
success();
});
$modal.find('[data-dismiss="modal"]').on('click', function() {
cancel();
});
$modal.on('hidden.bs.modal', function() {
$modal.remove();
});
break;
case 'sweetalert':
Swal.fire({
title: title,
text: message,
showCancelButton: true,
cancelButtonText: cancelText,
confirmButtonText: okText,
customClass: 'laravel-data-method'
}).then(function(result) {
if (result.value) {
success();
} else if (result.dismiss === Swal.DismissReason.cancel) {
cancel();
}
});
break;
}
}
// Disable button on submission form
function disabledButton($btn) {
$btn.prop('disabled', 'disabled');
}
$('form').submit(function(e) {
var form = this;
e.preventDefault();
var $submitbutton = $(this).find('[data-disabled-submit]');
var confirmMessage = $submitbutton.data('confirm');
if (confirmMessage) {
confirm($submitbutton.data(), function() {
disabledButton($submitbutton);
form.submit();
});
return false;
} else {
disabledButton($submitbutton);
}
form.submit();
});
$(document).on('click', 'a[data-method]', handleMethod);
window.dataMethod = dataMethod;
});