jQuery(document).ready(function () {
var playCanvasLoaded = false;
var playCanvasAppStarted = false;
window.addEventListener('message', function (event) {
console.log('Message received:', event.data);
if (event.data.type === 'PlayCanvasAppStarted') {
console.log('PlayCanvas app started, waiting for assets to load...');
playCanvasAppStarted = true;
} else if (event.data.type === 'PlayCanvasLoaded') {
console.log('PlayCanvas assets loaded, sending feature options...');
playCanvasLoaded = true;
fetchAllFeatureOptions();
}
});
function fetchFeatureOption(feature, data) {
console.log('Received data for feature', feature, ':', data);
if (data && data[0] && typeof data[0].option !== 'undefined') {
var option = data[0].option;
var iframe = document.getElementById('playcanvas-iframe');
console.log('Sending feature option to iframe:', feature, option);
iframe.contentWindow.postMessage({ type: 'FeatureOption', feature: feature, option: option }, '*');
}
}
function fetchAllFeatureOptions() {
console.log('Checking conditions for fetching all feature options:', playCanvasLoaded, playCanvasAppStarted);
if (playCanvasLoaded && playCanvasAppStarted) {
console.log('Fetching all feature options');
console.log('customData:', customData);
fetchFeatureOption('Car', customData.car);
fetchFeatureOption('CarColor', customData.carColor);
fetchFeatureOption('InteriorColor', customData.interiorColor);
fetchFeatureOption('Wheels', customData.wheels);
fetchFeatureOption('Engine', customData.engine);
fetchFeatureOption('MetalsTrim', customData.metalsTrim);
}
}
// Check if the PlayCanvas scene is already loaded and started before adding the event listener
if (playCanvasLoaded && playCanvasAppStarted) {
fetchAllFeatureOptions();
}
});