Pass options through init function

This commit is contained in:
Roman Hotsiy 2015-12-30 01:29:29 +02:00
parent 1c4a3ca20f
commit 7b7acb8679
5 changed files with 44 additions and 12 deletions

View File

@ -21,7 +21,7 @@
<script src="main.js"> </script> <script src="main.js"> </script>
<script> <script>
/* init redoc */ /* init redoc */
Redoc.init('swagger.json'); Redoc.init('swagger.json', {scrollYOffset: 'body>nav'});
</script> </script>
</body> </body>
</html> </html>

View File

@ -8,7 +8,7 @@ import ApiLogo from '../ApiLogo/api-logo';
import MethodsList from '../MethodsList/methods-list'; import MethodsList from '../MethodsList/methods-list';
import SideMenu from '../SideMenu/side-menu'; import SideMenu from '../SideMenu/side-menu';
import StickySidebar from '../../common/components/StickySidebar/sticky-sidebar'; import StickySidebar from '../../common/components/StickySidebar/sticky-sidebar';
import {options as defaultOptions} from '../../options'; import OptionsManager from '../../options';
import {ChangeDetectionStrategy} from 'angular2/core'; import {ChangeDetectionStrategy} from 'angular2/core';
import {ElementRef} from 'angular2/core'; import {ElementRef} from 'angular2/core';
@ -21,14 +21,14 @@ let optionNames = new Set(['scrollYOffset']);
@RedocComponent({ @RedocComponent({
selector: 'redoc', selector: 'redoc',
providers: [SchemaManager, BrowserDomAdapter], providers: [SchemaManager, BrowserDomAdapter, OptionsManager],
templateUrl: './lib/components/Redoc/redoc.html', templateUrl: './lib/components/Redoc/redoc.html',
styleUrls: ['./lib/components/Redoc/redoc.css'], styleUrls: ['./lib/components/Redoc/redoc.css'],
directives: [ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar], directives: [ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar],
changeDetection: ChangeDetectionStrategy.Default changeDetection: ChangeDetectionStrategy.Default
}) })
export default class Redoc extends BaseComponent { export default class Redoc extends BaseComponent {
constructor(schemaMgr, elementRef, dom) { constructor(schemaMgr, optionsMgr, elementRef, dom) {
super(schemaMgr); super(schemaMgr);
this.element = elementRef.nativeElement; this.element = elementRef.nativeElement;
@ -38,7 +38,7 @@ export default class Redoc extends BaseComponent {
//parse options (top level component doesn't support inputs) //parse options (top level component doesn't support inputs)
this.scrollParent = detectScollParent(el); this.scrollParent = detectScollParent(el);
this.parseOptions(); this.parseOptions();
this.options = Object.assign({}, defaultOptions, this.options); this.options = Object.assign({}, optionsMgr.options, this.options);
this.normalizeOptions(); this.normalizeOptions();
} }
@ -80,7 +80,7 @@ export default class Redoc extends BaseComponent {
} }
} }
} }
Redoc.parameters = Redoc.parameters.concat([[ElementRef], [BrowserDomAdapter]]); Redoc.parameters = Redoc.parameters.concat([[OptionsManager], [ElementRef], [BrowserDomAdapter]]);
// this doesn't work in side-menu.js because of some circular references issue // this doesn't work in side-menu.js because of some circular references issue
SideMenu.parameters = SideMenu.parameters.concat([[Redoc]]); SideMenu.parameters = SideMenu.parameters.concat([[Redoc]]);

View File

@ -14,14 +14,17 @@ import {
import Redoc from 'lib/components/Redoc/redoc'; import Redoc from 'lib/components/Redoc/redoc';
import SchemaManager from 'lib/utils/SchemaManager'; import SchemaManager from 'lib/utils/SchemaManager';
import {options} from 'lib/options'; import OptionsManager from 'lib/options';
let optsMgr = new OptionsManager();
describe('Redoc components', () => { describe('Redoc components', () => {
describe('Redoc Component', () => { describe('Redoc Component', () => {
let builder; let builder;
beforeEachProviders(() => [ beforeEachProviders(() => [
provide(SchemaManager, {useValue: new SchemaManager()}), provide(SchemaManager, {useValue: new SchemaManager()}),
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}) provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}),
provide(OptionsManager, {useValue: optsMgr})
]); ]);
beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => { beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
builder = tcb; builder = tcb;
@ -92,11 +95,11 @@ describe('Redoc components', () => {
}); });
it('should handle function scrollYOffset', (done) => { it('should handle function scrollYOffset', (done) => {
options.scrollYOffset = () => 123; optsMgr.options.scrollYOffset = () => 123;
build(`<redoc></redoc>`, err => { build(`<redoc></redoc>`, err => {
if (err) return done.fail(err); if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(123); component.options.scrollYOffset().should.be.equal(123);
options.scrollYOffset = 0; optsMgr.options.scrollYOffset = 0;
done(); done();
}); });
}); });

View File

@ -4,13 +4,17 @@ import {bootstrap} from 'angular2/platform/browser';
import {Redoc} from './components/index'; import {Redoc} from './components/index';
import SchemaManager from './utils/SchemaManager'; import SchemaManager from './utils/SchemaManager';
import {redocEvents} from './events'; import {redocEvents} from './events';
import OptionsManager from './options';
export * from './components/index'; export * from './components/index';
export function init(schemaUrl) { export function init(schemaUrl, options) {
var promise = new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
SchemaManager.instance().load(schemaUrl) SchemaManager.instance().load(schemaUrl)
.then(() => bootstrap(Redoc)) .then(() => {
(new OptionsManager()).options = options;
return bootstrap(Redoc);
})
.then( .then(
() => { () => {
redocEvents.bootstrapped.next(); redocEvents.bootstrapped.next();

View File

@ -3,3 +3,28 @@
export var options = { export var options = {
scrollYOffset: 0 scrollYOffset: 0
}; };
// singleton
export default class OptionsManager {
constructor() {
if (OptionsManager.prototype._instance) {
return OptionsManager.prototype._instance;
}
OptionsManager.prototype._instance = this;
this._defaults = {
scrollYOffset: 0
};
this._options = {};
}
get options() {
return this._options;
}
set options(opts) {
this._options = Object.assign({}, this._defaults, opts);
}
}