Skip to content

Commit f375afc

Browse files
authored
Merge pull request #3501 from oysteinsigholt/master
Fixes #3492 - Add domNode parameter
2 parents cd422fe + faafe37 commit f375afc

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ urls.primaryName | When using `urls`, you can use this subparameter. If the valu
138138
spec | A JSON object describing the OpenAPI Specification. When used, the `url` parameter will not be parsed. This is useful for testing manually-generated specifications without hosting them.
139139
validatorUrl | By default, Swagger-UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to `null` will disable validation.
140140
dom_id | The id of a dom element inside which SwaggerUi will put the user interface for swagger.
141+
domNode | The HTML DOM element inside which SwaggerUi will put the user interface for swagger. Overrides `dom_id`.
141142
oauth2RedirectUrl | OAuth redirect URL
142143
tagsSorter | Apply a sort to the tag list of each API. It can be 'alpha' (sort by paths alphanumerically) or a function (see [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) to learn how to write a sort function). Two tag name strings are passed to the sorter for each pass. Default is the order determined by Swagger-UI.
143144
operationsSorter | Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.

src/core/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = function SwaggerUI(opts) {
2323
const defaults = {
2424
// Some general settings, that we floated to the top
2525
dom_id: null,
26+
domNode: null,
2627
spec: {},
2728
url: "",
2829
urls: null,
@@ -99,6 +100,12 @@ module.exports = function SwaggerUI(opts) {
99100

100101
let localConfig = system.specSelectors.getLocalConfig ? system.specSelectors.getLocalConfig() : {}
101102
let mergedConfig = deepExtend({}, localConfig, constructorConfig, fetchedConfig || {}, queryConfig)
103+
104+
// deep extend mangles domNode, we need to set it manually
105+
if(opts.domNode) {
106+
mergedConfig.domNode = opts.domNode
107+
}
108+
102109
store.setConfigs(mergedConfig)
103110

104111
if (fetchedConfig !== null) {
@@ -112,10 +119,13 @@ module.exports = function SwaggerUI(opts) {
112119
}
113120
}
114121

115-
if(mergedConfig.dom_id) {
116-
system.render(mergedConfig.dom_id, "App")
122+
if(mergedConfig.domNode) {
123+
system.render(mergedConfig.domNode, "App")
124+
} else if(mergedConfig.dom_id) {
125+
let domNode = document.querySelector(mergedConfig.dom_id)
126+
system.render(domNode, "App")
117127
} else {
118-
console.error("Skipped rendering: no `dom_id` was specified")
128+
console.error("Skipped rendering: no `dom_id` or `domNode` was specified")
119129
}
120130

121131
return system

src/core/plugins/view/root-injects.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export const makeMappedContainer = (getSystem, getStore, memGetComponent, getCom
5858

5959
}
6060

61-
export const render = (getSystem, getStore, getComponent, getComponents, dom) => {
62-
let domNode = document.querySelector(dom)
61+
export const render = (getSystem, getStore, getComponent, getComponents, domNode) => {
6362
let App = (getComponent(getSystem, getStore, getComponents, "App", "root"))
6463
ReactDOM.render(( <App/> ), domNode)
6564
}

0 commit comments

Comments
 (0)