mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 11:08:04 +00:00 
			
		
		
		
	 bc8736c08e
			
		
	
	bc8736c08e
	
	
	
		
			
			* feat: add ids to each element * feat: restore elements for apps that use view transitions * fix: remove generator check condition * feat: handle turbolinks * fix: new body handling * chore: undo debug changes
		
			
				
	
	
		
			75 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import { SDK_CSS } from './sdk.js';
 | |
| import { IFrameHelper } from './IFrameHelper';
 | |
| 
 | |
| export const loadCSS = () => {
 | |
|   const css = document.createElement('style');
 | |
|   css.innerHTML = `${SDK_CSS}`;
 | |
|   css.id = 'cw-widget-styles';
 | |
|   document.body.appendChild(css);
 | |
| };
 | |
| 
 | |
| // This is a method specific to Turbo
 | |
| // The body replacing strategy removes Chatwoot styles
 | |
| // as well as the widget, this help us get it back
 | |
| export const restoreElement = (id, newBody) => {
 | |
|   const element = document.getElementById(id);
 | |
|   const newElement = newBody.querySelector(`#${id}`);
 | |
| 
 | |
|   if (element && !newElement) {
 | |
|     newBody.appendChild(element);
 | |
|   }
 | |
| };
 | |
| 
 | |
| export const restoreWidgetInDOM = newBody => {
 | |
|   restoreElement('cw-bubble-holder', newBody);
 | |
|   restoreElement('cw-widget-holder', newBody);
 | |
|   restoreElement('cw-widget-styles', newBody);
 | |
| };
 | |
| 
 | |
| export const addClasses = (elm, classes) => {
 | |
|   elm.classList.add(...classes.split(' '));
 | |
| };
 | |
| 
 | |
| export const toggleClass = (elm, classes) => {
 | |
|   elm.classList.toggle(classes);
 | |
| };
 | |
| 
 | |
| export const removeClasses = (elm, classes) => {
 | |
|   elm.classList.remove(...classes.split(' '));
 | |
| };
 | |
| 
 | |
| export const onLocationChange = ({ referrerURL, referrerHost }) => {
 | |
|   IFrameHelper.events.onLocationChange({
 | |
|     referrerURL,
 | |
|     referrerHost,
 | |
|   });
 | |
| };
 | |
| 
 | |
| export const onLocationChangeListener = () => {
 | |
|   let oldHref = document.location.href;
 | |
|   const referrerHost = document.location.host;
 | |
|   const config = {
 | |
|     childList: true,
 | |
|     subtree: true,
 | |
|   };
 | |
|   onLocationChange({
 | |
|     referrerURL: oldHref,
 | |
|     referrerHost,
 | |
|   });
 | |
| 
 | |
|   const bodyList = document.querySelector('body');
 | |
|   const observer = new MutationObserver(mutations => {
 | |
|     mutations.forEach(() => {
 | |
|       if (oldHref !== document.location.href) {
 | |
|         oldHref = document.location.href;
 | |
|         onLocationChange({
 | |
|           referrerURL: oldHref,
 | |
|           referrerHost,
 | |
|         });
 | |
|       }
 | |
|     });
 | |
|   });
 | |
| 
 | |
|   observer.observe(bodyList, config);
 | |
| };
 |