import { wp } from '../../wp'; const { times } = require('lodash'); const { InnerBlocks } = wp.blockEditor; const { RangeControl, TextControl, SelectControl } = wp.components; const { useSelect } = wp.data; const el = wp.element.createElement; const Fragment = wp.element.Fragment; window.globaldate=function(){return new Date().valueOf().toString(36)}; /* SINGULAR TAB */ wp.blocks.registerBlockType('ads/tab',{ title:'Tab', category: 'agendahelsinki', icon: {background:'#eeeeee',foreground:'#000000',src:'editor-table'}, attributes:{ index:{type:'integer',default:0}, setdate:{type:'string',default:'notset'} }, example:()=>null, supports:{ 'inserter':false, }, edit:function(props){ const {setAttributes} = props; if(props.attributes.setdate==='notset'){ setAttributes({date:new Date().valueOf().toString(36)}); } return el('div',{className:'col-12 p-3 bg-light rounded border border-dark mb-4'}, el('h5',{className:'p-2 my-0 font-weight-600 text-dark col-12 px-0 text-center'},'Tab '+ (props.attributes.index+1)), el(InnerBlocks,{templateLock:"all",template:[['bootstrap/bsblock',{ blockeditortitle:' ',templatelock:'off', className:'border-0 rounded'}]]}) ); }, save:function(props){ return el('div',{id:('tab-'+props.attributes.index+'-'+props.attributes.setdate),className:'tab', 'role':'tabpanel','aria-labelledby':('tab-'+props.attributes.index+'-'+props.attributes.setdate), style:{display:(props.attributes.index==0 ? 'block' : 'none')}}, el(InnerBlocks.Content)); }, }) /* TAB WRAPPER WITH AMOUNT SELECTOR */ wp.blocks.registerBlockType( 'ads/tabblock', { title: 'Tab Block', icon: { background: '#eee', foreground:'#000', src:'schedule' }, category: 'agendahelsinki', attributes: { tabamount:{type: 'integer',default:2}, selectedstyle:{type: 'string',default:'horizontal'}, tab0:{type:'string',default:''}, tab1:{type:'string',default:''}, tab2:{type:'string',default:''}, tab3:{type:'string',default:''}, tab4:{type:'string',default:''}, tab5:{type:'string',default:''}, tab6:{type:'string',default:''}, tab7:{type:'string',default:''}, tab8:{type:'string',default:''}, tab9:{type:'string',default:''}, date:{type:'string',default:'notset'} }, supports:{defaultStylePicker:false}, edit: function ( props ) { const { setAttributes, attributes, clientId } = props; const { tabs } = useSelect(select => ({ tabs: select("core/block-editor").getBlocks(clientId) })); if(attributes.date==='notset'){ setAttributes({date:new Date().valueOf().toString(36)}); } // Update the child block's attributes var {child} = useSelect(select => ({ child: select("core/block-editor").getBlocksByClientId(clientId)[0].innerBlocks })); if(child.length){ child.forEach((element) => { if(element.setdate!='notset'){ wp.data.dispatch("core/block-editor").updateBlockAttributes(element.clientId, { setdate:attributes.date }); console.log('Rewrote list item dates'); } }); } /* LOOP TEXTCONTROLS TO BLOCK ACCORDING TO AMOUNT OF TABS */ let listitemnames=[]; for(var p=0;p
Tab {(p+1)} link name
{ if(attributes[selectedarr]!=newval) { setAttributes({[selectedarr]:newval}); } }} /> ); } /* Good practise to wrap return in */ return (
{ // DO THINGS ONLY IN ACTUAL CHANGE if(attributes.tabamount!=newtabamount) { // SET NEW TAB AMOUNT setAttributes({tabamount:newtabamount}); // REWRITE INNERBLOCKS ONLY IF NEW SLIDER VALUE let inner_blocks = tabs; if (tabs.length < newtabamount) { inner_blocks = [ ...inner_blocks, ...times(newtabamount - tabs.length, (i) => wp.blocks.createBlock('ads/tab',{index:(tabs.length+i), setdate:attributes.date}) ) ]; } else if (tabs.length > newtabamount) { inner_blocks = inner_blocks.slice(0, newtabamount); } const clientIds = tabs.map( block => block.clientId ); wp.data.dispatch( 'core/block-editor' ).removeBlocks( clientIds ); wp.data.dispatch( 'core/block-editor' ).insertBlocks( inner_blocks, 0, clientId ); } }} />
{listitemnames}
); }, save: function (props) { let elarr=[]; const { attributes } = props; for(var i=0;i { var t=jQuery(this), elems=jQuery(this).parent().parent().find(".tab-link").not(this), count = elems.length; elems.attr("aria-selected","false"); t.attr("aria-selected","true"); elems.each(function(i){ if (!--count) jQuery("#"+jQuery(this).attr("aria-controls")).fadeOut(400,function(){ jQuery("#"+t.attr("aria-controls")).fadeIn(400); }); else jQuery("#"+jQuery(this).attr("aria-controls")).fadeOut(400); }); }} style={{ display:'inline-block' }}> {attributes[selectedarr]} ); } return(
{elarr}
); } } );