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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | 1× 6× 23× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 6× 6× 6× 6× 6× 6× 6× 6× 6× 1× 35× 35× 35× 35× 35× 35× 35× 35× 35× 35× 35× 35× 35× 1× 41× 41× 41× 41× 41× 41× 24× 17× 2× 17× 17× 17× 16× 1× 7× 2× 2× 2× 7× 1× 7× 18× 18× 7× 1× 1× 1× 1× 1× 23× 23× 23× 23× 23× 23× 1× 23× 23× 1× 1× 22× 23× 1× 1× 1× 23× 2× 1× 1× 1× 1× 23× 3× 3× 1× 1× 20× 9× 9× 9× 9× 9× 9× 23× 23× 1× 8× 1× 23× 3× 3× 3× 3× 23× 23× 21× 23× 11× 11× 1× 45× 1× 1× 1× 1× 1× 1× 1× | /** * react-lazyload */ 'use strict'; exports.__esModule = true; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } 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; } var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _utilsEvent = require('./utils/event'); var _utilsScrollParent = require('./utils/scrollParent'); var _utilsScrollParent2 = _interopRequireDefault(_utilsScrollParent); var _utilsDebounce = require('./utils/debounce'); var _utilsDebounce2 = _interopRequireDefault(_utilsDebounce); var _utilsThrottle = require('./utils/throttle'); var _utilsThrottle2 = _interopRequireDefault(_utilsThrottle); var LISTEN_FLAG = 'data-lazyload-listened'; var listeners = []; var pending = []; /** * Check if `component` is visible in overflow container `parent` * @param {node} component React component * @param {node} parent component's scroll parent * @return {bool} */ var checkOverflowVisible = function checkOverflowVisible(component, parent) { var node = _reactDom2['default'].findDOMNode(component); var scrollTop = parent.scrollTop; var parentBottom = scrollTop + parent.offsetHeight; var _node$getBoundingClientRect = node.getBoundingClientRect(); var elementHeight = _node$getBoundingClientRect.height; var offsets = Array.isArray(component.props.offset) ? component.props.offset : [component.props.offset, component.props.offset]; // Be compatible with previous API var elementTop = node.offsetTop; var elementBottom = elementTop + elementHeight; return elementTop - offsets[0] <= parentBottom && elementBottom + offsets[1] >= scrollTop; }; /** * Check if `component` is visible in document * @param {node} component React component * @return {bool} */ var checkNormalVisible = function checkNormalVisible(component) { var node = _reactDom2['default'].findDOMNode(component); var supportPageOffset = window.pageXOffset !== undefined; var isCSS1Compat = (document.compatMode || '') === 'CSS1Compat'; var scrollTop = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop; var _node$getBoundingClientRect2 = node.getBoundingClientRect(); var top = _node$getBoundingClientRect2.top; var elementHeight = _node$getBoundingClientRect2.height; var elementTop = top + scrollTop; // element top relative to document var elementBottom = elementTop + elementHeight; var windowInnerHeight = window.innerHeight || document.documentElement.clientHeight; var documentBottom = scrollTop + windowInnerHeight; var offsets = Array.isArray(component.props.offset) ? component.props.offset : [component.props.offset, component.props.offset]; // Be compatible with previous API return elementTop - offsets[0] <= documentBottom && elementBottom + offsets[1] >= scrollTop; }; /** * Detect if element is visible in viewport, if so, set `visible` state to true. * If `once` prop is provided true, remove component as listener after checkVisible * * @param {React} component React component that respond to scroll and resize */ var checkVisible = function checkVisible(component) { var node = _reactDom2['default'].findDOMNode(component); Iif (!node) { return; } var parent = _utilsScrollParent2['default'](node); var isOverflow = parent !== (node.ownerDocument || document); var visible = isOverflow ? checkOverflowVisible(component, parent) : checkNormalVisible(component); if (visible) { // Avoid extra render if previously is visible, yeah I mean `render` call, // not actual DOM render if (!component.visible) { if (component.props.once) { pending.push(component); } component.visible = true; component.forceUpdate(); } } else if (!(component.props.once && component.visible)) { component.visible = false; } }; var purgePending = function purgePending() { pending.forEach(function (component) { var index = listeners.indexOf(component); Eif (index !== -1) { listeners.splice(index, 1); } }); pending = []; }; var lazyLoadHandler = function lazyLoadHandler() { for (var i = 0; i < listeners.length; ++i) { var listener = listeners[i]; checkVisible(listener); } // Remove `once` component in listeners purgePending(); }; // Depending on component's props var delayType = undefined; var finalLazyLoadHandler = null; var LazyLoad = (function (_Component) { _inherits(LazyLoad, _Component); function LazyLoad(props) { _classCallCheck(this, LazyLoad); _Component.call(this, props); this.visible = false; Iif (_react2['default'].Children.count(this.props.children) > 1) { console.warn('[react-lazyload] Only one child is allowed to be passed to `LazyLoad`.'); } Iif (typeof this.props.height !== 'number') { console.warn('[react-lazyload] Please add `height` props to <LazyLoad> for better performance.'); } Iif (this.props.wheel) { // eslint-disable-line console.warn('[react-lazyload] Props `wheel` is not supported anymore, try set `overflow` for lazy loading in overflow containers.'); } } LazyLoad.prototype.componentDidMount = function componentDidMount() { // It's unlikely to change delay type for an application, this is mainly // designed for tests var needResetFinalLazyLoadHandler = false; if (this.props.debounce !== undefined && delayType === 'throttle') { console.warn('[react-lazyload] Previous delay function is `throttle`, now switching to `debounce`, try set them unanimously'); needResetFinalLazyLoadHandler = true; } else Iif (delayType === 'debounce' && this.props.debounce === undefined) { console.warn('[react-lazyload] Previous delay function is `debounce`, now switching to `throttle`, try set them unanimously'); needResetFinalLazyLoadHandler = true; } if (needResetFinalLazyLoadHandler) { _utilsEvent.off(window, 'scroll', finalLazyLoadHandler); _utilsEvent.off(window, 'resize', finalLazyLoadHandler); finalLazyLoadHandler = null; } if (!finalLazyLoadHandler) { if (this.props.debounce !== undefined) { finalLazyLoadHandler = _utilsDebounce2['default'](lazyLoadHandler, typeof this.props.throttle === 'number' ? this.props.throttle : 300); delayType = 'debounce'; } else { finalLazyLoadHandler = _utilsThrottle2['default'](lazyLoadHandler, typeof this.props.debounce === 'number' ? this.props.debounce : 300); delayType = 'throttle'; } } if (this.props.overflow) { var _parent = _utilsScrollParent2['default'](_reactDom2['default'].findDOMNode(this)); if (_parent && _parent.getAttribute(LISTEN_FLAG) === null) { _parent.addEventListener('scroll', finalLazyLoadHandler); _parent.setAttribute(LISTEN_FLAG, 1); } } else if (listeners.length === 0 || needResetFinalLazyLoadHandler) { var _props = this.props; var _scroll = _props.scroll; var resize = _props.resize; Eif (_scroll) { _utilsEvent.on(window, 'scroll', finalLazyLoadHandler); } Iif (resize) { _utilsEvent.on(window, 'resize', finalLazyLoadHandler); } } listeners.push(this); checkVisible(this); }; LazyLoad.prototype.shouldComponentUpdate = function shouldComponentUpdate() { return this.visible; }; LazyLoad.prototype.componentWillUnmount = function componentWillUnmount() { if (this.props.overflow) { var _parent2 = _utilsScrollParent2['default'](_reactDom2['default'].findDOMNode(this)); Eif (_parent2) { _parent2.removeEventListener('scroll', finalLazyLoadHandler); _parent2.removeAttribute(LISTEN_FLAG); } } var index = listeners.indexOf(this); if (index !== -1) { listeners.splice(index, 1); } if (listeners.length === 0) { _utilsEvent.off(window, 'resize', finalLazyLoadHandler); _utilsEvent.off(window, 'scroll', finalLazyLoadHandler); } }; LazyLoad.prototype.render = function render() { return this.visible ? this.props.children : _react2['default'].createElement('div', { style: { height: this.props.height }, className: 'lazyload-placeholder' }); }; return LazyLoad; })(_react.Component); LazyLoad.propTypes = { once: _react.PropTypes.bool, height: _react.PropTypes.number.isRequired, offset: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.arrayOf(_react.PropTypes.number)]), overflow: _react.PropTypes.bool, resize: _react.PropTypes.bool, scroll: _react.PropTypes.bool, children: _react.PropTypes.node, throttle: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.bool]), debounce: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.bool]) }; LazyLoad.defaultProps = { once: false, height: 100, offset: 0, overflow: false, resize: false, scroll: true }; exports['default'] = LazyLoad; var _decorator = require('./decorator'); var _decorator2 = _interopRequireDefault(_decorator); exports.lazyload = _decorator2['default']; |