generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move swagger ui navigation to separate block
- Loading branch information
1 parent
252f684
commit cd8d633
Showing
5 changed files
with
112 additions
and
92 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 |
---|---|---|
@@ -1,22 +1,3 @@ | ||
raqn-swaggerui .topbar { | ||
display: none; | ||
} | ||
|
||
raqn-swaggerui .swagger-ui-selection ul input { | ||
transition: opacity 0.5s ease; | ||
margin: auto 10px; | ||
} | ||
|
||
raqn-swaggerui .swagger-ui-selection ul li.closed input { | ||
opacity: 0; | ||
} | ||
|
||
raqn-swaggerui .swagger-ui-selection ul ul { | ||
max-height: 300px; | ||
overflow-y: scroll; | ||
transition: max-height 0.5s ease-out, opacity 0.5s ease-out; | ||
} | ||
|
||
raqn-swaggerui .swagger-ui-selection ul li.closed ul { | ||
height: 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
raqn-swaggerui-navigation > input { | ||
margin: auto 10px; | ||
} | ||
|
||
raqn-swaggerui-navigation > ul { | ||
height: calc(100vh - var(--header-height, 110px) - 250px); | ||
overflow-y: auto; | ||
} | ||
|
||
raqn-swaggerui-navigation > ul.closed { | ||
height: 0; | ||
padding: 0; | ||
margin: 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import ComponentBase from '../../scripts/component-base.js'; | ||
import { prefixPath, apiSwitchEvent } from '../swaggerui/swaggerui.js'; | ||
|
||
export default class SwaggerUINavigation extends ComponentBase { | ||
|
||
navigationClick(e, hash) { | ||
e?.preventDefault(); | ||
this.querySelectorAll(':scope > ul[data-environment]').forEach((item) => { | ||
if(hash.startsWith(`#${item.dataset.environment}`)) { | ||
item.classList.remove('closed'); | ||
} else if(!item.classList.contains('closed')) { | ||
item.classList.add('closed'); | ||
} | ||
}); | ||
if(!window.location.hash.startsWith(hash)) { | ||
window.location.hash = hash; | ||
document.dispatchEvent(new CustomEvent(apiSwitchEvent, { detail: { hash } })); | ||
} | ||
} | ||
|
||
async generateAPISelection() { | ||
const response = await fetch(`${prefixPath}/environments.json`); | ||
const environments = await response.json(); | ||
const environmentOptions = await Promise.all(environments.map(async (environment) => { | ||
const item = document.createElement('option'); | ||
item.textContent = environment.label; | ||
item.value = environment.folder; | ||
if(window.location.hash.startsWith(`#${environment.folder}`)) { | ||
item.selected = true; | ||
} | ||
|
||
const apiResponse = await fetch(`${prefixPath}/${environment.folder}/index.json`); | ||
const apis = await apiResponse.json(); | ||
const definitionsElement = document.createElement('ul'); | ||
definitionsElement.dataset.environment = environment.folder; | ||
apis.sort((a, b) => a.label.localeCompare(b.label)).forEach((api) => { | ||
const apiItem = document.createElement('li'); | ||
const apiAnchor = document.createElement('a'); | ||
const apiUrl = new URL(window.location.href); | ||
apiUrl.hash = `${environment.folder}--${api.id}`; | ||
apiAnchor.addEventListener('click', (e) => this.navigationClick(e, apiUrl.hash)); | ||
apiAnchor.href = apiUrl.toString(); | ||
apiAnchor.textContent = `${api.label}${api.version ? ` (${api.version})` : ''}`; | ||
apiItem.appendChild(apiAnchor); | ||
definitionsElement.appendChild(apiItem); | ||
}); | ||
this.appendChild(definitionsElement); | ||
return item; | ||
})); | ||
const environmentsSelect = this.querySelector(':scope > select[name="environment-selection"]'); | ||
environmentOptions.forEach((option) => environmentsSelect.appendChild(option)); | ||
|
||
environmentsSelect.addEventListener('change', (event) => this.navigationClick(event, `#${event.target.value}`)); | ||
this.navigationClick(null, `#${environmentsSelect.selectedOptions[0].value}`); | ||
} | ||
|
||
async init() { | ||
super.init(); | ||
|
||
this.innerHTML = ` | ||
<label for="environment-selection">Environment:</label> | ||
<select name="environment-selection"></select> | ||
<label for="environment-filter">Filter:</label> | ||
<input name="environment-filter"> | ||
`; | ||
|
||
await this.generateAPISelection(); | ||
const filter = this.querySelector(':scope > input'); | ||
const allApis = this.querySelectorAll(':scope > ul[data-environment] > li'); | ||
filter.addEventListener('input', () => { | ||
allApis.forEach((apiItem) => { | ||
if (apiItem.textContent.toLowerCase().includes(filter.value.toLowerCase())) { | ||
apiItem.style.display = 'block'; | ||
} else { | ||
apiItem.style.display = 'none'; | ||
} | ||
}); | ||
}); | ||
|
||
} | ||
|
||
} |
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