-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
39 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,18 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len | |
; | ||
(function (window, undefined) { | ||
window.initTry = window.initTry || initTry; | ||
|
||
/** | ||
global variable: | ||
window.initTry | ||
window.initTry.cfg | ||
window.initTry.renderPos | ||
window.initTry.$operation | ||
*/ | ||
|
||
function initTry(userCfg) { | ||
loadScript("https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js").then(function () { | ||
return loadScript("https://cdn.jsdelivr.net/npm/[email protected]/jquery.scrollTo.min.js"); | ||
|
@@ -28,7 +40,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len | |
return loadScript("https://cdn.jsdelivr.net/npm/[email protected]/index.min.js"); | ||
}).then(function () { | ||
var cfg = cfgHandle(userCfg); | ||
window.cfg = cfg; | ||
window.initTry.cfg = cfg; | ||
if (cfg.onlySwagger) { | ||
initSwagger(cfg.swaggerOptions); | ||
$(".swaggerBox").addClass("onlySwagger"); | ||
|
@@ -150,6 +162,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len | |
var $tryBtn = $(this); | ||
$(".swaggerShadow").remove(); // First clear all temporary elements | ||
var $operation = $tryBtn.parents("[data-section-id]").last(); // Get the outermost api box | ||
window.initTry.$operation = $operation; | ||
if ($operation.hasClass("try") === true) { | ||
// If the current API is already in the try state, uninstall and exit the function | ||
$(".swaggerBox").addClass("hide").removeClass("show"); | ||
|
@@ -162,7 +175,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len | |
// The following 3 lines add class names to some necessary elements to facilitate acquisition or identification | ||
$(".try>div>div:nth-child(2)").addClass("apiBlock"); | ||
$(".try .apiBlock>div:nth-child(1)").addClass("fullApiBox"); | ||
if (window.cfg.redocVersion !== 'next' && window.compareVersions.compare(window.cfg.redocVersion, "2.0.0-rc.32", "<=")) { | ||
if (window.initTry.cfg.redocVersion !== 'next' && window.compareVersions.compare(window.initTry.cfg.redocVersion, "2.0.0-rc.32", "<=")) { | ||
$(".try .apiBlock>div>div:nth-child(1)").addClass("fullApi"); | ||
} else { | ||
$(".try .apiBlock>div>button").addClass("fullApi"); | ||
|
@@ -198,7 +211,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len | |
var getShadowPos = function getShadowPos() { | ||
// Get the position of swaggerShadow | ||
var pos = {}; | ||
pos = getAbsolutePosition($(".try .swaggerShadow")[0]); | ||
pos = getAbsolutePosition($(".try .swaggerShadow")[0]) || {}; | ||
pos = Object.keys(pos).reduce(function (prev, cur, index) { | ||
// Add px to the number without unit, undefined when the number is 0 | ||
var val = pos[cur]; | ||
|
@@ -251,25 +264,42 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len | |
} | ||
renderPos(); | ||
setTimeout(renderPos, 0); | ||
window.renderPos = renderPos; | ||
window.initTry.renderPos = renderPos; | ||
var observer = new MutationObserver(changeFn); | ||
observer.disconnect(); | ||
observer.observe($opblock[0], { | ||
attributes: true, | ||
childList: true, | ||
subtree: true | ||
}); | ||
var redoc_dom = window.cfg.redocOptions[2]; | ||
var redoc_dom = window.initTry.cfg.redocOptions[2]; | ||
$(redoc_dom).off('click.redoc_dom').on('click.redoc_dom', function () { | ||
renderPos(); | ||
if (isVisible(document.querySelector(".try .fullApiBox")) === false) { | ||
$(".swaggerBox").addClass("hide").removeClass("show"); | ||
window.initTry.$operation.removeClass("try"); | ||
} | ||
}); | ||
}); | ||
|
||
// When changing the browser window size, reset the state of swaggerBox | ||
$(window).resize(debounce(function () { | ||
window.renderPos(); | ||
window.initTry.renderPos(); | ||
}, 500)); | ||
} | ||
function isVisible(element) { | ||
var isVisible = true; | ||
var parentElement = element; | ||
while (parentElement) { | ||
var parentStyle = getComputedStyle(parentElement); | ||
if (false || parentStyle.display === 'none' || parentStyle.visibility === 'hidden' || parentStyle.opacity === '0' || parentStyle.opacity === '0.0') { | ||
isVisible = false; | ||
break; | ||
} | ||
parentElement = parentElement.offsetParent; | ||
} | ||
return isVisible; | ||
} | ||
function loadScript(src) { | ||
return new Promise(function (resolve, reject) { | ||
var script = document.createElement('script'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
!window.initTryOptions.openApi && (delete window.initTryOptions.openApi); | ||
</script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/bundles/redoc.standalone.min.js"> </script> | ||
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].7/dist/try.js"></script> | ||
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].8/dist/try.js"></script> | ||
<script> | ||
// initTry(`https://petstore.swagger.io/v2/swagger.json`) | ||
initTry({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ Add `Try it out` function like [swagger](https://petstore.swagger.io/) on [redoc | |
<body> | ||
<div id="redoc-container"></div> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/bundles/redoc.standalone.min.js"> </script> | ||
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].7/dist/try.js"></script> | ||
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].8/dist/try.js"></script> | ||
<script> | ||
// initTry(`https://petstore.swagger.io/v2/swagger.json`) | ||
initTry({ | ||
|