all files / lib/affix/ index.js

41.41% Statements 41/99
19.72% Branches 14/71
37.5% Functions 6/16
32.86% Lines 23/70
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168    1×       3×   1×   1×   1×   1×   1×   1×   1×   3×   1×   1×   1×   1×   1×                             1×                           1× 1×   1×                                                                                                                         1×                                                                     1×     1×       1×
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
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; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _reactDom = require('react-dom');
 
var _reactDom2 = _interopRequireDefault(_reactDom);
 
var _rcUtil = require('rc-util');
 
var _classnames = require('classnames');
 
var _classnames2 = _interopRequireDefault(_classnames);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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) { Iif (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 } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
function getScroll(w, top) {
  var ret = w['page' + (top ? 'Y' : 'X') + 'Offset'];
  var method = 'scroll' + (top ? 'Top' : 'Left');
  if (typeof ret !== 'number') {
    var d = w.document;
    // ie6,7,8 standard mode
    ret = d.documentElement[method];
    if (typeof ret !== 'number') {
      // quirks mode
      ret = d.body[method];
    }
  }
  return ret;
}
 
function getOffset(element) {
  var rect = element.getBoundingClientRect();
  var body = document.body;
  var clientTop = element.clientTop || body.clientTop || 0;
  var clientLeft = element.clientLeft || body.clientLeft || 0;
  var scrollTop = getScroll(window, true);
  var scrollLeft = getScroll(window);
 
  return {
    top: rect.top + scrollTop - clientTop,
    left: rect.left + scrollLeft - clientLeft
  };
}
 
var Affix = function (_React$Component) {
  _inherits(Affix, _React$Component);
 
  function Affix(props) {
    _classCallCheck(this, Affix);
 
    var _this = _possibleConstructorReturn(this, (Affix.__proto__ || Object.getPrototypeOf(Affix)).call(this, props));
 
    _this.handleScroll = function () {
      var _this$props = _this.props,
          offsetTop = _this$props.offsetTop,
          offsetBottom = _this$props.offsetBottom;
 
      var scrollTop = getScroll(window, true);
      var elemOffset = getOffset(_reactDom2.default.findDOMNode(_this));
      var elemSize = {
        width: _reactDom2.default.findDOMNode(_this.refs.fixedNode).offsetWidth,
        height: _reactDom2.default.findDOMNode(_this.refs.fixedNode).offsetHeight
      };
 
      var offsetMode = {};
      if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') {
        offsetMode.top = true;
        offsetTop = 0;
      } else {
        offsetMode.top = typeof offsetTop === 'number';
        offsetMode.bottom = typeof offsetBottom === 'number';
      }
 
      if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
        // Fixed Top
        if (!_this.state.affixStyle) {
          _this.setState({
            affixStyle: {
              position: 'fixed',
              top: offsetTop,
              left: elemOffset.left
            }
          });
        }
      } else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - window.innerHeight && offsetMode.bottom) {
        // Fixed Bottom
        if (!_this.state.affixStyle) {
          _this.setState({
            affixStyle: {
              position: 'fixed',
              bottom: offsetBottom,
              left: elemOffset.left
            }
          });
        }
      } else if (_this.state.affixStyle) {
        _this.setState({
          affixStyle: null
        });
      }
    };
 
    _this.state = {
      affixStyle: null
    };
    return _this;
  }
 
  _createClass(Affix, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.scrollEvent = _rcUtil.Dom.addEventListener(window, 'scroll', this.handleScroll);
      this.resizeEvent = _rcUtil.Dom.addEventListener(window, 'resize', this.handleScroll);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      if (this.scrollEvent) {
        this.scrollEvent.remove();
      }
      if (this.resizeEvent) {
        this.resizeEvent.remove();
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _classNames;
 
      var className = (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, this.props.className, this.props.className), _defineProperty(_classNames, 'ant-affix', this.state.affixStyle), _classNames));
 
      return _react2.default.createElement(
        'div',
        this.props,
        _react2.default.createElement(
          'div',
          { className: className, ref: 'fixedNode', style: this.state.affixStyle },
          this.props.children
        )
      );
    }
  }]);
 
  return Affix;
}(_react2.default.Component);
 
Affix.propTypes = {
  offsetTop: _react2.default.PropTypes.number,
  offsetBottom: _react2.default.PropTypes.number
};
exports.default = Affix;