You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

11 lines
14 KiB

/*!
* # Semantic UI x.x - Visibility
* http://github.com/semantic-org/semantic-ui/
*
*
* Copyright 2015 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
!function(e,o,n,t){"use strict";e.fn.visibility=function(i){var s,c=e(this),r=c.selector||"",a=(new Date).getTime(),l=[],u=arguments[0],d="string"==typeof u,m=[].slice.call(arguments,1);return c.each(function(){var c,f,g=e.isPlainObject(i)?e.extend(!0,{},e.fn.visibility.settings,i):e.extend({},e.fn.visibility.settings),b=g.className,v=g.namespace,p=g.error,h="."+v,P="module-"+v,C=e(o),V=e(this),y=e(g.context),x=(V.selector||"",V.data(P)),S=o.requestAnimationFrame||o.mozRequestAnimationFrame||o.webkitRequestAnimationFrame||o.msRequestAnimationFrame||function(e){setTimeout(e,0)},R=this;f={initialize:function(){f.debug("Initializing",g),f.setup.cache(),f.save.position(),f.should.trackChanges()&&(f.bind.events(),"image"==g.type&&f.setup.image(),"fixed"==g.type&&f.setup.fixed(),g.observeChanges&&f.observeChanges(),f.is.visible()||f.error(p.visible,V)),g.initialCheck&&f.checkVisibility(),f.instantiate()},instantiate:function(){f.debug("Storing instance",f),V.data(P,f),x=f},destroy:function(){f.verbose("Destroying previous module"),c&&c.disconnect(),C.off("load"+h,f.event.load).off("resize"+h,f.event.resize),y.off("scrollchange"+h,f.event.scrollchange),V.off(h).removeData(P)},observeChanges:function(){y[0];"MutationObserver"in o&&(c=new MutationObserver(function(e){f.verbose("DOM tree modified, updating visibility calculations"),f.timer=setTimeout(function(){f.verbose("DOM tree modified, updating sticky menu"),f.refresh()},100)}),c.observe(R,{childList:!0,subtree:!0}),f.debug("Setting up mutation observer",c))},bind:{events:function(){f.verbose("Binding visibility events to scroll and resize"),C.on("load"+h,f.event.load).on("resize"+h,f.event.resize),y.off("scroll"+h).on("scroll"+h,f.event.scroll).on("scrollchange"+h,f.event.scrollchange)},imageLoad:function(){var o=V.find("img"),t=o.length,i=t,s=0,c=[],r=[],a=n.createElement("img"),l=function(){s++,s>=t&&(f.debug("Images finished loading inside element, refreshing position"),f.refresh())};if(t>0)for(o.each(function(){c.push(e(this).attr("src"))});i--;)a=n.createElement("img"),a.onload=l,a.onerror=l,a.src=c[i],r.push(a)}},event:{resize:function(){f.debug("Window resized"),S(f.refresh)},load:function(){f.debug("Page finished loading"),S(f.refresh)},scroll:function(){g.throttle?(clearTimeout(f.timer),f.timer=setTimeout(function(){y.trigger("scrollchange"+h,[y.scrollTop()])},g.throttle)):S(function(){y.trigger("scrollchange"+h,[y.scrollTop()])})},scrollchange:function(e,o){f.checkVisibility(o)}},precache:function(o,t){o instanceof Array||(o=[o]);for(var i=o.length,s=0,c=[],r=n.createElement("img"),a=function(){s++,s>=o.length&&e.isFunction(t)&&t()};i--;)r=n.createElement("img"),r.onload=a,r.onerror=a,r.src=o[i],c.push(r)},should:{trackChanges:function(){return d?(f.debug("One time query, no need to bind events"),!1):(f.debug("Callbacks being attached"),!0)}},setup:{cache:function(){f.cache={occurred:{},screen:{},element:{}}},image:function(){var e=V.data("src");e&&(f.verbose("Lazy loading image",e),g.observeChanges=!1,f.topVisible(function(){f.debug("Image top visible",R),f.precache(e,function(){f.set.image(e),g.onTopVisible=!1})}))},fixed:function(){f.verbose("Setting up fixed on element pass"),g.once=!1,g.onTopPassed=function(){V.addClass(b.fixed).css({top:g.offset+"px"}),g.transition&&e.fn.transition!==t&&V.transition(g.transition,g.duration)},g.onTopPassedReverse=function(){V.removeClass(b.fixed).css({position:"",top:""})}}},set:{image:function(o){var n=f.cache.screen.bottom<f.cache.element.top;V.attr("src",o),n?(f.verbose("Image outside browser, no show animation"),V.show()):g.transition?e.fn.transition!==t?V.transition(g.transition,g.duration):V.fadeIn(g.duration):V.show()}},is:{onScreen:function(){var e=f.get.elementCalculations();return e.onScreen},offScreen:function(){var e=f.get.elementCalculations();return e.offScreen},visible:function(){return f.cache&&f.cache.element?f.cache.element.width>0:!1}},refresh:function(){f.debug("Refreshing constants (width/height)"),f.reset(),f.save.position(),f.checkVisibility(),g.onRefresh.call(R)},reset:function(){f.verbose("Reseting all cached values"),e.isPlainObject(f.cache)&&(f.cache.screen={},f.cache.element={})},checkVisibility:function(e){f.verbose("Checking visibility of element",f.cache.element),f.is.visible()&&(f.save.scroll(e),f.save.calculations(),f.passed(),f.passingReverse(),f.topVisibleReverse(),f.bottomVisibleReverse(),f.topPassedReverse(),f.bottomPassedReverse(),f.onScreen(),f.offScreen(),f.passing(),f.topVisible(),f.bottomVisible(),f.topPassed(),f.bottomPassed(),g.onUpdate&&g.onUpdate.call(R,f.get.elementCalculations()))},passed:function(o,n){var i=f.get.elementCalculations();if(o&&n)g.onPassed[o]=n;else{if(o!==t)return f.get.pixelsPassed(o)>i.pixelsPassed;i.passing&&e.each(g.onPassed,function(e,o){i.bottomVisible||i.pixelsPassed>f.get.pixelsPassed(e)?f.execute(o,e):g.once||f.remove.occurred(o)})}},onScreen:function(e){var o=f.get.elementCalculations(),n=e||g.onOnScreen,i="onScreen";return e&&(f.debug("Adding callback for onScreen",e),g.onOnScreen=e),o.onScreen?f.execute(n,i):g.once||f.remove.occurred(i),e!==t?o.onOnScreen:void 0},offScreen:function(e){var o=f.get.elementCalculations(),n=e||g.onOffScreen,i="offScreen";return e&&(f.debug("Adding callback for offScreen",e),g.onOffScreen=e),o.offScreen?f.execute(n,i):g.once||f.remove.occurred(i),e!==t?o.onOffScreen:void 0},passing:function(e){var o=f.get.elementCalculations(),n=e||g.onPassing,i="passing";return e&&(f.debug("Adding callback for passing",e),g.onPassing=e),o.passing?f.execute(n,i):g.once||f.remove.occurred(i),e!==t?o.passing:void 0},topVisible:function(e){var o=f.get.elementCalculations(),n=e||g.onTopVisible,i="topVisible";return e&&(f.debug("Adding callback for top visible",e),g.onTopVisible=e),o.topVisible?f.execute(n,i):g.once||f.remove.occurred(i),e===t?o.topVisible:void 0},bottomVisible:function(e){var o=f.get.elementCalculations(),n=e||g.onBottomVisible,i="bottomVisible";return e&&(f.debug("Adding callback for bottom visible",e),g.onBottomVisible=e),o.bottomVisible?f.execute(n,i):g.once||f.remove.occurred(i),e===t?o.bottomVisible:void 0},topPassed:function(e){var o=f.get.elementCalculations(),n=e||g.onTopPassed,i="topPassed";return e&&(f.debug("Adding callback for top passed",e),g.onTopPassed=e),o.topPassed?f.execute(n,i):g.once||f.remove.occurred(i),e===t?o.topPassed:void 0},bottomPassed:function(e){var o=f.get.elementCalculations(),n=e||g.onBottomPassed,i="bottomPassed";return e&&(f.debug("Adding callback for bottom passed",e),g.onBottomPassed=e),o.bottomPassed?f.execute(n,i):g.once||f.remove.occurred(i),e===t?o.bottomPassed:void 0},passingReverse:function(e){var o=f.get.elementCalculations(),n=e||g.onPassingReverse,i="passingReverse";return e&&(f.debug("Adding callback for passing reverse",e),g.onPassingReverse=e),o.passing?g.once||f.remove.occurred(i):f.get.occurred("passing")&&f.execute(n,i),e!==t?!o.passing:void 0},topVisibleReverse:function(e){var o=f.get.elementCalculations(),n=e||g.onTopVisibleReverse,i="topVisibleReverse";return e&&(f.debug("Adding callback for top visible reverse",e),g.onTopVisibleReverse=e),o.topVisible?g.once||f.remove.occurred(i):f.get.occurred("topVisible")&&f.execute(n,i),e===t?!o.topVisible:void 0},bottomVisibleReverse:function(e){var o=f.get.elementCalculations(),n=e||g.onBottomVisibleReverse,i="bottomVisibleReverse";return e&&(f.debug("Adding callback for bottom visible reverse",e),g.onBottomVisibleReverse=e),o.bottomVisible?g.once||f.remove.occurred(i):f.get.occurred("bottomVisible")&&f.execute(n,i),e===t?!o.bottomVisible:void 0},topPassedReverse:function(e){var o=f.get.elementCalculations(),n=e||g.onTopPassedReverse,i="topPassedReverse";return e&&(f.debug("Adding callback for top passed reverse",e),g.onTopPassedReverse=e),o.topPassed?g.once||f.remove.occurred(i):f.get.occurred("topPassed")&&f.execute(n,i),e===t?!o.onTopPassed:void 0},bottomPassedReverse:function(e){var o=f.get.elementCalculations(),n=e||g.onBottomPassedReverse,i="bottomPassedReverse";return e&&(f.debug("Adding callback for bottom passed reverse",e),g.onBottomPassedReverse=e),o.bottomPassed?g.once||f.remove.occurred(i):f.get.occurred("bottomPassed")&&f.execute(n,i),e===t?!o.bottomPassed:void 0},execute:function(e,o){var n=f.get.elementCalculations(),t=f.get.screenCalculations();e=e||!1,e&&(g.continuous?(f.debug("Callback being called continuously",o,n),e.call(R,n,t)):f.get.occurred(o)||(f.debug("Conditions met",o,n),e.call(R,n,t))),f.save.occurred(o)},remove:{occurred:function(e){e?f.cache.occurred[e]!==t&&f.cache.occurred[e]===!0&&(f.debug("Callback can now be called again",e),f.cache.occurred[e]=!1):f.cache.occurred={}}},save:{calculations:function(){f.verbose("Saving all calculations necessary to determine positioning"),f.save.direction(),f.save.screenCalculations(),f.save.elementCalculations()},occurred:function(e){e&&(f.cache.occurred[e]===t||f.cache.occurred[e]!==!0)&&(f.verbose("Saving callback occurred",e),f.cache.occurred[e]=!0)},scroll:function(e){e=e+g.offset||y.scrollTop()+g.offset,f.cache.scroll=e},direction:function(){var e,o=f.get.scroll(),n=f.get.lastScroll();return e=o>n&&n?"down":n>o&&n?"up":"static",f.cache.direction=e,f.cache.direction},elementPosition:function(){var e=f.cache.element,o=f.get.screenSize();return f.verbose("Saving element position"),e.fits=e.height<o.height,e.offset=V.offset(),e.width=V.outerWidth(),e.height=V.outerHeight(),f.cache.element=e,e},elementCalculations:function(){var e=f.get.screenCalculations(),o=f.get.elementPosition();return g.includeMargin?(o.margin={},o.margin.top=parseInt(V.css("margin-top"),10),o.margin.bottom=parseInt(V.css("margin-bottom"),10),o.top=o.offset.top-o.margin.top,o.bottom=o.offset.top+o.height+o.margin.bottom):(o.top=o.offset.top,o.bottom=o.offset.top+o.height),o.topVisible=e.bottom>=o.top,o.topPassed=e.top>=o.top,o.bottomVisible=e.bottom>=o.bottom,o.bottomPassed=e.top>=o.bottom,o.pixelsPassed=0,o.percentagePassed=0,o.onScreen=o.topVisible&&!o.bottomPassed,o.passing=o.topPassed&&!o.bottomPassed,o.offScreen=!o.onScreen,o.passing&&(o.pixelsPassed=e.top-o.top,o.percentagePassed=(e.top-o.top)/o.height),f.cache.element=o,f.verbose("Updated element calculations",o),o},screenCalculations:function(){var e=f.get.scroll();return f.save.direction(),f.cache.screen.top=e,f.cache.screen.bottom=e+f.cache.screen.height,f.cache.screen},screenSize:function(){f.verbose("Saving window position"),f.cache.screen={height:y.height()}},position:function(){f.save.screenSize(),f.save.elementPosition()}},get:{pixelsPassed:function(e){var o=f.get.elementCalculations();return e.search("%")>-1?o.height*(parseInt(e,10)/100):parseInt(e,10)},occurred:function(e){return f.cache.occurred!==t?f.cache.occurred[e]||!1:!1},direction:function(){return f.cache.direction===t&&f.save.direction(),f.cache.direction},elementPosition:function(){return f.cache.element===t&&f.save.elementPosition(),f.cache.element},elementCalculations:function(){return f.cache.element===t&&f.save.elementCalculations(),f.cache.element},screenCalculations:function(){return f.cache.screen===t&&f.save.screenCalculations(),f.cache.screen},screenSize:function(){return f.cache.screen===t&&f.save.screenSize(),f.cache.screen},scroll:function(){return f.cache.scroll===t&&f.save.scroll(),f.cache.scroll},lastScroll:function(){return f.cache.screen===t?(f.debug("First scroll event, no last scroll could be found"),!1):f.cache.screen.top}},setting:function(o,n){if(e.isPlainObject(o))e.extend(!0,g,o);else{if(n===t)return g[o];g[o]=n}},internal:function(o,n){if(e.isPlainObject(o))e.extend(!0,f,o);else{if(n===t)return f[o];f[o]=n}},debug:function(){g.debug&&(g.performance?f.performance.log(arguments):(f.debug=Function.prototype.bind.call(console.info,console,g.name+":"),f.debug.apply(console,arguments)))},verbose:function(){g.verbose&&g.debug&&(g.performance?f.performance.log(arguments):(f.verbose=Function.prototype.bind.call(console.info,console,g.name+":"),f.verbose.apply(console,arguments)))},error:function(){f.error=Function.prototype.bind.call(console.error,console,g.name+":"),f.error.apply(console,arguments)},performance:{log:function(e){var o,n,t;g.performance&&(o=(new Date).getTime(),t=a||o,n=o-t,a=o,l.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:R,"Execution Time":n})),clearTimeout(f.performance.timer),f.performance.timer=setTimeout(f.performance.display,500)},display:function(){var o=g.name+":",n=0;a=!1,clearTimeout(f.performance.timer),e.each(l,function(e,o){n+=o["Execution Time"]}),o+=" "+n+"ms",r&&(o+=" '"+r+"'"),(console.group!==t||console.table!==t)&&l.length>0&&(console.groupCollapsed(o),console.table?console.table(l):e.each(l,function(e,o){console.log(o.Name+": "+o["Execution Time"]+"ms")}),console.groupEnd()),l=[]}},invoke:function(o,n,i){var c,r,a,l=x;return n=n||m,i=R||i,"string"==typeof o&&l!==t&&(o=o.split(/[\. ]/),c=o.length-1,e.each(o,function(n,i){var s=n!=c?i+o[n+1].charAt(0).toUpperCase()+o[n+1].slice(1):o;if(e.isPlainObject(l[s])&&n!=c)l=l[s];else{if(l[s]!==t)return r=l[s],!1;if(!e.isPlainObject(l[i])||n==c)return l[i]!==t?(r=l[i],!1):(f.error(p.method,o),!1);l=l[i]}})),e.isFunction(r)?a=r.apply(i,n):r!==t&&(a=r),e.isArray(s)?s.push(a):s!==t?s=[s,a]:a!==t&&(s=a),r}},d?(x===t&&f.initialize(),x.save.scroll(),x.save.calculations(),f.invoke(u)):(x!==t&&x.invoke("destroy"),f.initialize())}),s!==t?s:this},e.fn.visibility.settings={name:"Visibility",namespace:"visibility",debug:!1,verbose:!1,performance:!0,observeChanges:!0,refreshOnLoad:!0,once:!0,continuous:!1,offset:0,includeMargin:!1,context:o,initialCheck:!0,throttle:!1,type:!1,transition:!1,duration:1e3,onPassed:{},onOnScreen:!1,onOffScreen:!1,onPassing:!1,onTopVisible:!1,onBottomVisible:!1,onTopPassed:!1,onBottomPassed:!1,onPassingReverse:!1,onTopVisibleReverse:!1,onBottomVisibleReverse:!1,onTopPassedReverse:!1,onBottomPassedReverse:!1,onUpdate:!1,onRefresh:function(){},className:{fixed:"fixed"},error:{method:"The method you called is not defined.",visible:"Element is hidden, you must call refresh after element becomes visible"}}}(jQuery,window,document);