Skip to content

Commit

Permalink
feat(module:theme): add recursive upward find in menu service (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Oct 31, 2018
1 parent 1145d47 commit c391d3a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/abc/page-header/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ config: PageHeaderConfig
`[homeLink]` | 首页链接 | `string` | `/`
`[homeI18n]` | 首页链接国际化参数 | `string` | -
`[autoBreadcrumb]` | 自动生成导航,以当前路由从主菜单中定位 | `boolean` | `true`
`[recursiveBreadcrumb]` | 自动向上递归查找,菜单数据源包含 `/ware`,则 `/ware/1` 也视为 `/ware` 项 | `boolean` | `false`
`[loading]` | 是否加载中 | `boolean` | `false`
`[wide]` | 是否定宽 | `boolean` | `false`
`[fixed]` | 是否固定模式 | `boolean` | `false`
Expand Down
6 changes: 5 additions & 1 deletion packages/abc/page-header/page-header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PageHeaderComponent
if (this._menus) {
return this._menus;
}
this._menus = this.menuSrv.getPathByUrl(this.route.url.split('?')[0]);
this._menus = this.menuSrv.getPathByUrl(this.route.url.split('?')[0], this.recursiveBreadcrumb);

return this._menus;
}
Expand Down Expand Up @@ -121,6 +121,10 @@ export class PageHeaderComponent
@Input()
breadcrumb: TemplateRef<any>;

@Input()
@InputBoolean()
recursiveBreadcrumb: boolean;

@Input()
logo: TemplateRef<any>;

Expand Down
5 changes: 5 additions & 0 deletions packages/abc/page-header/page-header.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export class PageHeaderConfig {
* 自动生成导航,以当前路由从主菜单中定位
*/
autoBreadcrumb?: boolean = true;
/**
* 自动向上递归查找
* - 菜单数据源包含 `/ware`,则 `/ware/1` 也视为 `/ware` 项
*/
recursiveBreadcrumb?: boolean = false;
/**
* 自动生成标题,以当前路由从主菜单中定位
*/
Expand Down
8 changes: 6 additions & 2 deletions packages/theme/src/services/menu/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ This is because menus it's essential part of the applications, And it can be use
| `add` | `items: Menu[]` | Setting menu data |
| `clear` | - | Clear menu data |
| `resume` | `callback: Funection` | Reset menu, may need call when I18N, user acl changed |
| `openedByUrl` | `url: string` | Set menu `_open` attribute by URL (`_open` expands the submenu) |
| `getPathByUrl` | `url: string` | Get menu list based on url |
| `openedByUrl` | `url, recursive = false` | Set menu `_open` attribute by URL (`_open` expands the submenu) |
| `getPathByUrl` | `url, recursive = false` | Get menu list based on url |

**recursive**

Recursive upward find, for example, the menu data source contains `/ware`, then `/ware/1` is equivalent to `/ware`.
8 changes: 6 additions & 2 deletions packages/theme/src/services/menu/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ type: Service
| `add` | `items: Menu[]` | 设置菜单数据 |
| `clear` | - | 清空菜单数据 |
| `resume` | `callback: Funection` | 重置菜单,可能I18N、用户权限变动时需要调用刷新 |
| `openedByUrl` | `url: string` | 根据URL设置菜单 `_open` 属性(`_open`用于是否展开菜单的条件值) |
| `getPathByUrl` | `url: string` | 根据url获取菜单列表 |
| `openedByUrl` | `url, recursive = false` | 根据URL设置菜单 `_open` 属性(`_open`用于是否展开菜单的条件值) |
| `getPathByUrl` | `url, recursive = false` | 根据url获取菜单列表 |

**recursive**

表示自动向上递归查找,例如菜单数据源包含 `/ware`,则 `/ware/1` 也视为 `/ware` 项。
10 changes: 10 additions & 0 deletions packages/theme/src/services/menu/menu.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ describe('Service: Menu', () => {
srv.openedByUrl(null);
expect(srv.menus.filter(w => w._open === false).length).toBe(0);
});
it('recursive up find', () => {
srv.add(deepCopy(DATA));
srv.openedByUrl(`/dashboard/v1/1`, true);
expect(srv.menus[0]._open).toBe(true);
});
});

