/** * THECHURCHCO EMBEDS * -------------------------- * Clean and Simple. @thechurchco snippets */ (function () { var TheChurchCo_Embeds = { embeds: {}, init: function () { // Get Snippets var snippets = document.querySelectorAll('thechurchco'); if (snippets.length <= 0) return; // Convert Snippets to Embeds snippets.forEach(TheChurchCo_Embeds.convertSnippetToEmbed) }, convertSnippetToEmbed: function (element, index) { // Get the embed an id var snippetId = 'thechurchco_embeb_' + index; // Create an Iframe var iFrame = document.createElement('iframe'); iFrame.src = element.getAttribute('src'); iFrame.frameBorder = 0; iFrame.id = snippetId; // Set The Width & Height iFrame.style.width = '100%'; iFrame.style.height = '0px'; iFrame.style.transition = 'height 500ms'; // Replace Element element.parentNode.replaceChild(iFrame, element); // Save elements TheChurchCo_Embeds.embeds[snippetId] = { initCount: 0, element: iFrame, id: snippetId, } // Initialize Embed TheChurchCo_Embeds.initEmbed(snippetId, iFrame); }, initEmbed: function (snippetId, element) { if (element.contentWindow.document) { TheChurchCo_Embeds.onEmbedLoaded(snippetId, element); } else { TheChurchCo_Embeds.embeds[snippetId].initCount++; if (TheChurchCo_Embeds.embeds[snippetId].initCount < 3) { setTimeout(TheChurchCo_Embeds.initEmbed(snippetId, element), 2000); } else { console.log("ERROR: The Embed could not be loaded"); } } }, onEmbedLoaded: function (snippetId, element) { // Set Height on Load TheChurchCo_Embeds.setHeight(snippetId, TheChurchCo_Embeds.embeds[snippetId].element.contentWindow.document.documentElement.scrollHeights); }, setHeight: function (snippetId, height) { TheChurchCo_Embeds.embeds[snippetId].element.style.height = (parseInt(height) + 100) + 'px'; }, handleResizeMessage: function (e) { // Match Orgins var origin = e.origin; var data = e.data.data; Object.keys(TheChurchCo_Embeds.embeds).forEach(function (snippetId) { var embedSrc = TheChurchCo_Embeds.embeds[snippetId].element.src; if (embedSrc === data.origin || data.origin.indexOf('ccb') > -1) { // CCB Subpage Click throughs // Resize... TheChurchCo_Embeds.setHeight(snippetId, data.height) } }) }, parseMessage: function (e) { var data = e.data; // Handle Resize if (data.action === 'RESIZE_PARENT') TheChurchCo_Embeds.handleResizeMessage(e); } } // Reader Set Go window.TheChurchCo_Embeds = TheChurchCo_Embeds; document.addEventListener("DOMContentLoaded", function () { TheChurchCo_Embeds.init(); }); window.addEventListener("message", TheChurchCo_Embeds.parseMessage, true); })()