2015-12-27 02:02:17 +03:00
|
|
|
'use strict';
|
2016-05-06 12:46:41 +03:00
|
|
|
import { Injectable } from '@angular/core';
|
2016-10-29 12:31:56 +03:00
|
|
|
import { isFunction, isString } from '../utils/helpers';
|
2016-08-28 21:46:10 +03:00
|
|
|
import { BrowserDomAdapter as DOM } from '../utils/browser-adapter';
|
2015-12-27 02:02:17 +03:00
|
|
|
|
2016-05-06 12:46:41 +03:00
|
|
|
const defaults = {
|
2016-02-10 16:48:07 +03:00
|
|
|
scrollYOffset: 0,
|
2016-08-28 21:46:10 +03:00
|
|
|
disableLazySchemas: false
|
2015-12-27 02:02:17 +03:00
|
|
|
};
|
2015-12-30 02:29:29 +03:00
|
|
|
|
2016-11-30 14:23:24 +03:00
|
|
|
const OPTION_NAMES = new Set([
|
|
|
|
'scrollYOffset',
|
|
|
|
'disableLazySchemas',
|
|
|
|
'specUrl',
|
|
|
|
'suppressWarnings',
|
2017-02-26 02:54:12 +03:00
|
|
|
'hideHostname',
|
2016-12-14 15:49:02 +03:00
|
|
|
'lazyRendering',
|
2017-02-27 13:08:57 +03:00
|
|
|
'expandResponses',
|
2017-04-18 16:39:58 +03:00
|
|
|
'requiredPropsFirst',
|
2017-04-23 15:39:36 +03:00
|
|
|
'noAutoAuth',
|
|
|
|
'pathInMiddlePanel',
|
2017-08-02 15:35:23 +03:00
|
|
|
'untrustedSpec',
|
2017-09-21 01:18:38 +03:00
|
|
|
'hideLoading',
|
|
|
|
'ignoredHeaderParameters',
|
2016-11-30 14:23:24 +03:00
|
|
|
]);
|
|
|
|
|
2017-03-09 21:58:10 +03:00
|
|
|
export interface Options {
|
2016-11-30 14:23:24 +03:00
|
|
|
scrollYOffset?: any;
|
|
|
|
disableLazySchemas?: boolean;
|
|
|
|
specUrl?: string;
|
|
|
|
suppressWarnings?: boolean;
|
2017-02-26 02:54:12 +03:00
|
|
|
hideHostname?: boolean;
|
2016-11-30 14:23:24 +03:00
|
|
|
lazyRendering?: boolean;
|
2016-12-14 15:49:02 +03:00
|
|
|
expandResponses?: Set<string> | 'all';
|
2016-11-30 14:23:24 +03:00
|
|
|
$scrollParent?: HTMLElement | Window;
|
2017-02-27 13:08:57 +03:00
|
|
|
requiredPropsFirst?: boolean;
|
2017-04-18 16:39:58 +03:00
|
|
|
noAutoAuth?: boolean;
|
2017-04-23 15:39:36 +03:00
|
|
|
pathInMiddlePanel?: boolean;
|
2017-05-12 12:38:05 +03:00
|
|
|
untrustedSpec?: boolean;
|
2017-08-02 15:35:23 +03:00
|
|
|
hideLoading?: boolean;
|
2017-03-09 21:58:10 +03:00
|
|
|
spec?: any;
|
2017-09-21 01:18:38 +03:00
|
|
|
ignoredHeaderParameters?: string[];
|
2016-11-30 14:23:24 +03:00
|
|
|
}
|
2016-02-11 14:38:44 +03:00
|
|
|
|
2016-05-06 12:46:41 +03:00
|
|
|
@Injectable()
|
|
|
|
export class OptionsService {
|
2016-11-30 14:23:24 +03:00
|
|
|
private _options: Options;
|
2016-06-13 20:54:24 +03:00
|
|
|
|
2016-08-28 21:46:10 +03:00
|
|
|
constructor() {
|
2016-02-10 16:48:07 +03:00
|
|
|
this._options = defaults;
|
2016-11-24 16:29:29 +03:00
|
|
|
this._normalizeOptions();
|
2016-01-24 22:27:15 +03:00
|
|
|
}
|
|
|
|
|
2016-12-14 15:49:02 +03:00
|
|
|
get options(): Options {
|
2015-12-30 02:29:29 +03:00
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
2016-11-30 14:23:24 +03:00
|
|
|
set options(opts:Options) {
|
2016-02-10 16:48:07 +03:00
|
|
|
this._options = Object.assign(this._options, opts);
|
|
|
|
}
|
|
|
|
|
2016-11-30 14:23:24 +03:00
|
|
|
parseOptions(el:HTMLElement):void {
|
2016-02-10 16:48:07 +03:00
|
|
|
let parsedOpts;
|
2016-08-28 21:46:10 +03:00
|
|
|
let attributesMap = DOM.attributeMap(el);
|
2016-02-10 16:48:07 +03:00
|
|
|
parsedOpts = {};
|
|
|
|
Array.from(attributesMap.keys())
|
|
|
|
//camelCasify
|
|
|
|
.map(k => ({
|
|
|
|
attrName: k,
|
2016-12-02 12:59:29 +03:00
|
|
|
name: k.replace(/-(.)/g, (_, $1) => $1.toUpperCase())
|
2016-02-10 16:48:07 +03:00
|
|
|
})
|
|
|
|
)
|
2016-02-11 14:38:44 +03:00
|
|
|
.filter(option => OPTION_NAMES.has(option.name))
|
2016-02-10 16:48:07 +03:00
|
|
|
.forEach(option => {
|
|
|
|
parsedOpts[option.name] = attributesMap.get(option.attrName);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.options = parsedOpts;
|
|
|
|
this._normalizeOptions();
|
|
|
|
}
|
|
|
|
|
2016-11-30 14:23:24 +03:00
|
|
|
_normalizeOptions():void {
|
2016-02-10 16:48:07 +03:00
|
|
|
// 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)) {
|
2016-08-28 21:46:10 +03:00
|
|
|
el = DOM.query(el);
|
2016-02-10 16:48:07 +03:00
|
|
|
}
|
|
|
|
if (!el) {
|
|
|
|
this._options.scrollYOffset = () => 0;
|
|
|
|
} else {
|
|
|
|
this._options.scrollYOffset = () => el.offsetTop + el.offsetHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isString(this._options.disableLazySchemas)) this._options.disableLazySchemas = true;
|
2016-07-27 18:57:23 +03:00
|
|
|
if (isString(this._options.suppressWarnings)) this._options.suppressWarnings = true;
|
2017-02-26 02:54:12 +03:00
|
|
|
if (isString(this._options.hideHostname)) this._options.hideHostname = true;
|
2016-11-30 14:23:24 +03:00
|
|
|
if (isString(this._options.lazyRendering)) this._options.lazyRendering = true;
|
2017-02-27 13:08:57 +03:00
|
|
|
if (isString(this._options.requiredPropsFirst)) this._options.requiredPropsFirst = true;
|
2017-04-18 16:39:58 +03:00
|
|
|
if (isString(this._options.noAutoAuth)) this._options.noAutoAuth = true;
|
2017-04-23 15:39:36 +03:00
|
|
|
if (isString(this._options.pathInMiddlePanel)) this._options.pathInMiddlePanel = true;
|
2017-05-12 12:38:05 +03:00
|
|
|
if (isString(this._options.untrustedSpec)) this._options.untrustedSpec = true;
|
2017-08-02 15:35:23 +03:00
|
|
|
if (isString(this._options.hideLoading)) this._options.hideLoading = true;
|
2016-12-14 15:49:02 +03:00
|
|
|
if (isString(this._options.expandResponses)) {
|
|
|
|
let str = this._options.expandResponses as string;
|
|
|
|
if (str === 'all') return;
|
|
|
|
this._options.expandResponses = new Set(str.split(','));
|
|
|
|
}
|
2016-11-01 21:40:59 +03:00
|
|
|
}
|
2015-12-30 02:29:29 +03:00
|
|
|
}
|