import './style.scss';
import { Button, ToggleControl } from '@wordpress/components';
import { useCallback, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import ControlsRender from '../controls-render';
import StepsWizard from './steps-wizard';
const { plugin_name: pluginName } = window.VPGutenbergVariables;
const NOTICE_LIMIT = parseInt(
window.VPGutenbergVariables.items_count_notice_limit,
10
);
function renderControls(props, category) {
return ;
}
function hasLayoutElement(element, attributes) {
const { layout_elements: layoutElements } = attributes;
const checkIn = element === 'filter' ? 'top' : 'bottom';
return (
typeof layoutElements[checkIn] !== 'undefined' &&
layoutElements[checkIn]?.elements.includes(element)
);
}
function toggleLayoutElement(element, attributes) {
const { layout_elements: layoutElements } = attributes;
const checkIn = element === 'filter' ? 'top' : 'bottom';
if (
typeof layoutElements[checkIn] === 'undefined' ||
!layoutElements[checkIn]?.elements
) {
return layoutElements;
}
const result = JSON.parse(JSON.stringify(layoutElements));
if (hasLayoutElement(element, attributes)) {
result[checkIn].elements = [];
} else {
result[checkIn].elements = [element];
}
return result;
}
/**
* Component Class
*
* @param props
*/
export default function SetupWizard(props) {
const { attributes, setAttributes } = props;
const {
align,
content_source: contentSource,
items_click_action: clickAction,
layout,
images,
} = attributes;
const [step, setStep] = useState(0);
const [allowNextStep, setAllowNextStep] = useState(false);
const maxSteps = 2.5;
// Add startup attributes.
useEffect(() => {
if (!align && !contentSource) {
setAttributes({ align: 'wide' });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Set some starter attributes for different content sources.
// And hide the setup wizard.
const setStarterAttributes = useCallback(() => {
let newAttributes = {};
switch (contentSource) {
case 'images':
// Hide setup wizard once user select images.
if (images && images.length) {
newAttributes = {
...newAttributes,
items_count: -1,
items_click_action: 'popup_gallery',
};
// Add infinite scroll to the gallery when user adds a lot of images.
if (layout !== 'slider' && images.length > NOTICE_LIMIT) {
newAttributes = {
...newAttributes,
items_count: NOTICE_LIMIT,
layout_elements: {
top: {
elements: [],
align: 'center',
},
items: {
elements: ['items'],
},
bottom: {
elements: ['pagination'],
align: 'center',
},
},
pagination: 'infinite',
pagination_hide_on_end: true,
};
}
}
break;
case 'post-based':
case 'social-stream':
newAttributes = {
...newAttributes,
layout_elements: {
top: {
elements: [],
align: 'center',
},
items: {
elements: ['items'],
},
bottom: {
elements: ['pagination'],
align: 'center',
},
},
};
break;
// no default
}
// Prepare better default settings for Popup.
// We can't change defaults of registered controls because it may break existing user galleries.
// This is why we change it here, in the Setup Wizard.
newAttributes = {
...newAttributes,
items_click_action_popup_title_source:
contentSource === 'post-based' ? 'title' : 'item_title',
items_click_action_popup_description_source:
contentSource === 'post-based'
? 'description'
: 'item_description',
items_click_action_popup_deep_link_pid: 'filename',
};
setAttributes(newAttributes);
setAllowNextStep(true);
}, [contentSource, images, layout, setAttributes]);
useEffect(() => {
if (contentSource) {
setStarterAttributes();
}
// We have to check for contentSource change here because we don't want to run this on every render.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contentSource]);
return (
{/* Step 0: Content Source */}
{pluginName}
more options will be available in the block settings. Select the Content Source first:',
'visual-portfolio'
),
}}
/>
{renderControls(props, 'content-source')}
{renderControls(props, 'content-source-images')}
{renderControls(props, 'content-source-social-stream')}
{/* Step 1: Items Style */}
{__('Items Style', 'visual-portfolio')}
More style settings will be available in the block settings.',
'visual-portfolio'
),
}}
/>
{renderControls(props, 'items-style')}
{/* Step 2: Layout Elements */}