From 4cdf51ccb39d2cccb8d321d9871f7f9d2bef5041 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Sat, 28 Jul 2018 23:52:03 -0400 Subject: [PATCH] feat(createViewer): Add interpolation enabled observer method --- src/createViewer.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/createViewer.js b/src/createViewer.js index 6e3969ff..c48a5bd0 100644 --- a/src/createViewer.js +++ b/src/createViewer.js @@ -223,15 +223,34 @@ const createViewer = ( } - let interpolationEnabled = true; - publicAPI.setInterpolationEnabled = (interpolation) => { - const toggleInterpolationButton = document.getElementById(`${viewerDOMId}-toggleInterpolationButton`); - if (interpolation && !interpolationEnabled || !interpolation && interpolationEnabled) { - interpolationEnabled = !interpolationEnabled; + const toggleInterpolationButton = document.getElementById(`${viewerDOMId}-toggleInterpolationButton`); + + const toggleInterpolationHandlers = []; + const toggleInterpolationButtonListener = (event) => { + const enabled = toggleInterpolationButton.checked; + toggleInterpolationHandlers.forEach((handler) => { + handler.call(null, enabled); + }) + } + toggleInterpolationButton.addEventListener('click', toggleInterpolationButtonListener) + + publicAPI.subscribeToggleInterpolation = (handler) => { + const index = toggleInterpolationHandlers.length; + toggleInterpolationHandlers.push(handler); + function unsubscribe() { + toggleInterpolationHandlers[index] = null; + } + return Object.freeze({ unsubscribe }); + } + + publicAPI.setInterpolationEnabled = (enabled) => { + const interpolation = toggleInterpolationButton.checked; + if (enabled && !interpolation || !enabled && interpolation) { toggleInterpolationButton.click(); } } + publicAPI.setViewMode = (mode) => { switch(mode) { case 'XPlane':