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 2.0.0 - 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=[],d=arguments[0],u="string"==typeof d,f=[].slice.call(arguments,1);return c.each(function(){var c,m,g,b=e.isPlainObject(i)?e.extend(!0,{},e.fn.visibility.settings,i):e.extend({},e.fn.visibility.settings),p=b.className,v=b.namespace,h=b.error,P=b.metadata,x="."+v,C="module-"+v,y=e(o),V=e(this),S=e(b.context),R=(V.selector||"",V.data(C)),k=o.requestAnimationFrame||o.mozRequestAnimationFrame||o.webkitRequestAnimationFrame||o.msRequestAnimationFrame||function(e){setTimeout(e,0)},T=this;g={initialize:function(){g.debug("Initializing",b),g.setup.cache(),g.should.trackChanges()&&("image"==b.type&&g.setup.image(),"fixed"==b.type&&g.setup.fixed(),b.observeChanges&&g.observeChanges(),g.bind.events()),g.save.position(),g.is.visible()||g.error(h.visible,V),b.initialCheck&&g.checkVisibility(),g.instantiate()},instantiate:function(){g.debug("Storing instance",g),V.data(C,g),R=g},destroy:function(){g.verbose("Destroying previous module"),m&&m.disconnect(),y.off("load"+x,g.event.load).off("resize"+x,g.event.resize),S.off("scrollchange"+x,g.event.scrollchange),V.off(x).removeData(C)},observeChanges:function(){S[0];"MutationObserver"in o&&(m=new MutationObserver(function(e){g.verbose("DOM tree modified, updating visibility calculations"),g.timer=setTimeout(function(){g.verbose("DOM tree modified, updating sticky menu"),g.refresh()},100)}),m.observe(T,{childList:!0,subtree:!0}),g.debug("Setting up mutation observer",m))},bind:{events:function(){g.verbose("Binding visibility events to scroll and resize"),b.refreshOnLoad&&y.on("load"+x,g.event.load),y.on("resize"+x,g.event.resize),S.off("scroll"+x).on("scroll"+x,g.event.scroll).on("scrollchange"+x,g.event.scrollchange)}},event:{resize:function(){g.debug("Window resized"),k(g.refresh)},load:function(){g.debug("Page finished loading"),k(g.refresh)},scroll:function(){b.throttle?(clearTimeout(g.timer),g.timer=setTimeout(function(){S.triggerHandler("scrollchange"+x,[S.scrollTop()])},b.throttle)):k(function(){S.triggerHandler("scrollchange"+x,[S.scrollTop()])})},scrollchange:function(e,o){g.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 u?(g.debug("One time query, no need to bind events"),!1):(g.debug("Callbacks being attached"),!0)}},setup:{cache:function(){g.cache={occurred:{},screen:{},element:{}}},image:function(){var e=V.data(P.src);e&&(g.verbose("Lazy loading image",e),b.once=!0,b.observeChanges=!1,b.onOnScreen=function(){g.debug("Image on screen",T),g.precache(e,function(){g.set.image(e)})})},fixed:function(){g.debug("Setting up fixed"),b.once=!1,b.observeChanges=!1,b.initialCheck=!0,b.refreshOnLoad=!0,i.transition||(b.transition=!1),g.create.placeholder(),g.debug("Added placeholder",c),b.onTopPassed=function(){g.debug("Element passed, adding fixed position",V),g.show.placeholder(),g.set.fixed(),b.transition&&e.fn.transition!==t&&V.transition(b.transition,b.duration)},b.onTopPassedReverse=function(){g.debug("Element returned to position, removing fixed",V),g.hide.placeholder(),g.remove.fixed()}}},create:{placeholder:function(){g.verbose("Creating fixed position placeholder"),c=V.clone(!1).css("display","none").addClass(p.placeholder).insertAfter(V)}},show:{placeholder:function(){g.verbose("Showing placeholder"),c.css("display","block").css("visibility","hidden")}},hide:{placeholder:function(){g.verbose("Hiding placeholder"),c.css("display","none").css("visibility","")}},set:{fixed:function(){g.verbose("Setting element to fixed position"),V.addClass(p.fixed).css({position:"fixed",top:b.offset+"px",left:"auto",zIndex:"1"})},image:function(o){V.attr("src",o),b.transition?e.fn.transition!==t?V.transition(b.transition,b.duration):V.fadeIn(b.duration):V.show()}},is:{onScreen:function(){var e=g.get.elementCalculations();return e.onScreen},offScreen:function(){var e=g.get.elementCalculations();return e.offScreen},visible:function(){return g.cache&&g.cache.element?!(0===g.cache.element.width&&0===g.cache.element.offset.top):!1}},refresh:function(){g.debug("Refreshing constants (width/height)"),"fixed"==b.type&&(g.remove.fixed(),g.remove.occurred()),g.reset(),g.save.position(),g.checkVisibility(),b.onRefresh.call(T)},reset:function(){g.verbose("Reseting all cached values"),e.isPlainObject(g.cache)&&(g.cache.screen={},g.cache.element={})},checkVisibility:function(e){g.verbose("Checking visibility of element",g.cache.element),g.is.visible()&&(g.save.scroll(e),g.save.calculations(),g.passed(),g.passingReverse(),g.topVisibleReverse(),g.bottomVisibleReverse(),g.topPassedReverse(),g.bottomPassedReverse(),g.onScreen(),g.offScreen(),g.passing(),g.topVisible(),g.bottomVisible(),g.topPassed(),g.bottomPassed(),b.onUpdate&&b.onUpdate.call(T,g.get.elementCalculations()))},passed:function(o,n){var i=g.get.elementCalculations();if(o&&n)b.onPassed[o]=n;else{if(o!==t)return g.get.pixelsPassed(o)>i.pixelsPassed;i.passing&&e.each(b.onPassed,function(e,o){i.bottomVisible||i.pixelsPassed>g.get.pixelsPassed(e)?g.execute(o,e):b.once||g.remove.occurred(o)})}},onScreen:function(e){var o=g.get.elementCalculations(),n=e||b.onOnScreen,i="onScreen";return e&&(g.debug("Adding callback for onScreen",e),b.onOnScreen=e),o.onScreen?g.execute(n,i):b.once||g.remove.occurred(i),e!==t?o.onOnScreen:void 0},offScreen:function(e){var o=g.get.elementCalculations(),n=e||b.onOffScreen,i="offScreen";return e&&(g.debug("Adding callback for offScreen",e),b.onOffScreen=e),o.offScreen?g.execute(n,i):b.once||g.remove.occurred(i),e!==t?o.onOffScreen:void 0},passing:function(e){var o=g.get.elementCalculations(),n=e||b.onPassing,i="passing";return e&&(g.debug("Adding callback for passing",e),b.onPassing=e),o.passing?g.execute(n,i):b.once||g.remove.occurred(i),e!==t?o.passing:void 0},topVisible:function(e){var o=g.get.elementCalculations(),n=e||b.onTopVisible,i="topVisible";return e&&(g.debug("Adding callback for top visible",e),b.onTopVisible=e),o.topVisible?g.execute(n,i):b.once||g.remove.occurred(i),e===t?o.topVisible:void 0},bottomVisible:function(e){var o=g.get.elementCalculations(),n=e||b.onBottomVisible,i="bottomVisible";return e&&(g.debug("Adding callback for bottom visible",e),b.onBottomVisible=e),o.bottomVisible?g.execute(n,i):b.once||g.remove.occurred(i),e===t?o.bottomVisible:void 0},topPassed:function(e){var o=g.get.elementCalculations(),n=e||b.onTopPassed,i="topPassed";return e&&(g.debug("Adding callback for top passed",e),b.onTopPassed=e),o.topPassed?g.execute(n,i):b.once||g.remove.occurred(i),e===t?o.topPassed:void 0},bottomPassed:function(e){var o=g.get.elementCalculations(),n=e||b.onBottomPassed,i="bottomPassed";return e&&(g.debug("Adding callback for bottom passed",e),b.onBottomPassed=e),o.bottomPassed?g.execute(n,i):b.once||g.remove.occurred(i),e===t?o.bottomPassed:void 0},passingReverse:function(e){var o=g.get.elementCalculations(),n=e||b.onPassingReverse,i="passingReverse";return e&&(g.debug("Adding callback for passing reverse",e),b.onPassingReverse=e),o.passing?b.once||g.remove.occurred(i):g.get.occurred("passing")&&g.execute(n,i),e!==t?!o.passing:void 0},topVisibleReverse:function(e){var o=g.get.elementCalculations(),n=e||b.onTopVisibleReverse,i="topVisibleReverse";return e&&(g.debug("Adding callback for top visible reverse",e),b.onTopVisibleReverse=e),o.topVisible?b.once||g.remove.occurred(i):g.get.occurred("topVisible")&&g.execute(n,i),e===t?!o.topVisible:void 0},bottomVisibleReverse:function(e){var o=g.get.elementCalculations(),n=e||b.onBottomVisibleReverse,i="bottomVisibleReverse";return e&&(g.debug("Adding callback for bottom visible reverse",e),b.onBottomVisibleReverse=e),o.bottomVisible?b.once||g.remove.occurred(i):g.get.occurred("bottomVisible")&&g.execute(n,i),e===t?!o.bottomVisible:void 0},topPassedReverse:function(e){var o=g.get.elementCalculations(),n=e||b.onTopPassedReverse,i="topPassedReverse";return e&&(g.debug("Adding callback for top passed reverse",e),b.onTopPassedReverse=e),o.topPassed?b.once||g.remove.occurred(i):g.get.occurred("topPassed")&&g.execute(n,i),e===t?!o.onTopPassed:void 0},bottomPassedReverse:function(e){var o=g.get.elementCalculations(),n=e||b.onBottomPassedReverse,i="bottomPassedReverse";return e&&(g.debug("Adding callback for bottom passed reverse",e),b.onBottomPassedReverse=e),o.bottomPassed?b.once||g.remove.occurred(i):g.get.occurred("bottomPassed")&&g.execute(n,i),e===t?!o.bottomPassed:void 0},execute:function(e,o){var n=g.get.elementCalculations(),t=g.get.screenCalculations();e=e||!1,e&&(b.continuous?(g.debug("Callback being called continuously",o,n),e.call(T,n,t)):g.get.occurred(o)||(g.debug("Conditions met",o,n),e.call(T,n,t))),g.save.occurred(o)},remove:{fixed:function(){g.debug("Removing fixed position"),V.removeClass(p.fixed).css({position:"",top:"",left:"",zIndex:""})},occurred:function(e){if(e){var o=g.cache.occurred;o[e]!==t&&o[e]===!0&&(g.debug("Callback can now be called again",e),g.cache.occurred[e]=!1)}else g.cache.occurred={}}},save:{calculations:function(){g.verbose("Saving all calculations necessary to determine positioning"),g.save.direction(),g.save.screenCalculations(),g.save.elementCalculations()},occurred:function(e){e&&(g.cache.occurred[e]===t||g.cache.occurred[e]!==!0)&&(g.verbose("Saving callback occurred",e),g.cache.occurred[e]=!0)},scroll:function(e){e=e+b.offset||S.scrollTop()+b.offset,g.cache.scroll=e},direction:function(){var e,o=g.get.scroll(),n=g.get.lastScroll();return e=o>n&&n?"down":n>o&&n?"up":"static",g.cache.direction=e,g.cache.direction},elementPosition:function(){var e=g.cache.element,o=g.get.screenSize();return g.verbose("Saving element position"),e.fits=e.height<o.height,e.offset=V.offset(),e.width=V.outerWidth(),e.height=V.outerHeight(),g.cache.element=e,e},elementCalculations:function(){var e=g.get.screenCalculations(),o=g.get.elementPosition();return b.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),g.cache.element=o,g.verbose("Updated element calculations",o),o},screenCalculations:function(){var e=g.get.scroll();return g.save.direction(),g.cache.screen.top=e,g.cache.screen.bottom=e+g.cache.screen.height,g.cache.screen},screenSize:function(){g.verbose("Saving window position"),g.cache.screen={height:S.height()}},position:function(){g.save.screenSize(),g.save.elementPosition()}},get:{pixelsPassed:function(e){var o=g.get.elementCalculations();return e.search("%")>-1?o.height*(parseInt(e,10)/100):parseInt(e,10)},occurred:function(e){return g.cache.occurred!==t?g.cache.occurred[e]||!1:!1},direction:function(){return g.cache.direction===t&&g.save.direction(),g.cache.direction},elementPosition:function(){return g.cache.element===t&&g.save.elementPosition(),g.cache.element},elementCalculations:function(){return g.cache.element===t&&g.save.elementCalculations(),g.cache.element},screenCalculations:function(){return g.cache.screen===t&&g.save.screenCalculations(),g.cache.screen},screenSize:function(){return g.cache.screen===t&&g.save.screenSize(),g.cache.screen},scroll:function(){return g.cache.scroll===t&&g.save.scroll(),g.cache.scroll},lastScroll:function(){return g.cache.screen===t?(g.debug("First scroll event, no last scroll could be found"),!1):g.cache.screen.top}},setting:function(o,n){if(e.isPlainObject(o))e.extend(!0,b,o);else{if(n===t)return b[o];b[o]=n}},internal:function(o,n){if(e.isPlainObject(o))e.extend(!0,g,o);else{if(n===t)return g[o];g[o]=n}},debug:function(){b.debug&&(b.performance?g.performance.log(arguments):(g.debug=Function.prototype.bind.call(console.info,console,b.name+":"),g.debug.apply(console,arguments)))},verbose:function(){b.verbose&&b.debug&&(b.performance?g.performance.log(arguments):(g.verbose=Function.prototype.bind.call(console.info,console,b.name+":"),g.verbose.apply(console,arguments)))},error:function(){g.error=Function.prototype.bind.call(console.error,console,b.name+":"),g.error.apply(console,arguments)},performance:{log:function(e){var o,n,t;b.performance&&(o=(new Date).getTime(),t=a||o,n=o-t,a=o,l.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:T,"Execution Time":n})),clearTimeout(g.performance.timer),g.performance.timer=setTimeout(g.performance.display,500)},display:function(){var o=b.name+":",n=0;a=!1,clearTimeout(g.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=R;return n=n||f,i=T||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):(g.error(h.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}},u?(R===t&&g.initialize(),R.save.scroll(),R.save.calculations(),g.invoke(d)):(R!==t&&R.invoke("destroy"),g.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:"fade in",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(){},metadata:{src:"src"},className:{fixed:"fixed",placeholder:"placeholder"},error:{method:"The method you called is not defined.",visible:"Element is hidden, you must call refresh after element becomes visible"}}}(jQuery,window,document);