(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents ) {
const { __ } = wpI18n;
const { Component, Fragment } = wpElement;
const { registerBlockType } = wpBlocks;
const { InspectorControls, RichText, PanelColorSettings } = wpEditor;
const { Dashicon, Tooltip, PanelBody, RangeControl, SelectControl } = wpComponents;
class AdvTabsBlock extends Component {
constructor() {
super( ...arguments );
}
componentWillMount() {
const { attributes, setAttributes } = this.props;
const currentBlockConfig = advgbDefaultConfig['advgb-tabs'];
// 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() {
setTimeout(() => this.initTabs(), 100);
if (!this.props.attributes.blockID) {
this.props.setAttributes( { blockID: this.props.clientId } );
}
}
componentDidUpdate( prevProps ) {
const { tabItems: prevItems } = prevProps.attributes;
const { tabItems } = this.props.attributes;
if (prevItems !== tabItems) {
this.initTabs( true );
}
if (tabItems.length === 0) {
this.props.setAttributes( {
tabItems: [
{
header: 'Tab 1',
body: 'At least one tab must remaining, to remove block use "Remove Block" button from right menu.',
},
],
} );
}
}
initTabs( refresh = false ) {
if (typeof jQuery !== "undefined") {
if (!refresh) {
jQuery(`#block-${this.props.clientId} .advgb-tabs-block`).tabs();
} else {
jQuery(`#block-${this.props.clientId} .advgb-tabs-block`).tabs('refresh');
}
jQuery(`#block-${this.props.clientId} .advgb-tabs-block a`).on( 'keydown', function ( e ) {
e.stopPropagation();
} )
}
}
updateTabs( value, index ) {
const { attributes, setAttributes } = this.props;
const { tabItems } = attributes;
let newItems = tabItems.map( ( item, thisIndex ) => {
if ( index === thisIndex ) {
item = { ...item, ...value };
}
return item;
} );
setAttributes( { tabItems: newItems } );
}
render() {
const { attributes, setAttributes, clientId } = this.props;
const {
tabItems,
headerBgColor,
headerTextColor,
bodyBgColor,
bodyTextColor,
borderStyle,
borderWidth,
borderColor,
borderRadius,
blockID,
activeTabBgColor,
activeTabTextColor,
} = attributes;
return (
{tabItems.map( ( item, index ) => (
{tabItems.map( ( item, index ) => (