Skip to content

Commit

Permalink
send manual inits on load
Browse files Browse the repository at this point in the history
  • Loading branch information
cea2aj committed Jun 1, 2021
1 parent 30d2e07 commit c52db98
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
14 changes: 14 additions & 0 deletions static/js/answers-experience-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { sendToIframe } from './iframe-common';
export default class AnswersExperienceFrame {
constructor (runtimeConfig) {
this.runtimeConfig = runtimeConfig;
this._hasManuallyInitialized = false;

runtimeConfig._onUpdate(updatedConfig => {
sendToIframe({ runtimeConfig: updatedConfig });
Expand All @@ -16,12 +17,25 @@ export default class AnswersExperienceFrame {
* @param {Object} config
*/
init (config) {
if (this.hasManuallyInitialized()) {
return;
}
Object.entries(config).forEach(([key, value]) => {
this.runtimeConfig.set(key, value);
});
sendToIframe({
initAnswersExperience: true,
runtimeConfig: this.runtimeConfig.getAll()
});
this._hasManuallyInitialized = true;
}

/**
* Returns whether or not the init function has been called
*
* @returns {boolean}
*/
hasManuallyInitialized () {
return this._hasManuallyInitialized;
}
}
15 changes: 8 additions & 7 deletions static/js/iframe-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const iframeMessageQueue = [];
* Puts an iframe on the page of an Answers experience and sets up resizing and cross-domain communication
*
* @param {string} domain The location of the answers experience
* @param {RuntimeConfig} runtimeConfig used for passing runtime config to the iframe
* @param {AnswersExperienceFrame} answersExperienceFrame
*/
export function generateIFrame(domain, runtimeConfig) {
export function generateIFrame(domain, answersExperienceFrame) {
var isLocalHost = window.location.host.split(':')[0] === 'localhost';
var containerEl = document.querySelector('#answers-container');
var iframe = document.createElement('iframe');
Expand Down Expand Up @@ -95,12 +95,13 @@ export function generateIFrame(domain, runtimeConfig) {
checkOrigin: false,
onInit: function() {
iframeInitialized = true;
runtimeConfig && iframeMessageQueue.push({
runtimeConfig: runtimeConfig.getAll()
});
iframeMessageQueue.forEach(message => {
sendToIframe(message);
iframeMessageQueue.push({
initAnswersExperience: answersExperienceFrame.hasManuallyInitialized(),
runtimeConfig: answersExperienceFrame.runtimeConfig.getAll()
});
while (iframeMessageQueue.length !== 0) {
sendToIframe(iframeMessageQueue.shift());
}
},
onMessage: function(messageData) {
const message = JSON.parse(messageData.message);
Expand Down
5 changes: 3 additions & 2 deletions static/js/iframe-prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import RuntimeConfig from './runtime-config';
import AnswersExperienceFrame from './answers-experience-frame';

const runtimeConfig = new RuntimeConfig();
window.AnswersExperienceFrame = new AnswersExperienceFrame(runtimeConfig);
const answersExperienceFrame = new AnswersExperienceFrame(runtimeConfig);
window.AnswersExperienceFrame = answersExperienceFrame;

const prodDomain = new InjectedData().getProdDomain();
generateIFrame(prodDomain, runtimeConfig);
generateIFrame(prodDomain, answersExperienceFrame);
5 changes: 3 additions & 2 deletions static/js/iframe-staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import RuntimeConfig from './runtime-config';
import AnswersExperienceFrame from './answers-experience-frame';

const runtimeConfig = new RuntimeConfig();
window.AnswersExperienceFrame = new AnswersExperienceFrame(runtimeConfig);
const answersExperienceFrame = new AnswersExperienceFrame(runtimeConfig);
window.AnswersExperienceFrame = answersExperienceFrame;

const stagingDomain = new InjectedData().getStagingDomain();
generateIFrame(stagingDomain, runtimeConfig);
generateIFrame(stagingDomain, answersExperienceFrame);
5 changes: 3 additions & 2 deletions static/js/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import RuntimeConfig from './runtime-config';
import AnswersExperienceFrame from './answers-experience-frame';

const runtimeConfig = new RuntimeConfig();
window.AnswersExperienceFrame = new AnswersExperienceFrame(runtimeConfig);
const answersExperienceFrame = new AnswersExperienceFrame(runtimeConfig);
window.AnswersExperienceFrame = answersExperienceFrame;

const domain = new InjectedData().getDomain();
generateIFrame(domain, runtimeConfig);
generateIFrame(domain, answersExperienceFrame);

0 comments on commit c52db98

Please sign in to comment.