redoc/lib/components/Redoc/redoc.js

65 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
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';
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-27 00:12:10 +03:00
import {options as defaultOptions} from '../../index';
2015-12-26 20:44:39 +03:00
import {ChangeDetectionStrategy} from 'angular2/core';
2015-12-26 20:44:39 +03:00
import {ElementRef} from 'angular2/core';
import {BrowserDomAdapter} from 'angular2/platform/browser';
import detectScollParent from 'scrollparent';
2015-12-27 00:12:10 +03:00
let optionNames = new Set(['scrollYOffset']);
@RedocComponent({
selector: 'redoc',
2015-12-26 20:44:39 +03:00
providers: [SchemaManager, BrowserDomAdapter],
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],
changeDetection: ChangeDetectionStrategy.Default
})
2015-10-27 20:44:08 +03:00
export default class Redoc extends BaseComponent {
2015-12-27 00:12:10 +03:00
constructor(schemaMgr, elementRef, dom) {
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();
this.options = Object.assign({}, defaultOptions, this.options);
}
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);
});
// post-process options
this.options.scrollYOffset = parseInt(this.options.scrollYOffset);
}
}
2015-12-27 00:12:10 +03:00
Redoc.parameters = Redoc.parameters.concat([[ElementRef], [BrowserDomAdapter]]);
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]]);