DocSpace-buildtools/public/scripts/ds-api.js

69 lines
1.7 KiB
JavaScript

((window, document) => {
const defaultConfig = {
src: "http://192.168.1.60:8092/products/files/",
width: "1024px",
height: "800px",
name: "frameDocSpace",
type: "desktop",
frameId: "ds-frame",
};
const getConfigFromParams = () => {
const src = document.currentScript.src;
if (!src || !src.length) return null;
const searchUrl = src.split("?")[1];
let object = {};
if (searchUrl && searchUrl.length) {
const decodedString = decodeURIComponent(searchUrl)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\\/g, "\\\\");
object = JSON.parse(`{"${decodedString}"}`);
}
return { ...defaultConfig, ...object };
};
const initDocSpace = (config = defaultConfig) => {
const target = document.getElementById(config.frameId);
let iframe;
if (target) {
iframe = createIframe(config);
target.parentNode && target.parentNode.replaceChild(iframe, target);
}
};
const createIframe = (config) => {
const iframe = document.createElement("iframe");
iframe.src = config.src;
iframe.width = config.width;
iframe.height = config.height;
iframe.name = config.name;
iframe.align = "top";
iframe.frameBorder = 0;
iframe.allowFullscreen = true;
iframe.setAttribute("allowfullscreen", "");
iframe.setAttribute("onmousewheel", "");
iframe.setAttribute("allow", "autoplay");
if (config.type == "mobile") {
iframe.style.position = "fixed";
iframe.style.overflow = "hidden";
document.body.style.overscrollBehaviorY = "contain";
}
return iframe;
};
const config = getConfigFromParams();
initDocSpace(config);
})(window, document);