(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents ) {
const { __ } = wpI18n;
const { Component, Fragment } = wpElement;
const { registerBlockType } = wpBlocks;
const { InspectorControls, RichText, PanelColorSettings, MediaUpload } = wpEditor;
const { RangeControl, ToggleControl, PanelBody, Tooltip } = wpComponents;
const { times } = lodash;
class AdvTestimonial extends Component {
constructor() {
super( ...arguments );
this.state = {
currentEdit: '',
}
}
componentWillMount() {
const { attributes, setAttributes } = this.props;
const currentBlockConfig = advgbDefaultConfig['advgb-testimonial'];
// 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, clientId } = this.props;
const { sliderView } = attributes;
if (sliderView) {
jQuery(`#block-${clientId} .advgb-testimonial.slider-view`).slick({
infinite: true,
centerMode: true,
centerPadding: '40px',
slidesToShow: 3,
});
}
}
componentWillUpdate(nextProps) {
const { sliderView: nextView, columns: nextColumns } = nextProps.attributes;
const { attributes, clientId } = this.props;
const { sliderView, columns } = attributes;
if (nextView !== sliderView || nextColumns !== columns) {
if (sliderView) {
jQuery(`#block-${clientId} .advgb-testimonial.slick-initialized`).slick('unslick');
jQuery(`#block-${clientId} .advgb-testimonial`)
.removeAttr('tabindex')
.removeAttr('role')
.removeAttr('aria-describedby');
}
}
}
componentDidUpdate(prevProps) {
const { sliderView: prevView, columns: prevColumns } = prevProps.attributes;
const { attributes, clientId } = this.props;
const { sliderView, columns } = attributes;
if (sliderView !== prevView || columns !== prevColumns) {
if (sliderView) {
jQuery(`#block-${clientId} .advgb-testimonial.slider-view`).slick({
infinite: true,
centerMode: true,
centerPadding: '40px',
slidesToShow: 3,
});
}
}
}
updateItems(idx, data) {
const { attributes, setAttributes } = this.props;
const { items } = attributes;
const newItems = items.map( (item, index) => {
if (idx === index) item = { ...item, ...data };
return item;
} );
setAttributes( { items: newItems } );
}
render() {
const { currentEdit } = this.state;
const { attributes, setAttributes, isSelected } = this.props;
const {
items,
sliderView,
avatarColor,
avatarBorderRadius,
avatarBorderWidth,
avatarBorderColor,
avatarSize,
nameColor,
positionColor,
descColor,
columns,
} = attributes;
const blockClass = [
'advgb-testimonial',
sliderView && 'slider-view',
].filter( Boolean ).join( ' ' );
const maxCols = sliderView ? 10 : 3;
const minCols = sliderView ? 4 : 1;
let i = 0;
let validCols = columns;
if (columns < 1) {
validCols = 1;
} else if (columns > 3 && !sliderView) {
validCols = 3;
setAttributes( { columns: 3 } );
} else if (columns < 4 && sliderView) {
validCols = 4;
setAttributes( { columns: 4 } );
} else if (columns > 10) {
validCols = 10;
setAttributes( { columns: 10 } );
} else if (columns === '' || !columns) {
validCols = sliderView ? 4 : 1;
}
return (
{ position }
{ desc }
{ position2 }
{ desc2 }
{ position3 }
{ desc3 }
{ item.position }
{ item.desc }