diff --git a/src/drf_yasg/static/drf-yasg/redoc-init.js b/src/drf_yasg/static/drf-yasg/redoc-init.js new file mode 100644 index 0000000..15ea8ef --- /dev/null +++ b/src/drf_yasg/static/drf-yasg/redoc-init.js @@ -0,0 +1,19 @@ +"use strict"; + +var currentPath = window.location.protocol + "//" + window.location.host + window.location.pathname; +var specURL = currentPath + '?format=openapi'; +var redoc = document.createElement("redoc"); +redoc.setAttribute("spec-url", specURL); + +var redocSettings = JSON.parse(document.getElementById('redoc-settings').innerHTML); +if (redocSettings.lazyRendering) { + redoc.setAttribute("lazy-rendering", ''); +} +if (redocSettings.pathInMiddle) { + redoc.setAttribute("path-in-middle-panel", ''); +} +if (redocSettings.hideHostname) { + redoc.setAttribute("hide-hostname", ''); +} +redoc.setAttribute("expand-responses", redocSettings.expandResponses); +document.body.appendChild(redoc); diff --git a/src/drf_yasg/static/drf-yasg/style.css b/src/drf_yasg/static/drf-yasg/style.css new file mode 100644 index 0000000..8cf61a1 --- /dev/null +++ b/src/drf_yasg/static/drf-yasg/style.css @@ -0,0 +1,73 @@ +html { + box-sizing: border-box; + overflow: -moz-scrollbars-vertical; + overflow-y: scroll; +} + +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + margin: 0; + background: #fafafa; +} + +#django-session-auth { + margin-right: 8px; +} + +.hidden { + display: none; +} + +#django-session-auth > div { + display: inline-block; +} + +#django-session-auth .btn.authorize { + padding: 10px 23px; +} + +#django-session-auth .btn.authorize a { + color: #49cc90; + text-decoration: none; +} + +#django-session-auth .hello { + margin-right: 5px; +} + +#django-session-auth .hello .django-session { + font-weight: bold; +} + +.label { + display: inline; + padding: .2em .6em .3em; + font-weight: 700; + line-height: 1; + color: #fff; + text-align: center; + white-space: nowrap; + vertical-align: baseline; + border-radius: .25em; +} + +.label-primary { + background-color: #337ab7; +} + +.divider { + margin-right: 8px; + background: #16222c44; + width: 2px; +} + +svg.swagger-defs { + position: absolute; + width: 0; + height: 0; +} diff --git a/src/drf_yasg/static/drf-yasg/swagger-ui-init.js b/src/drf_yasg/static/drf-yasg/swagger-ui-init.js new file mode 100644 index 0000000..39c6b85 --- /dev/null +++ b/src/drf_yasg/static/drf-yasg/swagger-ui-init.js @@ -0,0 +1,61 @@ +"use strict"; +var currentPath = window.location.protocol + "//" + window.location.host + window.location.pathname; +var specURL = currentPath + '?format=openapi'; + +function patchSwaggerUi() { + var authWrapper = document.querySelector('.auth-wrapper'); + var authorizeButton = document.querySelector('.auth-wrapper .authorize'); + var djangoSessionAuth = document.querySelector('#django-session-auth'); + if (document.querySelector('.auth-wrapper #django-session-auth')) { + console.log("session auth already patched"); + return; + } + + authWrapper.insertBefore(djangoSessionAuth, authorizeButton); + djangoSessionAuth.classList.remove("hidden"); + + var divider = document.createElement("div"); + divider.classList.add("divider"); + authWrapper.insertBefore(divider, authorizeButton); +} + +function initSwaggerUi() { + var swaggerConfig = { + url: specURL, + dom_id: '#swagger-ui', + displayOperationId: true, + displayRequestDuration: true, + presets: [ + SwaggerUIBundle.presets.apis, + SwaggerUIStandalonePreset + ], + plugins: [ + SwaggerUIBundle.plugins.DownloadUrl + ], + layout: "StandaloneLayout", + filter: true, + requestInterceptor: function(request) { + var headers = request.headers || {}; + var csrftoken = document.querySelector("[name=csrfmiddlewaretoken]"); + if (csrftoken) { + headers["X-CSRFToken"] = csrftoken.value; + } + return request; + } + }; + + var swaggerSettings = JSON.parse(document.getElementById('swagger-settings').innerHTML); + + console.log(swaggerSettings); + for (var p in swaggerSettings) { + if (swaggerSettings.hasOwnProperty(p)) { + swaggerConfig[p] = swaggerSettings[p]; + } + } + window.ui = SwaggerUIBundle(swaggerConfig); +} + +window.onload = function () { + insertionQ('.auth-wrapper .authorize').every(patchSwaggerUi); + initSwaggerUi(); +}; diff --git a/src/drf_yasg/templates/drf-yasg/redoc.html b/src/drf_yasg/templates/drf-yasg/redoc.html index 0a8ede3..4b75832 100644 --- a/src/drf_yasg/templates/drf-yasg/redoc.html +++ b/src/drf_yasg/templates/drf-yasg/redoc.html @@ -18,26 +18,9 @@
- - var redocSettings = {}; - redocSettings = {{ redoc_settings | safe }}; - if (redocSettings.lazyRendering) { - redoc.setAttribute("lazy-rendering", ''); - } - if (redocSettings.pathInMiddle) { - redoc.setAttribute("path-in-middle-panel", ''); - } - if (redocSettings.hideHostname) { - redoc.setAttribute("hide-hostname", ''); - } - redoc.setAttribute("expand-responses", redocSettings.expandResponses); - document.body.appendChild(redoc); - +