-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbootstrap-confirm-delete.js
More file actions
111 lines (96 loc) · 4.8 KB
/
bootstrap-confirm-delete.js
File metadata and controls
111 lines (96 loc) · 4.8 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
/**
* Bootstrap Confirm Delete
* Author: Tom Kaczocha <tom@rawphp.org>
* Licensed under the MIT license
*/
;
( function ( $, window, document, undefined )
{
var bootstrap_confirm_delete = function ( element, options )
{
this.element = $( element );
this.settings = $.extend(
{
debug: false,
heading: 'Delete',
message: 'Are you sure you want to delete this item?',
btn_ok_label: 'Yes',
btn_cancel_label: 'Cancel',
data_type: null,
callback: null,
delete_callback: null,
cancel_callback: null
}, options || {}
);
this.onDelete = function ( event )
{
event.preventDefault();
var plugin = $( this ).data( 'bootstrap_confirm_delete' );
if ( undefined !== $( this ).attr( 'data-type' ) )
{
var name = $( this ).attr( 'data-type' );
plugin.settings.heading = 'Delete ' + name[ 0 ].toUpperCase() + name.substr( 1 );
plugin.settings.message = 'Are you sure you want to delete this ' + name + '?';
}
if ( null === document.getElementById( 'bootstrap-confirm-delete-container' ) )
{
$( '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"><a id="bootstrap-confirm-dialog-cancel-delete-btn" type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</a><a id="bootstrap-confirm-dialog-delete-btn" href="#" class="btn btn-danger pull-right">Delete</a></div></div></div></div></div>' );
}
$( '#bootstrap-confirm-dialog-heading' ).html( plugin.settings.heading );
$( '#bootstrap-confirm-dialog-text' ).html( plugin.settings.message );
$( '#bootstrap-confirm-dialog-delete-btn' ).html( plugin.settings.btn_ok_label );
$( '#bootstrap-confirm-dialog-cancel-delete-btn' ).html( plugin.settings.btn_cancel_label );
$( '#bootstrap-confirm-dialog' ).modal( 'toggle' );
var deleteBtn = $( 'a#bootstrap-confirm-dialog-delete-btn' );
var cancelBtn = $( 'a#bootstrap-confirm-dialog-cancel-delete-btn' );
var hasCallback = false;
if ( null !== plugin.settings.callback )
{
if ( $.isFunction( plugin.settings.callback ) )
{
deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.callback );
hasCallback = true;
}
else
{
console.log( plugin.settings.callback + ' is not a valid callback' );
}
}
if ( null !== plugin.settings.delete_callback )
{
if ( $.isFunction( plugin.settings.delete_callback ) )
{
deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.delete_callback );
hasCallback = true;
}
else
{
console.log( plugin.settings.delete_callback + ' is not a valid callback' );
}
}
if ( !hasCallback && '' !== event.currentTarget.href )
{
deleteBtn.attr( 'href', event.currentTarget.href );
}
if ( null !== plugin.settings.cancel_callback )
{
cancelBtn.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.cancel_callback );
}
};
};
$.fn.bootstrap_confirm_delete = function ( options )
{
return this.each( function ()
{
var element = $( this );
if ( element.data( 'bootstrap_confirm_delete' ) )
{
return element.data( 'bootstrap_confirm_delete' );
}
var plugin = new bootstrap_confirm_delete( this, options );
element.data( 'bootstrap_confirm_delete', plugin );
element.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', plugin.onDelete );
return plugin;
} );
};
}( jQuery, window, document, undefined ));