Copy the Chatwoot (v4.14.0) frontend runnable subset into frontend/ for customization: - app/javascript/ (Vue SPA: dashboard, widget, sdk, portal, superadmin) - app/views/ (ERB templates for vite-plugin-ruby entrypoint resolution) - app/helpers/, app/assets/ (Rails view helpers, static assets) - enterprise/ (Enterprise edition frontend overlay) - config/vite.json, vite.config.ts, bin/vite (Vite-Rails toolchain) - package.json, pnpm-lock.yaml, tailwind/postcss/eslint configs - Gemfile, Gemfile.lock (vite_rails gem for bin/vite binstub) Excluded Rails backend: controllers, models, services, jobs, mailers, policies, db, lib, spec, public, node_modules. Update references to the new frontend location: - .gitignore: exclude frontend build artifacts (node_modules, tmp, packs), keep frontend/bin/ and frontend/vendor/ via negation - backend/scripts/parity_frontend_smoke.sh: CHATWOOT_DIR default reference/chatwoot -> ../frontend - backend/scripts/parity_frontend_browser_smoke.mjs: same default update - AGENTS.md: add frontend section with Rails/Vite coupling notes - README: architecture tree includes frontend/
76 lines
1.9 KiB
JavaScript
76 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';
|
|
css.dataset.turboPermanent = true;
|
|
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);
|
|
};
|