Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for dismissible option breaking with multiple popovers open #250

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions demo/test-issue248.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Issue (#248) - Dismissible option not working with multiple popovers</title>

<link rel="stylesheet" href="../src/jquery.webui-popover.css" />
<script src="jquery.js" ></script>
<script src="bootstrap.js" ></script>
<script src="../src/jquery.webui-popover.js" ></script>

<style type="text/css">
#popover1, #popover3 {
margin-left: 50px;
margin-right: 100px;
}
.pop {
display: inline-block;
border: 1px solid silver;
padding: 2px;
}
.popContainer {
margin-bottom: 60px;
}
.desc {
margin-bottom: 20px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#popover1").webuiPopover({
dismissible: false,
placement: "bottom",
content: "I'm not dismissible"
});
$("#popover2").webuiPopover({
dismissible: true,
placement: "bottom",
content: "I'm dismissible"
});

$("#popover3").webuiPopover({
multi: true,
dismissible: true,
placement: "bottom",
content: "I'm popover 3"
});
$("#popover4").webuiPopover({
multi: true,
dismissible: true,
placement: "bottom",
content: "I'm popover 4"
});
});
</script>
</head>
<body>
<div class="desc">
<b>Problem of issue#248</b>: Opening a dismissible popover causes non-dismissible popovers to become dismissible.<br /><br />

<b>Initial conditions</b>: Popover 1 is not dismissible. Popover 2 is dismissible.<br /><br />

Step 1) Click popover 1. Observe that it is not dismissible. Close it.<br />
Step 2) Click popover 2. Observe that it is dismissible. Close it.<br />
Step 3) Click popover 1. Observe that it is now dismissible!<br />
</div>
<div class="popContainer">
<div id="popover1" class="pop">Popover 1</div>
<div id="popover2" class="pop">Popover 2</div>
</div>

<div class="desc">
<b>With issue#248 solution in place, ensure that</b>: When an outside click is done for two open popovers,
both should dismiss themselves through their own bodyClickHandler individually.<br /><br />

<b>Initial conditions</b>: Both popovers are dismissible and have multi:true.<br /><br />

Step 1) Open Popover 3 and Popover 4 so that they're both open at the same time.<br />
Step 2) Click outside of both of them. Make sure both close.<br />
</div>
<div>
<div id="popover3" class="pop">Popover 3</div>
<div id="popover4" class="pop">Popover 4</div>
</div>
</body>
</html>
19 changes: 8 additions & 11 deletions src/jquery.webui-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
var _srcElements = [];
var backdrop = $('<div class="webui-popover-backdrop"></div>');
var _globalIdSeed = 0;
var _isBodyEventHandled = false;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This global setting was preventing the bodyClickHandler from being able to bind multiple times. It would only allow one bind. We want as many binds as there are popovers to happen.

var _offsetOut = -2000; // the value offset out of the screen
var $document = $(document);

Expand Down Expand Up @@ -727,19 +726,18 @@
},

bindBodyEvents: function() {
if (_isBodyEventHandled) {
return;
}
if (this.options.dismissible && this.getTrigger() === 'click') {
if (isMobile) {
$document.off('touchstart.webui-popover').on('touchstart.webui-popover', $.proxy(this.bodyTouchStartHandler, this));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested on mobile, but if we like the separation this provides in the lines below, we could add a unique id here as well.

} else {
$document.off('keyup.webui-popover').on('keyup.webui-popover', $.proxy(this.escapeHandler, this));
$document.off('click.webui-popover').on('click.webui-popover', $.proxy(this.bodyClickHandler, this));
$document.off('keyup.webui-popover' + this._idSeed)
.on('keyup.webui-popover' + this._idSeed, $.proxy(this.escapeHandler, this));
$document.off('click.webui-popover' + this._idSeed)
.on('click.webui-popover' + this._idSeed, $.proxy(this.bodyClickHandler, this));
}
} else if (this.getTrigger() === 'hover') {
$document.off('touchend.webui-popover')
.on('touchend.webui-popover', $.proxy(this.bodyClickHandler, this));
$document.off('touchend.webui-popover' + this._idSeed)
.on('touchend.webui-popover' + this._idSeed, $.proxy(this.bodyClickHandler, this));
}
},

Expand Down Expand Up @@ -770,7 +768,7 @@
},
escapeHandler: function(e) {
if (e.keyCode === 27) {
this.hideAll();
this.hide();
}
},
bodyTouchStartHandler: function(e) {
Expand All @@ -785,7 +783,6 @@
});
},
bodyClickHandler: function(e) {
_isBodyEventHandled = true;
var canHide = true;
for (var i = 0; i < _srcElements.length; i++) {
var pop = getPopFromElement(_srcElements[i]);
Expand All @@ -804,7 +801,7 @@
}
}
if (canHide) {
hideAllPop();
this.hide();
}
},

Expand Down