Skip to content

Commit 17372cf

Browse files
committed
Fix #15 add "copyAttributes" parameter
1 parent 92f70cf commit 17372cf

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

bootstrap-confirmation.js

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,40 @@
1414
// CONFIRMATION PUBLIC CLASS DEFINITION
1515
// ===============================
1616
var Confirmation = function (element, options) {
17+
options.trigger = 'click';
18+
1719
this.init('confirmation', element, options);
1820

21+
// keep trace of selectors
22+
this.options._isDelegate = false;
23+
if (options.selector) { // container of buttons
24+
this.options._selector = this._options._selector = options._root_selector +' '+ options.selector;
25+
}
26+
else if (options._selector) { // children of container
27+
this.options._selector = options._selector;
28+
this.options._isDelegate = true;
29+
}
30+
else { // standalone
31+
this.options._selector = options._root_selector;
32+
}
33+
1934
var that = this;
2035

2136
if (!this.options.selector) {
22-
// get existing href and target
23-
if (this.$element.attr('href')) {
24-
this.options.href = this.$element.attr('href');
25-
this.$element.removeAttr('href');
26-
if (this.$element.attr('target')) {
27-
this.options.target = this.$element.attr('target');
37+
// store copied attributes
38+
this.options._attributes = {};
39+
if (this.options.copyAttributes) {
40+
if (typeof this.options.copyAttributes === 'string') {
41+
this.options.copyAttributes = this.options.copyAttributes.split(' ');
2842
}
2943
}
44+
else {
45+
this.options.copyAttributes = [];
46+
}
47+
48+
this.options.copyAttributes.forEach(function(attr) {
49+
this.options._attributes[attr] = this.$element.attr(attr);
50+
}, this);
3051

3152
// cancel original event
3253
this.$element.on(that.options.trigger, function(e, ack) {
@@ -60,7 +81,7 @@
6081
if ($(that.options._selector).is(e.target)) {
6182
return;
6283
}
63-
84+
6485
// close all popover already initialized
6586
$(that.options._selector).filter(function() {
6687
return $(this).data('bs.confirmation') !== undefined;
@@ -78,10 +99,9 @@
7899
placement: 'top',
79100
title: 'Are you sure?',
80101
html: true,
81-
href: false,
82102
popout: false,
83103
singleton: false,
84-
target: '_self',
104+
copyAttributes: 'href target',
85105
onConfirm: $.noop,
86106
onCancel: $.noop,
87107
btnOkClass: 'btn-xs btn-primary',
@@ -111,25 +131,6 @@
111131
return Confirmation.DEFAULTS;
112132
};
113133

114-
// custom init keeping trace of selectors
115-
Confirmation.prototype.init = function(type, element, options) {
116-
options.trigger = 'click';
117-
118-
$.fn.popover.Constructor.prototype.init.call(this, type, element, options);
119-
120-
this.options._isDelegate = false;
121-
if (options.selector) { // container of buttons
122-
this.options._selector = this._options._selector = options._root_selector +' '+ options.selector;
123-
}
124-
else if (options._selector) { // children of container
125-
this.options._selector = options._selector;
126-
this.options._isDelegate = true;
127-
}
128-
else { // standalone
129-
this.options._selector = options._root_selector;
130-
}
131-
};
132-
133134
Confirmation.prototype.setContent = function () {
134135
var that = this,
135136
$tip = this.tip(),
@@ -141,6 +142,7 @@
141142
$tip.find('[data-apply="confirmation"]')
142143
.addClass(o.btnOkClass)
143144
.html(o.btnOkLabel)
145+
.attr(this.options._attributes)
144146
.prepend($('<i></i>').addClass(o.btnOkIcon), ' ')
145147
.off('click')
146148
.one('click', function(e) {
@@ -150,14 +152,6 @@
150152
that.$element.confirmation('hide');
151153
});
152154

153-
// add href to confirm button if needed
154-
if (o.href && o.href != "#") {
155-
$tip.find('[data-apply="confirmation"]').attr({
156-
href: o.href,
157-
target: o.target
158-
});
159-
}
160-
161155
// configure 'cancel' button
162156
$tip.find('[data-dismiss="confirmation"]')
163157
.addClass(o.btnCancelClass)

0 commit comments

Comments
 (0)