WorkDir.vue 11.0 KB
Newer Older
H
Hao Sun 已提交
1
<template>
aaronchen2k2k's avatar
aaronchen2k2k 已提交
2
  <div class="workdir">
Z
zhaoke 已提交
3 4 5 6 7 8 9 10 11
    <Tree 
    :data="treeData" 
    :checkable="checkable"
    ref="treeRef" 
    @active="selectNode" 
    @check="checkNode"
    @clickToolbar="onToolbarClicked" 
    @collapse="expandNode" 
    :defaultCollapsedMap="collapsedMap"
Z
zhaoke 已提交
12
    :defaultCollapsed="true"
Z
zhaoke 已提交
13
    />
14
    <FormNode :show="showModal" @submit="createNode" @cancel="modalClose" ref="formNode" />
aaronchen2k2k's avatar
aaronchen2k2k 已提交
15
  </div>
H
Hao Sun 已提交
16
</template>
aaronchen2k2k's avatar
aaronchen2k2k 已提交
17 18

<script setup lang="ts">
19 20
import { useI18n } from "vue-i18n";
import { useStore } from "vuex";
aaronchen2k2k's avatar
aaronchen2k2k 已提交
21
import { StateType as GlobalData } from "@/store/global";
22 23 24
import { ZentaoData } from "@/store/zentao";
import { ScriptData } from "@/views/script/store";
import { WorkspaceData } from "@/store/workspace";
aaronchen2k2k's avatar
aaronchen2k2k 已提交
25 26
import { resizeWidth } from "@/utils/dom";
import Tree from "@/components/Tree.vue";
R
root 已提交
27
import notification from "@/utils/notification";
Z
zhaoke 已提交
28
import { computed, defineExpose, onMounted, onUnmounted, ref, watch } from "vue";
Z
zhaoke 已提交
29 30 31

import bus from "@/utils/eventBus";
import {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
32 33 34 35
  getExpandedKeys,
  getScriptDisplayBy,
  getScriptFilters,
  setExpandedKeys,
Z
zhaoke 已提交
36 37
} from "@/utils/cache";
import {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
38 39 40
  getCaseIdsFromReport,
  getNodeMap,
  listFilterItems,
Z
zhaoke 已提交
41
} from "@/views/script/service";
42 43
import { useRouter } from "vue-router";
import { isWindows } from "@/utils/comm";
Z
zhaoke 已提交
44 45
import debounce from "lodash.debounce";
import throttle from "lodash.debounce";
Z
zhaoke 已提交
46
import Modal from "@/utils/modal"
Z
zhaoke 已提交
47
import FormNode from "./FormNode.vue";
Z
zhaoke 已提交
48
import { key } from "localforage";
Z
zhaoke 已提交
49

50
const { t } = useI18n();
aaronchen2k2k's avatar
aaronchen2k2k 已提交
51

aaronchen2k2k's avatar
aaronchen2k2k 已提交
52 53
const store = useStore<{ global: GlobalData, Zentao: ZentaoData, Script: ScriptData, Workspace: WorkspaceData }>();
const global = computed<any>(() => store.state.global.tabIdToWorkspaceIdMap);
aaronchen2k2k's avatar
aaronchen2k2k 已提交
54 55
const currSite = computed<any>(() => store.state.Zentao.currSite);
const currProduct = computed<any>(() => store.state.Zentao.currProduct);
aaronchen2k2k's avatar
aaronchen2k2k 已提交
56

aaronchen2k2k's avatar
aaronchen2k2k 已提交
57
const currWorkspace = computed<any>(() => store.state.Script.currWorkspace);
aaronchen2k2k's avatar
aaronchen2k2k 已提交
58

Z
zhaoke 已提交
59 60
const isWin = isWindows()

aaronchen2k2k's avatar
aaronchen2k2k 已提交
61 62
store.dispatch('Zentao/fetchLangs')
const langs = computed<any[]>(() => store.state.Zentao.langs);
Z
zhaoke 已提交
63

Z
zhaoke 已提交
64 65 66 67 68 69 70 71 72 73
const router = useRouter();
let workspace = router.currentRoute.value.params.workspace as string
workspace = workspace === '-' ? '' : workspace
let seq = router.currentRoute.value.params.seq as string
seq = seq === '-' ? '' : seq
let scope = router.currentRoute.value.params.scope as string
scope = scope === '-' ? '' : scope

const filerType = ref('')
const filerValue = ref('')
Z
zhaoke 已提交
74
const showModal = ref(false)
75
const toolbarAction = ref('')
aaronchen2k2k's avatar
aaronchen2k2k 已提交
76
const currentNode = ref({} as any) // parent node for create node
Z
zhaoke 已提交
77
const collapsedMap = ref({} as any)
Z
zhaoke 已提交
78

