chore: Remove unnecessary methods and polyfills (#5614)

This commit is contained in:
David Kubeš 2022-10-12 23:42:06 +02:00 committed by GitHub
parent fca629a32a
commit 0c8f744c33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 73 deletions

View file

@ -10,7 +10,7 @@ import {
getUserCookieName, getUserCookieName,
hasUserKeys, hasUserKeys,
} from '../sdk/cookieHelpers'; } from '../sdk/cookieHelpers';
import { addClass, removeClass } from '../sdk/DOMHelpers'; import { addClasses, removeClasses } from '../sdk/DOMHelpers';
import { SDK_SET_BUBBLE_VISIBILITY } from 'shared/constants/sharedFrameEvents'; import { SDK_SET_BUBBLE_VISIBILITY } from 'shared/constants/sharedFrameEvents';
const runSDK = ({ baseUrl, websiteToken }) => { const runSDK = ({ baseUrl, websiteToken }) => {
if (window.$chatwoot) { if (window.$chatwoot) {
@ -41,12 +41,12 @@ const runSDK = ({ baseUrl, websiteToken }) => {
let widgetElm = document.querySelector('.woot--bubble-holder'); let widgetElm = document.querySelector('.woot--bubble-holder');
let widgetHolder = document.querySelector('.woot-widget-holder'); let widgetHolder = document.querySelector('.woot-widget-holder');
if (visibility === 'hide') { if (visibility === 'hide') {
addClass(widgetHolder, 'woot-widget--without-bubble'); addClasses(widgetHolder, 'woot-widget--without-bubble');
addClass(widgetElm, 'woot-hidden'); addClasses(widgetElm, 'woot-hidden');
window.$chatwoot.hideMessageBubble = true; window.$chatwoot.hideMessageBubble = true;
} else if (visibility === 'show') { } else if (visibility === 'show') {
removeClass(widgetElm, 'woot-hidden'); removeClasses(widgetElm, 'woot-hidden');
removeClass(widgetHolder, 'woot-widget--without-bubble'); removeClasses(widgetHolder, 'woot-widget--without-bubble');
window.$chatwoot.hideMessageBubble = false; window.$chatwoot.hideMessageBubble = false;
} }
IFrameHelper.sendMessage(SDK_SET_BUBBLE_VISIBILITY, { IFrameHelper.sendMessage(SDK_SET_BUBBLE_VISIBILITY, {

View file

@ -3,68 +3,20 @@ import { IFrameHelper } from './IFrameHelper';
export const loadCSS = () => { export const loadCSS = () => {
const css = document.createElement('style'); const css = document.createElement('style');
css.type = 'text/css';
css.innerHTML = `${SDK_CSS}`; css.innerHTML = `${SDK_CSS}`;
document.body.appendChild(css); document.body.appendChild(css);
}; };
export const wootOn = (elm, event, fn) => { export const addClasses = (elm, classes) => {
if (document.addEventListener) { elm.classList.add(...classes.split(' '));
elm.addEventListener(event, fn, false);
} else if (document.attachEvent) {
// <= IE 8 loses scope so need to apply, we add this to object so we
// can detach later (can't detach anonymous functions)
// eslint-disable-next-line
elm[event + fn] = function() {
// eslint-disable-next-line
return fn.apply(elm, arguments);
};
elm.attachEvent(`on${event}`, elm[event + fn]);
}
};
export const classHelper = (classes, action, elm) => {
let search;
let replace;
let i;
let has = false;
if (classes) {
// Trim any whitespace
const classarray = classes.split(/\s+/);
for (i = 0; i < classarray.length; i += 1) {
search = new RegExp(`\\b${classarray[i]}\\b`, 'g');
replace = new RegExp(` *${classarray[i]}\\b`, 'g');
if (action === 'remove') {
// eslint-disable-next-line
elm.className = elm.className.replace(replace, '');
} else if (action === 'toggle') {
// eslint-disable-next-line
elm.className = elm.className.match(search)
? elm.className.replace(replace, '')
: `${elm.className} ${classarray[i]}`;
} else if (action === 'has') {
if (elm.className.match(search)) {
has = true;
break;
}
}
}
}
return has;
};
export const addClass = (elm, classes) => {
if (classes) {
elm.className += ` ${classes}`;
}
}; };
export const toggleClass = (elm, classes) => { export const toggleClass = (elm, classes) => {
classHelper(classes, 'toggle', elm); elm.classList.toggle(classes);
}; };
export const removeClass = (elm, classes) => { export const removeClasses = (elm, classes) => {
classHelper(classes, 'remove', elm); elm.classList.remove(...classes.split(' '));
}; };
export const onLocationChange = ({ referrerURL, referrerHost }) => { export const onLocationChange = ({ referrerURL, referrerHost }) => {

View file

@ -1,9 +1,8 @@
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import { import {
wootOn, addClasses,
addClass,
loadCSS, loadCSS,
removeClass, removeClasses,
onLocationChangeListener, onLocationChangeListener,
} from './DOMHelpers'; } from './DOMHelpers';
import { import {
@ -68,7 +67,7 @@ export const IFrameHelper = {
holderClassName += ` woot-widget-holder--flat`; holderClassName += ` woot-widget-holder--flat`;
} }
addClass(widgetHolder, holderClassName); addClasses(widgetHolder, holderClassName);
widgetHolder.appendChild(iframe); widgetHolder.appendChild(iframe);
body.appendChild(widgetHolder); body.appendChild(widgetHolder);
IFrameHelper.initPostMessageCommunication(); IFrameHelper.initPostMessageCommunication();
@ -99,7 +98,7 @@ export const IFrameHelper = {
}; };
}, },
initWindowSizeListener: () => { initWindowSizeListener: () => {
wootOn(window, 'resize', () => IFrameHelper.toggleCloseButton()); window.addEventListener('resize', () => IFrameHelper.toggleCloseButton());
}, },
preventDefaultScroll: () => { preventDefaultScroll: () => {
widgetHolder.addEventListener('wheel', event => { widgetHolder.addEventListener('wheel', event => {
@ -241,9 +240,9 @@ export const IFrameHelper = {
event.unreadMessageCount > 0 && event.unreadMessageCount > 0 &&
!bubbleElement.classList.contains('unread-notification') !bubbleElement.classList.contains('unread-notification')
) { ) {
addClass(bubbleElement, 'unread-notification'); addClasses(bubbleElement, 'unread-notification');
} else if (event.unreadMessageCount === 0) { } else if (event.unreadMessageCount === 0) {
removeClass(bubbleElement, 'unread-notification'); removeClasses(bubbleElement, 'unread-notification');
} }
}, },
@ -284,7 +283,7 @@ export const IFrameHelper = {
target: chatBubble, target: chatBubble,
}); });
addClass(closeBubble, closeBtnClassName); addClasses(closeBubble, closeBtnClassName);
chatIcon.style.background = widgetColor; chatIcon.style.background = widgetColor;
closeBubble.style.background = widgetColor; closeBubble.style.background = widgetColor;

View file

@ -1,4 +1,4 @@
import { addClass, removeClass, toggleClass, wootOn } from './DOMHelpers'; import { addClasses, removeClasses, toggleClass } from './DOMHelpers';
import { IFrameHelper } from './IFrameHelper'; import { IFrameHelper } from './IFrameHelper';
import { isExpandedView } from './settingsHelper'; import { isExpandedView } from './settingsHelper';
@ -41,14 +41,14 @@ export const createBubbleIcon = ({ className, src, target }) => {
export const createBubbleHolder = hideMessageBubble => { export const createBubbleHolder = hideMessageBubble => {
if (hideMessageBubble) { if (hideMessageBubble) {
addClass(bubbleHolder, 'woot-hidden'); addClasses(bubbleHolder, 'woot-hidden');
} }
addClass(bubbleHolder, 'woot--bubble-holder'); addClasses(bubbleHolder, 'woot--bubble-holder');
body.appendChild(bubbleHolder); body.appendChild(bubbleHolder);
}; };
export const createNotificationBubble = () => { export const createNotificationBubble = () => {
addClass(notificationBubble, 'woot--notification'); addClasses(notificationBubble, 'woot--notification');
return notificationBubble; return notificationBubble;
}; };
@ -71,15 +71,15 @@ export const onBubbleClick = (props = {}) => {
}; };
export const onClickChatBubble = () => { export const onClickChatBubble = () => {
wootOn(bubbleHolder, 'click', onBubbleClick); bubbleHolder.addEventListener('click', onBubbleClick);
}; };
export const addUnreadClass = () => { export const addUnreadClass = () => {
const holderEl = document.querySelector('.woot-widget-holder'); const holderEl = document.querySelector('.woot-widget-holder');
addClass(holderEl, 'has-unread-view'); addClasses(holderEl, 'has-unread-view');
}; };
export const removeUnreadClass = () => { export const removeUnreadClass = () => {
const holderEl = document.querySelector('.woot-widget-holder'); const holderEl = document.querySelector('.woot-widget-holder');
removeClass(holderEl, 'has-unread-view'); removeClasses(holderEl, 'has-unread-view');
}; };