wishthis/node_modules/fomantic-ui/dist/components/sticky.min.js

11 lines
No EOL
12 KiB
JavaScript

/*
* # Fomantic UI - 2.9.3
* https://github.com/fomantic/Fomantic-UI
* https://fomantic-ui.com/
*
* Copyright 2023 Contributors
* Released under the MIT license
* https://opensource.org/licenses/MIT
*
*/
!function(z,T,w){"use strict";T=void 0!==T&&T.Math===Math?T:globalThis,z.fn.sticky=function(p){var b,e=z(this),v=(z(w),Date.now()),x=[],C=p,S="string"==typeof C,y=[].slice.call(arguments,1),k=function(e,t){var o;return 0<=[T,w].indexOf(e)?o=z(e):0===(o=z(t.document).find(e)).length&&(o=t.frameElement?k(e,t.parent):T),o};return e.each(function(){var t,n,e,o,c=z.isPlainObject(p)?z.extend(!0,{},z.fn.sticky.settings,p):z.extend({},z.fn.sticky.settings),i=c.className,s=c.namespace,l=c.error,r="."+s,a="module-"+s,f=z(this),m=z(T),d=k(c.scrollContext,T),u=f.data(a),h=this,g={initialize:function(){g.determineContainer(),g.determineContext(),g.verbose("Initializing sticky",c,t),g.save.positions(),g.checkErrors(),g.bind.events(),c.observeChanges&&g.observeChanges(),g.instantiate()},instantiate:function(){g.verbose("Storing instance of module",g),u=g,f.data(a,g)},destroy:function(){g.verbose("Destroying previous instance"),g.reset(),e&&e.disconnect(),o&&o.disconnect(),m.off("load"+r,g.event.load).off("resize"+r,g.event.resize),d.off("scrollchange"+r,g.event.scrollchange),f.removeData(a)},observeChanges:function(){"MutationObserver"in T&&(e=new MutationObserver(g.event.documentChanged),o=new MutationObserver(g.event.changed),e.observe(w,{childList:!0,subtree:!0}),o.observe(h,{childList:!0,subtree:!0}),o.observe(n[0],{childList:!0,subtree:!0}),g.debug("Setting up mutation observer",o))},determineContainer:function(){t=c.container?k(c.container,T):f.offsetParent()},determineContext:function(){0===(n=c.context?k(c.context,T):t).length&&g.error(l.invalidContext,c.context,f)},checkErrors:function(){g.is.hidden()&&g.error(l.visible,f),g.cache.element.height>g.cache.context.height&&(g.reset(),g.error(l.elementSize,f))},bind:{events:function(){m.on("load"+r,g.event.load).on("resize"+r,g.event.resize),d.off("scroll"+r).on("scroll"+r,g.event.scroll).on("scrollchange"+r,g.event.scrollchange)}},event:{changed:function(e){clearTimeout(g.timer),g.timer=setTimeout(function(){g.verbose("DOM tree modified, updating sticky menu",e),g.refresh()},100)},documentChanged:function(e){[].forEach.call(e,function(e){e.removedNodes&&[].forEach.call(e.removedNodes,function(e){(e===h||0<z(e).find(h).length)&&(g.debug("Element removed from DOM, tearing down events"),g.destroy())})})},load:function(){g.verbose("Page contents finished loading"),requestAnimationFrame(g.refresh)},resize:function(){g.verbose("Window resized"),requestAnimationFrame(g.refresh)},scroll:function(){requestAnimationFrame(function(){d.triggerHandler("scrollchange"+r,d.scrollTop())})},scrollchange:function(e,t){g.stick(t),c.onScroll.call(h)}},refresh:function(e){g.reset(),c.context||g.determineContext(),e&&g.determineContainer(),g.save.positions(),g.stick(),c.onReposition.call(h)},supports:{sticky:function(){var e=z("<div/>");return e.addClass(i.supported),e.css("position").match("sticky")}},save:{lastScroll:function(e){g.lastScroll=e},elementScroll:function(e){g.elementScroll=e},positions:function(){var e={height:d.height()},t={margin:{top:parseInt(f.css("margin-top"),10),bottom:parseInt(f.css("margin-bottom"),10)},offset:f.offset(),width:f.outerWidth(),height:f.outerHeight()},o={offset:n.offset(),height:n.outerHeight()};g.is.standardScroll()||(g.debug("Non-standard scroll. Removing scroll offset from element offset"),e.top=d.scrollTop(),e.left=d.scrollLeft(),t.offset.top+=e.top,o.offset.top+=e.top,t.offset.left+=e.left,o.offset.left+=e.left),g.cache={fits:t.height+c.offset<=e.height,sameHeight:t.height===o.height,scrollContext:{height:e.height},element:{margin:t.margin,top:t.offset.top-t.margin.top,left:t.offset.left,width:t.width,height:t.height,bottom:t.offset.top+t.height},context:{top:o.offset.top,height:o.height,bottom:o.offset.top+o.height}},g.set.containerSize(),g.stick(),g.debug("Caching element positions",g.cache)}},get:{direction:function(e){var t="down";return e=e||d.scrollTop(),t=g.lastScroll&&g.lastScroll>e?"up":t},scrollChange:function(e){return e=e||d.scrollTop(),g.lastScroll?e-g.lastScroll:0},currentElementScroll:function(){return g.elementScroll||(g.is.top()?Math.abs(parseInt(f.css("top"),10))||0:Math.abs(parseInt(f.css("bottom"),10))||0)},elementScroll:function(e){e=e||d.scrollTop();var t=g.cache.element,o=g.cache.scrollContext,e=g.get.scrollChange(e),t=t.height-o.height+c.offset,o=g.get.currentElementScroll(),e=o+e;return o=g.cache.fits||e<0?0:t<e?t:e}},remove:{lastScroll:function(){delete g.lastScroll},elementScroll:function(){delete g.elementScroll},minimumSize:function(){t.css("min-height","")},offset:function(){f.css("margin-top","")}},set:{offset:function(){g.verbose("Setting offset on element",c.offset),f.css("margin-top",c.offset)},containerSize:function(){var e=t[0].tagName;"HTML"===e||"body"===e?g.determineContainer():((e=Math.max(g.cache.context.height,g.cache.element.height))-t.outerHeight()>c.jitter?(g.debug("Context is taller than container. Specifying exact height for container",g.cache.context.height),t.css({height:e})):t.css({height:""}),Math.abs(t.outerHeight()-g.cache.context.height)>c.jitter&&(g.debug("Context has padding, specifying exact height for container",g.cache.context.height),t.css({height:g.cache.context.height})))},minimumSize:function(){var e=g.cache.element;t.css("min-height",e.height)},scroll:function(e){g.debug("Setting scroll on element",e),g.elementScroll!==e&&(g.is.top()&&f.css("bottom","").css("top",-e+"px"),g.is.bottom()&&f.css("top","").css("bottom",e+"px"))},size:function(){0!==g.cache.element.height&&0!==g.cache.element.width&&(h.style.setProperty("width",g.cache.element.width+"px","important"),h.style.setProperty("height",g.cache.element.height+"px","important"))}},is:{standardScroll:function(){return d[0]===T},top:function(){return f.hasClass(i.top)},bottom:function(){return f.hasClass(i.bottom)},initialPosition:function(){return!g.is.fixed()&&!g.is.bound()},hidden:function(){return!f.is(":visible")},bound:function(){return f.hasClass(i.bound)},fixed:function(){return f.hasClass(i.fixed)}},stick:function(e){var e=e||d.scrollTop(),t=g.cache,o=t.fits,n=t.sameHeight,i=t.element,s=t.scrollContext,t=t.context,r=g.is.bottom()&&c.pushing?c.bottomOffset:c.offset,e={top:e+r,bottom:e+r+s.height},r=o?0:g.get.elementScroll(e.top),s=!o;0===i.height||n||(g.is.initialPosition()?e.top>=t.bottom?(g.debug("Initial element position is bottom of container"),g.bindBottom()):e.top>i.top&&(i.height+e.top-r>=t.bottom&&i.height<t.height?(g.debug("Initial element position is bottom of container"),g.bindBottom()):(g.debug("Initial element position is fixed"),g.fixTop())):g.is.fixed()?g.is.top()?e.top<=i.top?(g.debug("Fixed element reached top of container"),g.setInitialPosition()):i.height+e.top-r>=t.bottom?(g.debug("Fixed element reached bottom of container"),g.bindBottom()):s&&(g.set.scroll(r),g.save.lastScroll(e.top),g.save.elementScroll(r)):g.is.bottom()&&(e.bottom-i.height<=i.top?(g.debug("Bottom fixed rail has reached top of container"),g.setInitialPosition()):e.bottom>=t.bottom?(g.debug("Bottom fixed rail has reached bottom of container"),g.bindBottom()):s&&(g.set.scroll(r),g.save.lastScroll(e.top),g.save.elementScroll(r))):g.is.bottom()&&(e.top<=i.top?(g.debug("Jumped from bottom fixed to top fixed, most likely used home/end button"),g.setInitialPosition()):c.pushing?g.is.bound()&&e.bottom<=t.bottom&&(g.debug("Fixing bottom attached element to bottom of browser."),g.fixBottom()):g.is.bound()&&e.top<=t.bottom-i.height&&(g.debug("Fixing bottom attached element to top of browser."),g.fixTop())))},bindTop:function(){g.debug("Binding element to top of parent container"),g.remove.offset(),c.setSize&&g.set.size(),f.css({left:"",top:"",marginBottom:""}).removeClass(i.fixed).removeClass(i.bottom).addClass(i.bound).addClass(i.top),c.onTop.call(h),c.onUnstick.call(h)},bindBottom:function(){g.debug("Binding element to bottom of parent container"),g.remove.offset(),c.setSize&&g.set.size(),f.css({left:"",top:""}).removeClass(i.fixed).removeClass(i.top).addClass(i.bound).addClass(i.bottom),c.onBottom.call(h),c.onUnstick.call(h)},setInitialPosition:function(){g.debug("Returning to initial position"),g.unfix(),g.unbind()},fixTop:function(){g.debug("Fixing element to top of page"),c.setSize&&g.set.size(),g.set.minimumSize(),g.set.offset(),f.css({left:g.cache.element.left,bottom:"",marginBottom:""}).removeClass(i.bound).removeClass(i.bottom).addClass(i.fixed).addClass(i.top),c.onStick.call(h)},fixBottom:function(){g.debug("Sticking element to bottom of page"),c.setSize&&g.set.size(),g.set.minimumSize(),g.set.offset(),f.css({left:g.cache.element.left,bottom:"",marginBottom:""}).removeClass(i.bound).removeClass(i.top).addClass(i.fixed).addClass(i.bottom),c.onStick.call(h)},unbind:function(){g.is.bound()&&(g.debug("Removing container bound position on element"),g.remove.offset(),f.removeClass(i.bound).removeClass(i.top).removeClass(i.bottom))},unfix:function(){g.is.fixed()&&(g.debug("Removing fixed position on element"),g.remove.minimumSize(),g.remove.offset(),f.removeClass(i.fixed).removeClass(i.top).removeClass(i.bottom),c.onUnstick.call(h))},reset:function(){g.debug("Resetting elements position"),g.unbind(),g.unfix(),g.resetCSS(),g.remove.offset(),g.remove.lastScroll()},resetCSS:function(){f.css({width:"",height:""}),t.css({height:""})},setting:function(e,t){if(z.isPlainObject(e))z.extend(!0,c,e);else{if(void 0===t)return c[e];c[e]=t}},internal:function(e,t){if(z.isPlainObject(e))z.extend(!0,g,e);else{if(void 0===t)return g[e];g[e]=t}},debug:function(){!c.silent&&c.debug&&(c.performance?g.performance.log(arguments):(g.debug=Function.prototype.bind.call(console.info,console,c.name+":"),g.debug.apply(console,arguments)))},verbose:function(){!c.silent&&c.verbose&&c.debug&&(c.performance?g.performance.log(arguments):(g.verbose=Function.prototype.bind.call(console.info,console,c.name+":"),g.verbose.apply(console,arguments)))},error:function(){c.silent||(g.error=Function.prototype.bind.call(console.error,console,c.name+":"),g.error.apply(console,arguments))},performance:{log:function(e){var t,o;c.performance&&(o=(t=Date.now())-(v||t),v=t,x.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:h,"Execution Time":o})),clearTimeout(g.performance.timer),g.performance.timer=setTimeout(function(){g.performance.display()},0)},display:function(){var e=c.name+":",o=0;v=!1,clearTimeout(g.performance.timer),z.each(x,function(e,t){o+=t["Execution Time"]}),e+=" "+o+"ms",0<x.length&&(console.groupCollapsed(e),console.table?console.table(x):z.each(x,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),x=[]}},invoke:function(n,e,t){var i,s,o,r,c=u;return e=e||y,t=t||h,"string"==typeof n&&void 0!==c&&(n=n.split(/[ .]/),i=n.length-1,z.each(n,function(e,t){var o=e!==i?t+n[e+1].charAt(0).toUpperCase()+n[e+1].slice(1):n;if(z.isPlainObject(c[o])&&e!==i)c=c[o];else{if(void 0!==c[o])return s=c[o],!1;{if(!z.isPlainObject(c[t])||e===i)return void 0!==c[t]?s=c[t]:g.error(l.method,n),!1;c=c[t]}}})),"function"==typeof(r=s)&&"number"!=typeof r.nodeType?o=s.apply(t,e):void 0!==s&&(o=s),Array.isArray(b)?b.push(o):void 0!==b?b=[b,o]:void 0!==o&&(b=o),s}};S?(void 0===u&&g.initialize(),g.invoke(C)):(void 0!==u&&u.invoke("destroy"),g.initialize())}),void 0!==b?b:this},z.fn.sticky.settings={name:"Sticky",namespace:"sticky",silent:!1,debug:!1,verbose:!0,performance:!0,pushing:!1,context:!1,container:!1,scrollContext:T,offset:0,bottomOffset:0,jitter:5,setSize:!0,observeChanges:!1,onReposition:function(){},onScroll:function(){},onStick:function(){},onUnstick:function(){},onTop:function(){},onBottom:function(){},error:{visible:"Element is hidden, you must call refresh after element becomes visible. Use silent setting to suppress this warning in production.",method:"The method you called is not defined.",invalidContext:"Context specified does not exist",elementSize:"Sticky element is larger than its container, cannot create sticky."},className:{bound:"bound",fixed:"fixed",supported:"native",top:"top",bottom:"bottom"}}}(jQuery,window,document);