aaronchen2k2k's avatar
aaronchen2k2k 已提交
79
onMounted(() => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
80 81 82 83 84
  console.log('onMounted')
  initData();
  setTimeout(() => {
    resizeWidth('main', 'left', 'splitter-h', 'right', 380, 800)
  }, 600)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
85 86
})

aaronchen2k2k's avatar
aaronchen2k2k 已提交
87
const onToolbarClicked = (e) => {
Z
zhaoke 已提交
88
  const node = e.node == undefined ? treeDataMap.value[''] : treeDataMap.value[e.node.id]
aaronchen2k2k's avatar
aaronchen2k2k 已提交
89
  store.dispatch('Script/changeWorkspace',
90
    { id: node.workspaceId, type: node.workspaceType })
aaronchen2k2k's avatar
aaronchen2k2k 已提交
91

aaronchen2k2k's avatar
aaronchen2k2k 已提交
92 93 94
  currentNode.value = node;
  if (e.event.key == 'runTest') {
    runTest(currentNode);
95
  } else if (e.event.key == 'createFile' || e.event.key == 'createWorkspace' || e.event.key == 'createDir') {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
96
    showModal.value = true;
97
    toolbarAction.value = e.event.key;
98 99 100 101 102 103 104 105
  } else if (e.event.key === 'deleteWorkspace') {
    Modal.confirm({
      title: t('delete'),
      content: t('confirm_to_delete_workspace', { p: node.title }),
      showOkBtn: true
    },
      {
        "onOk": () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
106
          store.dispatch('Workspace/removeWorkspace', node.path)
107 108 109 110 111 112 113 114 115
            .then((response) => {
              if (response) {
                notification.success({ message: t('delete_success') });
                loadScripts()
              }
            })
        }
      }
    )
aaronchen2k2k's avatar
aaronchen2k2k 已提交
116
  }
Z
zhaoke 已提交
117 118 119
}

const runTest = (node) => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
120 121 122 123 124 125 126 127 128 129 130 131
  console.log('runTest', node.value)

  store.dispatch('tabs/open', {
    id: 'workspace-' + node.value.workspaceId,
    title: node.value.title,
    type: 'execUnit',
    changed: false,
    data: {
      workspaceId: node.value.workspaceId,
      workspaceType: node.value.workspaceType,
    }
  });
Z
zhaoke 已提交
132
}
Z
zhaoke 已提交
133

Z
zhaoke 已提交
134
const modalClose = () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
135
  showModal.value = false;
Z
zhaoke 已提交
136 137 138 139
}

const treeRef = ref<{ isAllCollapsed: () => boolean, toggleAllCollapsed: () => void }>();

aaronchen2k2k's avatar
aaronchen2k2k 已提交
140
let treeData = computed<any>(() => store.state.Script.list);
H
Hao Sun 已提交
141 142 143 144

const checkable = ref(false);

function toggleCheckable(toggle?: boolean) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
145 146 147 148
  if (toggle === undefined) {
    toggle = !checkable.value;
  }
  checkable.value = toggle;
H
Hao Sun 已提交
149 150
}

Z
zhaoke 已提交
151 152

const selectCasesFromReport = async (): Promise<void> => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
153
  if (!seq) return
Z
zhaoke 已提交
154

aaronchen2k2k's avatar
aaronchen2k2k 已提交
155 156 157 158
  getCaseIdsFromReport(workspace, seq, scope).then((json) => {
    checkedKeys.value = json.data
    router.push(`/script/index`) // remove the params of re-test
  })
Z
zhaoke 已提交
159 160 161 162
}
selectCasesFromReport()

watch(currProduct, () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
163 164
  console.log('watch currProduct', currProduct.value.id)
  initData()
165
}, { deep: true })
Z
zhaoke 已提交
166 167

watch(treeData, (currConfig) => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
168 169
  console.log('watch treeData', treeData.value)
  onTreeDataChanged()
170
}, { deep: true })
Z
zhaoke 已提交
171 172 173 174

let filerItems = ref([] as any)

const loadScripts = async () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
175 176
  console.log(`loadScripts should be executed only once`)
  console.log(`filerType: ${filerType.value}, filerValue: ${filerValue.value}`)
Z
zhaoke 已提交
177

178
  const params = { displayBy: displayBy.value, filerType: filerType.value, filerValue: filerValue.value } as any
aaronchen2k2k's avatar
aaronchen2k2k 已提交
179
  store.dispatch('Script/listScript', params)
Z
zhaoke 已提交
180 181 182
}

const onTreeDataChanged = async () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
183
  getNodeMapCall()
Z
zhaoke 已提交
184

