Browse Source

refactor _matches() out of _closest()

The _closest() method already defined a way to do what _matches() was
trying to accomplish.  This pulls the functionality out of _closest() so
that it can be reused outside of _closest() but refactors _closest() to
use this new _matches() method.
pull/607/head
Dan LaMotte 9 years ago
parent
commit
4facaff715
  1. 33
      Sortable.js

33
Sortable.js

@ -979,17 +979,11 @@
function _closest(/**HTMLElement*/el, /**String*/selector, /**HTMLElement*/ctx) { function _closest(/**HTMLElement*/el, /**String*/selector, /**HTMLElement*/ctx) {
if (el) { if (el) {
ctx = ctx || document; ctx = ctx || document;
selector = selector.split('.');
var tag = selector.shift().toUpperCase(),
re = new RegExp('\\s(' + selector.join('|') + ')(?=\\s)', 'g');
do { do {
if ( if (
(tag === '>*' && el.parentNode === ctx) || ( (selector === '>*' && el.parentNode === ctx)
(tag === '' || el.nodeName.toUpperCase() == tag) && || _matches(el, selector)
(!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
)
) { ) {
return el; return el;
} }
@ -1186,20 +1180,19 @@
} }
function _matches(/**HTMLElement*/el, /**String*/selector) { function _matches(/**HTMLElement*/el, /**String*/selector) {
var matches, if (el) {
i = 0; selector = selector.split('.');
if (el.matches) { var tag = selector.shift().toUpperCase(),
return el.matches(selector); re = new RegExp('\\s(' + selector.join('|') + ')(?=\\s)', 'g');
} else if (el.matchesSelector) {
return el.matchesSelector(selector); return (
} else { (tag === '' || el.nodeName.toUpperCase() == tag) &&
matches = (el.document || el.ownerDocument).querySelectorAll(selector); (!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
while (matches[i] && matches[i] !== el) { );
i++;
}
return matches[i] ? true : false;
} }
return false;
} }
function _throttle(callback, ms) { function _throttle(callback, ms) {

Loading…
Cancel
Save