CdsConnector/public/bundles/apiplatform/init-swagger-ui.js

152 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-06-22 17:02:10 +02:00
'use strict';
2022-07-19 15:39:38 +02:00
window.onload = function() {
2022-06-22 17:02:10 +02:00
manageWebbyDisplay();
new MutationObserver(function (mutations, self) {
const op = document.getElementById(`operations-${data.shortName}-${data.operationId}`);
2022-07-19 15:39:38 +02:00
if (!op) return;
2022-06-22 17:02:10 +02:00
self.disconnect();
op.querySelector('.opblock-summary').click();
const tryOutObserver = new MutationObserver(function (mutations, self) {
const tryOut = op.querySelector('.try-out__btn');
2022-07-19 15:39:38 +02:00
if (!tryOut) return;
2022-06-22 17:02:10 +02:00
self.disconnect();
tryOut.click();
if (data.id) {
const inputId = op.querySelector('.parameters input[placeholder="id"]');
inputId.value = data.id;
reactTriggerChange(inputId);
}
for (const input of op.querySelectorAll('.parameters input')) {
if (input.placeholder in data.queryParameters) {
input.value = data.queryParameters[input.placeholder];
reactTriggerChange(input);
}
}
// Wait input values to be populated before executing the query
2022-07-19 15:39:38 +02:00
setTimeout(function(){
2022-06-22 17:02:10 +02:00
op.querySelector('.execute').click();
op.scrollIntoView();
}, 500);
});
tryOutObserver.observe(document, {childList: true, subtree: true});
}).observe(document, {childList: true, subtree: true});
const data = JSON.parse(document.getElementById('swagger-data').innerText);
const ui = SwaggerUIBundle({
spec: data.spec,
dom_id: '#swagger-ui',
validatorUrl: null,
oauth2RedirectUrl: data.oauth.redirectUrl,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset,
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
],
layout: 'StandaloneLayout',
});
if (data.oauth.enabled) {
ui.initOAuth({
clientId: data.oauth.clientId,
clientSecret: data.oauth.clientSecret,
realm: data.oauth.type,
appName: data.spec.info.title,
scopeSeparator: ' ',
additionalQueryStringParams: {}
});
}
// Workaround for https://github.com/swagger-api/swagger-ui/issues/3028
// Adapted from https://github.com/vitalyq/react-trigger-change/blob/master/lib/change.js
// Copyright (c) 2017 Vitaly Kuznetsov
// MIT License
2022-07-19 15:39:38 +02:00
function reactTriggerChange(node) {
2022-06-22 17:02:10 +02:00
// Do not try to delete non-configurable properties.
// Value and checked properties on DOM elements are non-configurable in PhantomJS.
2022-07-19 15:39:38 +02:00
function deletePropertySafe(elem, prop) {
2022-06-22 17:02:10 +02:00
const desc = Object.getOwnPropertyDescriptor(elem, prop);
if (desc && desc.configurable) {
delete elem[prop];
}
}
// React 16
// Cache artificial value property descriptor.
// Property doesn't exist in React <16, descriptor is undefined.
const descriptor = Object.getOwnPropertyDescriptor(node, 'value');
// React 0.14: IE9
// React 15: IE9-IE11
// React 16: IE9
// Dispatch focus.
const focusEvent = document.createEvent('UIEvents');
focusEvent.initEvent('focus', false, false);
node.dispatchEvent(focusEvent);
// React 0.14: IE9
// React 15: IE9-IE11
// React 16
// In IE9-10 imperative change of node value triggers propertychange event.
// Update inputValueTracking cached value.
// Remove artificial value property.
// Restore initial value to trigger event with it.
const initialValue = node.value;
node.value = initialValue + '#';
deletePropertySafe(node, 'value');
node.value = initialValue;
// React 15: IE11
// For unknown reason React 15 added listener for propertychange with addEventListener.
// This doesn't work, propertychange events are deprecated in IE11,
// but allows us to dispatch fake propertychange which is handled by IE11.
const propertychangeEvent = document.createEvent('HTMLEvents');
propertychangeEvent.initEvent('propertychange', false, false);
propertychangeEvent.propertyName = 'value';
node.dispatchEvent(propertychangeEvent);
// React 0.14: IE10-IE11, non-IE
// React 15: non-IE
// React 16: IE10-IE11, non-IE
const inputEvent = document.createEvent('HTMLEvents');
inputEvent.initEvent('input', true, false);
node.dispatchEvent(inputEvent);
// React 16
// Restore artificial value property descriptor.
if (descriptor) {
Object.defineProperty(node, 'value', descriptor);
}
}
2022-07-19 15:39:38 +02:00
function manageWebbyDisplay() {
2022-06-22 17:02:10 +02:00
const webby = document.getElementsByClassName('webby')[0];
2022-07-19 15:39:38 +02:00
if (!webby) return;
2022-06-22 17:02:10 +02:00
const web = document.getElementsByClassName('web')[0];
webby.classList.add('calm');
web.classList.add('calm');
webby.addEventListener('click', () => {
if (webby.classList.contains('frighten')) {
return;
}
webby.classList.replace('calm', 'frighten');
web.classList.replace('calm', 'frighten');
setTimeout(() => {
webby.classList.replace('frighten', 'calm');
web.classList.replace('frighten', 'calm');
}, 10000);
});
}
};