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>
/* init redoc */
Redoc.init('swagger.json');
Redoc.init('swagger.json', {scrollYOffset: 'body>nav'});
</script>
</body>
</html>

View File

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

View File

@ -14,14 +14,17 @@ import {
import Redoc from 'lib/components/Redoc/redoc';
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 Component', () => {
let builder;
beforeEachProviders(() => [
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) => {
builder = tcb;
@ -92,11 +95,11 @@ describe('Redoc components', () => {
});
it('should handle function scrollYOffset', (done) => {
options.scrollYOffset = () => 123;
optsMgr.options.scrollYOffset = () => 123;
build(`<redoc></redoc>`, err => {
if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(123);
options.scrollYOffset = 0;
optsMgr.options.scrollYOffset = 0;
done();
});
});

View File

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

View File

@ -3,3 +3,28 @@
export var options = {
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);
}
}