-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
1 lines (1 loc) · 13.4 KB
/
index.js
1
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports["react-xmasonry"]=t(require("react")):e["react-xmasonry"]=t(e.React)}("undefined"!=typeof self?self:this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(t,n){t.exports=e},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.XBlock=t.XMasonry=void 0;var i=n(3),o=r(i),a=n(5),s=r(a);t.XMasonry=o.default,t.XBlock=s.default},function(e,t,n){"use strict";(function(e){Object.defineProperty(t,"__esModule",{value:!0}),t.debounce=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:45,n=0;return function(){0!==n&&clearTimeout(n),n=setTimeout(function(){n=0,e()},t)}},t.isServer="false"!==((e||{}).env||{}).REACT_XMASONRY_SSR_ENABLED&&!("undefined"!=typeof document&&"undefined"!=typeof window)}).call(t,n(4))},function(e,t,n){"use strict";function r(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function a(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},u=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),l=n(0),c=function(e){return e&&e.__esModule?e:{default:e}}(l),d=n(2),h=0,p=!1,f=function e(){if(!d.isServer){if(!document.body)return void document.addEventListener("DOMContentLoaded",e);var t=document.createElement("div");t.style.overflow="scroll",t.style.height=t.style.width="200px",t.style.visibility="hidden",t.style.padding=t.style.margin=t.style.border=0,document.body.appendChild(t),h=t.offsetWidth-t.clientWidth||0,p=!0,document.body.removeChild(t)}},m=function(e){function t(e){i(this,t);var n=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return n.state={blocks:{},containerHeight:0,columns:1,containerWidth:0},n.pastChildren=0,n.columns=n.state.columns,n.blocks=n.state.blocks,n.container=null,n.debouncedResize=(0,d.debounce)(n.updateContainerWidth.bind(n)),n.fixedHeight=0,n.containerWidth=n.state.containerWidth,n.update=n.updateInternal.bind(n),n.smartUpdate=0,p||f(),n.props.responsive&&!d.isServer&&window.addEventListener("resize",n.debouncedResize),!d.isServer&&n.props.updateOnFontLoad&&document.fonts&&document.fonts.addEventListener&&document.fonts.addEventListener("loadingdone",n.update),d.isServer&&(n.state.containerWidth=n.containerWidth=n.props.width||999*n.props.targetBlockWidth,n.columns=n.getColumnsNumber(n.state.containerWidth)),n.pastChildren=e.children,n}return a(t,e),u(t,[{key:"getBestFitColumn",value:function(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,n=Math.min(e.length-t+1,this.props.maxColumns-t+1),r=0,i=1/0,o=0;o<n;++o){var a=Math.max.apply(null,e.slice(o,o+t));a<i&&(i=a,r=o)}return{col:r,height:i}}}]),u(t,[{key:"updateInternal",value:function(){return!this.updateContainerWidth()&&this.measureChildren()}},{key:"componentDidMount",value:function(){this.updateInternal()}},{key:"componentWillUnmount",value:function(){d.isServer||window.removeEventListener("resize",this.debouncedResize),!d.isServer&&this.props.updateOnFontLoad&&document.fonts&&document.fonts.addEventListener&&document.fonts.removeEventListener("loadingdone",this.update),this.smartUpdate&&clearTimeout(this.smartUpdate)}},{key:"componentDidUpdate",value:function(){if(c.default.Children.count(this.props.children)<c.default.Children.count(this.pastChildren)){var e=new Set,t={};c.default.Children.forEach(this.pastChildren,function(t,n){return t&&e.add(null===t.key?n:t.key)}),c.default.Children.forEach(this.props.children,function(n,r){if(n){var i=null===n.key?r:n.key;e.has(i)||(t[i]={})}}),this.recalculatePositions(null,t)}this.pastChildren=this.props.children,this.updateInternal()&&this.props.smartUpdate&&this.runSmartUpdate()}},{key:"runSmartUpdate",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:100;if(100===t&&this.smartUpdate)clearInterval(this.smartUpdate),this.smartUpdate=0;else if(this.smartUpdate)return;this.smartUpdate=setTimeout(function(){var n=!d.isServer&&void 0!==document.hidden&&document.hidden;if(n)var r=document.addEventListener("visibilitychange",function(){document.hidden||(document.removeEventListener("visibilitychange",r),e.runSmartUpdate(2*t))},!1);if(e.smartUpdate=0,e.updateInternal())return void e.runSmartUpdate();n||e.runSmartUpdate(2*t)},Math.min(t,this.props.smartUpdateCeil))}},{key:"getColumnsNumber",value:function(e){return Math.max(1,Math.round(e/this.props.targetBlockWidth))}},{key:"updateContainerWidth",value:function(){var e=d.isServer&&!this.container?this.props.width||999*this.props.targetBlockWidth:this.container?this.container.clientWidth:0;return e!==this.containerWidth&&e!==this.containerWidth+h&&(this.setState({columns:this.columns=this.getColumnsNumber(e),containerWidth:this.containerWidth=e,blocks:this.blocks={}}),!0)}},{key:"measureChildren",value:function(){if(!this.container)return!1;for(var e={},t=!1,n=0;n<this.container.children.length;n++){var r=this.container.children[n],i=r.hasAttribute("data-xkey"),o=r.getAttribute("data-key"),a=+r.getAttribute("data-width");(i||(this.blocks[o]||{}).height!==r.clientHeight||(this.blocks[o]||{}).width!==a)&&(e[o]={height:r.clientHeight},t||(t=!0))}return t&&this.recalculatePositions(e),t}},{key:"recalculatePositions",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=void 0,r=[],i=0;i<this.columns;++i)r.push(0);for(var o in this.blocks)this.blocks.hasOwnProperty(o)&&void 0===this.blocks[o]&&(null===t&&(t={}),t[o]={});if(t){n={};for(var a in this.blocks)this.blocks.hasOwnProperty(a)&&!t.hasOwnProperty(a)&&(n[a]=this.blocks[a]);for(var u in e)e.hasOwnProperty(u)&&!t.hasOwnProperty(u)&&(n[u]=e[u])}else n=s({},this.blocks,e);for(var l=0;l<this.container.children.length;l++){var c=this.container.children[l],d=c.getAttribute("data-key");if(n.hasOwnProperty(d)&&(!t||!t.hasOwnProperty(d))){var h=+c.getAttribute("data-width")||1,p=this.getBestFitColumn(r,h),f=p.col,m=p.height,v=m+n[d].height;n[d].left=this.containerWidth*f/this.columns,n[d].top=m,n[d].width=Math.min(h,this.columns);for(var y=0;y<h;++y)r[f+y]=v}}if(this.props.center&&0===r[r.length-1]){for(var b=1;0===r[r.length-1-b];++b);var g=this.containerWidth*b/this.columns/2;for(var w in n)n.hasOwnProperty(w)&&(n[w].left+=g)}this.setState({blocks:this.blocks=n,containerHeight:Math.max.apply(null,r)})}},{key:"render",value:function(){var e=this,n={},i=0,o=0===this.containerWidth?[]:this.props.children||[],a=c.default.Children.map(o,function(t,r){if(!t)return null;var o=null===t.key?r:t.key,a=e.blocks[o];return a||++i,n[o]=null,a?c.default.cloneElement(t,{"data-key":o,key:o,style:{left:Math.floor(a.left),top:a.top},measured:!0,height:a.height,parent:e}):c.default.cloneElement(t,{"data-key":o,"data-xkey":o,key:o,style:{visibility:d.isServer?"visible":"hidden"},height:0,parent:e})});for(var u in this.blocks)this.blocks.hasOwnProperty(u)&&!n.hasOwnProperty(u)&&(this.blocks[u]=void 0);var l=a.length-i>0||0===a.length?this.fixedHeight=this.state.containerHeight:this.fixedHeight,h=this.props,p=(h.center,h.maxColumns,h.responsive,h.smartUpdate,h.smartUpdateCeil,h.targetBlockWidth,h.updateOnImagesLoad,h.updateOnFontLoad,h.className),f=h.style,m=r(h,["center","maxColumns","responsive","smartUpdate","smartUpdateCeil","targetBlockWidth","updateOnImagesLoad","updateOnFontLoad","className","style"]);return c.default.createElement("div",s({className:"xmasonry"+(d.isServer?" xmasonry-static":"")+(p?" "+p:""),style:s({},t.containerStyle,{height:d.isServer?void 0:l},f),ref:function(t){return e.container=t}},m),a)}}]),t}(c.default.Component);m.defaultProps={center:!0,maxColumns:d.isServer?3:1/0,responsive:!0,smartUpdate:!0,smartUpdateCeil:1/0,targetBlockWidth:300,updateOnFontLoad:!0,updateOnImagesLoad:!0},m.containerStyle={position:"relative"},t.default=m},function(e,t){function n(){throw new Error("setTimeout has not been defined")}function r(){throw new Error("clearTimeout has not been defined")}function i(e){if(c===setTimeout)return setTimeout(e,0);if((c===n||!c)&&setTimeout)return c=setTimeout,setTimeout(e,0);try{return c(e,0)}catch(t){try{return c.call(null,e,0)}catch(t){return c.call(this,e,0)}}}function o(e){if(d===clearTimeout)return clearTimeout(e);if((d===r||!d)&&clearTimeout)return d=clearTimeout,clearTimeout(e);try{return d(e)}catch(t){try{return d.call(null,e)}catch(t){return d.call(this,e)}}}function a(){m&&p&&(m=!1,p.length?f=p.concat(f):v=-1,f.length&&s())}function s(){if(!m){var e=i(a);m=!0;for(var t=f.length;t;){for(p=f,f=[];++v<t;)p&&p[v].run();v=-1,t=f.length}p=null,m=!1,o(e)}}function u(e,t){this.fun=e,this.array=t}function l(){}var c,d,h=e.exports={};!function(){try{c="function"==typeof setTimeout?setTimeout:n}catch(e){c=n}try{d="function"==typeof clearTimeout?clearTimeout:r}catch(e){d=r}}();var p,f=[],m=!1,v=-1;h.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];f.push(new u(e,t)),1!==f.length||m||i(s)},u.prototype.run=function(){this.fun.apply(null,this.array)},h.title="browser",h.browser=!0,h.env={},h.argv=[],h.version="",h.versions={},h.on=l,h.addListener=l,h.once=l,h.off=l,h.removeListener=l,h.removeAllListeners=l,h.emit=l,h.prependListener=l,h.prependOnceListener=l,h.listeners=function(e){return[]},h.binding=function(e){throw new Error("process.binding is not supported")},h.cwd=function(){return"/"},h.chdir=function(e){throw new Error("process.chdir is not supported")},h.umask=function(){return 0}},function(e,t,n){"use strict";function r(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function a(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},u=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),l=n(0),c=function(e){return e&&e.__esModule?e:{default:e}}(l),d=n(2),h=function(e){function t(){var e,n,r,a;i(this,t);for(var s=arguments.length,u=Array(s),l=0;l<s;l++)u[l]=arguments[l];return n=r=o(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(u))),r.divElement=null,r.placed=!1,a=n,o(r,a)}return a(t,e),u(t,[{key:"componentDidUpdate",value:function(){var e=this;if(!this.placed&&this.props.parent&&!this.props["data-xkey"]){this.placed=!0;var t=this.props.parent;requestAnimationFrame(function(){if(e.divElement){var n=Array.prototype.slice.call(e.divElement.querySelectorAll("img"));n.length>0&&t.props.updateOnImagesLoad&&n.forEach(function(e){return!e.complete&&e.addEventListener("load",t.update)}),e.props.height!==e.divElement.clientHeight&&t.update()}})}}},{key:"render",value:function(){var e=this,n=this.props,i=n.width,o=(n.height,n.measured),a=(n.parent,n.style),u=r(n,["width","height","measured","parent","style"]),l=this.props.parent.columns,h=Math.min(i||1,l,this.props.parent.props.maxColumns);return a.width=Math.floor(h*this.props.parent.containerWidth/l),c.default.createElement("div",s({"data-width":h},u,{style:s({},a,t.defaultStyle),className:o?"xblock":d.isServer?"xblock xblock-static":"",ref:function(t){return e.divElement=t}}),this.props.children)}}]),t}(c.default.Component);h.defaultProps={width:1,measured:!1},h.defaultStyle={position:d.isServer?void 0:"absolute",boxSizing:"border-box"},t.default=h}])});