Skip to content

Commit 64fd33f

Browse files
committed
#313 setContent works with jquery dom elements
1 parent 1c00d45 commit 64fd33f

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

index.html

+7-6
Original file line numberDiff line numberDiff line change
@@ -863,21 +863,21 @@ <h2>Themes</h2>
863863
$.confirm({
864864
title: 'Gracefully center aligned',
865865
content: '<p>You can add content and not worry about the alignment. The goal is to make a Interactive dialog!.</p>' +
866-
'<button type="button" class="btn btn-success">Click me to add more content</button> <span> <br></span> ',
866+
'<button type="button" class="btn btn-success">Click me to add more content</button> <div id="contentArea"></div> ',
867867
buttons: {
868868
someButton: {
869869
text: 'Add wow',
870870
btnClass: 'btn-green',
871871
action: function () {
872-
this.$content.find('span').append('<br>Wowww');
872+
this.$content.find('#contentArea').append('<br>Wowww');
873873
return false; // prevent dialog from closing.
874874
}
875875
},
876876
someOtherButton: {
877877
text: 'Clear it',
878878
btnClass: 'btn-orange',
879879
action: function () {
880-
this.$content.find('span').html('');
880+
this.$content.find('#contentArea').html('');
881881
return false; // prevent dialog from closing.
882882
}
883883
},
@@ -889,7 +889,7 @@ <h2>Themes</h2>
889889
// onOpen attach the events.
890890
var that = this;
891891
this.$content.find('button').click(function () {
892-
that.$content.find('span').append('<br>This is awesome!!!!');
892+
that.$content.find('#contentArea').append('<br>This is awesome!!!!');
893893
});
894894
},
895895
});
@@ -959,7 +959,7 @@ <h2>Themes</h2>
959959
</script>
960960
</section>
961961

