11/**
2- * Created by Tom on 9/07/2015.
2+ * Bootstrap Confirm Delete
3+ * Author: Tom Kaczocha <tom@rawphp.org>
4+ * Licensed under the MIT license
35 */
46
5- ( function ( $ )
7+ ;
8+ ( function ( $ , window , document , undefined )
69{
7- $ . fn . bootstrap_confirm_delete = function ( options )
10+ var bootstrap_confirm_delete = function ( element , options )
811 {
9- var settings = $ . extend (
12+ this . element = $ ( element ) ;
13+ this . settings = $ . extend (
1014 {
15+ debug : false ,
1116 heading : 'Delete' ,
1217 message : 'Are you sure you want to delete this item?' ,
1318 data_type : null ,
14- callback : null
15- } , options ) ;
19+ callback : null ,
20+ delete_callback : null ,
21+ cancel_callback : null
22+ } , options || { }
23+ ) ;
1624
17- $ ( this ) . on ( 'click' , function ( event )
25+ this . onDelete = function ( event )
1826 {
1927 event . preventDefault ( ) ;
2028
29+ var plugin = $ ( this ) . data ( 'bootstrap_confirm_delete' ) ;
30+
2131 if ( undefined !== $ ( this ) . attr ( 'data-type' ) )
2232 {
2333 var name = $ ( this ) . attr ( 'data-type' ) ;
2434
25- settings . heading = 'Delete ' + name [ 0 ] . toUpperCase ( ) + name . substr ( 1 ) ;
26- settings . message = 'Are you sure you want to delete this ' + name + '?' ;
35+ plugin . settings . heading = 'Delete ' + name [ 0 ] . toUpperCase ( ) + name . substr ( 1 ) ;
36+ plugin . settings . message = 'Are you sure you want to delete this ' + name + '?' ;
2737 }
2838
2939 if ( null === document . getElementById ( 'bootstrap-confirm-delete-container' ) )
3040 {
3141 $ ( '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>' ) ;
3242 }
3343
34- $ ( '#bootstrap-confirm-dialog-heading' ) . html ( settings . heading ) ;
35- $ ( '#bootstrap-confirm-dialog-text' ) . html ( settings . message ) ;
44+ $ ( '#bootstrap-confirm-dialog-heading' ) . html ( plugin . settings . heading ) ;
45+ $ ( '#bootstrap-confirm-dialog-text' ) . html ( plugin . settings . message ) ;
3646 $ ( '#bootstrap-confirm-dialog' ) . modal ( 'toggle' ) ;
3747
38- if ( null !== settings . callback )
48+ if ( null !== plugin . settings . callback )
3949 {
4050 $ ( '#bootstrap-confirm-dialog-delete-btn' ) . attr ( 'data-dismiss' , 'modal' ) ;
4151
42- if ( $ . isFunction ( settings . callback ) )
52+ if ( $ . isFunction ( plugin . settings . callback ) )
4353 {
44- $ ( 'a#bootstrap-confirm-dialog-delete-btn' ) . on ( 'click' , { originalObject : this } , settings . callback ) ;
54+ var deleteBtn = $ ( 'a#bootstrap-confirm-dialog-delete-btn' ) ;
55+
56+ deleteBtn . on ( 'click' , { originalObject : this } , plugin . settings . callback ) ;
57+
58+ if ( null !== plugin . settings . delete_callback )
59+ {
60+ deleteBtn . on ( 'click' , { originalObject : this } , plugin . settings . delete_callback ) ;
61+ }
4562 }
4663 else
4764 {
48- console . log ( settings . callback + ' is not a valid callback' ) ;
65+ console . log ( plugin . settings . callback + ' is not a valid callback' ) ;
4966 }
5067 }
5168 else if ( '' !== event . currentTarget . href )
5269 {
5370 $ ( 'a#bootstrap-confirm-dialog-delete-btn' ) . attr ( 'href' , event . currentTarget . href ) ;
5471 }
72+
73+ if ( null !== plugin . settings . cancel_callback )
74+ {
75+ $ ( '#bootstrap-confirm-dialog-cancel-delete-btn' ) . on ( 'click' , { originalObject : this } , plugin . settings . cancel_callback ) ;
76+ }
77+ } ;
78+ } ;
79+
80+ $ . fn . bootstrap_confirm_delete = function ( options )
81+ {
82+ return this . each ( function ( )
83+ {
84+ var element = $ ( this ) ;
85+
86+ if ( element . data ( 'bootstrap_confirm_delete' ) )
87+ {
88+ return element . data ( 'bootstrap_confirm_delete' ) ;
89+ }
90+
91+ var plugin = new bootstrap_confirm_delete ( this , options ) ;
92+
93+ element . data ( 'bootstrap_confirm_delete' , plugin ) ;
94+ element . on ( 'click' , plugin . onDelete ) ;
95+
96+ return plugin ;
5597 } ) ;
5698 } ;
57- } ( jQuery ) ) ;
99+ } ( jQuery , window , document , undefined ) ) ;
0 commit comments