'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _class, _temp; /**
                    * Created by baoyinghai on 4/18/17.
                    */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _IconHelper = require('../../framework/utils/IconHelper');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var BackTop = (_temp = _class = function (_React$Component) {
  _inherits(BackTop, _React$Component);

  function BackTop(props) {
    _classCallCheck(this, BackTop);

    var _this = _possibleConstructorReturn(this, (BackTop.__proto__ || Object.getPrototypeOf(BackTop)).call(this, props));

    _this._goToTop = function (e) {
      if (window.scrollTo) {
        window.scrollTo({ behavior: 'smooth', top: 0 });
      }
    };

    _this._showButton = function (e) {
      if (window.scrollY > _this.props.offset) {
        var ele = document.getElementById(_this.refStr);
        if (ele && !ele.style.display) {
          ele.style.display = 'block';
        }
      } else {
        var _ele = document.getElementById(_this.refStr);
        if (_ele && _ele.style.display) {
          _ele.style.display = '';
        }
      }
    };

    _this.refStr = 'back-to-top' + new Date().getTime();
    return _this;
  }

  _createClass(BackTop, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      window.addEventListener('scroll', this._showButton);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.addEventListener('scroll', this._showButton);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          _props$className = _props.className,
          className = _props$className === undefined ? '' : _props$className,
          _props$style = _props.style,
          style = _props$style === undefined ? {} : _props$style,
          _props$size = _props.size,
          size = _props$size === undefined ? 36 : _props$size;

      return _react2.default.createElement(
        'div',
        { id: this.refStr, className: 'back-to-top ' + className, onClick: this._goToTop },
        (0, _IconHelper.iconWrapper)('icon-icobacktop', undefined, undefined, undefined, Object.assign({}, style, { fontSize: size + 'px' }))
      );
    }
  }]);

  return BackTop;
}(_react2.default.Component), _class.defaultProps = {
  offset: 300
}, _temp);
exports.default = BackTop;