index.tsx 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { useRouter } from 'vue-router'
19
import { defineComponent, onMounted, ref, reactive, Ref } from 'vue'
20 21 22 23 24 25 26
import {
  NIcon,
  NSpace,
  NDataTable,
  NButtonGroup,
  NButton,
  NPagination,
27 28 29
  NInput,
  NBreadcrumb,
  NBreadcrumbItem
30
} from 'naive-ui'
31
import { useI18n } from 'vue-i18n'
32
import { SearchOutlined } from '@vicons/antd'
33 34 35 36 37 38
import Card from '@/components/card'
import { useTable } from './table/use-table'
import { useFileState } from './use-file'
import ResourceFolderModal from './folder'
import ResourceUploadModal from './upload'
import ResourceRenameModal from './rename'
39
import { BreadcrumbItem, IRenameFile } from './types'
40 41
import type { Router } from 'vue-router'
import styles from './index.module.scss'
42
import { useFileStore } from '@/store/file/file'
43 44 45 46
import {
  queryCurrentResourceById,
  queryResourceById
} from '@/service/modules/resources'
47
import { ResourceFile } from '@/service/modules/resources/types'
48 49 50 51 52 53 54 55 56 57 58

export default defineComponent({
  name: 'File',
  setup() {
    const router: Router = useRouter()
    const fileId = ref(Number(router.currentRoute.value.params.id) || -1)

    const resourceListRef = ref()
    const folderShowRef = ref(false)
    const uploadShowRef = ref(false)
    const renameShowRef = ref(false)
59
    const searchRef = ref()
60 61 62 63

    const renameInfo = reactive({
      id: -1,
      name: '',
64
      description: ''
65 66 67 68 69 70
    })

    const paginationReactive = reactive({
      page: 1,
      pageSize: 10,
      itemCount: 0,
71
      pageSizes: [10, 30, 50]
72 73 74 75 76 77
    })

    const handleUpdatePage = (page: number) => {
      paginationReactive.page = page
      resourceListRef.value = getResourceListState(
        fileId.value,
78
        searchRef.value,
79
        paginationReactive.page,
80
        paginationReactive.pageSize
81 82 83 84 85 86 87 88
      )
    }

    const handleUpdatePageSize = (pageSize: number) => {
      paginationReactive.page = 1
      paginationReactive.pageSize = pageSize
      resourceListRef.value = getResourceListState(
        fileId.value,
89
        searchRef.value,
90
        paginationReactive.page,
91
        paginationReactive.pageSize
92 93 94 95 96 97 98 99 100 101 102 103 104
      )
    }

    const handleShowModal = (showRef: Ref<Boolean>) => {
      showRef.value = true
    }

    const setPagination = (count: number) => {
      paginationReactive.itemCount = count
    }

    const { getResourceListState } = useFileState(setPagination)

105 106 107
    const handleConditions = () => {
      resourceListRef.value = getResourceListState(
        fileId.value,
108
        searchRef.value
109
      )
110 111 112 113 114 115 116 117 118 119 120 121
    }

    const handleCreateFolder = () => {
      handleShowModal(folderShowRef)
    }

    const handleCreateFile = () => {
      const name = fileId.value
        ? 'resource-subfile-create'
        : 'resource-file-create'
      router.push({
        name,
122
        params: { id: fileId.value }
123 124 125 126 127 128 129 130 131 132 133 134 135 136
      })
    }

    const handleUploadFile = () => {
      handleShowModal(uploadShowRef)
    }

    const handleRenameFile: IRenameFile = (id, name, description) => {
      renameInfo.id = id
      renameInfo.name = name
      renameInfo.description = description
      handleShowModal(renameShowRef)
    }

137 138 139 140 141 142
    const handleGoRoot = () => {
      router.push({
        name: 'file-manage'
      })
    }

143 144 145
    const updateList = () => {
      resourceListRef.value = getResourceListState(
        fileId.value,
146
        searchRef.value
147 148
      )
    }
149
    const fileStore = useFileStore()
150 151 152 153 154

    onMounted(() => {
      resourceListRef.value = getResourceListState(fileId.value)
    })

155 156 157 158 159 160 161 162 163 164 165 166 167 168
    const breadcrumbItemsRef: Ref<Array<BreadcrumbItem> | undefined> = ref([
      {
        id: 1,
        fullName: 'l1'
      },
      {
        id: 2,
        fullName: 'l2'
      },
      {
        id: 4,
        fullName: 'l3'
      }
    ])
169

170 171 172 173 174 175 176 177 178 179
    onMounted(() => {
      const currFileId = Number(router.currentRoute.value.params.id) || -1
      if (currFileId === -1) {
        fileStore.setCurrentDir('/')
      } else {
        queryCurrentResourceById(currFileId).then((res: ResourceFile) => {
          if (res.fullName) {
            fileStore.setCurrentDir(res.fullName)
          }
        })
180
      }
181
    })
182

183 184
    const initBreadcrumb = async (dirs: string[]) => {
      let index = 0
185
      for (const dir of dirs) {
186 187 188
        const newDir = dirs.slice(0, index + 1).join('/')
        if (newDir) {
          const id = 0
189
          const resource = await queryResourceById(
190 191 192 193 194 195
            {
              id,
              type: 'FILE',
              fullName: newDir
            },
            id
196
          )
197
          breadcrumbItemsRef.value?.push({ id: resource.id, fullName: dir })
198
        } else {
199
          breadcrumbItemsRef.value?.push({ id: 0, fullName: 'Root' })
200 201 202 203 204
        }
        index = index + 1
      }
    }

205 206 207 208 209 210 211 212 213
    onMounted(() => {
      breadcrumbItemsRef.value = []
      if (fileId.value != -1) {
        queryCurrentResourceById(fileId.value).then((res: ResourceFile) => {
          if (res.fullName) {
            const dirs = res.fullName.split('/')
            if (dirs && dirs.length > 1) {
              initBreadcrumb(dirs)
            }
214
          }
215 216 217
        })
      }
    })
218

219 220
    return {
      fileId,
221
      searchRef,
222 223 224 225 226 227 228 229 230 231 232 233 234
      folderShowRef,
      uploadShowRef,
      renameShowRef,
      handleShowModal,
      resourceListRef,
      updateList,
      handleConditions,
      handleCreateFolder,
      handleCreateFile,
      handleUploadFile,
      handleRenameFile,
      handleUpdatePage,
      handleUpdatePageSize,
235
      handleGoRoot,
236
      pagination: paginationReactive,
237
      renameInfo,
238
      breadcrumbItemsRef
239 240 241 242
    }
  },
  render() {
    const { t } = useI18n()
243 244 245 246
    const { columnsRef, tableWidth } = useTable(
      this.handleRenameFile,
      this.updateList
    )
247 248 249 250
    const {
      handleConditions,
      handleCreateFolder,
      handleCreateFile,
251
      handleUploadFile
252
    } = this
253

254 255
    return (
      <div>
256 257 258 259
        <Card style={{ marginBottom: '8px' }}>
          <div class={styles['conditions-model']}>
            <NSpace>
              <NButtonGroup>
260 261 262 263
                <NButton
                  onClick={handleCreateFolder}
                  class='btn-create-directory'
                >
264 265
                  {t('resource.file.create_folder')}
                </NButton>
266
                <NButton onClick={handleCreateFile} class='btn-create-file'>
267 268
                  {t('resource.file.create_file')}
                </NButton>
269
                <NButton onClick={handleUploadFile} class='btn-upload-file'>
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
                  {t('resource.file.upload_files')}
                </NButton>
              </NButtonGroup>
            </NSpace>
            <div class={styles.right}>
              <div class={styles['form-box']}>
                <div class={styles.list}>
                  <NButton onClick={handleConditions}>
                    <NIcon>
                      <SearchOutlined />
                    </NIcon>
                  </NButton>
                </div>
                <div class={styles.list}>
                  <NInput
                    placeholder={t('resource.file.enter_keyword_tips')}
286
                    v-model={[this.searchRef, 'value']}
287 288 289 290 291 292
                  />
                </div>
              </div>
            </div>
          </div>
        </Card>
293 294 295 296
        <Card
          title={t('resource.file.file_manage')}
          class={styles['table-card']}
        >
297 298
          {{
            'header-extra': () => (
299 300
              <NBreadcrumb separator='>' class={styles['breadcrumb']}>
                {this.breadcrumbItemsRef?.map((item: BreadcrumbItem) => {
301 302 303 304 305 306 307 308 309 310 311 312 313
                  if (item.id === 0) {
                    return (
                      <NBreadcrumbItem>
                        <span onClick={this.handleGoRoot}>{item.fullName}</span>
                      </NBreadcrumbItem>
                    )
                  } else {
                    return (
                      <NBreadcrumbItem href={item.id.toString()}>
                        {item.fullName}
                      </NBreadcrumbItem>
                    )
                  }
314 315
                })}
              </NBreadcrumb>
316 317
            ),
            default: () => (
318 319 320 321 322 323 324 325 326
              <div>
                <NDataTable
                  remote
                  columns={columnsRef}
                  data={this.resourceListRef?.value.table}
                  striped
                  size={'small'}
                  class={styles['table-box']}
                  row-class-name='items'
327
                  scrollX={tableWidth}
328 329 330
                />
                <div class={styles.pagination}>
                  <NPagination
331 332 333 334 335 336 337 338
                    v-model:page={this.pagination.page}
                    v-model:pageSize={this.pagination.pageSize}
                    pageSizes={this.pagination.pageSizes}
                    item-count={this.pagination.itemCount}
                    onUpdatePage={this.handleUpdatePage}
                    onUpdatePageSize={this.handleUpdatePageSize}
                    show-quick-jumper
                    show-size-picker
339
                  />
340
                </div>
341
              </div>
342 343 344 345
            )
          }}
        </Card>
        <ResourceFolderModal
346 347
          v-model:show={this.folderShowRef}
          onUpdateList={this.updateList}
348 349
        />
        <ResourceUploadModal
350 351
          v-model:show={this.uploadShowRef}
          onUpdateList={this.updateList}
352 353
        />
        <ResourceRenameModal
354 355 356 357 358
          v-model:show={this.renameShowRef}
          id={this.renameInfo.id}
          name={this.renameInfo.name}
          description={this.renameInfo.description}
          onUpdateList={this.updateList}
359
        />
360 361
      </div>
    )
362
  }
363
})