From 4d580bb7899ef6affd65a6099b2e65595feb25b5 Mon Sep 17 00:00:00 2001 From: Jiangchunfu Date: Mon, 12 Aug 2024 21:39:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=95=B4=E5=8D=B7=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E9=85=8D=E7=BD=AE=EF=BC=9A=E5=BF=85=E5=A1=AB?= =?UTF-8?q?=E3=80=81=E6=98=BE=E7=A4=BA=E7=B1=BB=E5=9E=8B=E3=80=81=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=BA=8F=E5=8F=B7=E3=80=81=E6=98=BE=E7=A4=BA=E5=88=86?= =?UTF-8?q?=E5=89=B2=E7=BA=BF=20(#391)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加整卷配置功能 * fix: 限制单题修改配置时只对基础配置进行更新全局基础配置操作 --------- Co-authored-by: jiangchunfu --- .../modules/questionModule/SetterPanel.vue | 105 ++++++++++++------ .../composables/useEditGlobalBaseConf.ts | 91 +++++++++++++++ web/src/management/stores/edit.ts | 38 +++++-- 3 files changed, 187 insertions(+), 47 deletions(-) create mode 100644 web/src/management/stores/composables/useEditGlobalBaseConf.ts diff --git a/web/src/management/pages/edit/modules/questionModule/SetterPanel.vue b/web/src/management/pages/edit/modules/questionModule/SetterPanel.vue index 1d478bd5..09ae7d56 100644 --- a/web/src/management/pages/edit/modules/questionModule/SetterPanel.vue +++ b/web/src/management/pages/edit/modules/questionModule/SetterPanel.vue @@ -1,37 +1,51 @@ + + diff --git a/web/src/management/stores/composables/useEditGlobalBaseConf.ts b/web/src/management/stores/composables/useEditGlobalBaseConf.ts new file mode 100644 index 00000000..a95bd1d5 --- /dev/null +++ b/web/src/management/stores/composables/useEditGlobalBaseConf.ts @@ -0,0 +1,91 @@ +import { reactive, type Ref } from 'vue' + +export type TypeMethod = 'INIT' | 'MODIFY' | 'REMOVE' | 'ADD' + +export default function useEditGlobalBaseConf( + questionDetailList: Ref, + updateTime: () => void +) { + // 整卷配置数据 + const globalBaseConfig = reactive({ + isRequired: true, + showIndex: true, + showType: true, + showSpliter: true + }) + + // 整卷配置各项选项选中个数统计 + const optionCheckedCounts = { + isRequiredCount: 0, + showIndexCount: 0, + showTypeCount: 0, + showSpliterCount: 0 + } + + // 初始化统计 + function initCounts() { + questionDetailList.value.forEach((question: any) => { + calculateCountsForQuestion('INIT', { question }) + }) + updateGlobalBaseConf() + } + + // 更新统计 + function updateCounts(type: TypeMethod, data: any) { + if (type === 'MODIFY') { + calculateOptionCounts(type, data) + } else { + calculateCountsForQuestion(type, data) + } + updateGlobalBaseConf() + } + + // 计算整卷配置各项选项选中个数 + function calculateCountsForQuestion(type: TypeMethod, { question }: any) { + Object.keys(globalBaseConfig).forEach((key) => { + calculateOptionCounts(type, { key, value: question[key] }) + }) + } + + // 计算单个选项选中个数 + function calculateOptionCounts(type: TypeMethod, { key, value }: any) { + const _key = `${key}Count` as keyof typeof optionCheckedCounts + if (value) { + if (type === 'REMOVE') optionCheckedCounts[_key]-- + else optionCheckedCounts[_key]++ + } else { + if (type === 'MODIFY') optionCheckedCounts[_key]-- + } + } + + // 更新整卷配置状态 + function updateGlobalBaseConf() { + const len = questionDetailList.value.length + const { isRequiredCount, showIndexCount, showSpliterCount, showTypeCount } = optionCheckedCounts + Object.assign(globalBaseConfig, { + isRequired: isRequiredCount === len, + showIndex: showIndexCount === len, + showSpliter: showSpliterCount === len, + showType: showTypeCount === len + }) + } + + // 整卷配置修改 + function updateGlobalConfOption({ key, value }: { key: string; value: boolean }) { + const len = questionDetailList.value.length + const _key = `${key}Count` as keyof typeof optionCheckedCounts + if (value) optionCheckedCounts[_key] = len + else optionCheckedCounts[_key] = 0 + questionDetailList.value.forEach((question: any) => { + question[key] = value + }) + updateTime() + } + + return { + globalBaseConfig, + initCounts, + updateCounts, + updateGlobalConfOption + } +} diff --git a/web/src/management/stores/edit.ts b/web/src/management/stores/edit.ts index a7c188c0..70c34514 100644 --- a/web/src/management/stores/edit.ts +++ b/web/src/management/stores/edit.ts @@ -19,6 +19,7 @@ import questionLoader from '@/materials/questions/questionLoader' import { SurveyPermissions } from '@/management/utils/types/workSpace' import { getBannerData } from '@/management/api/skin.js' import { getCollaboratorPermissions } from '@/management/api/space' +import useEditGlobalBaseConf, { type TypeMethod } from './composables/useEditGlobalBaseConf' import { CODE_MAP } from '../api/base' const innerMetaConfig = { @@ -28,7 +29,7 @@ const innerMetaConfig = { } } -function useInitializeSchema(surveyId: Ref) { +function useInitializeSchema(surveyId: Ref, initializeSchemaCallBack: () => void) { const schema = reactive({ metaData: null, bannerConf: { @@ -130,6 +131,7 @@ function useInitializeSchema(surveyId: Ref) { logicConf } }) + initializeSchemaCallBack() } else { throw new Error(res.errmsg || '问卷不存在') } @@ -142,11 +144,17 @@ function useInitializeSchema(surveyId: Ref) { } } -function useQuestionDataListOperations( - questionDataList: Ref, - updateTime: () => void, +function useQuestionDataListOperations({ + questionDataList, + updateTime, + pageOperations, + updateCounts +}: { + questionDataList: Ref + updateTime: () => void pageOperations: (type: string) => void -) { + updateCounts: (type: TypeMethod, data: any) => void +}) { function copyQuestion({ index }: { index: number }) { const newQuestion = _cloneDeep(questionDataList.value[index]) newQuestion.field = getNewField(questionDataList.value.map((item) => item.field)) @@ -157,12 +165,14 @@ function useQuestionDataListOperations( questionDataList.value.splice(index, 0, question) pageOperations('add') updateTime() + updateCounts('ADD', { question }) } function deleteQuestion({ index }: { index: number }) { pageOperations('remove') - questionDataList.value.splice(index, 1) + const [question] = questionDataList.value.splice(index, 1) updateTime() + updateCounts('REMOVE', { question }) } function moveQuestion({ index, range }: { index: number; range: number }) { @@ -432,9 +442,13 @@ export const useEditStore = defineStore('edit', () => { const bannerList: Ref = ref({}) const cooperPermissions = ref(Object.values(SurveyPermissions)) const schemaUpdateTime = ref(Date.now()) - const { schema, initSchema, getSchemaFromRemote } = useInitializeSchema(surveyId) + const { schema, initSchema, getSchemaFromRemote } = useInitializeSchema(surveyId, () => { + editGlobalBaseConf.initCounts() + }) const questionDataList = toRef(schema, 'questionDataList') + const editGlobalBaseConf = useEditGlobalBaseConf(questionDataList, updateTime) + function setQuestionDataList(data: any) { schema.questionDataList = data } @@ -497,9 +511,12 @@ export const useEditStore = defineStore('edit', () => { } = usePageEdit({ schema, questionDataList }, updateTime) const { copyQuestion, addQuestion, deleteQuestion, moveQuestion } = useQuestionDataListOperations( - questionDataList, - updateTime, - pageOperations + { + questionDataList, + updateTime, + pageOperations, + updateCounts: editGlobalBaseConf.updateCounts + } ) function moveQuestionDataList(data: any) { @@ -575,6 +592,7 @@ export const useEditStore = defineStore('edit', () => { } return { + editGlobalBaseConf, surveyId, setSurveyId, bannerList,