/** * Sets listener and attributes on model-viewer to * allow for click registering of a new hotspot */ readyAddHotspot = function() { const previewMv = $('model-viewer') previewMv.one('click', clickAddHotspot) previewMv.addClass('AddingHotspot') previewMv[0].disableTap = true } /** * Event listener callback to retrieve the info * about the model surface point selected by the * mouse and add that information to the editor * text input * * @param {PointerEvent} e */ clickAddHotspot = function(e) { const previewMv = $('model-viewer') previewMv.removeClass('AddingHotspot') let hsPosition = null; let targetModel = previewMv[0] targetModel.disableTap = false if (targetModel) { hsPosition = targetModel.positionAndNormalFromPoint(e.clientX, e.clientY); } if (hsPosition) { let currentText = $('#wpTextbox1').val(); let extractMetadata = currentText.match(/
([\S\s]*?)<\/pre>/);
        let metadata = (extractMetadata.length >= 2) ? JSON.parse(extractMetadata[1]) : {viewerConfig: {}, annotations: [], annotationSets: {}};
        if (metadata.annotations === undefined) {
            metadata.annotations = [];
        }
        let hsOutput = {label: 'NewHotspot'};
        hsOutput['data-position'] = hsPosition.position.toString().replaceAll(/(\d{5})(\d*?m)/g,"$1m");
        hsOutput['data-normal'] = hsPosition.normal.toString().replaceAll(/(\d{5})(\d*?m)/g,"$1m");
        let orbitObj = targetModel.getCameraOrbit()
        hsOutput['data-orbit'] = `${orbitObj.theta.toFixed(2)}rad ${orbitObj.phi.toFixed(2)}rad ${orbitObj.radius.toFixed(2)}m`;
        let targetObj = targetModel.getCameraTarget();
        hsOutput['data-target'] = `${targetObj.x.toFixed(5)}m ${targetObj.y.toFixed(5)}m ${targetObj.z.toFixed(5)}m`;
        //navigator.clipboard.writeText(JSON.stringify(hsOutput, null, 2));
        metadata.annotations.push(hsOutput);
        let newText = currentText.replace(/(.*?
)[\S\s]*?(<\/pre>.*)/,`$1\n${JSON.stringify(metadata, null, 2)}\n$2`);
        $('#wpTextbox1').val(newText);
    }
};

/**
 * Event listener callback to toggle the visibility
 * of a hotspot's annotation when the hotspot is 
 * clicked
 * 
 * @param {PointerEvent} e 
 */
onAnnotation = function(e) {
	e.stopPropagation();
	let targetAnnotation = e.target.slot.split('-')[1];
	let targetModel = e.target.closest('model-viewer');
	[...targetModel.querySelectorAll('.HotspotAnnotation')].forEach( (an, idx) => {
		if (idx + 1 == targetAnnotation && an.classList.contains('HiddenAnnotation')) {
			an.classList.remove('HiddenAnnotation');
			if (an.parentElement.dataset.target) {targetModel.cameraTarget = an.parentElement.dataset.target}
			if (an.parentElement.dataset.orbit) {targetModel.cameraOrbit = an.parentElement.dataset.orbit}
		} else {
			an.classList.add('HiddenAnnotation');
		}
	});
};

/**
 * Event listener callback to hide all hotspot
 * annotations when the model-viewer receives
 * a click event
 */
clearAnnotations = function() {
	[...document.getElementsByClassName('HotspotAnnotation')].forEach( an => {
		an.classList.add('HiddenAnnotation');
	});
};