redoc/lib/services/options.service.ts
Roman Hotsiy 7031176330
fix: remove unused hide-hostname option
(unused since eba219e)
2017-02-26 01:00:23 +02:00

98 lines
2.7 KiB
TypeScript

'use strict';
import { Injectable } from '@angular/core';
import { isFunction, isString } from '../utils/helpers';
import { BrowserDomAdapter as DOM } from '../utils/browser-adapter';
const defaults = {
scrollYOffset: 0,
disableLazySchemas: false
};
const OPTION_NAMES = new Set([
'scrollYOffset',
'disableLazySchemas',
'specUrl',
'suppressWarnings',
'lazyRendering',
'expandResponses'
]);
interface Options {
scrollYOffset?: any;
disableLazySchemas?: boolean;
specUrl?: string;
suppressWarnings?: boolean;
lazyRendering?: boolean;
expandResponses?: Set<string> | 'all';
$scrollParent?: HTMLElement | Window;
}
@Injectable()
export class OptionsService {
private _options: Options;
constructor() {
this._options = defaults;
this._normalizeOptions();
}
get options(): Options {
return this._options;
}
set options(opts:Options) {
this._options = Object.assign(this._options, opts);
}
parseOptions(el:HTMLElement):void {
let parsedOpts;
let attributesMap = DOM.attributeMap(el);
parsedOpts = {};
Array.from(attributesMap.keys())
//camelCasify
.map(k => ({
attrName: k,
name: k.replace(/-(.)/g, (_, $1) => $1.toUpperCase())
})
)
.filter(option => OPTION_NAMES.has(option.name))
.forEach(option => {
parsedOpts[option.name] = attributesMap.get(option.attrName);
});
this.options = parsedOpts;
this._normalizeOptions();
}
_normalizeOptions():void {
// modify scrollYOffset to always be a function
if (!isFunction(this._options.scrollYOffset)) {
if (isFinite(this._options.scrollYOffset)) {
// if number specified create function that returns this value
let numberOffset = parseFloat(this._options.scrollYOffset);
this.options.scrollYOffset = () => numberOffset;
} else {
// if selector or node function that returns bottom offset of this node
let el = this._options.scrollYOffset;
if (!(el instanceof Node)) {
el = DOM.query(el);
}
if (!el) {
this._options.scrollYOffset = () => 0;
} else {
this._options.scrollYOffset = () => el.offsetTop + el.offsetHeight;
}
}
}
if (isString(this._options.disableLazySchemas)) this._options.disableLazySchemas = true;
if (isString(this._options.suppressWarnings)) this._options.suppressWarnings = true;
if (isString(this._options.lazyRendering)) this._options.lazyRendering = true;
if (isString(this._options.expandResponses)) {
let str = this._options.expandResponses as string;
if (str === 'all') return;
this._options.expandResponses = new Set(str.split(','));
}
}
}