From 87eab7573c71b1d178fa60482f84d7526fe69a44 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Thu, 30 Nov 2017 12:26:40 +0000 Subject: [PATCH] Handle widget content loaded events. --- src/WidgetMessaging.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/WidgetMessaging.js b/src/WidgetMessaging.js index 7fe745fc81..5bb60702ad 100644 --- a/src/WidgetMessaging.js +++ b/src/WidgetMessaging.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import dis from './dispatcher'; + let listenerCount = 0; let messageEndpoints = []; @@ -28,13 +30,25 @@ function onMessage(event) { } // Event origin is empty string if undefined - if (event.origin.length === 0 || trustedEndpoint(event.origin) || !event.data.widgetData) { - console.warn("Ignoring postMessage", event); + if ( + event.origin.length === 0 || + trustedEndpoint(event.origin) || + !event.data.widgetData || + !event.data.widgetId + ) { return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise } - // TODO -- handle widget actions - alert(event.data.widgetData); + const widgetData = event.data.widgetData; + const widgetId = event.data.widgetId; + if (widgetData.action == 'content_loaded') { + dis.dispatch({ + action: 'widget_content_loaded', + widgetId: widgetId, + }); + } else { + console.warn("Widget postMessage event unhandled"); + } } /**