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

Add new option ignore for disable dragging on some elements #157

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Create a new instance of `Slideout`.
- `[options.tolerance]` (Number) - The number of `px` needed for the menu can be opened completely, otherwise it closes. Default: `70`.
- `[options.touch]` (Boolean) - Set this option to false to disable Slideout touch events. Default: `true`.
- `[options.side]` (String) - The side to open the slideout (`left` or `right`). Default: `left`.
- `[options.ignore]` (String) - Selector that prevents dragging on matching elements. Default: ``.

```js
var slideout = new Slideout({
Expand Down
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ function extend(destination, from) {
function inherits(child, uber) {
child.prototype = extend(child.prototype || {}, uber.prototype);
}
var matches = function (element, selector) {
var matches = element.matches ||
element.mozMatchesSelector ||
element.msMatchesSelector ||
element.oMatchesSelector ||
element.webkitMatchesSelector;

if (!matches) {
return false
}

return matches.call(element, selector)
};

/**
* Slideout constructor
Expand All @@ -60,6 +73,7 @@ function Slideout(options) {
this._opened = false;
this._preventOpen = false;
this._touch = options.touch === undefined ? true : options.touch && true;
this._ignore = options.ignore;

// Sets panel
this.panel = options.panel;
Expand Down Expand Up @@ -231,7 +245,7 @@ Slideout.prototype._initTouchEvents = function() {
*/
this._onTouchMoveFn = function(eve) {

if (scrolling || self._preventOpen || typeof eve.touches === 'undefined') {
if (scrolling || self._preventOpen || typeof eve.touches === 'undefined' || self._elementIsIgnored(eve.target)) {
return;
}

Expand Down Expand Up @@ -272,6 +286,19 @@ Slideout.prototype._initTouchEvents = function() {

};

this._elementIsIgnored = function (el) {
if (self._ignore) {
while (el) {
if (matches(el, self._ignore)) {
return true;
}
el = el.parentNode;
}
}

return false;
};

this.panel.addEventListener(touch.move, this._onTouchMoveFn);

return this;
Expand Down
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Slideout', function () {
'_initTouchEvents',
'_translateXTo',
'_setTransition',
'_elementIsIgnored',
'on',
'once',
'off',
Expand Down Expand Up @@ -191,4 +192,35 @@ describe('Slideout', function () {
setTimeout(function(){ slideout.close(); }, 750);
});
});

describe('._elementIsIgnored()', function() {
it('should return false when no ignore option.', function(){
slideout.destroy();
slideout = new Slideout({
'panel': doc.getElementById('panel'),
'menu': doc.getElementById('menu')
});
assert(slideout._elementIsIgnored(doc.getElementById('menu')) === false);
});

it('should return false when the provided element is not a children of a ignored path.', function(){
slideout.destroy();
slideout = new Slideout({
'panel': doc.getElementById('panel'),
'menu': doc.getElementById('menu'),
'ignore': '.menu-section-list'
});
assert(slideout._elementIsIgnored(doc.getElementById('menu')) === false);
});

it('should return true when the provided element is children of a ignored path.', function(){
slideout.destroy();
slideout = new Slideout({
'panel': doc.getElementById('panel'),
'menu': doc.getElementById('menu'),
'ignore': '.menu-section-list'
});
assert(slideout._elementIsIgnored(doc.querySelector('.menu-section-list a')) === true);
});
});
});