(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents ) { const { __ } = wpI18n; const { Component, Fragment } = wpElement; const { registerBlockType } = wpBlocks; const { InspectorControls, PanelColorSettings, MediaUpload } = wpEditor; const { PanelBody, RangeControl, ToggleControl , SelectControl, TextControl, TextareaControl, IconButton, Button, Placeholder, Tooltip } = wpComponents; const $ = jQuery; const imageSliderBlockIcon = ( ); class AdvImageSlider extends Component { constructor() { super( ...arguments ); this.state = { currentSelected: 0, inited: false, }; this.initSlider = this.initSlider.bind(this); } componentWillMount() { const { attributes, setAttributes } = this.props; const currentBlockConfig = advgbDefaultConfig['advgb-images-slider']; // No override attributes of blocks inserted before if (attributes.changed !== true) { if (typeof currentBlockConfig === 'object' && currentBlockConfig !== null) { Object.keys(currentBlockConfig).map((attribute) => { if (typeof attributes[attribute] === 'boolean') { attributes[attribute] = !!currentBlockConfig[attribute]; } else { attributes[attribute] = currentBlockConfig[attribute]; } }); } // Finally set changed attribute to true, so we don't modify anything again setAttributes( { changed: true } ); } } componentDidMount() { const { attributes } = this.props; if (attributes.images.length) { this.initSlider(); } } componentWillUpdate( nextProps ) { const { clientId, attributes } = this.props; const { images } = attributes; const { images: nextImages } = nextProps.attributes; if ( images.length !== nextImages.length ) { $(`#block-${clientId} .advgb-images-slider.slick-initialized`).slick('unslick'); $(`#block-${clientId} .advgb-image-slider-item`) .removeAttr('tabindex') .removeAttr('role') .removeAttr('aria-describedby'); } } componentDidUpdate( prevProps ) { const { attributes } = this.props; const { images } = attributes; const { images: prevImages } = prevProps.attributes; if (images.length !== prevImages.length) { if (images.length) { setTimeout(() => this.initSlider(), 10); } } } initSlider() { const { clientId } = this.props; $(`#block-${clientId} .advgb-images-slider:not(.slick-initialized)`).slick( { dots: true, adaptiveHeight: true, } ); $(`#block-${clientId} .advgb-images-slider`).on('afterChange', (e, s, currentSlide) => { if (this.state.currentSelected !== currentSlide) { this.setState( { currentSelected: currentSlide } ); } } ); } moveImage( currentIndex, newIndex ) { const { setAttributes, attributes } = this.props; const { images } = attributes; const image = images[currentIndex]; setAttributes( { images: [ ...images.filter( (img, idx) => idx !== currentIndex ).slice(0, newIndex), image, ...images.filter( (img, idx) => idx !== currentIndex ).slice(newIndex), ] } ); } updateImagesData(data) { const { currentSelected } = this.state; if (typeof currentSelected !== 'number') { return null; } const { attributes, setAttributes } = this.props; const { images } = attributes; const newImages = images.map( (image, index) => { if (index === currentSelected) { image = { ...image, ...data }; } return image; } ); setAttributes( { images: newImages } ); } render() { const { attributes, setAttributes, isSelected, clientId } = this.props; const { currentSelected } = this.state; const { images, actionOnClick, fullWidth, autoHeight, width, height, alwaysShowOverlay, hoverColor, titleColor, textColor, hAlign, vAlign, } = attributes; if (images.length === 0) { return ( { const imgInsert = image.map( (img) => ( { url: img.url, id: img.id, } ) ); setAttributes( { images: [ ...images, ...imgInsert, ] } ) } } render={ ( { open } ) => ( ) } /> ) } return ( setAttributes( { actionOnClick: value } ) } /> setAttributes( { fullWidth: !fullWidth } ) } /> setAttributes( { autoHeight: !autoHeight } ) } /> {!fullWidth && ( setAttributes( { width: value } ) } min={ 200 } max={ 1300 } /> ) } {!autoHeight && ( setAttributes( { height: value } ) } min={ 100 } max={ 1000 } /> ) } setAttributes( { alwaysShowOverlay: !alwaysShowOverlay } ) } /> setAttributes( { hoverColor: value } ), }, { label: __( 'Title Color' ), value: titleColor, onChange: ( value ) => setAttributes( { titleColor: value } ), }, { label: __( 'Text Color' ), value: textColor, onChange: ( value ) => setAttributes( { textColor: value } ), }, ] } /> setAttributes( { vAlign: value } ) } /> setAttributes( { hAlign: value } ) } />
{images.map( (image, index) => (
{

{ image.title }

{ image.text }

) ) }
{isSelected && (
this.updateImagesData( { title: value || '' } ) } />
this.updateImagesData( { text: value || '' } ) } />
{actionOnClick === 'link' && (
this.updateImagesData( { link: value || '' } ) } />
) }
{images.map( (image, index) => (
{index > 0 && ( this.moveImage( index, index - 1 ) } > ) } { { $(`#block-${clientId} .advgb-images-slider`).slick('slickGoTo', index, false); this.setState( { currentSelected: index } ) } } /> {index + 1 < images.length && ( this.moveImage( index, index + 1 ) } > ) } { if (index === currentSelected) this.setState( { currentSelected: null } ); setAttributes( { images: images.filter( (img, idx) => idx !== index ) } ) } } />
) ) }
setAttributes( { images: [...images, ...imgs.map( (img) => lodash.pick( img, 'id', 'url' ) ) ], } ) } render={ ( { open } ) => ( ) } />
) }
) } } const blockAttrs = { images: { type: 'array', default: [], // [ { id: int, url, title, text, link: string } ] }, actionOnClick: { type: 'string', }, fullWidth: { type: 'boolean', default: true, }, autoHeight: { type: 'boolean', default: true, }, width: { type: 'number', default: 700, }, height: { type: 'number', default: 500, }, alwaysShowOverlay: { type: 'boolean', default: false, }, hoverColor: { type: 'string', }, titleColor: { type: 'string', }, textColor: { type: 'string', }, vAlign: { type: 'string', default: 'center', }, hAlign: { type: 'string', default: 'center', }, changed: { type: 'boolean', default: false, } }; registerBlockType( 'advgb/images-slider', { title: __( 'Images Slider' ), description: __( 'Display your images in a slider.' ), icon: { src: imageSliderBlockIcon, foreground: typeof advgbBlocks !== 'undefined' ? advgbBlocks.color : undefined, }, category: 'advgb-category', keywords: [ __( 'slide' ), __( 'gallery' ), __( 'photos' ) ], attributes: blockAttrs, edit: AdvImageSlider, save: function ( { attributes } ) { const { images, actionOnClick, fullWidth, autoHeight, width, height, alwaysShowOverlay, hoverColor, titleColor, textColor, hAlign, vAlign, } = attributes; const blockClassName = [ 'advgb-images-slider-block', actionOnClick === 'lightbox' && 'advgb-images-slider-lightbox', ].filter( Boolean ).join( ' ' ); return (
{images.map( (image, index) => (
{

{ image.title }

{ image.text }

) ) }
); }, deprecated: [ { attributes: blockAttrs, save: function ( { attributes } ) { const { images, actionOnClick, fullWidth, autoHeight, width, height, alwaysShowOverlay, hoverColor, titleColor, textColor, hAlign, vAlign, } = attributes; const blockClassName = [ 'advgb-images-slider-block', actionOnClick === 'lightbox' && 'advgb-images-slider-lightbox', ].filter( Boolean ).join( ' ' ); return (
{images.map( (image, index) => (
{

{ image.title }

{ image.text }

) ) }
); }, } ] } ); })( wp.i18n, wp.blocks, wp.element, wp.editor, wp.components );