aaronchen2k2k's avatar
aaronchen2k2k 已提交
185 186
  getExpandedKeys(currSite.value.id, currProduct.value.id).then(async cachedKeys => {
    console.log('cachedKeys', currSite.value.id, currProduct.value.id)
Z
zhaoke 已提交
187

aaronchen2k2k's avatar
aaronchen2k2k 已提交
188
    if (cachedKeys) expandedKeys.value = cachedKeys
Z
zhaoke 已提交
189

aaronchen2k2k's avatar
aaronchen2k2k 已提交
190
    if (!cachedKeys || cachedKeys.length === 0) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
191
      // 修改
192 193
      // getOpenKeys(treeData.value[0], false) // expend first level folder
      // await setExpandedKeys(currSite.value.id, currProduct.value.id, expandedKeys.value)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
194 195
    }
  })
Z
zhaoke 已提交
196 197 198 199
}

// display
const loadDisplayBy = async () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
200
  displayBy.value = await getScriptDisplayBy(currSite.value.id, currProduct.value.id)
Z
zhaoke 已提交
201 202 203 204
}

// filters
const loadFilterItems = async () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  const data = await getScriptFilters(displayBy.value, currSite.value.id, currProduct.value.id)

  if (!filerType.value) {
    filerType.value = data.by
  }
  filerValue.value = data.val

  if (!currProduct.value.id && filerType.value !== 'workspace') {
    filerType.value = 'workspace'
    filerValue.value = ''
  }

  if (filerType.value) {
    const result = await listFilterItems(filerType.value)
    filerItems.value = result.data

    let found = false
    if (filerItems.value) {
      filerItems.value.forEach((item) => {
        // console.log(`${filerValue.value}, ${item.value}`)
        if (filerValue.value === item.value) found = true
      })
Z
zhaoke 已提交
227 228
    }

aaronchen2k2k's avatar
aaronchen2k2k 已提交
229 230
    if (!found) filerValue.value = ''
  }
Z
zhaoke 已提交
231 232 233
}

const initData = debounce(async () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
234 235
  console.log('init')
  if (!currSite.value.id) return
Z
zhaoke 已提交
236

aaronchen2k2k's avatar
aaronchen2k2k 已提交
237 238 239
  await loadDisplayBy()
  await loadFilterItems()
  await loadScripts()
Z
zhaoke 已提交
240 241 242 243 244 245 246
}, 50)

// only do it when switch from another pages, otherwise will called by watching currProduct method.
if (filerValue.value.length === 0) initData()

const expandedKeys = ref<string[]>([]);
const getOpenKeys = (treeNode: any, openAll: boolean) => { // expand top one level if openAll is false
aaronchen2k2k's avatar
aaronchen2k2k 已提交
247 248
  if (!treeNode) return
  expandedKeys.value.push(treeNode.path)
Z
zhaoke 已提交
249

aaronchen2k2k's avatar
aaronchen2k2k 已提交
250 251 252 253 254
  if (treeNode.children && openAll) {
    treeNode.children.forEach((item, index) => {
      getOpenKeys(item, openAll)
    })
  }
Z
zhaoke 已提交
255

aaronchen2k2k's avatar
aaronchen2k2k 已提交
256
  console.log('keys', expandedKeys.value)
Z
zhaoke 已提交
257 258
}

Z
zhaoke 已提交
259 260 261 262 263 264 265
watch(expandedKeys, () => {
    console.log('watch expandedKeys')
    for (let treeDataKey in treeDataMap.value) {
        collapsedMap.value[treeDataKey] = expandedKeys.value.indexOf(treeDataKey) !== -1 ? false : true
    }
}, { deep: true })

Z
zhaoke 已提交
266 267 268 269 270 271 272 273 274 275
const selectedKeys = ref<string[]>([])
const checkedKeys = ref<string[]>([])

let isExpand = ref(false);
let showCheckbox = ref(false)
let displayBy = ref('workspace')

let tree = ref(null)

onMounted(() => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
276
  console.log('onMounted', tree)
Z
zhaoke 已提交
277 278
})
onUnmounted(() => {
279
  console.log('onUnmounted', tree)
Z
zhaoke 已提交
280 281 282
})

const selectNode = (activeNode) => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
283
  console.log('selectNode', activeNode.activeID, global.value)
Z
zhaoke 已提交
284

Z
zhaoke 已提交
285
  const node = treeDataMap.value[activeNode.activeID]
aaronchen2k2k's avatar
aaronchen2k2k 已提交
286
  if (node.workspaceType !== 'ztf') checkNothing()
Z
zhaoke 已提交
287

aaronchen2k2k's avatar
aaronchen2k2k 已提交
288
  store.dispatch('Script/getScript', node)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
