UNPKG

twreporter-react

Version:

React-Redux site for The Reporter Foundation in Taiwan

7 lines (5 loc) 4.07 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Sticky = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ var React=require("react"),ReactDOM=require("react-dom"),Sticky=React.createClass({statics:{__frame:null,__instances:[],register:function(t){Sticky.__instances.push(t),Sticky.__instances.sort(function(t,e){return t.top()>e.top()?1:e.top()>t.top()?-1:0}),null===Sticky.__frame&&Sticky.resumeLoop()},unregister:function(t){var e=Sticky.__instances.indexOf(t);e>-1&&Sticky.__instances.splice(e,1),0===Sticky.__instances.length&&Sticky.cancelLoop()},instancesAbove:function(t){return Sticky.__instances.slice(0,Sticky.__instances.indexOf(t))},isModernBrowser:function(){return window&&window.requestAnimationFrame&&window.cancelAnimationFrame},resumeLoop:function(){var t=Sticky.isModernBrowser()?requestAnimationFrame:setTimeout;Sticky.__frame=t(Sticky.onFrame,1e3/60)},cancelLoop:function(){var t=Sticky.isModernBrowser()?cancelAnimationFrame:clearTimeout;t(Sticky.__frame),Sticky.__frame=null},onFrame:function(){for(var t=0;t<Sticky.__instances.length;t++){var e=Sticky.__instances[t];e.handleFrame()}Sticky.resumeLoop()}},getDefaultProps:function(){return{type:React.DOM.div,className:"",style:{},stickyClass:"sticky",stickyStyle:{position:"fixed",top:0,left:0,right:0,zIndex:1},topOffset:0,onStickyStateChange:function(){}}},getInitialState:function(){return{events:["scroll","resize","touchmove","touchend"]}},pageOffset:function(){for(var t=Sticky.instancesAbove(this),e=0,n=0;n<t.length;n++){var i=t[n];i.state.isSticky&&(e+=i.domNode.getBoundingClientRect().height)}return(window.pageYOffset||document.documentElement.scrollTop)+e},top:function(){return this.domNode.getBoundingClientRect().top},shouldBeSticky:function(){return this.pageOffset()>=this.origin+this.props.topOffset},handleFrame:function(){if(this.hasUnhandledEvent||this.hasTouchEvent){var t=this.shouldBeSticky();this.nextState(t),this.hasUnhandledEvent=!1}},handleEvent:function(t){switch(t.type){case"touchmove":this.hasTouchEvent=!0;break;case"touchend":this.hasTouchEvent=!1;break;default:this.hasUnhandledEvent=!0}},componentDidMount:function(){this.state.events.forEach(function(t){window.addEventListener?window.addEventListener(t,this.handleEvent):window.attachEvent("on"+t,this.handleEvent)},this),this.domNode=ReactDOM.findDOMNode(this),this.origin=this.top()+this.pageOffset(),this.hasUnhandledEvent=!0,Sticky.register(this)},componentWillUnmount:function(){this.state.events.forEach(function(t){window.removeEventListener?window.removeEventListener(t,this.handleEvent):window.detachEvent("on"+t,this.handleEvent)},this),this.domNode=null,Sticky.unregister(this)},nextStyle:function(t){if(t){var e=function(t,e){for(var n in e)t[n]=e[n];return t};return e(e({},this.props.style),this.props.stickyStyle)}return this.props.style},nextClassName:function(t){var e=this.props.className;return t&&(e+=" "+this.props.stickyClass),e},nextState:function(t){var e=this.state.isSticky!==t;this.setState({isSticky:t,style:this.nextStyle(t),className:this.nextClassName(t)}),e&&this.props.onStickyStateChange(t)},render:function(){return this.props.type({style:this.state.style,className:this.state.className},this.props.children)}});module.exports=Sticky; },{"react":undefined,"react-dom":undefined}]},{},[1])(1) }); //# sourceMappingURL=sticky.js.map