Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Clone component b-collapse with prop auto-permission
Browse files Browse the repository at this point in the history
  • Loading branch information
sawakisei committed May 18, 2019
1 parent 7efff36 commit d9dd05d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 34 deletions.
55 changes: 55 additions & 0 deletions uit.hotel.client/components/bootstrap/b-collapse.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<b-collapse
v-if="isShow"
:id="id"
:visible="visible"
@input="$emit('input', $event)"
>
<slot />
</b-collapse>
</template>
<script lang="ts">
import { Component, Prop, mixins, Vue } from 'nuxt-property-decorator';
import { PermissionMixin, PermissionUnion } from '~/components/mixins';
function isPermissionMixin(instance: Vue): instance is PermissionMixin {
return 'permission' in instance;
}
@Component({
name: 'b-collapse-',
})
export default class extends mixins<PermissionMixin>(PermissionMixin) {
@Prop({ default: false, type: Boolean })
visible!: boolean;
@Prop({ required: true, type: String, default: '' })
id!: string;
@Prop({ default: false, type: Boolean })
autoPermission!: boolean;
mounted() {
if (this.autoPermission === false) return;
const defaultSlot = this.$slots.default;
if (defaultSlot === undefined) return;
this.autoPermissionValue = defaultSlot.reduce<PermissionUnion[]>(
(output, current) => {
(() => {
const child = current.componentInstance;
if (child === undefined) return;
if (!isPermissionMixin(child)) return;
const permission = child.permission;
if (typeof permission === 'boolean') return;
output = output.concat(permission);
})();
return output;
},
[],
);
}
}
</script>
28 changes: 20 additions & 8 deletions uit.hotel.client/components/layout/sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
'ManageHiringRoom',
]"
/>
<b-collapse id="collapse_receptionist" v-model="showReceptionist">
<b-collapse-
id="collapse_receptionist"
v-model="showReceptionist"
auto-permission
>
<b-nav-item-icon-
to="/receptionist/map"
icon="grid"
Expand All @@ -34,7 +38,7 @@
exact
:permission="['ManageHiringRoom']"
/>
</b-collapse>
</b-collapse->
<b-nav-item-icon-
v-b-toggle.collapse_business
icon="chevron-down"
Expand All @@ -46,7 +50,11 @@
'ManageRate',
]"
/>
<b-collapse id="collapse_business" v-model="showBusiness">
<b-collapse-
id="collapse_business"
v-model="showBusiness"
auto-permission
>
<b-nav-item-icon-
to="/business/bill"
icon="dollar-sign"
Expand Down Expand Up @@ -75,15 +83,19 @@
exact
:permission="['ManageRate']"
/>
</b-collapse>
</b-collapse->
<b-nav-item-icon-
v-b-toggle.collapse_personnel
icon="chevron-down"
text="Quản lý nhân sự"
class="header-item"
:permission="['ManagePosition', 'ManageEmployee']"
/>
<b-collapse id="collapse_personnel" v-model="showPersonnel">
<b-collapse-
id="collapse_personnel"
v-model="showPersonnel"
auto-permission
>
<b-nav-item-icon-
to="/personnel/position"
icon="lock"
Expand All @@ -98,15 +110,15 @@
exact
:permission="['ManageEmployee']"
/>
</b-collapse>
</b-collapse->
<b-nav-item-icon-
v-b-toggle.collapse_manage
icon="chevron-down"
text="Cài đặt khách sạn"
class="header-item"
:permission="['ManageMap', 'ManagePatronKind']"
/>
<b-collapse id="collapse_manage" v-model="showManage">
<b-collapse- id="collapse_manage" v-model="showManage" auto-permission>
<b-nav-item-icon-
to="/manage/floor-room"
icon="archive"
Expand All @@ -128,7 +140,7 @@
exact
:permission="['ManagePatronKind']"
/>
</b-collapse>
</b-collapse->
</b-navbar-nav>
</template>
<script lang="ts">
Expand Down
52 changes: 26 additions & 26 deletions uit.hotel.client/components/mixins/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,46 @@ const PermissionInstance: PermissionType = {
permissionManageMap: true,
};

type PermissionUnion = keyof PermissionType;
export type PermissionUnion = keyof PermissionType;

@Component
export class PermissionMixin extends Vue {
@(namespace('user').State)
employee!: UserLogin.Employee;
private employee!: UserLogin.Employee;

@Prop({
default: true,
validator(value: boolean | string[]) {
if (typeof value === 'boolean') return true;
else if (typeof value === 'object' && Array.isArray(value)) {
return value.every((key: string) => {
const condition =
key in PermissionInstance ||
'permission' + key in PermissionInstance;
if (!condition) {
console.error(
`[Permission warn]: String "${key}" is not a is not a valid prop for permission.\n`,
`Expect key to be one of these types:`,
Object.keys(PermissionInstance).map(k =>
k.replace(/^permission/, ''),
),
);
}
return condition;
});
}
return false;
else if (!Array.isArray(value)) return false;

return value.every((key: string) => {
const condition = 'permission' + key in PermissionInstance;
if (!condition) {
console.error(
`[Permission warn]: String "${key}" is not a is not a valid prop for permission.\n`,
`Expect key to be one of these types:`,
Object.keys(PermissionInstance).map(k =>
k.replace(/^permission/, ''),
),
);
}
return condition;
});
},
})
permission!: boolean | PermissionUnion[] | string[];
public permission!: boolean | PermissionUnion[];
protected autoPermissionValue: PermissionUnion[] | null = null;

get isShow() {
const { permission, employee } = this;
protected get isShow(): boolean {
const { permission, autoPermissionValue, employee } = this;
const keys =
autoPermissionValue !== null ? autoPermissionValue : permission;

if (!employee) return true;
else if (typeof permission === 'boolean') return permission;
else if (permission === undefined) return false;
return permission.every((each: string | PermissionUnion) => {
else if (typeof keys === 'boolean') return keys;
else if (keys === undefined) return false;
return keys.every((each: string | PermissionUnion) => {
const key = each; // if p is string
return employee.position['permission' + key];
});
Expand Down

0 comments on commit d9dd05d

Please sign in to comment.