forked from d3ck-net/meteor-bootstrap-modal-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.js
187 lines (157 loc) · 5.12 KB
/
prompt.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
BootstrapModalPrompt = function() {
var exports = {};
/*
* Expected format of options:
* {
* title: 'Modal Title',
* content: 'Text content for modal',
* template: 'templateWithContentName',
* templateData: {},
* btnDismissText: 'Close',
* btnOkText: 'OK',
* onShown: function() {} // callback function.
* }
*/
exports.prompt = function(options, callback) {
options = _.extend({
title: 'Confirmation',
content: '',
template: null,
templateData: {},
dialogTemplate: Template.bsModalPrompt,
formSchema: null,
btnDismissText: 'Close',
btnOkText: 'OK',
// Callback called before the modal is shown.
// Arguments are the passed initial options, and the modal DOM node.
beforeShow: function(options, node) {
},
// Called after the modal is shown and all transitions have been completed.
// Arguments are the passed initial options, and the modal DOM node.
afterShow: function(options, node) {
},
// Callback called before the modal is hidden.
// Arguments are the passed initial options, and the modal DOM node.
beforeHide: function(options, node) {
},
// Callback called after the modal has been hidden and all transitions have completed.
// Arguments are the passed initial options, and the modal DOM node.
afterHide: function(options, node) {
},
// Called when the users clicks on the confirm button.
// If the function returns false, the confirm will be ABORTED.
// Otherwise the modal will be closed and the regular callback is called with the result.
// Arguments are the passed initial options, and the modal DOM node.
onConfirm: function(options, node) {
// return false; // Aborts closing of modal!
}
}, options);
// Make sure to clean up.
// Markup could remain if an error ocurred in a callback when processing
// or hiding.
$('.bs-modal-prompt').remove();
var dialogData = _.extend({
title: options.title,
content: options.content,
btnDismissText: options.btnDismissText,
btnOkText: options.btnOkText
}, options.templateData);
var dialog = Blaze.renderWithData(options.dialogTemplate, dialogData, $('body').get(0));
var modal = $(dialog.firstNode()).find('.modal');
// Function to be called when confirmed.
// Defined up here so it can be used in AutoForm submit hook.
function onConfirm(data) {
if (options.onConfirm) {
var flag = options.onConfirm(options, modal.get(0), data);
if (flag === false) {
return false;
}
}
if (options.beforeHide) {
options.beforeHide(options, modal.get(0), data);
}
modal.modal('hide');
if (callback) {
callback(data ? data : true);
}
}
if (options.template) {
// Render the given template with the specified data and insert it
// to the modal-body directly.
Blaze.renderWithData(
options.template,
options.templateData,
modal.find('.modal-body').get(0)
);
}
if (options.formSchema) {
// Render the form using the autoform quickForm template.
Blaze.renderWithData(
Template.quickForm,
{schema: options.formSchema, id: 'bootstrapModalPromptForm'},
modal.find('.modal-body').get(0)
);
// Handle form submit.
// Note the important second parameter true for replacing hooks.
AutoForm.addHooks('bootstrapModalPromptForm', {
onSubmit: function (insertDoc, updateDoc, currentDoc) {
onConfirm(insertDoc);
return false;
}
}, true);
}
modal.on('shown.bs.modal', function() {
if (options.afterShow) {
options.afterShow(options, modal.get(0));
}
});
modal.on('hidden.bs.modal', function() {
if (options.afterHide) {
options.afterHide(options, modal.get(0));
}
// Remove dialog from dom.
Blaze.remove(dialog);
});
if (options.beforeShow) {
options.beforeShow(options, modal.get(0));
}
// if btnDismissText is falsey, remove it
if (!options.btnDismissText) {
modal.find('.modal-btn-dismiss').remove();
}
else {
modal.find('.modal-btn-dismiss').off().click(function() {
if (options.beforeHide) {
options.beforeHide(options, modal.get(0));
}
modal.modal('hide');
if (callback)
callback(false);
return false;
});
}
// if btnOkText is falsey, remove it
if (!options.btnOkText) {
modal.find('.modal-btn-confirm').remove();
}
else {
modal.find('.modal-btn-confirm').off().click(function() {
if (options.formSchema) {
modal.find('form').submit();
}
else {
onConfirm();
}
return false;
});
}
modal.modal('show');
};
// Dismisses current modal if open
exports.hide = function() {
var modal = $('.bs-modal-prompt .modal');
modal.modal('hide');
//$('.bs-modal-prompt').remove();
}
return exports;
}();