Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jinja2/includes/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@
{% endif %}
{% endfor %}
{% endif %}

// interactive editor config
win.mdn.interactiveEditor = {
siteUrl: "{{ settings.SITE_URL }}",
editorUrl: "{{ settings.INTERACTIVE_EXAMPLES_BASE }}"
};

})(this);
</script>
4 changes: 4 additions & 0 deletions kuma/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def __call__(self, value):
STAGING_DOMAIN = 'developer.allizom.org'
STAGING_URL = PROTOCOL + STAGING_DOMAIN

INTERACTIVE_EXAMPLES_BASE = config(
'https://interactive-examples.mdn.mozilla.net',
default='https://interactive-examples.mdn.mozilla.net')

MAINTENANCE_MODE = config('MAINTENANCE_MODE', default=False, cast=bool)
ALLOW_ROBOTS = config('ALLOW_ROBOTS', default=False, cast=bool)

Expand Down
3 changes: 2 additions & 1 deletion kuma/static/js/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* @param {Object} event - The event Object received from the postMessage
*/
interactiveExamplesEvent: function(event) {
if (event.origin !== 'https://interactive-examples.mdn.mozilla.net') {
var allowedOrigin = win.mdn.interactiveEditor.editorUrl || 'https://interactive-examples.mdn.mozilla.net';
if (event.origin !== allowedOrigin) {
return false;
}
mdn.analytics.trackEvent(event.data);
Expand Down
36 changes: 26 additions & 10 deletions kuma/static/js/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

var iframe = document.querySelector('iframe.interactive');
var mediaQuery = window.matchMedia('(min-width: 63.9385em)');
var siteUrl =
window.mdn.interactiveEditor.siteUrl || 'https://developer.mozilla.org';
var targetOrigin =
window.mdn.interactiveEditor.editorUrl ||
'https://interactive-examples.mdn.mozilla.net';

/* If there is no `iframe`, or if this is a JS example,
simply return */
Expand All @@ -13,32 +18,43 @@
/**
* A simple wrapper function for the `postMessage`s sent
* to the interactive editor iframe
* @param {Boolean} isSmallViewport - Boolean indicating whether to add, or
* remove the `small-desktop-and-below` class
* @param {Object} message - The message object sent to the interactive editor
*/
function postToEditor(isSmallViewport) {
iframe.contentWindow.postMessage(
{ smallViewport: isSmallViewport },
'https://interactive-examples.mdn.mozilla.net'
);
function postToEditor(message) {
iframe.contentWindow.postMessage(message, targetOrigin);
}

/**
* Posts back the current site URL.
* @param {Object} event - The event Object received from postMessage
*/
function postSiteUrl(event) {
/* only post the site url if the correct property
exists on the message object, and its value is true */
if (event.data.siteUrl) {
postToEditor({ siteUrl: siteUrl });
}
}

/* As the user sizes the browser or tilts their device,
listen for mediaQuery events and communicate the new
viewport state to the interactive editor */
mediaQuery.addListener(function(event) {
if (event.matches) {
postToEditor(false);
postToEditor({ smallViewport: false });
} else {
postToEditor(true);
postToEditor({ smallViewport: true });
}
});

window.onload = function() {
// if the mediaQuery does not match on load
if (!mediaQuery.matches) {
// add the class `small-desktop-and-below`
postToEditor(true);
postToEditor({ smallViewport: true });
}

// add event listener for postMessages from the interactive editor
window.addEventListener('message', postSiteUrl, false);
};
})();