redoc/src/services/models/Group.model.ts

80 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-01-22 21:30:53 +03:00
import { action, observable } from 'mobx';
2017-10-12 00:01:37 +03:00
import { OpenAPIExternalDocumentation, OpenAPITag } from '../../types';
import { safeSlugify } from '../../utils';
import { MarkdownHeading } from '../MarkdownRenderer';
2017-10-12 00:01:37 +03:00
import { ContentItemModel } from '../MenuBuilder';
import { IMenuItem, MenuItemGroupType } from '../MenuStore';
/**
* Operations Group model ready to be used by components
*/
export class GroupModel implements IMenuItem {
//#region IMenuItem fields
id: string;
absoluteIdx?: number;
name: string;
description?: string;
type: MenuItemGroupType;
2018-01-22 21:30:53 +03:00
items: ContentItemModel[] = [];
2017-10-12 00:01:37 +03:00
parent?: GroupModel;
externalDocs?: OpenAPIExternalDocumentation;
2018-08-18 16:23:33 +03:00
@observable
active: boolean = false;
@observable
expanded: boolean = false;
2017-10-12 00:01:37 +03:00
depth: number;
level: number;
2017-10-12 00:01:37 +03:00
//#endregion
constructor(
type: MenuItemGroupType,
tagOrGroup: OpenAPITag | MarkdownHeading,
parent?: GroupModel,
) {
// markdown headings already have ids calculated as they are needed for heading anchors
this.id = (tagOrGroup as MarkdownHeading).id || type + '/' + safeSlugify(tagOrGroup.name);
2017-10-12 00:01:37 +03:00
this.type = type;
this.name = tagOrGroup['x-displayName'] || tagOrGroup.name;
this.level = (tagOrGroup as MarkdownHeading).level || 1;
2017-10-12 00:01:37 +03:00
this.description = tagOrGroup.description || '';
this.parent = parent;
this.externalDocs = (tagOrGroup as OpenAPITag).externalDocs;
2017-10-12 00:01:37 +03:00
// groups are active (expanded) by default
if (this.type === 'group') {
this.expanded = true;
2017-10-12 00:01:37 +03:00
}
}
@action
activate() {
this.active = true;
}
@action
expand() {
if (this.parent) {
this.parent.expand();
}
this.expanded = true;
}
@action
collapse() {
// disallow collapsing groups
2018-01-22 21:30:53 +03:00
if (this.type === 'group') {
return;
}
this.expanded = false;
}
@action
deactivate() {
2017-10-12 00:01:37 +03:00
this.active = false;
}
}