mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-11 03:16:48 +03:00
Various refactoring
- renam DOM elements variables to start from $ - use DI for OptionsManager - move loading styles into separate CSS file - minor other refactors
This commit is contained in:
parent
e68054553d
commit
5485df84fe
|
@ -10,14 +10,14 @@ import {BrowserDomAdapter} from 'angular2/platform/browser';
|
||||||
@Reflect.metadata('parameters', [[ElementRef], [BrowserDomAdapter]])
|
@Reflect.metadata('parameters', [[ElementRef], [BrowserDomAdapter]])
|
||||||
export default class StickySidebar {
|
export default class StickySidebar {
|
||||||
constructor(elementRef, dom) {
|
constructor(elementRef, dom) {
|
||||||
this.element = elementRef.nativeElement;
|
this.$element = elementRef.nativeElement;
|
||||||
this.dom = dom;
|
this.dom = dom;
|
||||||
|
|
||||||
// initial styling
|
// initial styling
|
||||||
this.dom.setStyle(this.element, 'position', 'absolute');
|
this.dom.setStyle(this.$element, 'position', 'absolute');
|
||||||
this.dom.setStyle(this.element, 'top', '0');
|
this.dom.setStyle(this.$element, 'top', '0');
|
||||||
this.dom.setStyle(this.element, 'bottom', '0');
|
this.dom.setStyle(this.$element, 'bottom', '0');
|
||||||
this.dom.setStyle(this.element, 'max-height', '100%');
|
this.dom.setStyle(this.$element, 'max-height', '100%');
|
||||||
}
|
}
|
||||||
|
|
||||||
bind() {
|
bind() {
|
||||||
|
@ -30,7 +30,7 @@ export default class StickySidebar {
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePosition() {
|
updatePosition() {
|
||||||
if ( this.scrollY + this.scrollYOffset() >= this.redocEl.offsetTop) {
|
if ( this.scrollY + this.scrollYOffset() >= this.$redocEl.offsetTop) {
|
||||||
this.stick();
|
this.stick();
|
||||||
} else {
|
} else {
|
||||||
this.unstick();
|
this.unstick();
|
||||||
|
@ -38,13 +38,13 @@ export default class StickySidebar {
|
||||||
}
|
}
|
||||||
|
|
||||||
stick() {
|
stick() {
|
||||||
this.dom.setStyle(this.element, 'position', 'fixed');
|
this.dom.setStyle(this.$element, 'position', 'fixed');
|
||||||
this.dom.setStyle(this.element, 'top', this.scrollYOffset() + 'px');
|
this.dom.setStyle(this.$element, 'top', this.scrollYOffset() + 'px');
|
||||||
}
|
}
|
||||||
|
|
||||||
unstick() {
|
unstick() {
|
||||||
this.dom.setStyle(this.element, 'position', 'absolute');
|
this.dom.setStyle(this.$element, 'position', 'absolute');
|
||||||
this.dom.setStyle(this.element, 'top', 0);
|
this.dom.setStyle(this.$element, 'top', 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
get scrollY() {
|
get scrollY() {
|
||||||
|
@ -52,7 +52,8 @@ export default class StickySidebar {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.redocEl = this.element.offsetParent;
|
// FIXME use more reliable code
|
||||||
|
this.$redocEl = this.$element.offsetParent;
|
||||||
this.bind();
|
this.bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,21 +20,23 @@ var cache = {};
|
||||||
template: '',
|
template: '',
|
||||||
directives: [CORE_DIRECTIVES]
|
directives: [CORE_DIRECTIVES]
|
||||||
})
|
})
|
||||||
@Reflect.metadata('parameters', [[ElementRef], [DynamicComponentLoader]])
|
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef], [DynamicComponentLoader], [OptionsManager]])
|
||||||
export default class JsonSchemaLazy {
|
export default class JsonSchemaLazy {
|
||||||
|
|
||||||
constructor(elementRef, dcl) {
|
constructor(schemaMgr, elementRef, dcl, optionsMgr) {
|
||||||
this.elementRef = elementRef;
|
this.elementRef = elementRef;
|
||||||
this.dcl = dcl;
|
this.dcl = dcl;
|
||||||
|
this.optionsMgr = optionsMgr;
|
||||||
|
this.schemaMgr = schemaMgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
normalizePointer() {
|
normalizePointer() {
|
||||||
let schema = SchemaManager.instance().byPointer(this.pointer);
|
let schema = this.schemaMgr.byPointer(this.pointer);
|
||||||
return schema && schema.$ref || this.pointer;
|
return schema && schema.$ref || this.pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
load() {
|
load() {
|
||||||
if (OptionsManager.instance().options.disableLazySchemas) return;
|
if (this.optionsMgr.options.disableLazySchemas) return;
|
||||||
if (this.loaded) return;
|
if (this.loaded) return;
|
||||||
if (this.pointer) {
|
if (this.pointer) {
|
||||||
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
||||||
|
@ -50,17 +52,17 @@ export default class JsonSchemaLazy {
|
||||||
if (cache[this.pointer]) {
|
if (cache[this.pointer]) {
|
||||||
cache[this.pointer].then((compRef) => {
|
cache[this.pointer].then((compRef) => {
|
||||||
setTimeout( ()=> {
|
setTimeout( ()=> {
|
||||||
let element = compRef.location.nativeElement;
|
let $element = compRef.location.nativeElement;
|
||||||
|
|
||||||
// skip caching view with discriminator as it needs attached controller
|
// skip caching view with tabs inside (discriminator) as it needs attached controller
|
||||||
if (element.querySelector('.discriminator')) {
|
if ($element.querySelector('tabs')) {
|
||||||
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
||||||
compRef.instance.pointer = this.pointer;
|
compRef.instance.pointer = this.pointer;
|
||||||
compRef.hostView.changeDetectorRef.markForCheck();
|
compRef.hostView.changeDetectorRef.markForCheck();
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
insertAfter(element.cloneNode(true), this.elementRef.nativeElement);
|
insertAfter($element.cloneNode(true), this.elementRef.nativeElement);
|
||||||
} );
|
} );
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -76,6 +78,11 @@ export default class JsonSchemaLazy {
|
||||||
if (!this.auto) return;
|
if (!this.auto) return;
|
||||||
this.loadCached();
|
this.loadCached();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngOnDestroy() {
|
||||||
|
// clear cache
|
||||||
|
cache = {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertAfter(newNode, referenceNode) {
|
function insertAfter(newNode, referenceNode) {
|
||||||
|
|
|
@ -17,7 +17,7 @@ import JsonPointer from '../../utils/JsonPointer';
|
||||||
export default class JsonSchema extends BaseComponent {
|
export default class JsonSchema extends BaseComponent {
|
||||||
constructor(schemaMgr, elementRef) {
|
constructor(schemaMgr, elementRef) {
|
||||||
super(schemaMgr);
|
super(schemaMgr);
|
||||||
this.element = elementRef.nativeElement;
|
this.$element = elementRef.nativeElement;
|
||||||
this.final = false;
|
this.final = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,20 +86,6 @@ export default class JsonSchema extends BaseComponent {
|
||||||
this.data.properties = props;
|
this.data.properties = props;
|
||||||
}
|
}
|
||||||
|
|
||||||
adjustNameColumnWidth() {
|
|
||||||
// TODO handle internal schemes differently
|
|
||||||
let names = [].slice.call(this.element.querySelectorAll('.param-name-content'));
|
|
||||||
let widths = [144];//names.map(el => el.offsetWidth);
|
|
||||||
let maxWidth = Math.max(...widths);
|
|
||||||
if (!maxWidth) return;
|
|
||||||
names.forEach(el => {
|
|
||||||
el.parentNode.style.minWidth = maxWidth + 'px';
|
|
||||||
});
|
|
||||||
|
|
||||||
let discrValues = this.element.querySelector('tabs ul');
|
|
||||||
if (discrValues) discrValues.style.paddingLeft = maxWidth + 'px';
|
|
||||||
}
|
|
||||||
|
|
||||||
static injectPropData(propData, propName, propPointer, requiredMap, schema) {
|
static injectPropData(propData, propName, propPointer, requiredMap, schema) {
|
||||||
let propEnum;
|
let propEnum;
|
||||||
|
|
||||||
|
@ -145,12 +131,27 @@ export default class JsonSchema extends BaseComponent {
|
||||||
|
|
||||||
ngAfterViewInit() {
|
ngAfterViewInit() {
|
||||||
// adjust widht only on parent level
|
// adjust widht only on parent level
|
||||||
let el = this.element.parentElement;
|
let $el = this.$element.parentElement;
|
||||||
while(el && el.tagName !== 'JSON-SCHEMA' && el.tagName !== 'REDOC') {
|
while($el && $el.tagName !== 'JSON-SCHEMA' && $el.tagName !== 'REDOC') {
|
||||||
el = el.parentElement;
|
$el = $el.parentElement;
|
||||||
}
|
}
|
||||||
if (el && el.tagName === 'REDOC' ) {
|
if ($el && $el.tagName === 'REDOC' ) {
|
||||||
this.adjustNameColumnWidth();
|
this.adjustNameColumnWidth();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
adjustNameColumnWidth() {
|
||||||
|
// TODO handle internal schemes differently
|
||||||
|
|
||||||
|
let names = [].slice.call(this.$element.querySelectorAll('.param-name-content'));
|
||||||
|
let widths = [144];//names.map(el => el.offsetWidth);
|
||||||
|
let maxWidth = Math.max(...widths);
|
||||||
|
if (!maxWidth) return;
|
||||||
|
names.forEach(el => {
|
||||||
|
el.parentNode.style.minWidth = maxWidth + 'px';
|
||||||
|
});
|
||||||
|
|
||||||
|
let discrValues = this.$element.querySelector('tabs ul');
|
||||||
|
if (discrValues) discrValues.style.paddingLeft = maxWidth + 'px';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
37
lib/components/Redoc/redoc-loading-styles.scss
Normal file
37
lib/components/Redoc/redoc-loading-styles.scss
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
redoc.loading {
|
||||||
|
position: relative;
|
||||||
|
display: block;
|
||||||
|
min-height:350px;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes move {
|
||||||
|
0% {transform: translateY(10px)}
|
||||||
|
25% {transform: translateY(0px)}
|
||||||
|
50% {transform: translateY(10px)}
|
||||||
|
75% {transform: translateY(20px)}
|
||||||
|
100% {transform: translateY(10px)}
|
||||||
|
}
|
||||||
|
|
||||||
|
redoc.loading:before {
|
||||||
|
content: "Loading...";
|
||||||
|
font-size: 28px;
|
||||||
|
text-align: center;
|
||||||
|
padding-top: 40px;
|
||||||
|
color: #3F5C9C;
|
||||||
|
font-weight: bold;
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
background-color: white;
|
||||||
|
z-index: 9999;
|
||||||
|
opacity: 1;
|
||||||
|
transition: all 0.6s ease-out;
|
||||||
|
animation: 2s move linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
redoc.loading-remove:before {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
<div class="redoc-wrap">
|
<div class="redoc-wrap">
|
||||||
<div class="menu-content" sticky-sidebar [scrollParent]="scrollParent" [scrollYOffset]="options.scrollYOffset">
|
<div class="menu-content" sticky-sidebar [scrollParent]="options.$scrollParent" [scrollYOffset]="options.scrollYOffset">
|
||||||
<api-logo> </api-logo>
|
<api-logo> </api-logo>
|
||||||
<side-menu> </side-menu>
|
<side-menu> </side-menu>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import {ChangeDetectionStrategy} from 'angular2/core';
|
import {ChangeDetectionStrategy, provide} from 'angular2/core';
|
||||||
import {ElementRef} from 'angular2/core';
|
import {ElementRef} from 'angular2/core';
|
||||||
import {BrowserDomAdapter, bootstrap} from 'angular2/platform/browser';
|
import {BrowserDomAdapter, bootstrap} from 'angular2/platform/browser';
|
||||||
import detectScollParent from 'scrollparent';
|
import detectScollParent from 'scrollparent';
|
||||||
|
|
||||||
import {isFunction, isString} from 'angular2/src/facade/lang';
|
|
||||||
|
|
||||||
import {RedocComponent, BaseComponent} from '../base';
|
import {RedocComponent, BaseComponent} from '../base';
|
||||||
import SchemaManager from '../../utils/SchemaManager';
|
import SchemaManager from '../../utils/SchemaManager';
|
||||||
|
|
||||||
|
@ -18,119 +16,36 @@ import StickySidebar from '../../common/components/StickySidebar/sticky-sidebar'
|
||||||
import OptionsManager from '../../options';
|
import OptionsManager from '../../options';
|
||||||
import {redocEvents} from '../../events';
|
import {redocEvents} from '../../events';
|
||||||
|
|
||||||
let optionNames = new Set(['scrollYOffset', 'disableLazySchemas']);
|
import './redoc-loading-styles.css!css';
|
||||||
|
|
||||||
let dom = new BrowserDomAdapter();
|
let dom = new BrowserDomAdapter();
|
||||||
|
|
||||||
@RedocComponent({
|
@RedocComponent({
|
||||||
selector: 'redoc',
|
selector: 'redoc',
|
||||||
providers: [SchemaManager, BrowserDomAdapter, OptionsManager],
|
providers: [
|
||||||
|
SchemaManager,
|
||||||
|
BrowserDomAdapter,
|
||||||
|
provide('OPTION_NAMES', {useValue: new Set(['scrollYOffset', 'disableLazySchemas'])}),
|
||||||
|
provide(OptionsManager, {useClass: OptionsManager})
|
||||||
|
],
|
||||||
templateUrl: './lib/components/Redoc/redoc.html',
|
templateUrl: './lib/components/Redoc/redoc.html',
|
||||||
styleUrls: ['./lib/components/Redoc/redoc.css'],
|
styleUrls: ['./lib/components/Redoc/redoc.css'],
|
||||||
directives: [ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar],
|
directives: [ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar],
|
||||||
changeDetection: ChangeDetectionStrategy.Default
|
changeDetection: ChangeDetectionStrategy.Default
|
||||||
})
|
})
|
||||||
@Reflect.metadata('parameters', [[SchemaManager], [OptionsManager], [ElementRef], [BrowserDomAdapter]])
|
@Reflect.metadata('parameters', [
|
||||||
|
[SchemaManager], [OptionsManager], [ElementRef]])
|
||||||
export default class Redoc extends BaseComponent {
|
export default class Redoc extends BaseComponent {
|
||||||
constructor(schemaMgr, optionsMgr, elementRef, dom) {
|
constructor(schemaMgr, optionsMgr, elementRef) {
|
||||||
super(schemaMgr);
|
super(schemaMgr);
|
||||||
this.element = elementRef.nativeElement;
|
this.element = elementRef.nativeElement;
|
||||||
|
|
||||||
this.dom = dom;
|
|
||||||
let el = this.element;
|
|
||||||
|
|
||||||
//parse options (top level component doesn't support inputs)
|
//parse options (top level component doesn't support inputs)
|
||||||
this.scrollParent = detectScollParent(el);
|
optionsMgr.parseOptions( this.element );
|
||||||
this.parseOptions();
|
optionsMgr.options.$scrollParent = detectScollParent( this.element );
|
||||||
this.options = Object.assign({}, optionsMgr.options, this.options);
|
this.options = optionsMgr.options;
|
||||||
this.normalizeOptions();
|
|
||||||
optionsMgr.options = 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isString(this.options.disableLazySchemas)) this.options.disableLazySchemas = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static showLoadingAnimation() {
|
static showLoadingAnimation() {
|
||||||
if (!dom.query('#redoc-loading-style')) {
|
|
||||||
let animStyle = dom.createStyleElement(`
|
|
||||||
redoc.loading {
|
|
||||||
position: relative;
|
|
||||||
display: block;
|
|
||||||
min-height:350px;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes move {
|
|
||||||
0% {transform: translateY(10px)}
|
|
||||||
25% {transform: translateY(0px)}
|
|
||||||
50% {transform: translateY(10px)}
|
|
||||||
75% {transform: translateY(20px)}
|
|
||||||
100% {transform: translateY(10px)}
|
|
||||||
}
|
|
||||||
|
|
||||||
redoc.loading:before {
|
|
||||||
content: "Loading...";
|
|
||||||
font-size: 28px;
|
|
||||||
text-align: center;
|
|
||||||
padding-top: 40px;
|
|
||||||
color: #3F5C9C;
|
|
||||||
font-weight: bold;
|
|
||||||
display: block;
|
|
||||||
position: absolute;
|
|
||||||
top: 0;
|
|
||||||
bottom: 0;
|
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
background-color: white;
|
|
||||||
z-index: 9999;
|
|
||||||
opacity: 1;
|
|
||||||
transition: all 0.6s ease-out;
|
|
||||||
animation: 2s move linear infinite;
|
|
||||||
}
|
|
||||||
|
|
||||||
redoc.loading-remove:before {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
animStyle.id = 'redoc-loading-style';
|
|
||||||
dom.appendChild(dom.defaultDoc().head, animStyle);
|
|
||||||
}
|
|
||||||
let elem = dom.query('redoc');
|
let elem = dom.query('redoc');
|
||||||
dom.addClass(elem, 'loading');
|
dom.addClass(elem, 'loading');
|
||||||
}
|
}
|
||||||
|
@ -158,6 +73,7 @@ export default class Redoc extends BaseComponent {
|
||||||
(appRef) => {
|
(appRef) => {
|
||||||
Redoc.hideLoadingAnimation();
|
Redoc.hideLoadingAnimation();
|
||||||
Redoc.appRef = appRef;
|
Redoc.appRef = appRef;
|
||||||
|
// setTimeout to allow cached elements to init
|
||||||
setTimeout(() => redocEvents.bootstrapped.next());
|
setTimeout(() => redocEvents.bootstrapped.next());
|
||||||
console.log('ReDoc bootstrapped!');
|
console.log('ReDoc bootstrapped!');
|
||||||
},
|
},
|
||||||
|
@ -170,7 +86,6 @@ export default class Redoc extends BaseComponent {
|
||||||
|
|
||||||
static autoInit() {
|
static autoInit() {
|
||||||
const specUrlAttributeName = 'spec-url';
|
const specUrlAttributeName = 'spec-url';
|
||||||
let dom = new BrowserDomAdapter();
|
|
||||||
let redocEl = dom.query('redoc');
|
let redocEl = dom.query('redoc');
|
||||||
if (!redocEl) return;
|
if (!redocEl) return;
|
||||||
if (dom.hasAttribute(redocEl, specUrlAttributeName)) {
|
if (dom.hasAttribute(redocEl, specUrlAttributeName)) {
|
||||||
|
@ -180,7 +95,6 @@ export default class Redoc extends BaseComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
static dispose() {
|
static dispose() {
|
||||||
let dom = new BrowserDomAdapter();
|
|
||||||
let el = dom.query('redoc');
|
let el = dom.query('redoc');
|
||||||
let elClone;
|
let elClone;
|
||||||
let parent;
|
let parent;
|
||||||
|
|
|
@ -6,7 +6,6 @@ import JsonSchema from '../JsonSchema/json-schema';
|
||||||
import JsonSchemaLazy from '../JsonSchema/json-schema-lazy';
|
import JsonSchemaLazy from '../JsonSchema/json-schema-lazy';
|
||||||
import Zippy from '../../common/components/Zippy/zippy';
|
import Zippy from '../../common/components/Zippy/zippy';
|
||||||
import {statusCodeType} from '../../utils/helpers';
|
import {statusCodeType} from '../../utils/helpers';
|
||||||
import OptionsManager from '../../options';
|
|
||||||
|
|
||||||
function isNumeric(n) {
|
function isNumeric(n) {
|
||||||
return (!isNaN(parseFloat(n)) && isFinite(n));
|
return (!isNaN(parseFloat(n)) && isFinite(n));
|
||||||
|
@ -26,7 +25,6 @@ export default class ResponsesList extends BaseComponent {
|
||||||
prepareModel() {
|
prepareModel() {
|
||||||
this.data = {};
|
this.data = {};
|
||||||
this.data.responses = [];
|
this.data.responses = [];
|
||||||
this.enabledLazy = !OptionsManager.instance().options.disableLazySchemas;
|
|
||||||
|
|
||||||
let responses = this.componentSchema;
|
let responses = this.componentSchema;
|
||||||
if (!responses) return;
|
if (!responses) return;
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import {NgZone, ChangeDetectionStrategy, ElementRef, forwardRef} from 'angular2/core';
|
import {NgZone, ChangeDetectionStrategy, ElementRef} from 'angular2/core';
|
||||||
import Redoc from '../Redoc/redoc';
|
|
||||||
import {document} from 'angular2/src/facade/browser';
|
import {document} from 'angular2/src/facade/browser';
|
||||||
import {BrowserDomAdapter} from 'angular2/platform/browser';
|
import {BrowserDomAdapter} from 'angular2/platform/browser';
|
||||||
import {global} from 'angular2/src/facade/lang';
|
import {global} from 'angular2/src/facade/lang';
|
||||||
|
|
||||||
import {RedocComponent, BaseComponent, SchemaManager} from '../base';
|
import {RedocComponent, BaseComponent, SchemaManager} from '../base';
|
||||||
import {redocEvents} from '../../events';
|
import {redocEvents} from '../../events';
|
||||||
|
import OptionsManager from '../../options';
|
||||||
|
|
||||||
const CHANGE = {
|
const CHANGE = {
|
||||||
NEXT : 1,
|
NEXT : 1,
|
||||||
|
@ -28,21 +28,19 @@ const INVIEW_POSITION = {
|
||||||
changeDetection: ChangeDetectionStrategy.Default
|
changeDetection: ChangeDetectionStrategy.Default
|
||||||
})
|
})
|
||||||
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef],
|
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef],
|
||||||
[BrowserDomAdapter], [NgZone], [forwardRef(() => Redoc)]])
|
[BrowserDomAdapter], [NgZone], OptionsManager])
|
||||||
export default class SideMenu extends BaseComponent {
|
export default class SideMenu extends BaseComponent {
|
||||||
constructor(schemaMgr, elementRef, adapter, zone, redoc) {
|
constructor(schemaMgr, elementRef, dom, zone, optionsMgr) {
|
||||||
super(schemaMgr);
|
super(schemaMgr);
|
||||||
this.zone = zone;
|
this.dom = dom;
|
||||||
this.adapter = adapter;
|
this.options = optionsMgr.options;
|
||||||
this.redoc = redoc;
|
this.$scrollParent = this.options.$scrollParent;
|
||||||
|
this.$mobileNav = dom.querySelector(elementRef.nativeElement, '.mobile-nav');
|
||||||
this.scrollParent = this.redoc.scrollParent;
|
this.$resourcesNav = dom.querySelector(elementRef.nativeElement, '#resources-nav');
|
||||||
this.mobileNav = adapter.querySelector(elementRef.nativeElement, '.mobile-nav');
|
|
||||||
this.resourcesNav = adapter.querySelector(elementRef.nativeElement, '#resources-nav');
|
|
||||||
|
|
||||||
// for some reason constructor is not run inside zone
|
// for some reason constructor is not run inside zone
|
||||||
// as workaround running it manually
|
// as workaround running it manually
|
||||||
this.zone.run(() => {
|
zone.run(() => {
|
||||||
this.bindEvents();
|
this.bindEvents();
|
||||||
});
|
});
|
||||||
this.activeCatIdx = 0;
|
this.activeCatIdx = 0;
|
||||||
|
@ -56,26 +54,26 @@ export default class SideMenu extends BaseComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollY() {
|
scrollY() {
|
||||||
return (this.scrollParent.pageYOffset != null) ? this.scrollParent.pageYOffset : this.scrollParent.scrollTop;
|
return (this.$scrollParent.pageYOffset != null) ? this.$scrollParent.pageYOffset : this.$scrollParent.scrollTop;
|
||||||
}
|
}
|
||||||
|
|
||||||
hashScroll(evt) {
|
hashScroll(evt) {
|
||||||
let hash = this.adapter.getLocation().hash;
|
let hash = this.dom.getLocation().hash;
|
||||||
if (!hash) return;
|
if (!hash) return;
|
||||||
|
|
||||||
let el;
|
let $el;
|
||||||
hash = hash.substr(1);
|
hash = hash.substr(1);
|
||||||
let namespace = hash.split('/')[0];
|
let namespace = hash.split('/')[0];
|
||||||
let ptr = hash.substr(namespace.length + 1);
|
let ptr = hash.substr(namespace.length + 1);
|
||||||
if (namespace === 'operation') {
|
if (namespace === 'operation') {
|
||||||
el = this.getMethodElByOperId(ptr);
|
$el = this.getMethodElByOperId(ptr);
|
||||||
} else if (namespace === 'tag') {
|
} else if (namespace === 'tag') {
|
||||||
let tag = ptr.split('/')[0];
|
let tag = ptr.split('/')[0];
|
||||||
ptr = ptr.substr(tag.length);
|
ptr = ptr.substr(tag.length);
|
||||||
el = this.getMethodElByPtr(ptr, tag);
|
$el = this.getMethodElByPtr(ptr, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (el) this.scrollTo(el);
|
if ($el) this.scrollTo($el);
|
||||||
if (evt) evt.preventDefault();
|
if (evt) evt.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,12 +82,12 @@ export default class SideMenu extends BaseComponent {
|
||||||
|
|
||||||
//decorate option.scrollYOffset to account mobile nav
|
//decorate option.scrollYOffset to account mobile nav
|
||||||
this.scrollYOffset = () => {
|
this.scrollYOffset = () => {
|
||||||
let mobileNavOffset = this.mobileNav.clientHeight;
|
let mobileNavOffset = this.$mobileNav.clientHeight;
|
||||||
return this.redoc.options.scrollYOffset() + mobileNavOffset;
|
return this.options.scrollYOffset() + mobileNavOffset;
|
||||||
};
|
};
|
||||||
this._cancel = {};
|
this._cancel = {};
|
||||||
this._cancel.scroll = this.adapter.onAndCancel(this.scrollParent, 'scroll', () => { this.scrollHandler(); });
|
this._cancel.scroll = this.dom.onAndCancel(this.$scrollParent, 'scroll', () => { this.scrollHandler(); });
|
||||||
this._cancel.hash = this.adapter.onAndCancel(global, 'hashchange', evt => this.hashScroll(evt));
|
this._cancel.hash = this.dom.onAndCancel(global, 'hashchange', evt => this.hashScroll(evt));
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
@ -105,14 +103,14 @@ export default class SideMenu extends BaseComponent {
|
||||||
this.scrollToActive();
|
this.scrollToActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollTo(el) {
|
scrollTo($el) {
|
||||||
// TODO: rewrite this to use offsetTop as more reliable solution
|
// TODO: rewrite this to use offsetTop as more reliable solution
|
||||||
let subjRect = el.getBoundingClientRect();
|
let subjRect = $el.getBoundingClientRect();
|
||||||
let offset = this.scrollY() + subjRect.top - this.scrollYOffset() + 1;
|
let offset = this.scrollY() + subjRect.top - this.scrollYOffset() + 1;
|
||||||
if (this.scrollParent.scrollTo) {
|
if (this.$scrollParent.scrollTo) {
|
||||||
this.scrollParent.scrollTo(0, offset);
|
this.$scrollParent.scrollTo(0, offset);
|
||||||
} else {
|
} else {
|
||||||
this.scrollParent.scrollTop = offset;
|
this.$scrollParent.scrollTop = offset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,12 +194,12 @@ export default class SideMenu extends BaseComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns 1 if element if above the view, 0 if in view and -1 below the view */
|
/* returns 1 if element if above the view, 0 if in view and -1 below the view */
|
||||||
getElementInViewPos(el) {
|
getElementInViewPos($el) {
|
||||||
if (Math.floor(el.getBoundingClientRect().top) > this.scrollYOffset()) {
|
if (Math.floor($el.getBoundingClientRect().top) > this.scrollYOffset()) {
|
||||||
return INVIEW_POSITION.ABOVE;
|
return INVIEW_POSITION.ABOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (el.getBoundingClientRect().bottom <= this.scrollYOffset()) {
|
if ($el.getBoundingClientRect().bottom <= this.scrollYOffset()) {
|
||||||
return INVIEW_POSITION.BELLOW;
|
return INVIEW_POSITION.BELLOW;
|
||||||
}
|
}
|
||||||
return INVIEW_POSITION.INVIEW;
|
return INVIEW_POSITION.INVIEW;
|
||||||
|
@ -212,9 +210,9 @@ export default class SideMenu extends BaseComponent {
|
||||||
this.prevOffsetY = this.scrollY();
|
this.prevOffsetY = this.scrollY();
|
||||||
let stable = false;
|
let stable = false;
|
||||||
while(!stable) {
|
while(!stable) {
|
||||||
let activeMethodHost = this.getCurrentMethodEl();
|
let $activeMethodHost = this.getCurrentMethodEl();
|
||||||
if (!activeMethodHost) return;
|
if (!$activeMethodHost) return;
|
||||||
var elementInViewPos = this.getElementInViewPos(activeMethodHost);
|
var elementInViewPos = this.getElementInViewPos($activeMethodHost);
|
||||||
if(isScrolledDown && elementInViewPos === INVIEW_POSITION.BELLOW) {
|
if(isScrolledDown && elementInViewPos === INVIEW_POSITION.BELLOW) {
|
||||||
stable = this.changeActive(CHANGE.NEXT);
|
stable = this.changeActive(CHANGE.NEXT);
|
||||||
continue;
|
continue;
|
||||||
|
@ -235,20 +233,20 @@ export default class SideMenu extends BaseComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
mobileMode() {
|
mobileMode() {
|
||||||
return this.mobileNav.clientHeight > 0;
|
return this.$mobileNav.clientHeight > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleMobileNav() {
|
toggleMobileNav() {
|
||||||
let dom = this.adapter;
|
let dom = this.dom;
|
||||||
let overflowParent = (this.scrollParent === global) ? dom.defaultDoc().body : this.scrollParent;
|
let $overflowParent = (this.$scrollParent === global) ? dom.defaultDoc().body : this.$scrollParent;
|
||||||
if (dom.hasStyle(this.resourcesNav, 'height')) {
|
if (dom.hasStyle(this.$resourcesNav, 'height')) {
|
||||||
dom.removeStyle(this.resourcesNav, 'height');
|
dom.removeStyle(this.$resourcesNav, 'height');
|
||||||
dom.removeStyle(overflowParent, 'overflow-y');
|
dom.removeStyle($overflowParent, 'overflow-y');
|
||||||
} else {
|
} else {
|
||||||
let viewportHeight = this.scrollParent.innerHeight || this.scrollParent.clientHeight;
|
let viewportHeight = this.$scrollParent.innerHeight || this.$scrollParent.clientHeight;
|
||||||
let height = viewportHeight - this.mobileNav.getBoundingClientRect().bottom;
|
let height = viewportHeight - this.$mobileNav.getBoundingClientRect().bottom;
|
||||||
dom.setStyle(overflowParent, 'overflow-y', 'hidden');
|
dom.setStyle($overflowParent, 'overflow-y', 'hidden');
|
||||||
dom.setStyle(this.resourcesNav, 'height', height + 'px');
|
dom.setStyle(this.$resourcesNav, 'height', height + 'px');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,5 +254,3 @@ export default class SideMenu extends BaseComponent {
|
||||||
this.changeActive(CHANGE.INITIAL);
|
this.changeActive(CHANGE.INITIAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//SideMenu.parameters = SideMenu.parameters.concat([[ElementRef],
|
|
||||||
// [BrowserDomAdapter], [NgZone], [forwardRef(() => Redoc)] ]);
|
|
||||||
|
|
|
@ -1,28 +1,20 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
export var options = {
|
import {Inject} from 'angular2/core';
|
||||||
scrollYOffset: 0
|
import {isFunction, isString} from 'angular2/src/facade/lang';
|
||||||
|
import {BrowserDomAdapter} from 'angular2/platform/browser';
|
||||||
|
|
||||||
|
var defaults = {
|
||||||
|
scrollYOffset: 0,
|
||||||
|
disableLazySchemas: false
|
||||||
};
|
};
|
||||||
|
|
||||||
// singleton
|
@Reflect.metadata('parameters', [[new Inject('OPTION_NAMES')], [BrowserDomAdapter]])
|
||||||
export default class OptionsManager {
|
export default class OptionsManager {
|
||||||
constructor() {
|
constructor(optionNames, dom) {
|
||||||
if (OptionsManager.prototype._instance) {
|
this._options = defaults;
|
||||||
return OptionsManager.prototype._instance;
|
this.optionNames = optionNames;
|
||||||
}
|
this.dom = dom;
|
||||||
|
|
||||||
OptionsManager.prototype._instance = this;
|
|
||||||
|
|
||||||
this._defaults = {
|
|
||||||
scrollYOffset: 0,
|
|
||||||
disableLazySchemas: false
|
|
||||||
};
|
|
||||||
|
|
||||||
this._options = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
static instance() {
|
|
||||||
return new OptionsManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get options() {
|
get options() {
|
||||||
|
@ -30,6 +22,50 @@ export default class OptionsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
set options(opts) {
|
set options(opts) {
|
||||||
this._options = Object.assign({}, this._defaults, opts);
|
this._options = Object.assign(this._options, opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
parseOptions(el) {
|
||||||
|
let parsedOpts;
|
||||||
|
let attributesMap = this.dom.attributeMap(el);
|
||||||
|
parsedOpts = {};
|
||||||
|
Array.from(attributesMap.keys())
|
||||||
|
//camelCasify
|
||||||
|
.map(k => ({
|
||||||
|
attrName: k,
|
||||||
|
name: k.replace(/-(.)/g, (m, $1) => $1.toUpperCase())
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.filter(option => this.optionNames.has(option.name))
|
||||||
|
.forEach(option => {
|
||||||
|
parsedOpts[option.name] = attributesMap.get(option.attrName);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.options = parsedOpts;
|
||||||
|
this._normalizeOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
_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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isString(this._options.disableLazySchemas)) this._options.disableLazySchemas = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user