Skip to content

Commit 2f4d1ab

Browse files
committed
Close #136 Prefer data attributes for "btn" options
1 parent 3a86f93 commit 2f4d1ab

File tree

3 files changed

+67
-36
lines changed

3 files changed

+67
-36
lines changed

.eslintrc.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
"stroustrup"
2424
],
2525
"no-param-reassign": [
26-
"error",
27-
{
28-
"props": false
29-
}
26+
"off"
3027
],
3128
"key-spacing": [
3229
"error",

example/index.html

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,16 @@ <h1>Bootstrap Confirmation</h1>
6161
<div class="card-body">
6262
<button class="btn btn-primary" data-toggle="confirmation" data-placement="left">Confirmation on left</button>
6363
<button class="btn btn-primary" data-toggle="confirmation" data-placement="top">Confirmation on top</button>
64-
<button class="btn btn-primary" data-toggle="confirmation" data-placement="bottom">Confirmation on bottom
65-
</button>
64+
<button class="btn btn-primary" data-toggle="confirmation" data-placement="bottom">Confirmation on bottom</button>
6665
<button class="btn btn-primary" data-toggle="confirmation" data-placement="right">Confirmation on right</button>
6766
</div>
6867
</div>
6968

7069
<div class="card mb-3">
7170
<div class="card-header">Singleton</div>
7271
<div class="card-body">
73-
<button class="btn btn-primary" data-toggle="confirmation-singleton" data-singleton="true">Confirmation 1
74-
</button>
75-
<button class="btn btn-primary" data-toggle="confirmation-singleton" data-singleton="true">Confirmation 2
76-
</button>
72+
<button class="btn btn-primary" data-toggle="confirmation-singleton" data-singleton="true">Confirmation 1</button>
73+
<button class="btn btn-primary" data-toggle="confirmation-singleton" data-singleton="true">Confirmation 2</button>
7774
</div>
7875
</div>
7976

@@ -107,8 +104,19 @@ <h1>Bootstrap Confirmation</h1>
107104
<div class="card mb-3">
108105
<div class="card-header">Custom buttons</div>
109106
<div class="card-body">
110-
<button class="btn btn-primary" id="custom-confirmation">Choose your transportation</button>
111-
<button class="btn btn-primary" id="custom-confirmation-links">Share to</button>
107+
<button class="btn btn-primary" data-toggle="confirmation-custom">Choose your transportation</button>
108+
<button class="btn btn-primary" data-toggle="confirmation-custom-links">Share to</button>
109+
</div>
110+
</div>
111+
112+
<div class="card mb-3">
113+
<div class="card-header">Override data attributes</div>
114+
<div class="card-body">
115+
<button class="btn btn-primary" data-toggle="confirmation-override">No overrides</button>
116+
<button class="btn btn-primary" data-toggle="confirmation-override"
117+
data-btn-ok-label="Ok" data-btn-ok-class="btn btn-sm btn-success">
118+
Override OK
119+
</button>
112120
</div>
113121
</div>
114122
</div>
@@ -126,25 +134,22 @@ <h1>Bootstrap Confirmation</h1>
126134

127135
$('[data-toggle=confirmation]').confirmation({
128136
rootSelector: '[data-toggle=confirmation]',
129-
container: 'body'
137+
container: 'body',
130138
});
131139
$('[data-toggle=confirmation-singleton]').confirmation({
132140
rootSelector: '[data-toggle=confirmation-singleton]',
133-
container: 'body'
134141
});
135142
$('[data-toggle=confirmation-popout]').confirmation({
136143
rootSelector: '[data-toggle=confirmation-popout]',
137-
container: 'body'
138144
});
139145

140146
$('#confirmation-delegate').confirmation({
141-
selector: 'button'
147+
selector: 'button',
142148
});
143149

144150
$('[data-toggle=custom-confirmation-events]')
145151
.confirmation({
146152
rootSelector: '[data-toggle=custom-confirmation-events]',
147-
container: 'body'
148153
})
149154
.on('mouseenter', function() {
150155
$(this).confirmation('show');
@@ -153,11 +158,16 @@ <h1>Bootstrap Confirmation</h1>
153158
alert('"myevent" triggered');
154159
});
155160

156-
$('#custom-confirmation').confirmation({
161+
$('[data-toggle=confirmation-override]').confirmation({
162+
rootSelector: '[data-toggle=confirmation-override]',
163+
btnOkLabel: 'Continue',
164+
btnCancelLabel: 'Cancel',
165+
});
166+
167+
$('[data-toggle=confirmation-custom]').confirmation({
157168
rootSelector: '#custom-confirmation',
158-
container: 'body',
159169
title: '',
160-
onConfirm: function(value) {
170+
onConfirm(value) {
161171
alert('You choosed ' + value);
162172
},
163173
buttons: [
@@ -166,62 +176,61 @@ <h1>Bootstrap Confirmation</h1>
166176
iconClass: 'material-icons mr-1',
167177
iconContent: 'directions_railway',
168178
label: 'Railway',
169-
value: 'Railway'
179+
value: 'Railway',
170180
},
171181
{
172182
class: 'btn btn-sm btn-primary',
173183
iconClass: 'material-icons mr-1',
174184
iconContent: 'directions_car',
175185
label: 'Car',
176-
value: 'Car'
186+
value: 'Car',
177187
},
178188
{
179189
class: 'btn btn-sm btn-warning',
180190
iconClass: 'material-icons mr-1',
181191
iconContent: 'directions_boat',
182192
label: 'Boat',
183-
value: 'Boat'
193+
value: 'Boat',
184194
},
185195
{
186196
class: 'btn btn-sm btn-secondary',
187197
iconClass: 'material-icons mr-1',
188198
iconContent: 'cancel',
189199
label: 'Cancel',
190-
cancel: true
191-
}
192-
]
200+
cancel: true,
201+
},
202+
],
193203
});
194204

195-
$('#custom-confirmation-links').confirmation({
205+
$('[data-toggle=confirmation-custom-links]').confirmation({
196206
rootSelector: '#custom-confirmation-link',
197-
container: 'body',
198207
title: '',
199208
buttons: [
200209
{
201210
label: 'Twitter',
202211
iconClass: 'fa fa-twitter fa-lg mr-1',
203212
attr: {
204213
href: 'https://twitter.com',
205-
target: '_blank'
206-
}
214+
target: '_blank',
215+
},
207216
},
208217
{
209218
label: 'Facebook',
210219
iconClass: 'fa fa-facebook fa-lg mr-1',
211220
attr: {
212221
href: 'https://facebook.com',
213-
target: '_blank'
214-
}
222+
target: '_blank',
223+
},
215224
},
216225
{
217226
label: 'Pinterest',
218227
iconClass: 'fa fa-pinterest fa-lg mr-1',
219228
attr: {
220229
href: 'https://pinterest.com',
221-
target: '_blank'
222-
}
223-
}
224-
]
230+
target: '_blank',
231+
},
232+
},
233+
],
225234
});
226235
</script>
227236

src/confirmation.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,31 @@ class Confirmation extends Popover {
231231

232232
// Private
233233

234+
/**
235+
* Build configuration object
236+
* Bootstrap standard is to give priority to JS config over data attributes,
237+
* but for Confirmation we prefer data attributes
238+
* @param config
239+
* @return {*}
240+
* @private
241+
*/
242+
_getConfig(config) {
243+
config = super._getConfig(config);
244+
245+
const dataAttributes = $(this.element).data();
246+
Object.keys(dataAttributes)
247+
.forEach((dataAttr) => {
248+
if (dataAttr.indexOf('btn') !== 0) {
249+
delete dataAttributes[dataAttr];
250+
}
251+
});
252+
253+
return {
254+
...config,
255+
...dataAttributes,
256+
};
257+
}
258+
234259
/**
235260
* Copy the value of `copyAttributes` on the config object
236261
* @private

0 commit comments

Comments
 (0)