debian-mirror-gitlab/app/assets/javascripts/blob/openapi/index.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { setAttributes } from '~/lib/utils/dom_utils';
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { getBaseURL, relativePathToAbsolute, joinPaths } from '~/lib/utils/url_utility';
const SANDBOX_FRAME_PATH = '/-/sandbox/swagger';
const getSandboxFrameSrc = () => {
const path = joinPaths(gon.relative_url_root || '', SANDBOX_FRAME_PATH);
return relativePathToAbsolute(path, getBaseURL());
};
2020-01-01 13:55:28 +05:30
2022-11-25 23:54:43 +05:30
const createSandbox = () => {
const iframeEl = document.createElement('iframe');
2023-04-23 21:23:45 +05:30
2022-11-25 23:54:43 +05:30
setAttributes(iframeEl, {
2023-04-23 21:23:45 +05:30
src: getSandboxFrameSrc(),
2023-03-04 22:38:38 +05:30
sandbox: 'allow-scripts allow-popups allow-forms',
2022-11-25 23:54:43 +05:30
frameBorder: 0,
width: '100%',
// The height will be adjusted dynamically.
// Follow-up issue: https://gitlab.com/gitlab-org/gitlab/-/issues/377969
height: '1000',
});
return iframeEl;
};
2023-03-17 16:20:25 +05:30
export default async (el = document.getElementById('js-openapi-viewer')) => {
const wrapperEl = el;
2022-11-25 23:54:43 +05:30
const sandboxEl = createSandbox();
const { data } = await axios.get(wrapperEl.dataset.endpoint);
wrapperEl.appendChild(sandboxEl);
2020-01-01 13:55:28 +05:30
2022-11-25 23:54:43 +05:30
sandboxEl.addEventListener('load', () => {
sandboxEl.contentWindow.postMessage(data, '*');
});
2020-01-01 13:55:28 +05:30
};