2015-10-07 12:47:57 +03:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-09 23:19:35 +03:00
|
|
|
import {RedocComponent, BaseComponent} from '../base';
|
2015-10-27 20:44:08 +03:00
|
|
|
import SchemaManager from '../../utils/SchemaManager';
|
2015-12-26 20:44:39 +03:00
|
|
|
|
2015-10-27 20:44:08 +03:00
|
|
|
import ApiInfo from '../ApiInfo/api-info';
|
2015-12-21 00:34:20 +03:00
|
|
|
import ApiLogo from '../ApiLogo/api-logo';
|
2015-10-27 20:44:08 +03:00
|
|
|
import MethodsList from '../MethodsList/methods-list';
|
2015-11-16 02:10:04 +03:00
|
|
|
import SideMenu from '../SideMenu/side-menu';
|
2015-12-26 20:44:39 +03:00
|
|
|
import StickySidebar from '../../common/components/StickySidebar/sticky-sidebar';
|
2015-12-30 02:29:29 +03:00
|
|
|
import OptionsManager from '../../options';
|
2015-12-30 11:16:36 +03:00
|
|
|
import {redocEvents} from '../../events';
|
2015-12-26 20:44:39 +03:00
|
|
|
|
2015-12-14 18:20:40 +03:00
|
|
|
import {ChangeDetectionStrategy} from 'angular2/core';
|
2015-12-26 20:44:39 +03:00
|
|
|
import {ElementRef} from 'angular2/core';
|
2015-12-30 11:16:36 +03:00
|
|
|
import {BrowserDomAdapter, bootstrap} from 'angular2/platform/browser';
|
2015-12-26 20:44:39 +03:00
|
|
|
import detectScollParent from 'scrollparent';
|
|
|
|
|
2015-12-27 02:02:17 +03:00
|
|
|
import {isFunction} from 'angular2/src/facade/lang';
|
|
|
|
|
2015-12-27 00:12:10 +03:00
|
|
|
let optionNames = new Set(['scrollYOffset']);
|
2015-10-07 12:47:57 +03:00
|
|
|
|
2015-10-09 23:19:35 +03:00
|
|
|
@RedocComponent({
|
2015-10-07 12:47:57 +03:00
|
|
|
selector: 'redoc',
|
2015-12-30 02:29:29 +03:00
|
|
|
providers: [SchemaManager, BrowserDomAdapter, OptionsManager],
|
2015-10-07 12:47:57 +03:00
|
|
|
templateUrl: './lib/components/Redoc/redoc.html',
|
2015-11-16 02:15:06 +03:00
|
|
|
styleUrls: ['./lib/components/Redoc/redoc.css'],
|
2015-12-26 20:44:39 +03:00
|
|
|
directives: [ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar],
|
2015-11-29 14:37:19 +03:00
|
|
|
changeDetection: ChangeDetectionStrategy.Default
|
2015-10-07 12:47:57 +03:00
|
|
|
})
|
2015-10-27 20:44:08 +03:00
|
|
|
export default class Redoc extends BaseComponent {
|
2015-12-30 02:29:29 +03:00
|
|
|
constructor(schemaMgr, optionsMgr, elementRef, dom) {
|
2015-10-09 23:19:35 +03:00
|
|
|
super(schemaMgr);
|
2015-12-26 20:44:39 +03:00
|
|
|
this.element = elementRef.nativeElement;
|
|
|
|
|
2015-12-27 00:12:10 +03:00
|
|
|
this.dom = dom;
|
2015-12-26 20:44:39 +03:00
|
|
|
let el = this.element;
|
|
|
|
|
|
|
|
//parse options (top level component doesn't support inputs)
|
2015-12-27 00:12:10 +03:00
|
|
|
this.scrollParent = detectScollParent(el);
|
|
|
|
this.parseOptions();
|
2015-12-30 02:29:29 +03:00
|
|
|
this.options = Object.assign({}, optionsMgr.options, this.options);
|
2015-12-27 02:02:17 +03:00
|
|
|
this.normalizeOptions();
|
2015-12-27 00:12:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
parseOptions() {
|
|
|
|
let attributesMap = this.dom.attributeMap(this.element);
|
|
|
|
this.options = {};
|
|
|
|
Array.from(attributesMap.keys())
|
|
|
|
//camelCasify
|
|
|
|
.map(k => ({
|
|
|
|
attrName: k,
|
|
|
|
name: k.replace(/-(.)/g, (m, $1) => $1.toUpperCase())
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.filter(option => optionNames.has(option.name))
|
|
|
|
.forEach(option => {
|
|
|
|
this.options[option.name] = attributesMap.get(option.attrName);
|
|
|
|
});
|
2015-12-27 02:02:17 +03:00
|
|
|
}
|
2015-12-27 00:12:10 +03:00
|
|
|
|
2015-12-27 02:02:17 +03:00
|
|
|
normalizeOptions() {
|
|
|
|
// 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 = this.dom.query(el);
|
|
|
|
}
|
|
|
|
if (!el) {
|
|
|
|
this.options.scrollYOffset = () => 0;
|
|
|
|
} else {
|
|
|
|
this.options.scrollYOffset = () => el.offsetTop + el.offsetHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 12:47:57 +03:00
|
|
|
}
|
2015-12-30 11:16:36 +03:00
|
|
|
|
|
|
|
static init(schemaUrl, options) {
|
2016-01-15 20:01:09 +03:00
|
|
|
if (Redoc.appRef) {
|
|
|
|
Redoc.dispose();
|
|
|
|
}
|
2016-01-15 12:35:45 +03:00
|
|
|
return SchemaManager.instance().load(schemaUrl)
|
|
|
|
.then(() => {
|
|
|
|
(new OptionsManager()).options = options;
|
|
|
|
return bootstrap(Redoc);
|
|
|
|
})
|
|
|
|
.then(
|
2016-01-15 20:01:09 +03:00
|
|
|
(appRef) => {
|
|
|
|
Redoc.appRef = appRef;
|
2016-01-15 12:35:45 +03:00
|
|
|
redocEvents.bootstrapped.next();
|
|
|
|
console.log('ReDoc bootstrapped!');
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
);
|
2015-12-30 11:16:36 +03:00
|
|
|
}
|
2016-01-15 12:35:45 +03:00
|
|
|
|
2016-01-15 20:01:09 +03:00
|
|
|
static dispose() {
|
|
|
|
let dom = new BrowserDomAdapter();
|
|
|
|
let el = dom.query('redoc');
|
|
|
|
let parent = el.parentElement;
|
|
|
|
let nextSibling = el.nextElementSibling;
|
|
|
|
|
|
|
|
Redoc.appRef && Redoc.appRef.dispose();
|
|
|
|
Redoc.appRef = null;
|
|
|
|
|
|
|
|
// Redoc dispose removes host element, so need to restore it
|
|
|
|
el = dom.createElement('redoc');
|
|
|
|
el.innerText = 'Loading...';
|
|
|
|
parent.insertBefore(el, nextSibling);
|
|
|
|
}
|
2015-10-07 12:47:57 +03:00
|
|
|
}
|
2015-12-30 02:29:29 +03:00
|
|
|
Redoc.parameters = Redoc.parameters.concat([[OptionsManager], [ElementRef], [BrowserDomAdapter]]);
|
2015-12-26 20:44:39 +03:00
|
|
|
|
2016-01-15 12:35:45 +03:00
|
|
|
// TODO
|
2015-12-26 20:44:39 +03:00
|
|
|
// this doesn't work in side-menu.js because of some circular references issue
|
|
|
|
SideMenu.parameters = SideMenu.parameters.concat([[Redoc]]);
|