1 | /** |
||
2 | * Allows you to add data-method="METHOD to links to automatically inject a form |
||
3 | * with the method on click |
||
4 | * |
||
5 | * Example: <a href="{{route('customers.destroy', $customer->id)}}" |
||
6 | * data-method="delete" name="delete_item">Delete</a> |
||
7 | * |
||
8 | * Injects a form with that's fired on click of the link with a DELETE request. |
||
9 | * Good because you don't have to dirty your HTML with delete forms everywhere. |
||
10 | */ |
||
11 | function addDeleteForms() { |
||
12 | $('[data-method]') |
||
13 | .append(function() { |
||
14 | if (!$(this).find('form').length > 0) { |
||
15 | return ( |
||
16 | "\n<form action='" + |
||
17 | $(this).attr('href') + |
||
18 | "' method='POST' name='delete_item' style='display:none'>\n" + |
||
19 | "<input type='hidden' name='_method' value='" + |
||
20 | $(this).attr('data-method') + |
||
21 | "'>\n" + |
||
22 | "<input type='hidden' name='_token' value='" + |
||
23 | $('meta[name="csrf-token"]').attr('content') + |
||
24 | "'>\n" + |
||
25 | '</form>\n' |
||
26 | ) |
||
27 | } else { |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
28 | return '' |
||
29 | } |
||
30 | }) |
||
31 | .attr('href', '#') |
||
32 | .attr('style', 'cursor:pointer;') |
||
33 | .attr('onclick', '$(this).find("form").submit();') |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Place any jQuery/helper plugins in here. |
||
38 | */ |
||
39 | $(function() { |
||
40 | /** |
||
41 | * Add the data-method="delete" forms to all delete links |
||
42 | */ |
||
43 | addDeleteForms() |
||
44 | |||
45 | /** |
||
46 | * Disable all submit buttons once clicked |
||
47 | */ |
||
48 | $('form').submit(function() { |
||
49 | $(this) |
||
50 | .find('input[type="submit"]') |
||
51 | .attr('disabled', true) |
||
52 | $(this) |
||
53 | .find('button[type="submit"]') |
||
54 | .attr('disabled', true) |
||
55 | return true |
||
56 | }) |
||
57 | |||
58 | /** |
||
59 | * Generic confirm form delete using Sweet Alert |
||
60 | */ |
||
61 | $('body') |
||
62 | .on('submit', 'form[name=delete_item]', function(e) { |
||
63 | e.preventDefault() |
||
64 | |||
65 | const form = this |
||
66 | const link = $('a[data-method="delete"]') |
||
67 | const cancel = link.attr('data-trans-button-cancel') |
||
68 | ? link.attr('data-trans-button-cancel') |
||
69 | : 'Cancel' |
||
70 | const confirm = link.attr('data-trans-button-confirm') |
||
71 | ? link.attr('data-trans-button-confirm') |
||
72 | : 'Yes, delete' |
||
73 | const title = link.attr('data-trans-title') |
||
74 | ? link.attr('data-trans-title') |
||
75 | : 'Are you sure you want to delete this item?' |
||
76 | |||
77 | swal({ |
||
78 | title: title, |
||
79 | showCancelButton: true, |
||
80 | confirmButtonText: confirm, |
||
81 | cancelButtonText: cancel, |
||
82 | type: 'warning' |
||
83 | }).then(result => { |
||
84 | result.value && form.submit() |
||
85 | }) |
||
86 | }) |
||
87 | .on('click', 'a[name=confirm_item]', function(e) { |
||
88 | /** |
||
89 | * Generic 'are you sure' confirm box |
||
90 | */ |
||
91 | e.preventDefault() |
||
92 | |||
93 | const link = $(this) |
||
94 | const title = link.attr('data-trans-title') |
||
95 | ? link.attr('data-trans-title') |
||
96 | : 'Are you sure you want to do this?' |
||
97 | const cancel = link.attr('data-trans-button-cancel') |
||
98 | ? link.attr('data-trans-button-cancel') |
||
99 | : 'Cancel' |
||
100 | const confirm = link.attr('data-trans-button-confirm') |
||
101 | ? link.attr('data-trans-button-confirm') |
||
102 | : 'Continue' |
||
103 | |||
104 | swal({ |
||
105 | title: title, |
||
106 | showCancelButton: true, |
||
107 | confirmButtonText: confirm, |
||
108 | cancelButtonText: cancel, |
||
109 | type: 'info' |
||
110 | }).then(result => { |
||
111 | result.value && window.location.assign(link.attr('href')) |
||
112 | }) |
||
113 | }) |
||
114 | }) |
||
115 |