Hangar/frontend/components/mixins.ts

139 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-03-21 10:06:09 +08:00
import { Component, mixins, Prop, State, Vue, Watch } from 'nuxt-property-decorator';
2021-02-10 07:56:02 +08:00
import { PropType } from 'vue';
2021-03-22 14:39:06 +08:00
import { HangarProject, HangarUser, HangarVersion, IPlatform, Organization, ProjectPage } from 'hangar-internal';
2021-03-21 10:06:09 +08:00
import { User } from 'hangar-api';
2021-03-20 15:24:17 +08:00
import MarkdownEditor from '~/components/markdown/MarkdownEditor.vue';
import { Platform, ReviewState } from '~/types/enums';
2021-03-12 15:59:36 +08:00
import { RootState } from '~/store';
2021-03-21 10:06:09 +08:00
import { AuthState } from '~/store/auth';
2021-02-10 07:56:02 +08:00
2021-03-18 04:16:54 +08:00
@Component
2021-03-21 10:06:09 +08:00
export class HangarComponent extends Vue {
@State((state: AuthState) => state.user, { namespace: 'auth' })
2021-03-22 14:39:06 +08:00
currentUser!: HangarUser | null;
2021-03-21 10:06:09 +08:00
@State((state: RootState) => state.validations)
validations!: RootState['validations'];
}
2021-03-22 14:39:06 +08:00
export class Authed extends HangarComponent {
@State((state: AuthState) => state.user, { namespace: 'auth' })
currentUser!: HangarUser;
}
export class UserPage extends HangarComponent {
user!: User;
organization!: Organization | null;
get isCurrentUser() {
return this.currentUser && this.currentUser.name === this.user.name;
}
}
2021-03-21 10:06:09 +08:00
@Component
2021-03-22 14:39:06 +08:00
export class UserPropPage extends UserPage {
2021-03-21 10:06:09 +08:00
@Prop({ type: Object as PropType<User>, required: true })
user!: User;
2021-03-22 14:39:06 +08:00
@Prop({ type: Object as PropType<Organization> })
2021-03-22 14:39:06 +08:00
organization!: Organization | null;
2021-03-18 04:16:54 +08:00
}
2021-02-10 07:56:02 +08:00
@Component
2021-03-21 10:06:09 +08:00
export class HangarProjectMixin extends HangarComponent {
2021-02-10 07:56:02 +08:00
@Prop({ type: Object as PropType<HangarProject>, required: true })
project!: HangarProject;
}
@Component
export class HangarProjectVersionMixin extends HangarProjectMixin {
@Prop({ type: Map as PropType<Map<Platform, HangarVersion>>, required: true })
versions!: Map<Platform, HangarVersion>;
2021-03-12 15:59:36 +08:00
get projectVersion(): HangarVersion {
return this.versions.get(<Platform>this.$route.params.platform.toUpperCase())!;
}
2021-03-12 15:59:36 +08:00
get platform(): IPlatform {
return (this.$store.state as RootState).platforms.get(this.$route.params.platform.toUpperCase() as Platform)!;
}
get isReviewStateChecked(): boolean {
return this.projectVersion.reviewState === ReviewState.PARTIALLY_REVIEWED || this.projectVersion.reviewState === ReviewState.REVIEWED;
}
get isUnderReview(): boolean {
return this.projectVersion.reviewState === ReviewState.UNDER_REVIEW;
}
}
2021-02-10 07:56:02 +08:00
@Component
export class DocPageMixin extends HangarProjectMixin {
editingPage: boolean = false;
page = {
contents: '',
deletable: false,
} as ProjectPage;
$refs!: {
editor: MarkdownEditor;
};
savePage(content: string) {
this.$api
.requestInternal(`pages/save/${this.project.id}/${this.page.id}`, true, 'post', {
content,
})
.then(() => {
this.page.contents = content;
this.editingPage = false;
})
.catch((err) => {
this.$refs.editor.loading.save = false;
2021-02-13 14:36:53 +08:00
this.$util.handleRequestError(err, 'page.new.error.save');
2021-02-10 07:56:02 +08:00
});
}
deletePage() {
this.$api
.requestInternal(`pages/delete/${this.project.id}/${this.page.id}`, true, 'post')
.then(() => {
this.$refs.editor.loading.delete = false;
this.$router.replace(`/${this.$route.params.author}/${this.$route.params.slug}`);
})
.catch(this.$util.handleRequestError);
}
}
@Component
2021-03-21 10:06:09 +08:00
export class HangarModal extends HangarComponent {
2021-02-10 07:56:02 +08:00
dialog: boolean = false;
@Prop({ type: String, default: '' })
activatorClass!: string;
@Watch('dialog')
2021-03-18 07:55:48 +08:00
onToggleView(val: boolean) {
if (!val) {
this.$nextTick(() => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
if (typeof this.$refs.modalForm !== 'undefined') {
// @ts-ignore
this.$refs.modalForm.reset();
}
});
2021-02-10 07:56:02 +08:00
}
}
}
@Component
2021-03-21 10:06:09 +08:00
export class HangarForm extends HangarComponent {
2021-02-10 07:56:02 +08:00
loading: boolean = false;
validForm: boolean = false;
}
2021-03-13 18:10:56 +08:00
@Component
export class HangarFormModal extends mixins(HangarModal, HangarForm) {}