289
  if (node.type === 'file') {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
290 291
    const tabId = node.workspaceType === 'ztf' && node.path.indexOf('.exp') !== node.path.length - 4
        ? 'script-' + node.path : 'code-' + node.path
aaronchen2k2k's avatar
aaronchen2k2k 已提交
292 293
    global.value[tabId] = node.workspaceId

aaronchen2k2k's avatar
aaronchen2k2k 已提交
294
    store.dispatch('tabs/open', {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
295
      id: tabId,
aaronchen2k2k's avatar
aaronchen2k2k 已提交
296 297 298 299 300 301 302
      title: node.title,
      changed: false,
      type: 'script',
      data: node.path
    });
  }

aaronchen2k2k's avatar
aaronchen2k2k 已提交
303
  store.dispatch('Script/changeWorkspace',
304
    { id: node.workspaceId, type: node.workspaceType })
Z
zhaoke 已提交
305 306
}

R
root 已提交
307 308
const checkNode = (checkedKeys) => {
  console.log('checkNode', checkedKeys.checked)
Z
zhaoke 已提交
309
  store.dispatch('Script/setCheckedNodes', checkedKeys.checked)
310 311
  //   scriptStore.dispatch('Script/changeWorkspace',
  //       {id: e.node.dataRef.workspaceId, type: e.node.dataRef.workspaceType})
Z
zhaoke 已提交
312 313 314
}

const checkNothing = () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
315
  checkedKeys.value = []
Z
zhaoke 已提交
316 317 318 319 320 321 322
}

let contextNode = ref({} as any)
let menuStyle = ref({} as any)
const editedData = ref<any>({})
const nameFormVisible = ref(false)

Z
zhaoke 已提交
323
const treeDataMap = computed<any>(() => store.state.Script.treeDataMap);
Z
zhaoke 已提交
324
const getNodeMapCall = throttle(async () => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
325
  treeData.value.forEach(item => {
Z
zhaoke 已提交
326
    getNodeMap(item, treeDataMap.value)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
327
  })
Z
zhaoke 已提交
328 329 330 331 332
}, 300)

let rightClickedNode = {} as any
let createAct = ''

Z
zhaoke 已提交
333
const formNode = ref({} as any)
Z
zhaoke 已提交
334
const createNode = (formData) => {
335 336
  const mode = 'child';
  let type = 'dir';
337
  if(toolbarAction.value === 'createFile') type = 'node'
aaronchen2k2k's avatar
aaronchen2k2k 已提交
338
  store.dispatch('Script/createScript', {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    name: formData.name, mode: mode, type: type, target: currentNode.value.path,
    workspaceId: currentNode.value.workspaceId, productId: currProduct.value.id,
  }).then((result) => {
    if (result) {
      formNode.value.clearFormData()
      showModal.value = false;
      notification.success({ message: t('create_success') });
      nameFormVisible.value = false

      if (mode == 'child') {
        expandedKeys.value.push(rightClickedNode.path)
      }
      if (type === 'dir') {
        expandedKeys.value.push(result)
      }
      setExpandedKeys(currSite.value.id, currProduct.value.id, expandedKeys.value)
    } else {
      notification.error({ message: t('create_fail') });
aaronchen2k2k's avatar
aaronchen2k2k 已提交
357
    }
358
  })
Z
zhaoke 已提交
359 360
}

Z
zhaoke 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
const expandNode = (expandedKeysMap) => {
    console.log('expandNode', expandedKeysMap.collapsed)
    for(var key in expandedKeysMap.collapsed){
        if(expandedKeysMap.collapsed[key]){
            expandedKeys.value.forEach((item, index) => {
                if(item === key){
                    expandedKeys.value.splice(index, 1)
                }
            })
        }else{
            expandedKeys.value.push(key)
        }
    }
    setExpandedKeys(currSite.value.id, currProduct.value.id, expandedKeys.value)
}

H
Hao Sun 已提交
377
defineExpose({
aaronchen2k2k's avatar
aaronchen2k2k 已提交
378 379 380 381 382 383 384 385 386 387
  get isCheckable() {
    return checkable.value;
  },
  get isAllCollapsed() {
    return treeRef.value?.isAllCollapsed();
  },
  toggleAllCollapsed() {
    return treeRef.value?.toggleAllCollapsed();
  },
  toggleCheckable,
388 389
  onToolbarClicked,
  loadScripts
H
Hao Sun 已提交
390
});
aaronchen2k2k's avatar
aaronchen2k2k 已提交
391 392 393 394
</script>

<style lang="less" scoped>
.workdir {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
395
  height: calc(100vh - 80px);
aaronchen2k2k's avatar
aaronchen2k2k 已提交
396 397
}
</style>