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

Fixed #9766 #10360

Merged
merged 3 commits into from
Jul 21, 2017
Merged
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
53 changes: 35 additions & 18 deletions js/foundation.dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ class Dropdown extends Positionable {
_init() {
var $id = this.$element.attr('id');

this.$anchor = $(`[data-toggle="${$id}"]`).length ? $(`[data-toggle="${$id}"]`) : $(`[data-open="${$id}"]`);
this.$anchor.attr({
this.$anchors = $(`[data-toggle="${$id}"]`).length ? $(`[data-toggle="${$id}"]`) : $(`[data-open="${$id}"]`);
this.$anchors.attr({
'aria-controls': $id,
'data-is-focus': false,
'data-yeti-box': $id,
'aria-haspopup': true,
'aria-expanded': false

});

this._setCurrentAnchor(this.$anchors.first());

if(this.options.parentClass){
this.$parent = this.$element.parents('.' + this.options.parentClass);
}else{
Expand All @@ -69,7 +70,7 @@ class Dropdown extends Positionable {
'aria-hidden': 'true',
'data-yeti-box': $id,
'data-resize': $id,
'aria-labelledby': this.$anchor[0].id || GetYoDigits(6, 'dd-anchor')
'aria-labelledby': this.$currentAnchor.id || GetYoDigits(6, 'dd-anchor')
});
super._init();
this._events();
Expand All @@ -87,7 +88,7 @@ class Dropdown extends Positionable {

_getDefaultAlignment() {
// handle legacy float approach
var horizontalPosition = /float-(\S+)/.exec(this.$anchor[0].className);
var horizontalPosition = /float-(\S+)/.exec(this.$currentAnchor.className);
if(horizontalPosition) {
return horizontalPosition[1];
}
Expand All @@ -104,7 +105,18 @@ class Dropdown extends Positionable {
* @private
*/
_setPosition() {
super._setPosition(this.$anchor, this.$element, this.$parent);
super._setPosition(this.$currentAnchor, this.$element, this.$parent);
}

/**
* Make it a current anchor.
* Current anchor as the reference for the position of Dropdown panes.
* @param {HTML} el - DOM element of the anchor.
* @function
* @private
*/
_setCurrentAnchor(el) {
this.$currentAnchor = $(el);
}

/**
Expand All @@ -121,22 +133,27 @@ class Dropdown extends Positionable {
'resizeme.zf.trigger': this._setPosition.bind(this)
});

this.$anchors.off('click.zf.trigger')
.on('click.zf.trigger', function() { _this._setCurrentAnchor(this); });

if(this.options.hover){
this.$anchor.off('mouseenter.zf.dropdown mouseleave.zf.dropdown')
this.$anchors.off('mouseenter.zf.dropdown mouseleave.zf.dropdown')
.on('mouseenter.zf.dropdown', function(){
_this._setCurrentAnchor(this);

var bodyData = $('body').data();
if(typeof(bodyData.whatinput) === 'undefined' || bodyData.whatinput === 'mouse') {
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function(){
_this.open();
_this.$anchor.data('hover', true);
_this.$anchors.data('hover', true);
}, _this.options.hoverDelay);
}
}).on('mouseleave.zf.dropdown', function(){
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function(){
_this.close();
_this.$anchor.data('hover', false);
_this.$anchors.data('hover', false);
}, _this.options.hoverDelay);
});
if(this.options.hoverPane){
Expand All @@ -147,27 +164,27 @@ class Dropdown extends Positionable {
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function(){
_this.close();
_this.$anchor.data('hover', false);
_this.$anchors.data('hover', false);
}, _this.options.hoverDelay);
});
}
}
this.$anchor.add(this.$element).on('keydown.zf.dropdown', function(e) {
this.$anchors.add(this.$element).on('keydown.zf.dropdown', function(e) {

var $target = $(this),
visibleFocusableElements = Keyboard.findFocusable(_this.$element);

Keyboard.handleKey(e, 'Dropdown', {
open: function() {
if ($target.is(_this.$anchor)) {
if ($target.is(_this.$anchors)) {
_this.open();
_this.$element.attr('tabindex', -1).focus();
e.preventDefault();
}
},
close: function() {
_this.close();
_this.$anchor.focus();
_this.$anchors.focus();
}
});
});
Expand All @@ -183,7 +200,7 @@ class Dropdown extends Positionable {
_this = this;
$body.off('click.zf.dropdown')
.on('click.zf.dropdown', function(e){
if(_this.$anchor.is(e.target) || _this.$anchor.find(e.target).length) {
if(_this.$anchors.is(e.target) || _this.$anchors.find(e.target).length) {
return;
}
if(_this.$element.find(e.target).length) {
Expand All @@ -207,7 +224,7 @@ class Dropdown extends Positionable {
* @event Dropdown#closeme
*/
this.$element.trigger('closeme.zf.dropdown', this.$element.attr('id'));
this.$anchor.addClass('hover')
this.$anchors.addClass('hover')
.attr({'aria-expanded': true});
// this.$element/*.show()*/;

Expand Down Expand Up @@ -248,7 +265,7 @@ class Dropdown extends Positionable {
this.$element.removeClass('is-open')
.attr({'aria-hidden': true});

this.$anchor.removeClass('hover')
this.$anchors.removeClass('hover')
.attr('aria-expanded', false);

/**
Expand All @@ -268,7 +285,7 @@ class Dropdown extends Positionable {
*/
toggle() {
if(this.$element.hasClass('is-open')){
if(this.$anchor.data('hover')) return;
if(this.$anchors.data('hover')) return;
this.close();
}else{
this.open();
Expand All @@ -281,7 +298,7 @@ class Dropdown extends Positionable {
*/
_destroy() {
this.$element.off('.zf.trigger').hide();
this.$anchor.off('.zf.dropdown');
this.$anchors.off('.zf.dropdown');
$(document.body).off('click.zf.dropdown');

}
Expand Down
43 changes: 43 additions & 0 deletions test/visual/dropdown/open-the-same-dropdown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!doctype html>
<!--[if IE 9]><html class="lt-ie10" lang="en" dir="rtl"> <![endif]-->
<html class="no-js" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link href="../assets/css/foundation.css" rel="stylesheet" />
<title>Foundation for Sites Testing</title>
</head>

<body>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="cell">
<h1>Dropdown: Open The Same</h1>

<p>Clickable:</p>

<button class="button" type="button" data-toggle="example-dropdown">Toggle Dropdown</button>
<button class="button" type="button" data-toggle="example-dropdown">Toggle Dropdown</button>
<div class="dropdown-pane" id="example-dropdown" data-dropdown data-auto-focus="true">
<p>This is a dropdown.</p>
</div>

<p>Hoverable:</p>

<button class="button" type="button" data-toggle="example-dropdown-1">Hoverable Dropdown</button>
<button class="button" type="button" data-toggle="example-dropdown-1">Hoverable Dropdown</button>
<div class="dropdown-pane" id="example-dropdown-1" data-dropdown data-hover="true" data-hover-pane="true">
Just some junk that needs to be said. Or not. Your choice.
</div>
</div>
</div>
</div>

<script src="../assets/js/vendor.js"></script>
<script src="../assets/js/foundation.js"></script>
<script>
$(document).foundation();
</script>
</body>
</html>