describe('#getPathByUrl', () => {
Expand All @@ -130,6 +135,11 @@ describe('Service: Menu', () => {
const menus = srv.getPathByUrl(`/dashboard/v1111`);
expect(menus.length).toBe(0);
});
it('recursive up find', () => {
srv.add(deepCopy(DATA));
expect(srv.getPathByUrl(`/dashboard/1`).length).toBe(0);
expect(srv.getPathByUrl(`/dashboard/1`, true).length).toBe(1);
});
});

describe('#shortcuts', () => {
Expand Down
59 changes: 37 additions & 22 deletions packages/theme/src/services/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export class MenuService implements OnDestroy {
// compatible `anticon anticon-user`
if (~item.icon.indexOf(`anticon-`)) {
type = 'icon';
value = value.split('-').slice(1).join('-');
value = value
.split('-')
.slice(1)
.join('-');
} else if (/^https?:\/\//.test(item.icon)) {
type = 'img';
}
Expand Down Expand Up @@ -168,23 +171,39 @@ export class MenuService implements OnDestroy {
this._change$.next(this.data);
}

private getHit(url: string, recursive = false, cb: (i: Menu) => void = null) {
let item: Menu = null;

while (!item && url) {
this.visit(i => {
if (cb) {
cb(i);
}
if (i.link != null && i.link === url) {
item = i;
}
});

if (!recursive) break;

url = url
.split('/')
.slice(0, -1)
.join('/');
}

return item;
}

/**
* 根据URL设置菜单 `_open` 属性
* @param url URL地址
* - 若 `recursive: true` 则会自动向上递归查找
* - 菜单数据源包含 `/ware`,则 `/ware/1` 也视为 `/ware` 项
*/
openedByUrl(url: string) {
openedByUrl(url: string, recursive = false) {
if (!url) return;

let findItem: Menu = null;
this.visit(item => {
item._open = false;
if (!item.link) {
return;
}
if (!findItem && url.startsWith(item.link)) {
findItem = item;
}
});
let findItem = this.getHit(url, recursive, i => (i._open = false));
if (!findItem) return;

do {
Expand All @@ -195,17 +214,13 @@ export class MenuService implements OnDestroy {

/**
* 根据url获取菜单列表
* @param url
* - 若 `recursive: true` 则会自动向上递归查找
* - 菜单数据源包含 `/ware`,则 `/ware/1` 也视为 `/ware` 项
*/
getPathByUrl(url: string): Menu[] {
let item: Menu = null;
this.visit((i, parent, depth) => {
if (i.link === url) {
item = i;
}
});

getPathByUrl(url: string, recursive = false): Menu[] {
const ret: Menu[] = [];
let item = this.getHit(url, recursive);

if (!item) return ret;

do {
Expand Down
10 changes: 5 additions & 5 deletions scripts/ci/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ travisFoldStart "publish.dist"

echo "Removed everything from ${packageRepo}#${branchName} and added the new build output."

if [[ $(git ls-remote origin "refs/tags/${buildTagName}") ]]; then
echo "Skipping publish because tag is already published"
exit 0
fi

# 替换版本号
if [[ $commitMessageCheck =~ "release(" ]]; then
echo "===== Release version does not need to change version ====="
Expand All @@ -98,6 +93,11 @@ travisFoldStart "publish.dist"

echo "https://${DELON_BUILDS_TOKEN}:@github.com" > .git/credentials

if [[ $(git ls-remote origin "refs/tags/${buildTagName}") ]]; then
echo "removed tag because tag is already published"
git push origin :refs/tags/${buildTagName}
fi

echo "Git configuration has been updated to match the last commit author. Publishing now.."

git add -A
Expand Down

0 comments on commit c391d3a

Please sign in to comment.