-
Notifications
You must be signed in to change notification settings - Fork 2k
/
confirm-action.js
102 lines (94 loc) · 2.9 KB
/
confirm-action.js
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
this.ckan.module('confirm-action', function (jQuery, _) {
return {
/* An object of module options */
options: {
/* Locale options can be overidden with data-module-i18n attribute */
i18n: {
heading: _('Please Confirm Action'),
content: _('Are you sure you want to perform this action?'),
confirm: _('Confirm'),
cancel: _('Cancel')
},
template: [
'<div class="modal">',
'<div class="modal-header">',
'<button type="button" class="close" data-dismiss="modal">×</button>',
'<h3></h3>',
'</div>',
'<div class="modal-body"></div>',
'<div class="modal-footer">',
'<button class="btn btn-cancel"></button>',
'<button class="btn btn-primary"></button>',
'</div>',
'</div>'
].join('\n')
},
/* Sets up the event listeners for the object. Called internally by
* module.createInstance().
*
* Returns nothing.
*/
initialize: function () {
jQuery.proxyAll(this, /_on/);
this.el.on('click', this._onClick);
},
/* Presents the user with a confirm dialogue to ensure that they wish to
* continue with the current action.
*
* Examples
*
* jQuery('.delete').click(function () {
* module.confirm();
* });
*
* Returns nothing.
*/
confirm: function () {
this.sandbox.body.append(this.createModal());
this.modal.modal('show');
},
/* Performs the action for the current item.
*
* Returns nothing.
*/
performAction: function () {
// create a form and submit it to confirm the deletion
var form = jQuery('<form/>', {
action: this.el.attr('href'),
method: 'POST'
});
form.appendTo('body').submit();
},
/* Creates the modal dialog, attaches event listeners and localised
* strings.
*
* Returns the newly created element.
*/
createModal: function () {
if (!this.modal) {
var element = this.modal = jQuery(this.options.template);
element.on('click', '.btn-primary', this._onConfirmSuccess);
element.on('click', '.btn-cancel', this._onConfirmCancel);
element.modal({show: false});
element.find('h3').text(this.i18n('heading'));
element.find('.modal-body').text(this.i18n('content'));
element.find('.btn-primary').text(this.i18n('confirm'));
element.find('.btn-cancel').text(this.i18n('cancel'));
}
return this.modal;
},
/* Event handler that displays the confirm dialog */
_onClick: function (event) {
event.preventDefault();
this.confirm();
},
/* Event handler for the success event */
_onConfirmSuccess: function (event) {
this.performAction();
},
/* Event handler for the cancel event */
_onConfirmCancel: function (event) {
this.modal.modal('hide');
}
};
});