{"version":3,"file":"isInViewport.js","sources":["index.js"],"sourcesContent":["import $ from 'jquery'\n\n/**\n * @author Mudit Ameta\n * @license https://github.com/zeusdeux/isInViewport/blob/master/license.md MIT\n */\n\n// expose isInViewport as a custom pseudo-selector\n$.extend($.expr.pseudos || $.expr[':'], {\n // if $.expr.createPseudo is available, use it\n 'in-viewport': $.expr.createPseudo\n ? $.expr.createPseudo(argsString => currElement => isInViewport(currElement, getSelectorArgs(argsString)))\n : (currObj, index, meta) => isInViewport(currObj, getSelectorArgs(meta[3]))\n})\n\n\n// expose isInViewport as a function too\n// this lets folks pass around actual objects as options (like custom viewport)\n// and doesn't tie 'em down to strings. It also prevents isInViewport from\n// having to look up and wrap the dom element corresponding to the viewport selector\n$.fn.isInViewport = function(options) {\n return this.filter((i, el) => isInViewport(el, options))\n}\n\n$.fn.run = run\n\n// lets you chain any arbitrary function or an array of functions and returns a jquery object\nfunction run(args) {\n if (arguments.length === 1 && typeof args === 'function') {\n args = [args]\n }\n\n if (!(args instanceof Array)) {\n throw new SyntaxError('isInViewport: Argument(s) passed to .do/.run should be a function or an array of functions')\n }\n\n args.forEach(arg => {\n if (typeof arg !== 'function') {\n console.warn('isInViewport: Argument(s) passed to .do/.run should be a function or an array of functions')\n console.warn('isInViewport: Ignoring non-function values in array and moving on')\n } else {\n [].slice.call(this).forEach(t => arg.call($(t)))\n }\n })\n\n return this\n}\n\n\n// gets the width of the scrollbar\nfunction getScrollbarWidth(viewport) {\n // append a div that has 100% width to get true width of viewport\n const el = $('
').css({\n width: '100%'\n })\n viewport.append(el)\n\n // subtract true width from the viewport width which is inclusive\n // of scrollbar by default\n const scrollBarWidth = viewport.width() - el.width()\n\n // remove our element from DOM\n el.remove()\n return scrollBarWidth\n}\n\n\n// Returns true if DOM element `element` is in viewport\nfunction isInViewport(element, options) {\n let {top, bottom, left, right} = element.getBoundingClientRect()\n\n let settings = $.extend({\n tolerance: 0,\n viewport: window\n }, options)\n let isVisibleFlag = false\n let $viewport = settings.viewport.jquery ? settings.viewport : $(settings.viewport)\n\n if (!$viewport.length) {\n console.warn('isInViewport: The viewport selector you have provided matches no element on page.')\n console.warn('isInViewport: Defaulting to viewport as window')\n $viewport = $(window)\n }\n\n const $viewportHeight = $viewport.height()\n let $viewportWidth = $viewport.width()\n const typeofViewport = $viewport[0].toString()\n\n // if the viewport is other than window recalculate the top,\n // bottom,left and right wrt the new viewport\n // the [object DOMWindow] check is for window object type in PhantomJS\n if ($viewport[0] !== window && typeofViewport !== '[object Window]' && typeofViewport !== '[object DOMWindow]') {\n // use getBoundingClientRect() instead of $.Offset()\n // since the original top/bottom positions are calculated relative to browser viewport and not document\n const viewportRect = $viewport[0].getBoundingClientRect()\n\n // recalculate these relative to viewport\n top = top - viewportRect.top\n bottom = bottom - viewportRect.top\n left = left - viewportRect.left\n right = right - viewportRect.left\n\n // get the scrollbar width from cache or calculate it\n isInViewport.scrollBarWidth = isInViewport.scrollBarWidth || getScrollbarWidth($viewport)\n\n // remove the width of the scrollbar from the viewport width\n $viewportWidth -= isInViewport.scrollBarWidth\n }\n\n // handle falsy, non-number and non-integer tolerance value\n // same as checking using isNaN and then setting to 0\n // bitwise operators deserve some love too you know\n settings.tolerance = ~~Math.round(parseFloat(settings.tolerance))\n\n if (settings.tolerance < 0) {\n settings.tolerance = $viewportHeight + settings.tolerance // viewport height - tol\n }\n\n // the element is NOT in viewport iff it is completely out of\n // viewport laterally or if it is completely out of the tolerance\n // region. Therefore, if it is partially in view then it is considered\n // to be in the viewport and hence true is returned. Because we have adjusted\n // the left/right positions relative to the viewport, we should check the\n // element's right against the viewport's 0 (left side), and the element's\n // left against the viewport's width to see if it is outside of the viewport.\n\n if (right <= 0 || left >= $viewportWidth) {\n return isVisibleFlag\n }\n\n // if the element is bound to some tolerance\n isVisibleFlag = settings.tolerance ? top <= settings.tolerance && bottom >= settings.tolerance : bottom > 0 && top <= $viewportHeight\n\n return isVisibleFlag\n}\n\n\n// get the selector args from the args string proved by Sizzle\nfunction getSelectorArgs(argsString) {\n if (argsString) {\n const args = argsString.split(',')\n\n // when user only gives viewport and no tolerance\n if (args.length === 1 && isNaN(args[0])) {\n args[1] = args[0]\n args[0] = void 0\n }\n\n return {\n tolerance: args[0] ? args[0].trim() : void 0,\n viewport: args[1] ? $(args[1].trim()) : void 0\n }\n }\n return {}\n}\n"],"names":["this","const","let"],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ;;;;;;;;AAQtB,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;;EAEtC,aAAa,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY;MAC9B,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,UAAA,UAAU,CAAA,CAAC,AAAG,SAAA,UAAA,WAAW,CAAA,CAAC,AAAG,SAAA,YAAY,CAAC,WAAW,EAAE,eAAe,CAAC,UAAU,CAAC,CAAC,MAAA,CAAC;IAC1G,SAAA,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,AAAG,SAAA,YAAY,CAAC,OAAO,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAA;CAC5E,CAAC;;;;;;;AAOF,CAAC,CAAC,EAAE,CAAC,YAAY,GAAG,SAAS,OAAO,EAAE;EACpC,OAAO,IAAI,CAAC,MAAM,CAAC,SAAA,CAAC,CAAC,EAAE,EAAE,EAAE,AAAG,SAAA,YAAY,CAAC,EAAE,EAAE,OAAO,CAAC,GAAA,CAAC;CACzD;;AAED,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,GAAG;;;AAGd,SAAS,GAAG,CAAC,IAAI,EAAE,CAAC;;AAAA;EAClB,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;IACxD,IAAI,GAAG,CAAC,IAAI,CAAC;GACd;;EAED,IAAI,CAAC,CAAC,IAAI,YAAY,KAAK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