2020-08-09 10:37:32 +00:00
|
|
|
import { SDK_CSS } from './sdk.js';
|
2021-05-17 16:08:35 +00:00
|
|
|
import { IFrameHelper } from './IFrameHelper';
|
2020-04-03 07:34:58 +00:00
|
|
|
|
|
|
|
export const loadCSS = () => {
|
|
|
|
const css = document.createElement('style');
|
|
|
|
css.type = 'text/css';
|
|
|
|
css.innerHTML = `${SDK_CSS}`;
|
|
|
|
document.body.appendChild(css);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const wootOn = (elm, event, fn) => {
|
|
|
|
if (document.addEventListener) {
|
|
|
|
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) => {
|
|
|
|
classHelper(classes, 'toggle', elm);
|
|
|
|
};
|
2020-07-07 18:34:44 +00:00
|
|
|
|
|
|
|
export const removeClass = (elm, classes) => {
|
|
|
|
classHelper(classes, 'remove', elm);
|
|
|
|
};
|
2021-05-17 16:08:35 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|