UNPKG

doevisualizations

Version:

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

96 lines (82 loc) 3.04 kB
steal('jquery', function($) { // Checks if x and y coordinates are within a box with left, top, width and height var withinBox = function(x, y, left, top, width, height ){ return (y >= top && y < top + height && x >= left && x < left + width); } /** * @function jQuery.fn.within * @parent jQuery.within * @plugin jquerypp/dom/within * @hide * * Returns all elements matching the selector that touch a given point: * * // get all elements that touch 200x200. * $('*').within(200, 200); * * @param {Number} left the position from the left of the page * @param {Number} top the position from the top of the page * @param {Boolean} [useOffsetCache=false] cache the dimensions and offset of the elements. * @return {jQuery} a jQuery collection of elements whos area * overlaps the element position. */ $.fn.within= function(left, top, useOffsetCache) { var ret = [] this.each(function(){ var q = jQuery(this); if (this == document.documentElement) { return ret.push(this); } // uses either the cached offset or .offset() var offset = useOffsetCache ? $.data(this,"offsetCache") || $.data(this,"offsetCache", q.offset()) : q.offset(); // Check if the given coordinates are within the area of the current element var res = withinBox(left, top, offset.left, offset.top, this.offsetWidth, this.offsetHeight ); if (res) { // Add it to the results ret.push(this); } }); return this.pushStack( $.unique( ret ), "within", left+","+top ); } /** * @function jQuery.fn.withinBox * @parent jQuery.within * * Returns all elements matching the selector that have a given area in common: * * $('*').withinBox(200, 200, 100, 100) * * @param {Number} left the position from the left of the page * @param {Number} top the position from the top of the page * @param {Number} width the width of the area * @param {Number} height the height of the area * @param {Boolean} [useOffsetCache=false] cache the dimensions and offset of the elements. * @return {jQuery} a jQuery collection of elements whos area * overlaps the element position. */ $.fn.withinBox = function(left, top, width, height, useOffsetCache){ var ret = [] this.each(function(){ var q = jQuery(this); if(this == document.documentElement) return ret.push(this); // use cached offset or .offset() var offset = useOffsetCache ? $.data(this,"offset") || $.data(this,"offset", q.offset()) : q.offset(); var ew = q.width(), eh = q.height(), // Checks if the element offset is within the given box res = !( (offset.top > top+height) || (offset.top +eh < top) || (offset.left > left+width ) || (offset.left+ew < left)); if(res) ret.push(this); }); return this.pushStack( $.unique( ret ), "withinBox", $.makeArray(arguments).join(",") ); } return $; })