From 230f4a846725c8c25e7c913a323db9421040eb48 Mon Sep 17 00:00:00 2001 From: oysteinsigholt Date: Tue, 1 Aug 2017 11:11:10 +0200 Subject: [PATCH] add domNode parameter --- README.md | 1 + src/core/index.js | 16 +++++++++++++--- src/core/plugins/view/root-injects.js | 3 +-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e3384151..a0cc7944 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ urls.primaryName | When using `urls`, you can use this subparameter. If the valu 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. 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. dom_id | The id of a dom element inside which SwaggerUi will put the user interface for swagger. +domNode | The HTML DOM element inside which SwaggerUi will put the user interface for swagger. Overrides `dom_id`. oauth2RedirectUrl | OAuth redirect URL 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. 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. diff --git a/src/core/index.js b/src/core/index.js index 126cbc54..3dbcede4 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -23,6 +23,7 @@ module.exports = function SwaggerUI(opts) { const defaults = { // Some general settings, that we floated to the top dom_id: null, + domNode: null, spec: {}, url: "", urls: null, @@ -99,6 +100,12 @@ module.exports = function SwaggerUI(opts) { let localConfig = system.specSelectors.getLocalConfig ? system.specSelectors.getLocalConfig() : {} let mergedConfig = deepExtend({}, localConfig, constructorConfig, fetchedConfig || {}, queryConfig) + + // deep extend mangles domNode, we need to set it manually + if(opts.domNode) { + mergedConfig.domNode = opts.domNode + } + store.setConfigs(mergedConfig) if (fetchedConfig !== null) { @@ -112,10 +119,13 @@ module.exports = function SwaggerUI(opts) { } } - if(mergedConfig.dom_id) { - system.render(mergedConfig.dom_id, "App") + if(mergedConfig.domNode) { + system.render(mergedConfig.domNode, "App") + } else if(mergedConfig.dom_id) { + let domNode = document.querySelector(mergedConfig.dom_id) + system.render(domNode, "App") } else { - console.error("Skipped rendering: no `dom_id` was specified") + console.error("Skipped rendering: no `dom_id` or `domNode` was specified") } return system diff --git a/src/core/plugins/view/root-injects.js b/src/core/plugins/view/root-injects.js index e86c7519..1d996102 100644 --- a/src/core/plugins/view/root-injects.js +++ b/src/core/plugins/view/root-injects.js @@ -58,8 +58,7 @@ export const makeMappedContainer = (getSystem, getStore, memGetComponent, getCom } -export const render = (getSystem, getStore, getComponent, getComponents, dom) => { - let domNode = document.querySelector(dom) +export const render = (getSystem, getStore, getComponent, getComponents, domNode) => { let App = (getComponent(getSystem, getStore, getComponents, "App", "root")) ReactDOM.render(( ), domNode) }