Debian Codegraph @

src/drf_yasg/static/drf-yasg/redoc-init.js

raw symbols SCIP
1 // redoc-init.js
2 // https://github.com/axnsan12/drf-yasg
3 // Copyright 2017 - 2021, Cristian V. <[email protected]>
4 // This file is licensed under the BSD 3-Clause License.
5 // License text available at https://opensource.org/licenses/BSD-3-Clause
6
7 "use strict";
8
9 var currentPath = window.location.protocol + "//" + window.location.host + window.location.pathname;
10 var specURL = currentPath + '?format=openapi';
11 var redoc = document.createElement("redoc");
12
13 var redocSettings = JSON.parse(document.getElementById('redoc-settings').innerHTML);
14 if (redocSettings.url) {
15 specURL = redocSettings.url;
16 }
17 delete redocSettings.url;
18 if (redocSettings.fetchSchemaWithQuery) {
19 var query = new URLSearchParams(window.location.search || '').entries();
20 var url = specURL.split('?');
21 var usp = new URLSearchParams(url[1] || '');
22 for (var it = query.next(); !it.done; it = query.next()) {
23 usp.set(it.value[0], it.value[1]);
24 }
25 url[1] = usp.toString();
26 specURL = url[1] ? url.join('?') : url[0];
27 }
28 delete redocSettings.fetchSchemaWithQuery;
29
30 redoc.setAttribute("spec-url", specURL);
31
32 function camelToKebab(str) {
33 return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
34 }
35
36 for (var p in redocSettings) {
37 if (redocSettings.hasOwnProperty(p)) {
38 if (redocSettings[p] !== null && redocSettings[p] !== undefined && redocSettings[p] !== false) {
39 redoc.setAttribute(camelToKebab(p), redocSettings[p].toString());
40 }
41 }
42 }
43
44 document.body.replaceChild(redoc, document.getElementById('redoc-placeholder'));
45
46 function hideEmptyVersion() {
47 // 'span.api-info-version' is for redoc 1.x, 'div.api-info span' is for redoc 2-alpha
48 var apiVersion = document.querySelector('span.api-info-version') || document.querySelector('div.api-info span');
49 if (!apiVersion) {
50 console.log("WARNING: could not find API versionString element (span.api-info-version)");
51 return;
52 }
53
54 var versionString = apiVersion.innerText;
55 if (versionString) {
56 // trim spaces and surrounding ()
57 versionString = versionString.replace(/ /g, '');
58 versionString = versionString.replace(/(^\()|(\)$)/g, '');
59 }
60
61 if (!versionString) {
62 // hide version element if empty
63 apiVersion.classList.add("hidden");
64 }
65 }
66
67 if (document.querySelector('span.api-info-version') || document.querySelector('div.api-info span')) {
68 hideEmptyVersion();
69 }
70 else {
71 insertionQ('span.api-info-version').every(hideEmptyVersion);
72 insertionQ('div.api-info span').every(hideEmptyVersion);
73 }