From 7b7acb8679c4f317bbf96844437f42c669725f93 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 30 Dec 2015 01:29:29 +0200 Subject: [PATCH] Pass options through init function --- demo/index.html | 2 +- lib/components/Redoc/redoc.js | 10 +++++----- lib/components/Redoc/redoc.spec.js | 11 +++++++---- lib/index.js | 8 ++++++-- lib/options.js | 25 +++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/demo/index.html b/demo/index.html index f1b97272..1095cdaf 100644 --- a/demo/index.html +++ b/demo/index.html @@ -21,7 +21,7 @@ diff --git a/lib/components/Redoc/redoc.js b/lib/components/Redoc/redoc.js index ac90a6b5..2d9c20d3 100644 --- a/lib/components/Redoc/redoc.js +++ b/lib/components/Redoc/redoc.js @@ -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]]); diff --git a/lib/components/Redoc/redoc.spec.js b/lib/components/Redoc/redoc.spec.js index 4a939245..4e6be661 100644 --- a/lib/components/Redoc/redoc.spec.js +++ b/lib/components/Redoc/redoc.spec.js @@ -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(``, err => { if (err) return done.fail(err); component.options.scrollYOffset().should.be.equal(123); - options.scrollYOffset = 0; + optsMgr.options.scrollYOffset = 0; done(); }); }); diff --git a/lib/index.js b/lib/index.js index 93854637..9f6594f6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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(); diff --git a/lib/options.js b/lib/options.js index be544497..9ca80fe4 100644 --- a/lib/options.js +++ b/lib/options.js @@ -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); + } +}