|
1 |
| -$(function () { |
2 |
| - var data = JSON.parse($('#swagger-data').html()); |
3 |
| - window.swaggerUi = new SwaggerUi({ |
4 |
| - url: data.url, |
5 |
| - spec: data.spec, |
6 |
| - dom_id: 'swagger-ui-container', |
7 |
| - supportedSubmitMethods: ['get', 'post', 'put', 'delete'], |
8 |
| - onComplete: function() { |
9 |
| - if (data.oauth.enabled && 'function' === typeof initOAuth) { |
10 |
| - initOAuth({ |
11 |
| - clientId: data.oauth.clientId, |
12 |
| - clientSecret: data.oauth.clientSecret, |
13 |
| - realm: data.oauth.type, |
14 |
| - appName: data.spec.info.title, |
15 |
| - scopeSeparator: ' ', |
16 |
| - additionalQueryStringParams: {} |
17 |
| - }); |
18 |
| - } |
19 |
| - |
20 |
| - $('pre code').each(function(i, e) { |
21 |
| - hljs.highlightBlock(e) |
22 |
| - }); |
23 |
| - |
24 |
| - if (data.operationId !== undefined) { |
25 |
| - var queryParameters = data.queryParameters; |
26 |
| - var domSelector = '#' + data.shortName+'_'+data.operationId; |
27 |
| - |
28 |
| - $(domSelector + ' form.sandbox input.parameter').each(function (i, e) { |
29 |
| - var $e = $(e); |
30 |
| - var name = $e.attr('name'); |
31 |
| - |
32 |
| - if (name in queryParameters) { |
33 |
| - $e.val(queryParameters[name]); |
34 |
| - } |
35 |
| - }); |
36 |
| - |
37 |
| - if (data.id) { |
38 |
| - $(domSelector + ' form.sandbox input[name="id"]').val(data.id); |
39 |
| - } |
40 |
| - |
41 |
| - $(domSelector + ' form.sandbox').submit(); |
42 |
| - document.location.hash = '#!/' + data.shortName + '/' + data.operationId; |
43 |
| - } |
44 |
| - }, |
45 |
| - onFailure: function() { |
46 |
| - log('Unable to Load SwaggerUI'); |
47 |
| - }, |
48 |
| - docExpansion: 'list', |
49 |
| - jsonEditor: false, |
50 |
| - defaultModelRendering: 'schema', |
51 |
| - showRequestHeaders: true |
| 1 | +window.onload = () => { |
| 2 | + const data = JSON.parse(document.getElementById('swagger-data').innerText); |
| 3 | + const ui = SwaggerUIBundle({ |
| 4 | + spec: data.spec, |
| 5 | + dom_id: '#swagger-ui', |
| 6 | + validatorUrl: null, |
| 7 | + presets: [ |
| 8 | + SwaggerUIBundle.presets.apis, |
| 9 | + SwaggerUIStandalonePreset |
| 10 | + ], |
| 11 | + plugins: [ |
| 12 | + SwaggerUIBundle.plugins.DownloadUrl |
| 13 | + ], |
| 14 | + layout: 'StandaloneLayout' |
| 15 | + }); |
| 16 | + |
| 17 | + if (data.oauth.enabled) { |
| 18 | + ui.initOAuth({ |
| 19 | + clientId: data.oauth.clientId, |
| 20 | + clientSecret: data.oauth.clientSecret, |
| 21 | + realm: data.oauth.type, |
| 22 | + appName: data.spec.info.title, |
| 23 | + scopeSeparator: ' ', |
| 24 | + additionalQueryStringParams: {} |
52 | 25 | });
|
| 26 | + } |
53 | 27 |
|
54 |
| - window.swaggerUi.load(); |
| 28 | + window.ui = ui; |
55 | 29 |
|
56 |
| - function log() { |
57 |
| - if ('console' in window) { |
58 |
| - console.log.apply(console, arguments); |
59 |
| - } |
| 30 | + if (!data.operationId) return; |
| 31 | + |
| 32 | + const observer = new MutationObserver(function (mutations, self) { |
| 33 | + const op = document.getElementById(`operations,${data.method}-${data.path},${data.shortName}`); |
| 34 | + if (!op) return; |
| 35 | + |
| 36 | + self.disconnect(); |
| 37 | + |
| 38 | + op.querySelector('.opblock-summary').click(); |
| 39 | + op.querySelector('.try-out__btn').click(); |
| 40 | + |
| 41 | + if (data.id) { |
| 42 | + const inputId = op.querySelector('.parameters input[placeholder="id"]'); |
| 43 | + inputId.value = data.id; |
| 44 | + inputId.dispatchEvent(new Event('input', { bubbles: true })); |
60 | 45 | }
|
61 |
| -}); |
| 46 | + |
| 47 | + for (let input of op.querySelectorAll('.parameters input')) { |
| 48 | + if (input.placeholder in data.queryParameters) { |
| 49 | + input.value = data.queryParameters[input.placeholder]; |
| 50 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + op.querySelector('.execute').click(); |
| 55 | + op.scrollIntoView(); |
| 56 | + }); |
| 57 | + |
| 58 | + observer.observe(document, {childList: true, subtree: true}); |
| 59 | +}; |
0 commit comments