import _extends from"babel-runtime/helpers/extends";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _inherits from"babel-runtime/helpers/inherits";import*as React from"react";import*as ReactDOM from"react-dom";import*as PropTypes from"prop-types";import classNames from"classnames";import addEventListener from"rc-util/es/Dom/addEventListener";import Affix from"../affix";import getScroll from"../_util/getScroll";import getRequestAnimationFrame from"../_util/getRequestAnimationFrame";function getDefaultTarget(){return window}function getOffsetTop(e){if(!e)return 0;if(!e.getClientRects().length)return 0;var t=e.getBoundingClientRect();if(t.width||t.height){var n=e.ownerDocument.documentElement;return t.top-n.clientTop}return t.top}function easeInOutCubic(e,t,n,r){var i=n-t;return(e/=r/2)<1?i/2*e*e*e+t:i/2*((e-=2)*e*e+2)+t}var reqAnimFrame=getRequestAnimationFrame(),sharpMatcherRegx=/#([^#]+)$/;function scrollTo(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments[2],r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:function(){},i=getScroll(n(),!0),o=sharpMatcherRegx.exec(e);if(o){var a=document.getElementById(o[1]);if(a){var s=getOffsetTop(a),l=i+s-t,c=Date.now();reqAnimFrame(function e(){var t=Date.now()-c;window.scrollTo(window.pageXOffset,easeInOutCubic(t,i,l,450)),t<450?reqAnimFrame(e):r()}),history.pushState(null,"",e)}}}var Anchor=function(e){function t(e){_classCallCheck(this,t);var n=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return n.handleScroll=function(){if(!n.animating){var e=n.props,t=e.offsetTop,r=e.bounds;n.setState({activeLink:n.getCurrentAnchor(t,r)})}},n.handleScrollTo=function(e){var t=n.props,r=t.offsetTop,i=t.target,o=void 0===i?getDefaultTarget:i;n.animating=!0,n.setState({activeLink:e}),scrollTo(e,r,o,function(){n.animating=!1})},n.updateInk=function(){if("undefined"!=typeof document){var e=n.props.prefixCls,t=ReactDOM.findDOMNode(n).getElementsByClassName(e+"-link-title-active")[0];t&&(n.inkNode.style.top=t.offsetTop+t.clientHeight/2-4.5+"px")}},n.saveInkNode=function(e){n.inkNode=e},n.state={activeLink:null},n.links=[],n}return _inherits(t,e),_createClass(t,[{key:"getChildContext",value:function(){var e=this;return{antAnchor:{registerLink:function(t){e.links.includes(t)||e.links.push(t)},unregisterLink:function(t){var n=e.links.indexOf(t);-1!==n&&e.links.splice(n,1)},activeLink:this.state.activeLink,scrollTo:this.handleScrollTo}}}},{key:"componentDidMount",value:function(){var e=this.props.target||getDefaultTarget;this.scrollEvent=addEventListener(e(),"scroll",this.handleScroll),this.handleScroll()}},{key:"componentWillUnmount",value:function(){this.scrollEvent&&this.scrollEvent.remove()}},{key:"componentDidUpdate",value:function(){this.updateInk()}},{key:"getCurrentAnchor",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:5;if("undefined"==typeof document)return"";var n=[];return this.links.forEach(function(r){var i=sharpMatcherRegx.exec(r.toString());if(i){var o=document.getElementById(i[1]);if(o&&getOffsetTop(o)<e+t){var a=getOffsetTop(o);n.push({link:r,top:a})}}}),n.length?n.reduce(function(e,t){return t.top>e.top?t:e}).link:""}},{key:"render",value:function(){var e=this.props,t=e.prefixCls,n=e.className,r=void 0===n?"":n,i=e.style,o=e.offsetTop,a=e.affix,s=e.showInkInFixed,l=e.children,c=this.state.activeLink,f=classNames(t+"-ink-ball",{visible:c}),u=classNames(r,t+"-wrapper"),p=classNames(t,{fixed:!a&&!s}),m=_extends({maxHeight:o?"calc(100vh - "+o+"px)":"100vh"},i),h=React.createElement("div",{className:u,style:m},React.createElement("div",{className:p},React.createElement("div",{className:t+"-ink"},React.createElement("span",{className:f,ref:this.saveInkNode})),l));return a?React.createElement(Affix,{offsetTop:o},h):h}}]),t}(React.Component);export default Anchor;Anchor.defaultProps={prefixCls:"dt-anchor",affix:!0,showInkInFixed:!1},Anchor.childContextTypes={antAnchor:PropTypes.object};