962-
<section>
962+
<section id="whats-new">
963963
<h2>Whats new <span style="color: #aaa">in v3.2.3</span></h2>
964964
<ul>
965965
<li>Added lazyOpen option</li>
@@ -1068,7 +1068,8 @@ <h4 id="confirm">
10681068
$('.example2').on('click', function () {
10691069
$.confirm({
10701070
title: 'Confirm!',
1071-
content: 'Simple confirm!',
1071+
// content: 'Simple confirm!',
1072+
content: $('#whats-new').clone(),
10721073
buttons: {
10731074
confirm: function () {
10741075
$.alert('Confirmed!');

js/jquery-confirm.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var jconfirm, Jconfirm;
3030
$(this).each(function () {
3131
var $this = $(this);
3232
if ($this.attr('jc-attached')) {
33-
console.warn('jConfirm has already binded to this element ', $this[0]);
33+
console.warn('jConfirm has already been attached to this element ', $this[0]);
3434
return;
3535
}
3636

@@ -165,6 +165,10 @@ var jconfirm, Jconfirm;
165165
jconfirm.lastFocused = $('body').find(':focus');
166166

167167
this._id = Math.round(Math.random() * 99999);
168+
/**
169+
* contentParsed maintains the contents for $content, before it is put in DOM
170+
*/
171+
this.contentParsed = $(document.createElement('div'));
168172

169173
if (!this.lazyOpen) {
170174
setTimeout(function () {
@@ -267,7 +271,7 @@ var jconfirm, Jconfirm;
267271
that.onContentReady();
268272
}, 50);
269273
else {
270-
that.setContent();
274+
// that.setContent();
271275
that._updateContentMaxHeight();
272276
that.setTitle();
273277
that.setIcon();
@@ -376,8 +380,6 @@ var jconfirm, Jconfirm;
376380
},
377381
_updateContentMaxHeight: function () {
378382
var height = $(window).height() - (this.$jconfirmBox.outerHeight() - this.$contentPane.outerHeight()) - (this.offsetTop + this.offsetBottom);
379-
// console.log($(window).height(), this.$jconfirmBox.outerHeight() - this.$contentPane.outerHeight());
380-
381383
this.$contentPane.css({
382384
'max-height': height + 'px'
383385
});
@@ -655,8 +657,8 @@ var jconfirm, Jconfirm;
655657
.css('display', that.buttons[key].isHidden ? 'none' : '')
656658
.click(function (e) {
657659
e.preventDefault();
658-
var res = that.buttons[key].action.apply(that, that.buttons[key]);
659-
that.onAction.apply(that, key, that.buttons[key]);
660+
var res = that.buttons[key].action.apply(that, [that.buttons[key]]);
661+
that.onAction.apply(that, [key, that.buttons[key]]);
660662
that._stopCountDown();
661663
if (typeof res === 'undefined' || res)
662664
that.close();
@@ -798,28 +800,28 @@ var jconfirm, Jconfirm;
798800
this.$titleContainer.show();
799801
}
800802
},
801-
setContentPrepend: function (string, force) {
802-
this.contentParsed = string + this.contentParsed;
803-
if (this.isAjaxLoading && !force)
803+
setContentPrepend: function (content, force) {
804+
if (!content)
804805
return;
805806

806-
this.$content.prepend(string);
807+
this.contentParsed.prepend(content);
807808
},
808-
setContentAppend: function (string, force) {
809-
this.contentParsed = this.contentParsed + string;
810-
if (this.isAjaxLoading && !force)
809+
setContentAppend: function (content) {
810+
if (!content)
811811
return;
812812

813-
this.$content.append(string);
813+
this.contentParsed.append(content);
814814
},
815-
setContent: function (string, force) {
816-
force = force || false;
815+
setContent: function (content, force) {
816+
force = !!force;
817817
var that = this;
818-
this.contentParsed = (typeof string == 'undefined') ? this.contentParsed : string;
818+
if (content)
819+
this.contentParsed.html('').append(content);
819820
if (this.isAjaxLoading && !force)
820821
return;
821822

822-
this.$content.html(this.contentParsed);
823+
this.$content.html('');
824+
this.$content.append(this.contentParsed);
823825
setTimeout(function () {
824826
that.$body.find('input[autofocus]:visible:first').focus();
825827
}, 100);
@@ -877,7 +879,7 @@ var jconfirm, Jconfirm;
877879
this.isAjaxLoading = true;
878880
var u = this.content.substring(4, this.content.length);
879881
$.get(u).done(function (html) {
880-
that.contentParsed = html;
882+
that.contentParsed.html(html);
881883
}).always(function (data, status, xhr) {
882884
that.ajaxResponse = {
883885
data: data,
@@ -894,8 +896,8 @@ var jconfirm, Jconfirm;
894896
this.content = e;
895897

896898
if (!this.isAjax) {
897-
this.contentParsed = this.content;
898-
this.setContent(this.contentParsed);
899+
this.contentParsed.html(this.content);
900+
this.setContent();
899901
that._contentReady.resolve();
900902
}
901903
},
@@ -1118,16 +1120,11 @@ var jconfirm, Jconfirm;
11181120
return false;
11191121
}
11201122

1121-
// console.log(el);
1122-
// console.log(jconfirm.lastClicked);
1123-
11241123
if (!el)
11251124
return false;
11261125

11271126
var offset = el.offset();
1128-
// console.log(offset.top, offset.left);
11291127

1130-
// originate from center of the clicked element
11311128
var iTop = el.outerHeight() / 2;
11321129
var iLeft = el.outerWidth() / 2;
11331130

@@ -1237,6 +1234,9 @@ var jconfirm, Jconfirm;
12371234
dragWindowGap: 15,
12381235
dragWindowBorder: true,
12391236
animateFromElement: true,
1237+
/**
1238+
* @deprecated
1239+
*/
12401240
alignMiddle: true,
12411241
smoothContent: true,
12421242
content: 'Are you sure to continue?',

0 commit comments

Comments
 (0)