@doyosi/laraisy 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/package.json +1 -1
- package/src/CodeInput.js +48 -48
- package/src/DSAlert.js +352 -352
- package/src/DSAvatar.js +207 -207
- package/src/DSDelete.js +274 -274
- package/src/DSForm.js +568 -568
- package/src/DSGridOrTable.js +453 -453
- package/src/DSLocaleSwitcher.js +239 -239
- package/src/DSLogout.js +293 -293
- package/src/DSNotifications.js +365 -365
- package/src/DSRestore.js +181 -181
- package/src/DSSelect.js +1071 -1071
- package/src/DSSelectBox.js +563 -563
- package/src/DSSimpleSlider.js +517 -517
- package/src/DSSvgFetch.js +69 -69
- package/src/DSTable/DSTableExport.js +68 -68
- package/src/DSTable/DSTableFilter.js +224 -224
- package/src/DSTable/DSTablePagination.js +136 -136
- package/src/DSTable/DSTableSearch.js +40 -40
- package/src/DSTable/DSTableSelection.js +192 -192
- package/src/DSTable/DSTableSort.js +58 -58
- package/src/DSTable.js +353 -353
- package/src/DSTabs.js +488 -488
- package/src/DSUpload.js +887 -887
- package/dist/CodeInput.d.ts +0 -10
- package/dist/DSAlert.d.ts +0 -112
- package/dist/DSAvatar.d.ts +0 -45
- package/dist/DSDelete.d.ts +0 -61
- package/dist/DSForm.d.ts +0 -151
- package/dist/DSGridOrTable/DSGOTRenderer.d.ts +0 -60
- package/dist/DSGridOrTable/DSGOTViewToggle.d.ts +0 -26
- package/dist/DSGridOrTable.d.ts +0 -296
- package/dist/DSLocaleSwitcher.d.ts +0 -71
- package/dist/DSLogout.d.ts +0 -76
- package/dist/DSNotifications.d.ts +0 -54
- package/dist/DSRestore.d.ts +0 -56
- package/dist/DSSelect.d.ts +0 -221
- package/dist/DSSelectBox.d.ts +0 -123
- package/dist/DSSimpleSlider.d.ts +0 -136
- package/dist/DSSvgFetch.d.ts +0 -17
- package/dist/DSTable/DSTableExport.d.ts +0 -11
- package/dist/DSTable/DSTableFilter.d.ts +0 -40
- package/dist/DSTable/DSTablePagination.d.ts +0 -12
- package/dist/DSTable/DSTableSearch.d.ts +0 -8
- package/dist/DSTable/DSTableSelection.d.ts +0 -46
- package/dist/DSTable/DSTableSort.d.ts +0 -8
- package/dist/DSTable.d.ts +0 -116
- package/dist/DSTabs.d.ts +0 -156
- package/dist/DSUpload.d.ts +0 -220
- package/dist/index.d.ts +0 -17
package/src/DSDelete.js
CHANGED
|
@@ -1,274 +1,274 @@
|
|
|
1
|
-
import { DSAlert } from './DSAlert.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* DSDelete
|
|
5
|
-
*
|
|
6
|
-
* A plugin to handle delete actions with confirmation dialogs and AJAX requests.
|
|
7
|
-
* Integrates with DSAlert for UI.
|
|
8
|
-
*/
|
|
9
|
-
export class DSDelete {
|
|
10
|
-
static defaults = {
|
|
11
|
-
selector: '[data-delete]',
|
|
12
|
-
method: 'DELETE',
|
|
13
|
-
ajaxFunction: 'axios', // axios | fetch
|
|
14
|
-
|
|
15
|
-
// Confirmation Dialog
|
|
16
|
-
title: 'Are you sure?',
|
|
17
|
-
text: "You won't be able to revert this!",
|
|
18
|
-
icon: 'warning',
|
|
19
|
-
confirmButtonText: 'Yes, delete it!',
|
|
20
|
-
cancelButtonText: 'Cancel',
|
|
21
|
-
confirmButtonColor: 'btn btn-sm btn-error', // Destructive action
|
|
22
|
-
|
|
23
|
-
// Success Dialog
|
|
24
|
-
successTitle: 'Deleted!',
|
|
25
|
-
successText: 'Your file has been deleted.',
|
|
26
|
-
successIcon: 'success',
|
|
27
|
-
|
|
28
|
-
// Error Dialog
|
|
29
|
-
errorTitle: 'Error!',
|
|
30
|
-
errorText: 'Something went wrong.',
|
|
31
|
-
errorIcon: 'error',
|
|
32
|
-
|
|
33
|
-
// Callbacks
|
|
34
|
-
onSuccess: null, // function(response, element)
|
|
35
|
-
onError: null, // function(error, element)
|
|
36
|
-
onDelete: null, // function(element) - before delete, return false to cancel
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Static method for programmatic delete confirmation
|
|
41
|
-
* Usage: DSDelete.confirm({ url, title, text, successMessage, onSuccess })
|
|
42
|
-
*/
|
|
43
|
-
static async confirm(options = {}) {
|
|
44
|
-
const config = { ...DSDelete.defaults, ...options };
|
|
45
|
-
|
|
46
|
-
const confirmed = await DSAlert.fire({
|
|
47
|
-
title: config.title || DSDelete.defaults.title,
|
|
48
|
-
text: config.text || DSDelete.defaults.text,
|
|
49
|
-
icon: config.icon || DSDelete.defaults.icon,
|
|
50
|
-
showCancelButton: true,
|
|
51
|
-
confirmButtonText: config.confirmButtonText || DSDelete.defaults.confirmButtonText,
|
|
52
|
-
cancelButtonText: config.cancelButtonText || DSDelete.defaults.cancelButtonText,
|
|
53
|
-
confirmButtonColor: config.confirmButtonColor || DSDelete.defaults.confirmButtonColor
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
if (confirmed.isConfirmed) {
|
|
57
|
-
try {
|
|
58
|
-
const method = config.method || 'DELETE';
|
|
59
|
-
let response;
|
|
60
|
-
|
|
61
|
-
if (window.axios) {
|
|
62
|
-
response = await window.axios({
|
|
63
|
-
method: method,
|
|
64
|
-
url: config.url,
|
|
65
|
-
data: config.data || {}
|
|
66
|
-
});
|
|
67
|
-
response = response.data;
|
|
68
|
-
} else {
|
|
69
|
-
const res = await fetch(config.url, {
|
|
70
|
-
method: method,
|
|
71
|
-
headers: {
|
|
72
|
-
'Content-Type': 'application/json',
|
|
73
|
-
'Accept': 'application/json',
|
|
74
|
-
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content
|
|
75
|
-
},
|
|
76
|
-
body: JSON.stringify(config.data || {})
|
|
77
|
-
});
|
|
78
|
-
response = await res.json();
|
|
79
|
-
if (!res.ok) throw { response: res, data: response };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Show success message
|
|
83
|
-
const successMessage = config.successMessage || response.message || DSDelete.defaults.successText;
|
|
84
|
-
await DSAlert.fire({
|
|
85
|
-
title: DSDelete.defaults.successTitle,
|
|
86
|
-
text: successMessage,
|
|
87
|
-
icon: DSDelete.defaults.successIcon,
|
|
88
|
-
timer: 2000,
|
|
89
|
-
timerProgressBar: true
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
if (config.onSuccess) {
|
|
93
|
-
config.onSuccess(response);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return response;
|
|
97
|
-
|
|
98
|
-
} catch (error) {
|
|
99
|
-
console.error('DSDelete Error:', error);
|
|
100
|
-
|
|
101
|
-
let errorMessage = DSDelete.defaults.errorText;
|
|
102
|
-
if (error.response?.data?.message) {
|
|
103
|
-
errorMessage = error.response.data.message;
|
|
104
|
-
} else if (error.data?.message) {
|
|
105
|
-
errorMessage = error.data.message;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
DSAlert.fire({
|
|
109
|
-
title: DSDelete.defaults.errorTitle,
|
|
110
|
-
text: errorMessage,
|
|
111
|
-
icon: DSDelete.defaults.errorIcon
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
if (config.onError) {
|
|
115
|
-
config.onError(error);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
throw error;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
constructor(options = {}) {
|
|
126
|
-
this.config = { ...DSDelete.defaults, ...options };
|
|
127
|
-
this._init();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
_init() {
|
|
131
|
-
// Event Delegation
|
|
132
|
-
document.addEventListener('click', (e) => {
|
|
133
|
-
const trigger = e.target.closest(this.config.selector);
|
|
134
|
-
if (trigger) {
|
|
135
|
-
e.preventDefault();
|
|
136
|
-
this._handleDelete(trigger);
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async _handleDelete(element) {
|
|
142
|
-
// Allow cancellation via hook
|
|
143
|
-
if (this.config.onDelete && this.config.onDelete(element) === false) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const url = element.dataset.delete || element.getAttribute('href');
|
|
148
|
-
const id = element.dataset.deleteId;
|
|
149
|
-
|
|
150
|
-
if (!url) {
|
|
151
|
-
console.error('DSDelete: No delete URL found on element', element);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Construct final URL if ID is provided and URL doesn't look complete (optional logic, kept simple for now)
|
|
156
|
-
// User instructions said: "custom url with data-delete-id custom url + data - id send by request"
|
|
157
|
-
// Interpretation: If data-delete-id exists, maybe append it?
|
|
158
|
-
// Usually Laravel routes are full URLs. Let's assume URL is full unless we want to support building it.
|
|
159
|
-
// If data-delete-id is preset, maybe we send it as data body?
|
|
160
|
-
// Default standard: DELETE request to URL.
|
|
161
|
-
|
|
162
|
-
const contentTitle = element.dataset.deleteTitle ? `"${element.dataset.deleteTitle}"` : '';
|
|
163
|
-
const confirmText = contentTitle ? `${this.config.text} ${contentTitle}` : this.config.text;
|
|
164
|
-
|
|
165
|
-
const confirmed = await DSAlert.fire({
|
|
166
|
-
title: this.config.title,
|
|
167
|
-
text: confirmText,
|
|
168
|
-
icon: this.config.icon,
|
|
169
|
-
showCancelButton: true,
|
|
170
|
-
confirmButtonText: this.config.confirmButtonText,
|
|
171
|
-
cancelButtonText: this.config.cancelButtonText,
|
|
172
|
-
confirmButtonColor: this.config.confirmButtonColor
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
if (confirmed.isConfirmed) {
|
|
176
|
-
this._performDelete(url, id, element);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async _performDelete(url, id, element) {
|
|
181
|
-
try {
|
|
182
|
-
this._emit('delete:start', { element, url, id });
|
|
183
|
-
|
|
184
|
-
let response;
|
|
185
|
-
const data = id ? { id } : {};
|
|
186
|
-
|
|
187
|
-
if (this.config.ajaxFunction === 'axios' && window.axios) {
|
|
188
|
-
response = await window.axios({
|
|
189
|
-
method: this.config.method,
|
|
190
|
-
url: url,
|
|
191
|
-
data: data
|
|
192
|
-
});
|
|
193
|
-
// Axios returns data in response.data
|
|
194
|
-
await this._handleSuccess(response.data, element);
|
|
195
|
-
} else if (this.config.ajaxFunction === 'fetch' || window.fetch) {
|
|
196
|
-
const options = {
|
|
197
|
-
method: this.config.method,
|
|
198
|
-
headers: {
|
|
199
|
-
'Content-Type': 'application/json',
|
|
200
|
-
'Accept': 'application/json',
|
|
201
|
-
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content
|
|
202
|
-
},
|
|
203
|
-
body: JSON.stringify(data)
|
|
204
|
-
};
|
|
205
|
-
const res = await fetch(url, options);
|
|
206
|
-
const json = await res.json();
|
|
207
|
-
|
|
208
|
-
if (!res.ok) throw { response: res, data: json };
|
|
209
|
-
|
|
210
|
-
await this._handleSuccess(json, element);
|
|
211
|
-
} else {
|
|
212
|
-
throw new Error('DSDelete: No valid ajax function found');
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
} catch (error) {
|
|
216
|
-
this._handleError(error, element);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async _handleSuccess(response, element) {
|
|
221
|
-
// Standard Laravel response: { success: true, message: '...' }
|
|
222
|
-
const message = response.message || this.config.successText;
|
|
223
|
-
|
|
224
|
-
// Fire Success Alert
|
|
225
|
-
await DSAlert.fire({
|
|
226
|
-
title: this.config.successTitle,
|
|
227
|
-
text: message,
|
|
228
|
-
icon: this.config.successIcon,
|
|
229
|
-
timer: 2000,
|
|
230
|
-
timerProgressBar: true
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
if (this.config.onSuccess) {
|
|
234
|
-
this.config.onSuccess(response, element);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
this._emit('delete:success', { response, element });
|
|
238
|
-
|
|
239
|
-
// Optional: Remove row from table if inside one
|
|
240
|
-
const row = element.closest('tr');
|
|
241
|
-
if (row) {
|
|
242
|
-
row.remove();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
_handleError(error, element) {
|
|
247
|
-
console.error('DSDelete Error:', error);
|
|
248
|
-
|
|
249
|
-
let message = this.config.errorText;
|
|
250
|
-
if (error.response && error.response.data && error.response.data.message) {
|
|
251
|
-
message = error.response.data.message;
|
|
252
|
-
} else if (error.data && error.data.message) {
|
|
253
|
-
message = error.data.message;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
DSAlert.fire({
|
|
257
|
-
title: this.config.errorTitle,
|
|
258
|
-
text: message,
|
|
259
|
-
icon: this.config.errorIcon
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
if (this.config.onError) {
|
|
263
|
-
this.config.onError(error, element);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
this._emit('delete:error', { error, element });
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
_emit(event, detail = {}) {
|
|
270
|
-
document.dispatchEvent(new CustomEvent(`ds:${event}`, { bubbles: true, detail }));
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export default DSDelete;
|
|
1
|
+
import { DSAlert } from './DSAlert.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* DSDelete
|
|
5
|
+
*
|
|
6
|
+
* A plugin to handle delete actions with confirmation dialogs and AJAX requests.
|
|
7
|
+
* Integrates with DSAlert for UI.
|
|
8
|
+
*/
|
|
9
|
+
export class DSDelete {
|
|
10
|
+
static defaults = {
|
|
11
|
+
selector: '[data-delete]',
|
|
12
|
+
method: 'DELETE',
|
|
13
|
+
ajaxFunction: 'axios', // axios | fetch
|
|
14
|
+
|
|
15
|
+
// Confirmation Dialog
|
|
16
|
+
title: 'Are you sure?',
|
|
17
|
+
text: "You won't be able to revert this!",
|
|
18
|
+
icon: 'warning',
|
|
19
|
+
confirmButtonText: 'Yes, delete it!',
|
|
20
|
+
cancelButtonText: 'Cancel',
|
|
21
|
+
confirmButtonColor: 'btn btn-sm btn-error', // Destructive action
|
|
22
|
+
|
|
23
|
+
// Success Dialog
|
|
24
|
+
successTitle: 'Deleted!',
|
|
25
|
+
successText: 'Your file has been deleted.',
|
|
26
|
+
successIcon: 'success',
|
|
27
|
+
|
|
28
|
+
// Error Dialog
|
|
29
|
+
errorTitle: 'Error!',
|
|
30
|
+
errorText: 'Something went wrong.',
|
|
31
|
+
errorIcon: 'error',
|
|
32
|
+
|
|
33
|
+
// Callbacks
|
|
34
|
+
onSuccess: null, // function(response, element)
|
|
35
|
+
onError: null, // function(error, element)
|
|
36
|
+
onDelete: null, // function(element) - before delete, return false to cancel
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Static method for programmatic delete confirmation
|
|
41
|
+
* Usage: DSDelete.confirm({ url, title, text, successMessage, onSuccess })
|
|
42
|
+
*/
|
|
43
|
+
static async confirm(options = {}) {
|
|
44
|
+
const config = { ...DSDelete.defaults, ...options };
|
|
45
|
+
|
|
46
|
+
const confirmed = await DSAlert.fire({
|
|
47
|
+
title: config.title || DSDelete.defaults.title,
|
|
48
|
+
text: config.text || DSDelete.defaults.text,
|
|
49
|
+
icon: config.icon || DSDelete.defaults.icon,
|
|
50
|
+
showCancelButton: true,
|
|
51
|
+
confirmButtonText: config.confirmButtonText || DSDelete.defaults.confirmButtonText,
|
|
52
|
+
cancelButtonText: config.cancelButtonText || DSDelete.defaults.cancelButtonText,
|
|
53
|
+
confirmButtonColor: config.confirmButtonColor || DSDelete.defaults.confirmButtonColor
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (confirmed.isConfirmed) {
|
|
57
|
+
try {
|
|
58
|
+
const method = config.method || 'DELETE';
|
|
59
|
+
let response;
|
|
60
|
+
|
|
61
|
+
if (window.axios) {
|
|
62
|
+
response = await window.axios({
|
|
63
|
+
method: method,
|
|
64
|
+
url: config.url,
|
|
65
|
+
data: config.data || {}
|
|
66
|
+
});
|
|
67
|
+
response = response.data;
|
|
68
|
+
} else {
|
|
69
|
+
const res = await fetch(config.url, {
|
|
70
|
+
method: method,
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'application/json',
|
|
73
|
+
'Accept': 'application/json',
|
|
74
|
+
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify(config.data || {})
|
|
77
|
+
});
|
|
78
|
+
response = await res.json();
|
|
79
|
+
if (!res.ok) throw { response: res, data: response };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Show success message
|
|
83
|
+
const successMessage = config.successMessage || response.message || DSDelete.defaults.successText;
|
|
84
|
+
await DSAlert.fire({
|
|
85
|
+
title: DSDelete.defaults.successTitle,
|
|
86
|
+
text: successMessage,
|
|
87
|
+
icon: DSDelete.defaults.successIcon,
|
|
88
|
+
timer: 2000,
|
|
89
|
+
timerProgressBar: true
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (config.onSuccess) {
|
|
93
|
+
config.onSuccess(response);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return response;
|
|
97
|
+
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error('DSDelete Error:', error);
|
|
100
|
+
|
|
101
|
+
let errorMessage = DSDelete.defaults.errorText;
|
|
102
|
+
if (error.response?.data?.message) {
|
|
103
|
+
errorMessage = error.response.data.message;
|
|
104
|
+
} else if (error.data?.message) {
|
|
105
|
+
errorMessage = error.data.message;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
DSAlert.fire({
|
|
109
|
+
title: DSDelete.defaults.errorTitle,
|
|
110
|
+
text: errorMessage,
|
|
111
|
+
icon: DSDelete.defaults.errorIcon
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
if (config.onError) {
|
|
115
|
+
config.onError(error);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
constructor(options = {}) {
|
|
126
|
+
this.config = { ...DSDelete.defaults, ...options };
|
|
127
|
+
this._init();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_init() {
|
|
131
|
+
// Event Delegation
|
|
132
|
+
document.addEventListener('click', (e) => {
|
|
133
|
+
const trigger = e.target.closest(this.config.selector);
|
|
134
|
+
if (trigger) {
|
|
135
|
+
e.preventDefault();
|
|
136
|
+
this._handleDelete(trigger);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async _handleDelete(element) {
|
|
142
|
+
// Allow cancellation via hook
|
|
143
|
+
if (this.config.onDelete && this.config.onDelete(element) === false) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const url = element.dataset.delete || element.getAttribute('href');
|
|
148
|
+
const id = element.dataset.deleteId;
|
|
149
|
+
|
|
150
|
+
if (!url) {
|
|
151
|
+
console.error('DSDelete: No delete URL found on element', element);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Construct final URL if ID is provided and URL doesn't look complete (optional logic, kept simple for now)
|
|
156
|
+
// User instructions said: "custom url with data-delete-id custom url + data - id send by request"
|
|
157
|
+
// Interpretation: If data-delete-id exists, maybe append it?
|
|
158
|
+
// Usually Laravel routes are full URLs. Let's assume URL is full unless we want to support building it.
|
|
159
|
+
// If data-delete-id is preset, maybe we send it as data body?
|
|
160
|
+
// Default standard: DELETE request to URL.
|
|
161
|
+
|
|
162
|
+
const contentTitle = element.dataset.deleteTitle ? `"${element.dataset.deleteTitle}"` : '';
|
|
163
|
+
const confirmText = contentTitle ? `${this.config.text} ${contentTitle}` : this.config.text;
|
|
164
|
+
|
|
165
|
+
const confirmed = await DSAlert.fire({
|
|
166
|
+
title: this.config.title,
|
|
167
|
+
text: confirmText,
|
|
168
|
+
icon: this.config.icon,
|
|
169
|
+
showCancelButton: true,
|
|
170
|
+
confirmButtonText: this.config.confirmButtonText,
|
|
171
|
+
cancelButtonText: this.config.cancelButtonText,
|
|
172
|
+
confirmButtonColor: this.config.confirmButtonColor
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
if (confirmed.isConfirmed) {
|
|
176
|
+
this._performDelete(url, id, element);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async _performDelete(url, id, element) {
|
|
181
|
+
try {
|
|
182
|
+
this._emit('delete:start', { element, url, id });
|
|
183
|
+
|
|
184
|
+
let response;
|
|
185
|
+
const data = id ? { id } : {};
|
|
186
|
+
|
|
187
|
+
if (this.config.ajaxFunction === 'axios' && window.axios) {
|
|
188
|
+
response = await window.axios({
|
|
189
|
+
method: this.config.method,
|
|
190
|
+
url: url,
|
|
191
|
+
data: data
|
|
192
|
+
});
|
|
193
|
+
// Axios returns data in response.data
|
|
194
|
+
await this._handleSuccess(response.data, element);
|
|
195
|
+
} else if (this.config.ajaxFunction === 'fetch' || window.fetch) {
|
|
196
|
+
const options = {
|
|
197
|
+
method: this.config.method,
|
|
198
|
+
headers: {
|
|
199
|
+
'Content-Type': 'application/json',
|
|
200
|
+
'Accept': 'application/json',
|
|
201
|
+
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content
|
|
202
|
+
},
|
|
203
|
+
body: JSON.stringify(data)
|
|
204
|
+
};
|
|
205
|
+
const res = await fetch(url, options);
|
|
206
|
+
const json = await res.json();
|
|
207
|
+
|
|
208
|
+
if (!res.ok) throw { response: res, data: json };
|
|
209
|
+
|
|
210
|
+
await this._handleSuccess(json, element);
|
|
211
|
+
} else {
|
|
212
|
+
throw new Error('DSDelete: No valid ajax function found');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
} catch (error) {
|
|
216
|
+
this._handleError(error, element);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async _handleSuccess(response, element) {
|
|
221
|
+
// Standard Laravel response: { success: true, message: '...' }
|
|
222
|
+
const message = response.message || this.config.successText;
|
|
223
|
+
|
|
224
|
+
// Fire Success Alert
|
|
225
|
+
await DSAlert.fire({
|
|
226
|
+
title: this.config.successTitle,
|
|
227
|
+
text: message,
|
|
228
|
+
icon: this.config.successIcon,
|
|
229
|
+
timer: 2000,
|
|
230
|
+
timerProgressBar: true
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
if (this.config.onSuccess) {
|
|
234
|
+
this.config.onSuccess(response, element);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this._emit('delete:success', { response, element });
|
|
238
|
+
|
|
239
|
+
// Optional: Remove row from table if inside one
|
|
240
|
+
const row = element.closest('tr');
|
|
241
|
+
if (row) {
|
|
242
|
+
row.remove();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
_handleError(error, element) {
|
|
247
|
+
console.error('DSDelete Error:', error);
|
|
248
|
+
|
|
249
|
+
let message = this.config.errorText;
|
|
250
|
+
if (error.response && error.response.data && error.response.data.message) {
|
|
251
|
+
message = error.response.data.message;
|
|
252
|
+
} else if (error.data && error.data.message) {
|
|
253
|
+
message = error.data.message;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
DSAlert.fire({
|
|
257
|
+
title: this.config.errorTitle,
|
|
258
|
+
text: message,
|
|
259
|
+
icon: this.config.errorIcon
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
if (this.config.onError) {
|
|
263
|
+
this.config.onError(error, element);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
this._emit('delete:error', { error, element });
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
_emit(event, detail = {}) {
|
|
270
|
+
document.dispatchEvent(new CustomEvent(`ds:${event}`, { bubbles: true, detail }));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export default DSDelete;
|