Add support for grouping items in menu

This commit is contained in:
Roman Hotsiy 2016-12-25 17:14:31 +02:00
parent c41ffe8209
commit 90ae0448eb
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 145 additions and 74 deletions

View File

@ -1,5 +1,5 @@
<li *ngFor="let item of items; let idx = index" class="menu-item" <li *ngFor="let item of items; let idx = index" class="menu-item"
ngClass="menu-item-level-{{item.level}}{{item.active ? ' active' : ''}}"> ngClass="menu-item-depth-{{item.depth}} {{item.active ? 'active' : ''}}">
<label class="menu-item-header" [ngClass]="{disabled: !item.ready}" (click)="activateItem(item)"> {{item.name}}</label> <label class="menu-item-header" [ngClass]="{disabled: !item.ready}" (click)="activateItem(item)"> {{item.name}}</label>
<ul *ngIf="item.items" class="menu-subitems"> <ul *ngIf="item.items" class="menu-subitems">
<side-menu-items [items]="item.items" (activate)="activateItem($event)"> </side-menu-items> <side-menu-items [items]="item.items" (activate)="activateItem($event)"> </side-menu-items>

View File

@ -2,7 +2,7 @@
.menu-item-header { .menu-item-header {
cursor: pointer; cursor: pointer;
color: rgba($text-color, .6); color: rgba($text-color, .9);
-webkit-transition: all .15s ease-in-out; -webkit-transition: all .15s ease-in-out;
-moz-transition: all .15s ease-in-out; -moz-transition: all .15s ease-in-out;
-ms-transition: all .15s ease-in-out; -ms-transition: all .15s ease-in-out;
@ -35,7 +35,7 @@
.menu-item:hover, .menu-item:hover,
.menu-item.active { .menu-item.active {
background: darken($side-menu-active-bg-color, 6%); //background: darken($side-menu-active-bg-color, 6%);
} }
.menu-subitems { .menu-subitems {
@ -53,7 +53,7 @@
} }
} }
.menu-item-level-1 { .menu-item-depth-1 {
> .menu-item-header { > .menu-item-header {
font-family: $headers-font, $headers-font-family; font-family: $headers-font, $headers-font-family;
font-weight: $light; font-weight: $light;
@ -67,11 +67,11 @@
background: $side-menu-active-bg-color; background: $side-menu-active-bg-color;
} }
&.active { &.active {
background: $side-menu-active-bg-color; //background: $side-menu-active-bg-color;
} }
} }
.menu-item-level-2 { .menu-item-depth-2 {
> .menu-item-header { > .menu-item-header {
padding-left: 2*$side-menu-item-hpadding; padding-left: 2*$side-menu-item-hpadding;
} }
@ -81,3 +81,25 @@
background: darken($side-menu-active-bg-color, 6%); background: darken($side-menu-active-bg-color, 6%);
} }
} }
// group items
.menu-item-depth-0 {
margin-top: 15px;
> .menu-subitems {
height: auto;
}
> .menu-item-header {
font-family: $headers-font, $headers-font-family;
color: rgba($text-color, .4);
text-transform: uppercase;
font-size: 0.8em;
padding-bottom: 0;
cursor: default;
}
&:hover,
&.active {
//background: none;
}
}

View File

