|
| 1 | +importReact,{PropTypes}from'react'; |
| 2 | +importCSSModulesfrom'react-css-modules'; |
| 3 | +importSliderfrom'react-slick'; |
| 4 | +import_from'lodash'; |
| 5 | +importMeasurefrom'react-measure'; |
| 6 | +importLightboxfrom'react-image-lightbox'; |
| 7 | +importconfigfrom'../../config'; |
| 8 | +importstylesfrom'./CloudinaryGallery.scss'; |
| 9 | +importCloudinaryGalleryItemfrom'./CloudinaryGalleryItem'; |
| 10 | + |
| 11 | +constCLOUDINARY_PREFIX=`http://res.cloudinary.com/${config.CLOUDINARY_ACCOUNT_NAME}/image/fetch/`; |
| 12 | + |
| 13 | +constsliderProps={ |
| 14 | +infinite:false, |
| 15 | +dots:true, |
| 16 | +speed:500, |
| 17 | +slidesToShow:1, |
| 18 | +slidesToScroll:1, |
| 19 | +vertical:false, |
| 20 | +variableWidth:false, |
| 21 | +}; |
| 22 | + |
| 23 | +// css margin |
| 24 | +constMARGIN=8; |
| 25 | + |
| 26 | +classCloudinaryGalleryextendsReact.Component{ |
| 27 | +constructor(props){ |
| 28 | +super(props); |
| 29 | +this.state={ |
| 30 | +photoIndex:0, |
| 31 | +isOpen:false, |
| 32 | +}; |
| 33 | +} |
| 34 | + |
| 35 | +render(){ |
| 36 | +const{items, width, count, height, noItemsText}=this.props; |
| 37 | +const{isOpen, photoIndex}=this.state; |
| 38 | + |
| 39 | +if(!items||!items.length){ |
| 40 | +return( |
| 41 | +<pstyleName="no-items">{noItemsText}</p> |
| 42 | +); |
| 43 | +} |
| 44 | +constitemWidth=Math.floor(width/count)-2*MARGIN; |
| 45 | +constresizedItems=items.map((item)=>({ |
| 46 | + ...item, |
| 47 | +// c_fill = crop with retaining original proportions |
| 48 | +// g_auto = auto detect point of interests |
| 49 | +// see http://cloudinary.com/blog/introducing_smart_cropping_intelligent_quality_selection_and_automated_responsive_images#automatic_content_aware_cropping_g_auto |
| 50 | +src:`${CLOUDINARY_PREFIX}w_${itemWidth},h_${height},c_fill,g_auto/${item.src}`, |
| 51 | +})); |
| 52 | +return( |
| 53 | +<divstyleName="cloudinary-gallery"> |
| 54 | +{isOpen&& |
| 55 | +<Lightbox |
| 56 | +mainSrc={items[photoIndex].src} |
| 57 | +nextSrc={items[(photoIndex+1)%items.length].src} |
| 58 | +prevSrc={items[(photoIndex+items.length-1)%items.length].src} |
| 59 | +onCloseRequest={()=>this.setState({isOpen:false})} |
| 60 | +onMovePrevRequest={()=>this.setState({ |
| 61 | +photoIndex:(photoIndex+items.length-1)%items.length, |
| 62 | +})} |
| 63 | +onMoveNextRequest={()=>this.setState({ |
| 64 | +photoIndex:(photoIndex+1)%items.length, |
| 65 | +})} |
| 66 | +/> |
| 67 | +} |
| 68 | +<Slider{...sliderProps}> |
| 69 | +{_.chunk(resizedItems,count).map((slideItems,slideIndex)=>( |
| 70 | +<divkey={slideIndex}styleName="slide"> |
| 71 | +<divstyleName="slide-inner"> |
| 72 | +{slideItems.map((item,itemIndex)=>( |
| 73 | +<divkey={itemIndex}styleName="item"> |
| 74 | +<CloudinaryGalleryItem |
| 75 | +{...item} |
| 76 | +onClick={()=>{ |
| 77 | +this.setState({ |
| 78 | +isOpen:true, |
| 79 | +photoIndex:slideIndex*count+itemIndex, |
| 80 | +}); |
| 81 | +}} |
| 82 | +/> |
| 83 | +</div> |
| 84 | +))} |
| 85 | +</div> |
| 86 | +</div> |
| 87 | +))} |
| 88 | +</Slider> |
| 89 | +</div> |
| 90 | +); |
| 91 | +} |
| 92 | +} |
| 93 | + |
| 94 | +CloudinaryGallery.propTypes={ |
| 95 | +items:PropTypes.array.isRequired, |
| 96 | +width:PropTypes.number, |
| 97 | +count:PropTypes.number.isRequired, |
| 98 | +height:PropTypes.number.isRequired, |
| 99 | +noItemsText:PropTypes.string.isRequired, |
| 100 | +}; |
| 101 | + |
| 102 | +// HOC wrapping |
| 103 | + |
| 104 | +constCloudinaryGalleryWithStyles=CSSModules(CloudinaryGallery,styles); |
| 105 | + |
| 106 | +constCloudinaryGalleryWithMeasure=(props)=>( |
| 107 | +<Measure> |
| 108 | +{ |
| 109 | +({width})=><CloudinaryGalleryWithStyles{...props}width={width}/> |
| 110 | +} |
| 111 | +</Measure> |
| 112 | +); |
| 113 | +exportdefaultCloudinaryGalleryWithMeasure; |