|
1 | | -/** |
2 | | - * Bootstrap Confirm Delete |
3 | | - * Author: Tom Kaczocha <tom@rawphp.org> |
4 | | - * Licensed under the MIT license |
5 | | - */ |
6 | | - |
7 | | -; |
8 | | -( function ( $, window, document, undefined ) |
9 | | -{ |
10 | | - var bootstrap_confirm_delete = function ( element, options ) |
11 | | - { |
12 | | - this.element = $( element ); |
13 | | - this.settings = $.extend( |
14 | | - { |
15 | | - debug: false, |
16 | | - heading: 'Delete', |
17 | | - message: 'Are you sure you want to delete this item?', |
18 | | - data_type: null, |
19 | | - callback: null, |
20 | | - delete_callback: null, |
21 | | - cancel_callback: null |
22 | | - }, options || {} |
23 | | - ); |
24 | | - |
25 | | - this.onDelete = function ( event ) |
26 | | - { |
27 | | - event.preventDefault(); |
28 | | - |
29 | | - var plugin = $( this ).data( 'bootstrap_confirm_delete' ); |
30 | | - |
31 | | - if ( undefined !== $( this ).attr( 'data-type' ) ) |
32 | | - { |
33 | | - var name = $( this ).attr( 'data-type' ); |
34 | | - |
35 | | - plugin.settings.heading = 'Delete ' + name[ 0 ].toUpperCase() + name.substr( 1 ); |
36 | | - plugin.settings.message = 'Are you sure you want to delete this ' + name + '?'; |
37 | | - } |
38 | | - |
39 | | - if ( null === document.getElementById( 'bootstrap-confirm-delete-container' ) ) |
40 | | - { |
41 | | - $( 'body' ).append( '<div id="bootstrap-confirm-delete-container"><div id="bootstrap-confirm-dialog" class="modal fade"><div class="modal-dialog modal-sm"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h4 id="bootstrap-confirm-dialog-heading"></h4></div><div class="modal-body"><p id="bootstrap-confirm-dialog-text"></p></div><div class="modal-footer"><button id="bootstrap-confirm-dialog-cancel-delete-btn" type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button><a id="bootstrap-confirm-dialog-delete-btn" href="#" class="btn btn-danger pull-right">Delete</a></div></div></div></div></div>' ); |
42 | | - } |
43 | | - |
44 | | - $( '#bootstrap-confirm-dialog-heading' ).html( plugin.settings.heading ); |
45 | | - $( '#bootstrap-confirm-dialog-text' ).html( plugin.settings.message ); |
46 | | - $( '#bootstrap-confirm-dialog' ).modal( 'toggle' ); |
47 | | - |
48 | | - var deleteBtn = $( 'a#bootstrap-confirm-dialog-delete-btn' ); |
49 | | - var cancelBtn = $( 'a#bootstrap-confirm-dialog-cancel-delete-btn' ); |
50 | | - var hasCallback = false; |
51 | | - if ( null !== plugin.settings.callback ) |
52 | | - { |
53 | | - if ( $.isFunction( plugin.settings.callback ) ) |
54 | | - { |
55 | | - deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.callback ); |
56 | | - hasCallback = true; |
57 | | - } |
58 | | - else |
59 | | - { |
60 | | - console.log( plugin.settings.callback + ' is not a valid callback' ); |
61 | | - } |
62 | | - } |
63 | | - if ( null !== plugin.settings.delete_callback ) |
64 | | - { |
65 | | - if ( $.isFunction( plugin.settings.delete_callback ) ) |
66 | | - { |
67 | | - deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.delete_callback ); |
68 | | - hasCallback = true; |
69 | | - } |
70 | | - else |
71 | | - { |
72 | | - console.log( plugin.settings.delete_callback + ' is not a valid callback' ); |
73 | | - } |
74 | | - } |
75 | | - if ( !hasCallback && '' !== event.currentTarget.href ) |
76 | | - { |
77 | | - deleteBtn.attr( 'href', event.currentTarget.href ); |
78 | | - } |
79 | | - |
80 | | - if ( null !== plugin.settings.cancel_callback ) |
81 | | - { |
82 | | - cancelBtn.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.cancel_callback ); |
83 | | - } |
84 | | - }; |
85 | | - }; |
86 | | - |
87 | | - $.fn.bootstrap_confirm_delete = function ( options ) |
88 | | - { |
89 | | - return this.each( function () |
90 | | - { |
91 | | - var element = $( this ); |
92 | | - |
93 | | - if ( element.data( 'bootstrap_confirm_delete' ) ) |
94 | | - { |
95 | | - return element.data( 'bootstrap_confirm_delete' ); |
96 | | - } |
97 | | - |
98 | | - var plugin = new bootstrap_confirm_delete( this, options ); |
99 | | - |
100 | | - element.data( 'bootstrap_confirm_delete', plugin ); |
101 | | - element.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', plugin.onDelete ); |
102 | | - |
103 | | - return plugin; |
104 | | - } ); |
105 | | - }; |
106 | | -}( jQuery, window, document, undefined )); |
| 1 | +/** |
| 2 | + * Bootstrap Confirm Delete |
| 3 | + * Author: Tom Kaczocha <tom@rawphp.org> |
| 4 | + * Licensed under the MIT license |
| 5 | + */ |
| 6 | + |
| 7 | +; |
| 8 | +( function ( $, window, document, undefined ) |
| 9 | +{ |
| 10 | + var bootstrap_confirm_delete = function ( element, options ) |
| 11 | + { |
| 12 | + this.element = $( element ); |
| 13 | + this.settings = $.extend( |
| 14 | + { |
| 15 | + debug: false, |
| 16 | + heading: 'Delete', |
| 17 | + message: 'Are you sure you want to delete this item?', |
| 18 | + data_type: null, |
| 19 | + callback: null, |
| 20 | + delete_callback: null, |
| 21 | + cancel_callback: null |
| 22 | + }, options || {} |
| 23 | + ); |
| 24 | + |
| 25 | + this.onDelete = function ( event ) |
| 26 | + { |
| 27 | + event.preventDefault(); |
| 28 | + |
| 29 | + var plugin = $( this ).data( 'bootstrap_confirm_delete' ); |
| 30 | + |
| 31 | + if ( undefined !== $( this ).attr( 'data-type' ) ) |
| 32 | + { |
| 33 | + var name = $( this ).attr( 'data-type' ); |
| 34 | + |
| 35 | + plugin.settings.heading = 'Delete ' + name[ 0 ].toUpperCase() + name.substr( 1 ); |
| 36 | + plugin.settings.message = 'Are you sure you want to delete this ' + name + '?'; |
| 37 | + } |
| 38 | + |
| 39 | + if ( null === document.getElementById( 'bootstrap-confirm-delete-container' ) ) |
| 40 | + { |
| 41 | + $( 'body' ).append( '<div id="bootstrap-confirm-delete-container"><div id="bootstrap-confirm-dialog" class="modal fade"><div class="modal-dialog modal-sm"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h4 id="bootstrap-confirm-dialog-heading"></h4></div><div class="modal-body"><p id="bootstrap-confirm-dialog-text"></p></div><div class="modal-footer"><button id="bootstrap-confirm-dialog-cancel-delete-btn" type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button><a id="bootstrap-confirm-dialog-delete-btn" href="#" class="btn btn-danger pull-right">Delete</a></div></div></div></div></div>' ); |
| 42 | + } |
| 43 | + |
| 44 | + $( '#bootstrap-confirm-dialog-heading' ).html( plugin.settings.heading ); |
| 45 | + $( '#bootstrap-confirm-dialog-text' ).html( plugin.settings.message ); |
| 46 | + $( '#bootstrap-confirm-dialog' ).modal( 'toggle' ); |
| 47 | + |
| 48 | + var deleteBtn = $( 'a#bootstrap-confirm-dialog-delete-btn' ); |
| 49 | + var cancelBtn = $( 'a#bootstrap-confirm-dialog-cancel-delete-btn' ); |
| 50 | + var hasCallback = false; |
| 51 | + |
| 52 | + if ( null !== plugin.settings.callback ) |
| 53 | + { |
| 54 | + if ( $.isFunction( plugin.settings.callback ) ) |
| 55 | + { |
| 56 | + deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.callback ); |
| 57 | + hasCallback = true; |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + console.log( plugin.settings.callback + ' is not a valid callback' ); |
| 62 | + } |
| 63 | + } |
| 64 | + if ( null !== plugin.settings.delete_callback ) |
| 65 | + { |
| 66 | + if ( $.isFunction( plugin.settings.delete_callback ) ) |
| 67 | + { |
| 68 | + deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.delete_callback ); |
| 69 | + hasCallback = true; |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + console.log( plugin.settings.delete_callback + ' is not a valid callback' ); |
| 74 | + } |
| 75 | + } |
| 76 | + if ( !hasCallback && '' !== event.currentTarget.href ) |
| 77 | + { |
| 78 | + deleteBtn.attr( 'href', event.currentTarget.href ); |
| 79 | + } |
| 80 | + |
| 81 | + if ( null !== plugin.settings.cancel_callback ) |
| 82 | + { |
| 83 | + cancelBtn.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.cancel_callback ); |
| 84 | + } |
| 85 | + }; |
| 86 | + }; |
| 87 | + |
| 88 | + $.fn.bootstrap_confirm_delete = function ( options ) |
| 89 | + { |
| 90 | + return this.each( function () |
| 91 | + { |
| 92 | + var element = $( this ); |
| 93 | + |
| 94 | + if ( element.data( 'bootstrap_confirm_delete' ) ) |
| 95 | + { |
| 96 | + return element.data( 'bootstrap_confirm_delete' ); |
| 97 | + } |
| 98 | + |
| 99 | + var plugin = new bootstrap_confirm_delete( this, options ); |
| 100 | + |
| 101 | + element.data( 'bootstrap_confirm_delete', plugin ); |
| 102 | + element.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', plugin.onDelete ); |
| 103 | + |
| 104 | + return plugin; |
| 105 | + } ); |
| 106 | + }; |
| 107 | +}( jQuery, window, document, undefined )); |
0 commit comments