2017-11-20 19:02:49 +03:00
|
|
|
import { ThemeInterface } from '../theme';
|
|
|
|
import { isNumeric } from '../utils/helpers';
|
|
|
|
|
2017-11-20 23:36:21 +03:00
|
|
|
export interface RedocRawOptions {
|
|
|
|
theme?: ThemeInterface;
|
|
|
|
scrollYOffset?: number | string | Function;
|
|
|
|
hideHostname?: boolean | string;
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:02:49 +03:00
|
|
|
export class RedocNormalizedOptions {
|
|
|
|
theme: ThemeInterface;
|
|
|
|
scrollYOffset: () => number;
|
2017-11-20 23:36:21 +03:00
|
|
|
hideHostname: boolean;
|
2017-11-20 19:02:49 +03:00
|
|
|
|
|
|
|
constructor(raw: RedocRawOptions) {
|
|
|
|
this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
|
2017-11-20 23:36:21 +03:00
|
|
|
this.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
static normalizeHideHostname(value: RedocRawOptions['hideHostname']): boolean {
|
|
|
|
return !!value;
|
2017-11-20 19:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static normalizeScrollYOffset(value: RedocRawOptions['scrollYOffset']): () => number {
|
|
|
|
// just number is not valid selector and leads to crash so checking if isNumeric here
|
|
|
|
if (typeof value === 'string' && !isNumeric(value)) {
|
|
|
|
const el = document.querySelector(value);
|
|
|
|
if (!el) {
|
|
|
|
console.warn(
|
|
|
|
'scrollYOffset value is a selector to non-existing element. Using offset 0 by default',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const bottom = (el && el.getBoundingClientRect().bottom) || 0;
|
|
|
|
return () => bottom;
|
|
|
|
} else if (typeof value === 'number' || isNumeric(value)) {
|
|
|
|
return () => (typeof value === 'number' ? value : parseFloat(value));
|
|
|
|
} else if (typeof value === 'function') {
|
|
|
|
return () => {
|
|
|
|
const res = value();
|
|
|
|
if (typeof res !== 'number') {
|
|
|
|
console.warn(
|
|
|
|
`scrollYOffset should return number but returned value "${res}" of type ${typeof res}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
} else if (value !== undefined) {
|
|
|
|
console.warn(
|
|
|
|
'Wrong value for scrollYOffset ReDoc option: should be string, number or function',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => 0;
|
|
|
|
}
|
|
|
|
}
|