UNPKG

doevisualizations

Version:

Data Visualization Library based on RequireJS and D3.js (v4+)

50 lines (46 loc) 1.37 kB
/** * @add jQuery.fn */ steal('jquery', function($) { /** * @function closest * @parent dom * @plugin jquerypp/dom/closest * Overwrites closest to allow open > selectors. This allows controller * actions such as: * * ">li click" : function( el, ev ) { ... } */ var oldClosest = $.fn.closest; $.fn.closest = function(selectors, context){ var rooted = {}, res, result, thing, i, j, selector, rootedIsEmpty = true, selector, selectorsArr = selectors; if(typeof selectors == "string") selectorsArr = [selectors]; $.each(selectorsArr, function(i, selector){ if(selector.indexOf(">") == 0 ){ if(selector.indexOf(" ") != -1){ throw " closest does not work with > followed by spaces!" } rooted[( selectorsArr[i] = selector.substr(1) )] = selector; if(typeof selectors == "string") selectors = selector.substr(1); rootedIsEmpty = false; } }) res = oldClosest.call(this, selectors, context); if(rootedIsEmpty) return res; i =0; while(i < res.length){ result = res[i], selector = result.selector; if (rooted[selector] !== undefined) { result.selector = rooted[selector]; rooted[selector] = false; if(typeof result.selector !== "string" || result.elem.parentNode !== context ){ res.splice(i,1); continue; } } i++; } return res; } return $; });