FormWorkspace.vue 4.7 KB
Newer Older
Z
zhaoke 已提交
1 2 3 4 5
<template>
  <ZModal
    :showModal="showModalRef"
    @onCancel="cancel"
    @onOk="submit"
Z
zhaoke 已提交
6
    :title="t('create_workspace')"
Z
zhaoke 已提交
7
    :contentStyle="{width: '600px'}"
Z
zhaoke 已提交
8
  >
Z
zhaoke 已提交
9
    <Form>
Z
zhaoke 已提交
10
      <FormItem name="name" :label="t('name')" :info="validateInfos.name">
H
Hao Sun 已提交
11
        <input type="text" v-model="modelRef.name" />
Z
zhaoke 已提交
12
      </FormItem>
Z
zhaoke 已提交
13
      <FormItem name="path" :label="t('path')" :info="validateInfos.path">
H
Hao Sun 已提交
14
        <input type="text" v-if="isElectron" v-model="modelRef.path" />
H
Hao Sun 已提交
15
        <Button  v-if="isElectron" @click="selectDir" class="state secondary flex-none rounded">{{t('select')}}</Button>
H
Hao Sun 已提交
16
        <input type="text" v-if="!isElectron" v-model="modelRef.path" />
Z
zhaoke 已提交
17 18
      </FormItem>
      <FormItem name="type" :label="t('type')" :info="validateInfos.type">
H
Hao Sun 已提交
19 20 21 22 23 24 25 26 27 28 29
        <div class="select">
          <select name="type" @change="selectType" v-model="modelRef.type">
            <option
              v-for="item in testTypes"
              :key="item.value"
              :value="item.value"
            >
              {{ item.label }}
            </option>
          </select>
        </div>
Z
zhaoke 已提交
30 31
      </FormItem>
      <FormItem
Z
zhaoke 已提交
32
        v-if="modelRef.type === 'ztf'"
Z
zhaoke 已提交
33 34 35 36
        name="lang"
        :label="t('default_lang')"
        :info="validateInfos.lang"
      >
H
Hao Sun 已提交
37 38 39 40 41 42 43
        <div class="select">
          <select name="type" v-model="modelRef.lang">
            <option v-for="item in langs" :key="item.code" :value="item.code">
              {{ item.name }}
            </option>
          </select>
        </div>
Z
zhaoke 已提交
44
      </FormItem>
H
Hao Sun 已提交
45
      <FormItem v-if="showCmd" name="cmd" :label="t('cmd')" :info="validateInfos.cmd" :helpText="t('tips_test_cmd', {cmd: cmdSample})">
Z
zhaoke 已提交
46 47
        <textarea v-model="modelRef.cmd" />
      </FormItem>
Z
zhaoke 已提交
48 49 50 51 52 53 54 55
    </Form>
  </ZModal>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { useStore } from "vuex";
import { ZentaoData } from "@/store/zentao";
aaronchen2k2k's avatar
aaronchen2k2k 已提交
56

Z
zhaoke 已提交
57 58 59 60 61 62 63 64 65
import { unitTestTypesDef, ztfTestTypesDef } from "@/utils/const";
import {
  computed,
  defineExpose,
  onMounted,
  withDefaults,
  ref,
  defineProps,
  defineEmits,
Z
zhaoke 已提交
66
  watch,
Z
zhaoke 已提交
67 68
} from "vue";
import { useForm } from "@/utils/form";
aaronchen2k2k's avatar
aaronchen2k2k 已提交
69 70
import Form from "@/components/Form.vue";
import FormItem from "@/components/FormItem.vue";
Z
zhaoke 已提交
71
import {arrToMap} from "@/utils/array";
Z
zhaoke 已提交
72
import settings from "@/config/settings";
aaronchen2k2k's avatar
aaronchen2k2k 已提交
73
import Button from "@/components/Button.vue";
Z
zhaoke 已提交
74 75 76 77 78 79 80 81 82

export interface FormWorkspaceProps {
  show?: boolean;
}
const { t } = useI18n();
const props = withDefaults(defineProps<FormWorkspaceProps>(), {
  show: false,
});

Z
zhaoke 已提交
83 84 85 86 87 88 89 90
watch(props, () => {
    if(!props.show){
        setTimeout(() => {
            validateInfos.value = {};
        }, 200);
    }
})

Z
zhaoke 已提交
91 92 93
const showModalRef = computed(() => {
  return props.show;
});
Z
zhaoke 已提交
94

Z
zhaoke 已提交
95
const testTypes = ref([...ztfTestTypesDef, ...unitTestTypesDef]);
aaronchen2k2k's avatar
aaronchen2k2k 已提交
96 97
const store = useStore<{ Zentao: ZentaoData }>();
const langs = computed<any[]>(() => store.state.Zentao.langs);
Z
zhaoke 已提交
98 99 100 101 102 103 104 105
const cmdSample = ref('')
const cmdMap = ref(arrToMap(testTypes.value))
const selectType = () => {
    console.log('selectType')

    if (modelRef.value.type !== 'ztf') {
        cmdSample.value = cmdMap.value[modelRef.value.type].cmd
        modelRef.value.cmd = cmdSample.value.split('product_id')[1].trim()
106
        rulesRef.value.lang = [{ required: false, msg: t("pls_script_lang") }]
Z
zhaoke 已提交
107
    }else{
108
        rulesRef.value.lang = [{ required: true, msg: t("pls_script_lang") }]
Z
zhaoke 已提交
109 110
    }
}
Z
zhaoke 已提交
111 112 113 114 115

const cancel = () => {
  emit("cancel", {});
};

Z
zhaoke 已提交
116
const isElectron = ref(!!window.require)
117
const modelRef = ref({type: 'ztf'} as any);
aaronchen2k2k's avatar
aaronchen2k2k 已提交
118 119

const showCmd = computed(() => { return modelRef.value.type && modelRef.value.type !== 'ztf' })
Z
zhaoke 已提交
120 121 122
const rulesRef = ref({
  name: [{ required: true, msg: t("pls_name") }],
  path: [{ required: true, msg: t("pls_workspace_path") }],
123
  lang: [{ required: true, msg: t("pls_script_lang") }],
Z
zhaoke 已提交
124
  type: [{ required: true, msg: t("pls_workspace_type") }],
Z
zhaoke 已提交
125 126 127 128 129 130 131 132 133 134
  cmd: [
        {
          trigger: 'blur',
          validator: async (rule: any, value: string) => {
            if (modelRef.value.type !== 'ztf' && (value === '' || !value)) {
              throw new Error(t('pls_cmd'));
            }
          }
        },
      ],
Z
zhaoke 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
});

const { validate, reset, validateInfos } = useForm(modelRef, rulesRef);

const emit = defineEmits<{
  (type: "submit", event: {}): void;
  (type: "cancel", event: {}): void;
}>();

const submit = () => {
  if (validate()) {
    emit("submit", modelRef.value);
  }
};

const clearFormData = () => {
Z
zhaoke 已提交
151
  modelRef.value = {type: 'ztf'};
Z
zhaoke 已提交
152 153
};

154 155 156 157 158 159 160 161 162 163 164 165
const selectDir = () => {
    console.log('selectDir')

    const { ipcRenderer } = window.require('electron')
    ipcRenderer.send(settings.electronMsg, 'selectDir')

    ipcRenderer.on(settings.electronMsgReplay, (event, arg) => {
    console.log(arg)
    modelRef.value.path = arg
    })
}

Z
zhaoke 已提交
166 167 168 169
defineExpose({
  clearFormData,
});
</script>