@ -5,9 +5,8 @@ import { Component, EventEmitter, Input, Output, ElementRef, ChangeDetectorRef,
//import { global } from '@angular/core/src/facade/lang'; //import { global } from '@angular/core/src/facade/lang';
import { trigger, state, animate, transition, style } from '@angular/core'; import { trigger, state, animate, transition, style } from '@angular/core';
import { BaseComponent, SpecManager } from '../base'; import { BaseComponent, SpecManager } from '../base';
import { ScrollService, MenuService, OptionsService } from '../../services/index'; import { ScrollService, MenuService, OptionsService, MenuItem } from '../../services/';
import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter'; import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter';
import { MenuItem } from '../../services/schema-helper.service';
const global = window; const global = window;

View File

@ -5,7 +5,7 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { ScrollService, INVIEW_POSITION } from './scroll.service'; import { ScrollService, INVIEW_POSITION } from './scroll.service';
import { Hash } from './hash.service'; import { Hash } from './hash.service';
import { SpecManager } from '../utils/spec-manager'; import { SpecManager } from '../utils/spec-manager';
import { SchemaHelper, MenuItem } from './schema-helper.service'; import { SchemaHelper } from './schema-helper.service';
import { AppStateService } from './app-state.service'; import { AppStateService } from './app-state.service';
import { LazyTasksService } from '../shared/components/LazyFor/lazy-for'; import { LazyTasksService } from '../shared/components/LazyFor/lazy-for';
import { JsonPointer } from '../utils/JsonPointer'; import { JsonPointer } from '../utils/JsonPointer';
@ -17,6 +17,30 @@ const CHANGE = {
BACK : -1, BACK : -1,
}; };
interface TagGroup {
name: string;
tags: string[];
}
export interface MenuItem {
id: string;
name: string;
description?: string;
items?: Array<MenuItem>;
parent?: MenuItem;
active?: boolean;
ready?: boolean;
level?: number;
flatIdx?: number;
metadata?: any;
isGroup?: boolean;
}
@Injectable() @Injectable()
export class MenuService { export class MenuService {
changed: EventEmitter<any> = new EventEmitter(); changed: EventEmitter<any> = new EventEmitter();
@ -27,6 +51,7 @@ export class MenuService {
private _flatItems: MenuItem[]; private _flatItems: MenuItem[];
private _hashSubscription: Subscription; private _hashSubscription: Subscription;
private _scrollSubscription: Subscription; private _scrollSubscription: Subscription;
private _tagsWithMethods: any;
constructor( constructor(
private hash:Hash, private hash:Hash,
@ -111,9 +136,14 @@ export class MenuService {
getEl(flatIdx:number):Element { getEl(flatIdx:number):Element {
if (flatIdx < 0) return null; if (flatIdx < 0) return null;
let currentItem = this.flatItems[flatIdx]; let currentItem = this.flatItems[flatIdx];
if (currentItem.isGroup) currentItem = this.flatItems[flatIdx + 1];
let selector = ''; let selector = '';
while(currentItem) { while(currentItem) {
if (currentItem.id) {
selector = `[section="${currentItem.id}"] ` + selector selector = `[section="${currentItem.id}"] ` + selector
}
currentItem = currentItem.parent; currentItem = currentItem.parent;
} }
selector = selector.trim(); selector = selector.trim();
@ -129,8 +159,9 @@ export class MenuService {
let item = this.flatItems[idx]; let item = this.flatItems[idx];
item.active = false; item.active = false;
if (item.parent) { while (item.parent) {
item.parent.active = false; item.parent.active = false;
item = item.parent;
} }
} }
@ -143,10 +174,12 @@ export class MenuService {
if (idx < 0) return; if (idx < 0) return;
item.active = true; item.active = true;
if (item.parent) {
item.parent.active = true;
}
let cItem = item;
while (cItem.parent) {
cItem.parent.active = true;
cItem = cItem.parent;
}
this.changed.next(item); this.changed.next(item);
} }
@ -207,33 +240,10 @@ export class MenuService {
} }
} }
addTagsAndOperationItems() { getMethodsItems(parent: MenuItem, tag:any):MenuItem[] {
let schema = this.specMgr.schema; if (!tag.methods || !tag.methods.length) return null;
let menu = this.items;
let tags = SchemaHelper.getTagsWithMethods(schema); let res = [];
for (let tag of tags || []) {
let id = 'tag/' + slugify(tag.name);
let item: MenuItem;
let items: MenuItem[];
// don't put empty tag into menu, instead put their methods
if (tag.name !== '') {
item = {
name: tag['x-displayName'] || tag.name,
id: id,
description: tag.description,
metadata: { type: 'tag' }
};
if (tag.methods && tag.methods.length) {
item.items = items = [];
}
} else {
item = null;
items = menu;
}
if (items) {
for (let method of tag.methods) { for (let method of tag.methods) {
let subItem = { let subItem = {
name: SchemaHelper.methodSummary(method), name: SchemaHelper.methodSummary(method),
@ -244,36 +254,94 @@ export class MenuService {
pointer: method._pointer, pointer: method._pointer,
operationId: method.operationId operationId: method.operationId
}, },
parent: item parent: parent
} }
items.push(subItem); res.push(subItem);
} }
return res;
} }
if (item) menu.push(item); getTagsItems(parent: MenuItem, tagsGroup:string[] = null):MenuItem[] {
let schema = this.specMgr.schema;
if (!tagsGroup) {
// all tags
tagsGroup = Object.keys(this._tagsWithMethods);
} }
let tags = tagsGroup.map(k => this._tagsWithMethods[k]);
let res = [];
for (let tag of tags || []) {
let id = 'tag/' + slugify(tag.name);
let item: MenuItem;
// don't put empty tag into menu, instead put their methods
if (tag.name === '') {
let items = this.getMethodsItems(null, tag);
res.push(...items);
continue;
}
item = {
name: tag['x-displayName'] || tag.name,
id: id,
description: tag.description,
metadata: { type: 'tag' },
parent: parent,
items: null
};
item.items = this.getMethodsItems(item, tag);
res.push(item);
}
return res;
}
getTagGroupsItems(parent: MenuItem, groups: TagGroup[]):MenuItem[] {
let res = [];
for (let group of groups) {
let item;
item = {
name: group.name,
id: null,
description: '',
parent: parent,
isGroup: true,
items: null
};
item.items = this.getTagsItems(item, group.tags);
res.push(item);
}
return res;
} }
buildMenu() { buildMenu() {
this._tagsWithMethods = SchemaHelper.getTagsWithMethods(this.specMgr.schema);
this.items = this.items || []; this.items = this.items || [];
this.addMarkdownItems(); this.addMarkdownItems();
this.addTagsAndOperationItems(); if (this.specMgr.schema['x-tagGroups']) {
this.items.push(...this.getTagGroupsItems(null, this.specMgr.schema['x-tagGroups']));
} else {
this.items.push(...this.getTagsItems(null));
}
} }
flatMenu():MenuItem[] { flatMenu():MenuItem[] {
let menu = this.items; let menu = this.items;
let res = []; let res = [];
let level = 1; let curDepth = 1;
let recursive = function(items) { let recursive = (items) => {
for (let item of items) { for (let item of items) {
res.push(item); res.push(item);
item.level = item.level || level; item.depth = item.isGroup ? 0 : curDepth;
item.flatIdx = res.length - 1; item.flatIdx = res.length - 1;
if (item.items) { if (item.items) {
level++; if (!item.isGroup) curDepth++;
recursive(item.items); recursive(item.items);
level--; if (!item.isGroup) curDepth--;
} }
} }
} }

View File

@ -9,24 +9,6 @@ interface PropertyPreprocessOptions {
skipReadOnly?: boolean; skipReadOnly?: boolean;
} }
export interface MenuItem {
id: string;
name: string;
description?: string;
items?: Array<MenuItem>;
parent?: MenuItem;
active?: boolean;
ready?: boolean;
level?: number;
flatIdx?: number;
metadata?: any;
}
// global var for this module // global var for this module
var specMgrInstance; var specMgrInstance;
@ -331,6 +313,6 @@ export class SchemaHelper {
} }
} }
return Object.keys(tags).map(k => tags[k]); return tags;
} }
} }