(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 (
{ image.text }
{ image.title }
{
$(`#block-${clientId} .advgb-images-slider`).slick('slickGoTo', index, false);
this.setState( { currentSelected: index } )
} }
/>
{index + 1 < images.length && (