(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 ( setAttributes( { headerBgColor: value === undefined ? '#000' : value } ), }, { label: __( 'Text Color' ), value: headerTextColor, onChange: ( value ) => setAttributes( { headerTextColor: value === undefined ? '#fff' : value } ), }, { label: __( 'Active Tab Background Color' ), value: activeTabBgColor, onChange: ( value ) => setAttributes( { activeTabBgColor: value } ), }, { label: __( 'Active Tab Text Color' ), value: activeTabTextColor, onChange: ( value ) => setAttributes( { activeTabTextColor: value } ), }, ] } /> setAttributes( { bodyBgColor: value } ), }, { label: __( 'Text Color' ), value: bodyTextColor, onChange: ( value ) => setAttributes( { bodyTextColor: value } ), }, ] } /> setAttributes( { borderStyle: value } ) } /> setAttributes( { borderColor: value } ), }, ] } /> setAttributes( { borderWidth: value } ) } /> setAttributes( { borderRadius: value } ) } />
{tabItems.map( ( item, index ) => (
this.updateTabs( { body: value }, index ) } placeholder={ __( 'Enter text…' ) } />
) ) }
{!!blockID && }
) } } const tabsBlockIcon = ( ); const tabBlockAttrs = { tabItems: { type: "array", default: [ { header: __( 'Tab 1' ), body: __( 'Filler text (also placeholder text or dummy text) is text that shares some characteristics of a real written text, but is random or otherwise generated.' ) }, { header: __( 'Tab 2' ), body: __( 'Filler text (also placeholder text or dummy text) is text that shares some characteristics of a real written text, but is random or otherwise generated.' ) }, { header: __( 'Tab 3' ), body: __( 'Filler text (also placeholder text or dummy text) is text that shares some characteristics of a real written text, but is random or otherwise generated.' ) }, ] }, headerBgColor: { type: 'string', default: '#000', }, headerTextColor: { type: 'string', default: '#fff', }, bodyBgColor: { type: 'string', }, bodyTextColor: { type: 'string', }, borderStyle: { type: 'string', default: 'solid', }, borderWidth: { type: 'number', default: 1, }, borderColor: { type: 'string', }, borderRadius: { type: 'number', default: 2, }, blockID: { type: 'string', }, activeTabBgColor: { type: 'string', }, activeTabTextColor: { type: 'string', }, changed: { type: 'boolean', default: false, }, }; registerBlockType( 'advgb/tabs', { title: __( 'Tabs' ), description: __( 'Create your own tabs never easy like this.' ), icon: { src: tabsBlockIcon, foreground: typeof advgbBlocks !== 'undefined' ? advgbBlocks.color : undefined, }, category: "advgb-category", keywords: [ __( 'tabs' ), __( 'cards' ) ], attributes: tabBlockAttrs, edit: AdvTabsBlock, save: function ( { attributes } ) { const { tabItems, headerBgColor, headerTextColor, bodyBgColor, bodyTextColor, borderStyle, borderWidth, borderColor, borderRadius, blockID, activeTabBgColor, activeTabTextColor, } = attributes; return (
{tabItems.map( ( item, index ) => (
) ) } {!!blockID && }
); }, deprecated: [ { attributes: tabBlockAttrs, save: function ( { attributes } ) { const { tabItems, headerBgColor, headerTextColor, bodyBgColor, bodyTextColor, borderStyle, borderWidth, borderColor, borderRadius, blockID, activeTabBgColor, activeTabTextColor, } = attributes; return (
{tabItems.map( ( item, index ) => (
) ) } {!!blockID && }
); } } ] } ); })( wp.i18n, wp.blocks, wp.element, wp.editor, wp.components );