window.vahara.siteLibsLoaded(function() { var $ = window.vahara.jQuery; var jQuery = window.vahara.jQuery; var v = window.vahara; // prepare to run any html preparsers needed when we get new widget html v.htmlPreParsers = []; window.vahara.addPreParser = function(callback) { v.htmlPreParsers.push(callback); }; // In case of responsive image srcsets // This method replaces the srcset with updated image // whenever an image is inserted or repalced v.replaceSrcSet = function(target, imageUrl) { if (typeof target === 'string') { target = $('body').find('img#'+target); } if (target !== null) { const source = target.prev(); if (typeof source !== 'undefined' && source.length === 1 && source[0].nodeName.toLowerCase() === 'source') { const srcSet = source.attr('srcset'); let newSrcSetPieces = []; let result = null; if (srcSet.length > 1) { const sourceRegexUrl = /^http[s]*:\/\/.*\/.*\.(avif|webp|png|jpeg|jpg|gif|bmp)\?/gi; const sourceRegexEmptySrcSet = /([\d\w]+).(avif|webp|png|jpeg|jpg|gif|bmp)\?/gi; const srcSetPieces = srcSet.split(','); for (var i in srcSetPieces) { if (srcSetPieces[i].trim().match(sourceRegexUrl)) { result = srcSetPieces[i].trim().replace(sourceRegexUrl, imageUrl+'?'); } else if (srcSetPieces[i].trim().match(sourceRegexEmptySrcSet)) { result = srcSetPieces[i].trim().replace(sourceRegexEmptySrcSet, imageUrl+'?'); } newSrcSetPieces.push(result); } source.attr('srcset', newSrcSetPieces.join(', ')); } } } }; // If we are dropping widgets and using an image host, optimize first then trigger widget-dropped v.onEvent('vh-editor:widget-dropped-image-optimize', function(e) { v.optimizeImages(function() { v.triggerEvent('vh-editor:widget-dropped', { element: e.element, type: e.type }); }, $(e.element)); }); //Update the html after widget updated. v.onEvent('updateElementFromVahara', function(event) { let $node = event.node; let widgetId = event.widgetId; let elementId = event.elementId; if ($node === undefined) { $node = $('.vhc-js-element[data-widget-id="' + widgetId + '"][data-element-id="' + elementId + '"]'); } else if (widgetId === undefined && elementId === undefined) { widgetId = $node.attr('data-widget-id'); elementId = $node.attr('data-element-id'); } //Load the updated html for the widget. if (elementId && widgetId) { $.ajax(v.serverUrl + '/remote-site/access/multiple-widgets-load', { type: 'GET', data: 'e=' + widgetId + '_' + elementId, xhrFields: { withCredentials: true }, dataType: 'json', success:function(response) { if (response.status === 'ok') { let widgetHtml = response.elements[widgetId + '_' + elementId]; // pass the HTML to each defined preparser for any updates needed v.htmlPreParsers.forEach(function(callback) { widgetHtml = callback(widgetHtml); }); $node.html(widgetHtml); if (event.callback) { event.callback($node); } window.vahara.triggerEvent('elementUpdated', {node:$node[0]}); } } }); } }); v.triggerEvent('vh-show-edit-buttons'); v.showEditButtons = true; v.getEditorItemId = function() { var $display = $('.vh-js-display'); if ($display.length) { return $display.attr('data-vh-display-id'); } else { var $item = $('.vhc-js-widget-drop'); return $item.attr('data-vhc-id'); } }; window.vahara.gotIframeEditBootMessage = false; // Load the editor button if we aren't already in an iframe if (window === window.top) { v.loadScript(v.serverUrl + '/js/editor/vahara-edit-button.js?r=285'); } v.remoteEditorLibsLoaded = function (callback) { // If libs have loaded already, call the code now if (v.remoteEditorLibsLoadedFlag) { callback(); // If not, launch them when they are loaded } else { v.onEvent('vaharaRemoteEditorLibsLoaded', function (e) { callback(); }); } }; v.onEvent('vh-login:logged-out', function(data) { $('.vhe-js-go-admin').remove(); $('.vhe-js-activate-edit').remove(); }); v.bootEditing = function() { // Select the original element by its class const originalDiv = document.querySelector('.vhc-js-widget-drop'); if (originalDiv) { // Clone the original element window.vaharaWidgetDropCloneDiv = originalDiv.cloneNode(true); // Deep clone including child nodes // Remove the class vhc-js-widget-drop from the duplicate window.vaharaWidgetDropCloneDiv.classList.remove('vhc-js-widget-drop'); // Set the minimum height of the duplicate to 1800px window.vaharaWidgetDropCloneDiv.style.minHeight = '2800px'; // Remove the vhc-editable class from all elements within the duplicate const editableElements = window.vaharaWidgetDropCloneDiv.querySelectorAll('.vhc-editable'); editableElements.forEach(element => { element.classList.remove('vhc-editable'); }); // Insert the duplicate directly before the original, this shouldn't be visible in the browser originalDiv.parentNode.insertBefore(window.vaharaWidgetDropCloneDiv, originalDiv); } else { console.error('Element with class "vhc-js-widget-drop" not found.'); } //console.log('boot edit user id'); //console.log(v.userId); $('.vhe-js-activate-edit').hide(); if ($('.vh-js-disable-edit').length) { if (!v.editDisableHandled) { v.editDisableHandled = true; var message = $('.vh-js-disable-edit').attr('data-message'); alert(message); parent.postMessage({action: 'abortEdit', projectId: v.projectId, link: window.location.href}, '*'); } return; } v.triggerEvent('vh-booting-editing'); window.vahara.gotIframeEditBootMessage = true; v.editMode = true; v.froalaColors = null; v.froalaFonts = null; v.froalaHideFonts = null; v.froalaHideFontSize = null; v.injectCss = ''; v.loadScript(v.serverUrl + '/collections/js/vahara-remote-editor-libs.js'); v.loadCss(v.serverUrl + '/collections/css/vahara-remote-editor-libs.css'); v.loadScript(v.serverUrl + '/js/promise-polyfill.min.js'); v.loadScript(v.serverUrl + '/vf/froala-custom/v4/froala-interactions-plugin-v4.js'); v.loadScript(v.serverUrl + '/vf/drag-edit/drag-edit.js?r=285'); v.loadScript(v.serverUrl + '/vf/superframe/superframe.js'); v.loadScript(v.serverUrl + '/vf/superframe/modal.js'); v.loadCss(v.serverUrl + '/vf/drag-edit/drag-edit.css?r=285'); v.loadCss(v.serverUrl + '/vf/drag-edit/drag-edit-page.css?r=285'); v.loadScript('/js/libs/polyfill/v3/polyfill-promise-fetch.min.js?v=285', null, false, true, true, true); v.widgetStatus = 'on'; v.widgetBarStatus = 'open'; v.widgetBarPositionX = ''; v.widgetBarPositionY = ''; // Let drag-edit.js do this-- //v.triggerEvent('vh-booted-editing'); }; window.addEventListener('message', function(e) { if (e.data == 'vahara-editor-iframe-started' && !window.vahara.gotIframeEditBootMessage) { v.bootEditing(); } else if (e.data.key && e.data.key === 'page_slug_updated') { if (e.data.data.old_slug && e.data.data.new_slug) { if (window.location.pathname === '/') { if (e.data.data.old_slug === 'index') { if (e.data.data.new_slug !== 'index') { var newPathName = '/' + e.data.data.new_slug; newPathName = newPathName.replace(/^\/+/, ''); //console.log('new path name: ' + newPathName); window.location.pathname = newPathName; } else { window.location.reload(); } } } else { var oldSlugRegex = new RegExp(e.data.data.old_slug, 'i'); if (window.location.pathname.match(oldSlugRegex)) { var newPathName = window.location.pathname.replace(oldSlugRegex, e.data.data.new_slug); newPathName = newPathName.replace(/^\/+/, ''); //console.log('new path name: ' + newPathName); window.location.pathname = '/' + newPathName; } } } } }, false); }); window.addEventListener('message', function(event) { // Check if the message is requesting location // iframe location changed message to parent if (event.data === 'getLocation') { // Send the location back to the parent var locationData = window.location.href; window.parent.postMessage({action: 'iframeLocationChanged', link: window.location.href}, '*'); } }); // Notify parent about whether this page contains a vhc-js-widget-drop element. // This is used when the parent cannot access the iframe DOM (cross-origin). function reportVhcDropPresence() { try { var hasDrop = !!document.querySelector('.vhc-js-widget-drop'); window.parent.postMessage({action: 'vhc-drop-presence', hasDrop: hasDrop, viewUrl: window.location.href}, '*'); } catch (e) { // Silently ignore } } if (document.readyState === 'complete' || document.readyState === 'interactive') { reportVhcDropPresence(); } else { document.addEventListener('DOMContentLoaded', function() { reportVhcDropPresence(); }); } // Also send again after a short delay in case content is rendered dynamically setTimeout(reportVhcDropPresence, 500);