2018-02-22 12:26:53 +03:00
|
|
|
import { observe } from 'mobx';
|
|
|
|
|
2017-11-14 18:46:50 +03:00
|
|
|
import { OpenAPISpec } from '../types';
|
2017-11-19 13:51:59 +03:00
|
|
|
import { loadAndBundleSpec } from '../utils/loadAndBundleSpec';
|
2018-01-22 21:30:53 +03:00
|
|
|
import { MenuStore } from './MenuStore';
|
|
|
|
import { SpecStore } from './models';
|
2017-11-21 14:00:33 +03:00
|
|
|
import { RedocNormalizedOptions, RedocRawOptions } from './RedocNormalizedOptions';
|
2018-01-22 21:30:53 +03:00
|
|
|
import { ScrollService } from './ScrollService';
|
2018-02-08 19:41:02 +03:00
|
|
|
import { SearchStore } from './SearchStore';
|
2018-02-22 12:26:53 +03:00
|
|
|
import { MarkerService } from './MarkerService';
|
2017-10-12 00:01:37 +03:00
|
|
|
|
2018-01-22 21:30:53 +03:00
|
|
|
interface StoreData {
|
2017-11-14 18:46:50 +03:00
|
|
|
menu: {
|
|
|
|
activeItemIdx: number;
|
|
|
|
};
|
|
|
|
spec: {
|
|
|
|
url: string;
|
|
|
|
data: any;
|
|
|
|
};
|
2017-11-21 14:00:33 +03:00
|
|
|
options: RedocRawOptions;
|
2018-01-22 21:30:53 +03:00
|
|
|
}
|
2017-11-14 18:46:50 +03:00
|
|
|
|
2017-11-21 14:00:33 +03:00
|
|
|
export async function createStore(
|
|
|
|
spec: object,
|
|
|
|
specUrl: string | undefined,
|
|
|
|
options: RedocRawOptions = {},
|
|
|
|
) {
|
2017-11-19 13:51:59 +03:00
|
|
|
const resolvedSpec = await loadAndBundleSpec(spec || specUrl);
|
2017-11-21 14:00:33 +03:00
|
|
|
return new AppStore(resolvedSpec, specUrl, options);
|
2017-11-19 13:51:59 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
export class AppStore {
|
2018-01-22 21:30:53 +03:00
|
|
|
/**
|
|
|
|
* deserialize store
|
|
|
|
* **SUPER HACKY AND NOT OPTIMAL IMPLEMENTATION**
|
|
|
|
*/
|
|
|
|
// TODO:
|
|
|
|
static fromJS(state: StoreData): AppStore {
|
|
|
|
const inst = new AppStore(state.spec.data, state.spec.url, state.options);
|
|
|
|
inst.menu.activeItemIdx = state.menu.activeItemIdx || 0;
|
|
|
|
inst.menu.activate(inst.menu.flatItems[inst.menu.activeItemIdx]);
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
menu: MenuStore;
|
|
|
|
spec: SpecStore;
|
2017-11-21 14:00:33 +03:00
|
|
|
rawOptions: RedocRawOptions;
|
|
|
|
options: RedocNormalizedOptions;
|
2018-02-08 19:41:02 +03:00
|
|
|
search: SearchStore;
|
2018-02-22 12:26:53 +03:00
|
|
|
marker = new MarkerService();
|
2017-10-12 00:01:37 +03:00
|
|
|
|
2017-11-14 18:46:50 +03:00
|
|
|
private scroll: ScrollService;
|
2018-02-22 12:26:53 +03:00
|
|
|
private disposer;
|
2017-10-12 00:01:37 +03:00
|
|
|
|
2017-11-21 14:00:33 +03:00
|
|
|
constructor(spec: OpenAPISpec, specUrl?: string, options: RedocRawOptions = {}) {
|
|
|
|
this.rawOptions = options;
|
|
|
|
this.options = new RedocNormalizedOptions(options);
|
2018-01-11 19:08:33 +03:00
|
|
|
this.scroll = new ScrollService(this.options);
|
2017-11-21 14:00:33 +03:00
|
|
|
this.spec = new SpecStore(spec, specUrl, this.options);
|
2017-10-12 00:01:37 +03:00
|
|
|
this.menu = new MenuStore(this.spec, this.scroll);
|
2018-02-08 19:41:02 +03:00
|
|
|
|
|
|
|
this.search = new SearchStore(this.spec);
|
2018-02-22 12:26:53 +03:00
|
|
|
|
|
|
|
this.disposer = observe(this.menu, 'activeItemIdx', change => {
|
|
|
|
this.updateMarkOnMenu(change.newValue as number);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMarkOnMenu(idx: number) {
|
|
|
|
console.log('update marker');
|
|
|
|
const start = Math.max(0, idx);
|
|
|
|
const end = Math.min(this.menu.flatItems.length, start + 5);
|
|
|
|
|
|
|
|
const elements: Element[] = [];
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
let elem = this.menu.getElementAt(i);
|
|
|
|
if (!elem) continue;
|
|
|
|
if (this.menu.flatItems[i].type === 'section') {
|
|
|
|
elem = elem.parentElement!.parentElement;
|
|
|
|
}
|
|
|
|
if (elem) elements.push(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.marker.addOnly(elements);
|
|
|
|
this.marker.mark();
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this.scroll.dispose();
|
|
|
|
this.menu.dispose();
|
2018-02-22 12:26:53 +03:00
|
|
|
this.disposer();
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serializes store
|
|
|
|
* **SUPER HACKY AND NOT OPTIMAL IMPLEMENTATION**
|
|
|
|
*/
|
|
|
|
// TODO:
|
2017-11-14 18:46:50 +03:00
|
|
|
toJS(): StoreData {
|
2017-10-12 00:01:37 +03:00
|
|
|
return {
|
|
|
|
menu: {
|
2017-11-14 18:46:50 +03:00
|
|
|
activeItemIdx: this.menu.activeItemIdx,
|
2017-10-12 00:01:37 +03:00
|
|
|
},
|
|
|
|
spec: {
|
2017-11-14 18:46:50 +03:00
|
|
|
url: this.spec.parser.specUrl,
|
|
|
|
data: this.spec.parser.spec,
|
2017-10-12 00:01:37 +03:00
|
|
|
},
|
2017-11-21 14:00:33 +03:00
|
|
|
options: this.rawOptions,
|
2017-10-12 00:01:37 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|