UNPKG

@onesy/utils

Version:
96 lines (73 loc) 4.14 kB
import is from './is'; import isEnvironment from './isEnvironment'; import Try from './try'; function element(value) { const object = {}; object.value = value; if (is('string', value)) object.value = window.document.querySelector(value); if (!is('element', object.value)) delete object.value; const matches = function () { let value_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : object.value; const method = is('element', value_) && (value_.matches || value_['webkitMatchesSelector'] || value_['mozMatchesSelector'] || value_['oMatchesSelector'] || value_['msMatchesSelector']); if (!method) return () => false; return method.bind(value_); }; // Parent object.parent = function () { if (this.value && isEnvironment('browser') && this.value.parentNode) return this.value.parentNode; }; // Parents object.parents = function (selectors) { let arrayMethod = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'some'; const parents = []; let parent = this.value; while (parent && parent !== document) { parent = element(parent).parent(); if (parent && (!(selectors !== null && selectors !== void 0 && selectors.length) || selectors[arrayMethod] && selectors[arrayMethod](item => Try(() => matches(parent)(item))))) parents.push(parent); } return parents; }; // Nearest object.nearest = function (selectors) { let arrayMethod = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'some'; // Value matches // return itself in that use case if (!(selectors !== null && selectors !== void 0 && selectors.length) || selectors[arrayMethod] && selectors[arrayMethod](item => Try(() => matches(this.value)(item)))) return this.value; let parent = this.value; while (parent && parent !== document) { parent = element(parent).parent(); if (parent && (!(selectors !== null && selectors !== void 0 && selectors.length) || selectors[arrayMethod] && selectors[arrayMethod](item => Try(() => matches(parent)(item))))) return parent; } }; // Furthest object.furthest = function (selectors) { let arrayMethod = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'some'; const parents = this.parents(selectors, arrayMethod); return parents[parents.length - 1]; }; // hasParent object.hasParent = function (selectors) { let grandparents = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let arrayMethod = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'some'; let parent = this.value; if (!grandparents) return !(selectors !== null && selectors !== void 0 && selectors.length) || selectors[arrayMethod] && selectors[arrayMethod](item => Try(() => matches(this.parent())(item))); while (parent && parent !== document) { parent = element(parent).parent(); if (parent && (!(selectors !== null && selectors !== void 0 && selectors.length) || selectors[arrayMethod] && selectors[arrayMethod](item => Try(() => matches(parent)(item))))) return true; } return false; }; // hasParents // If unique is true, sort selectors argument by most specifc first // and lowest specificty last for the most proper result object.hasParents = function (selectors) { let unique = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let arrayMethod = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'some'; if (!(selectors !== null && selectors !== void 0 && selectors.length)) return !!this.parent(); const parents = this.parents(); return !!(this.value && selectors !== null && selectors !== void 0 && selectors.length && selectors[arrayMethod] && selectors[arrayMethod](selector => { const index = parents.findIndex(item => is('string', selector) ? Try(() => matches(item)(selector)) : item === selector); if (index > -1) { if (unique) parents.splice(index, 1); return true; } return false; })); }; return object; } export default element;