UNPKG

@bigfishtv/cockpit

Version:

103 lines (76 loc) 4.37 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _class, _temp2; var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _scrollUtils = require('../../utils/scrollUtils'); var scrollUtils = _interopRequireWildcard(_scrollUtils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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; } /** * Wrapper for a section that determines whether or not it should update based on if it's visible * This makes rendering less expensive on huge complex pages with many sections */ var Section = (_temp2 = _class = function (_Component) { _inherits(Section, _Component); function Section() { var _temp, _this, _ret; _classCallCheck(this, Section); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.el = null, _this.state = { inView: true }, _this.handleScroll = function (scroll, windowHeight) { var position = _this.el.getBoundingClientRect(); var inView = position.bottom > 0 && position.top < windowHeight; if (inView !== _this.state.inView) { _this.setState({ inView: inView }); } }, _temp), _possibleConstructorReturn(_this, _ret); } Section.prototype.componentDidMount = function componentDidMount() { scrollUtils.addListener(this.handleScroll); }; Section.prototype.componentWillUnmount = function componentWillUnmount() { scrollUtils.removeListener(this.handleScroll); }; Section.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) { // if (!this.state.inView && nextState.inView) console.log(this.props.sectionProperty + ' now in view') // if (this.state.inView && !nextState.inView) console.log(this.props.sectionProperty + ' out of view') return nextState.inView; }; Section.prototype.render = function render() { var _this2 = this; var _props = this.props, Component = _props.Component, props = _objectWithoutProperties(_props, ['Component']); return _react2.default.createElement( 'div', { ref: function ref(el) { return _this2.el = el; } }, _react2.default.createElement(Component, props) ); }; return Section; }(_react.Component), _class.propTypes = { /** Section component */ Component: _propTypes2.default.func.isRequired, /** Component icon */ icon: _propTypes2.default.string, /** Section property, e.g. hero_sections */ sectionProperty: _propTypes2.default.string, collapsed: _propTypes2.default.bool, isNew: _propTypes2.default.bool, onToggleCollapsed: _propTypes2.default.func, onDuplicate: _propTypes2.default.func, onRemove: _propTypes2.default.func, onReorder: _propTypes2.default.func }, _temp2); exports.default = Section;