index.tsx 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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'
import {
  defineComponent,
  onMounted,
  ref,
  reactive,
  Ref,
  watch,
26
  inject
27
} from 'vue'
28 29 30 31 32 33 34
import {
  NIcon,
  NSpace,
  NDataTable,
  NButtonGroup,
  NButton,
  NPagination,
35 36 37
  NInput,
  NBreadcrumb,
  NBreadcrumbItem
38
} from 'naive-ui'
39
import { useI18n } from 'vue-i18n'
40
import { SearchOutlined } from '@vicons/antd'
41 42 43 44 45 46
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'
47
import { BreadcrumbItem, IRenameFile } from './types'
48 49
import type { Router } from 'vue-router'
import styles from './index.module.scss'
50
import { useFileStore } from '@/store/file/file'
51 52 53 54
import {
  queryCurrentResourceById,
  queryResourceById
} from '@/service/modules/resources'
55
import { ResourceFile } from '@/service/modules/resources/types'
56 57 58 59 60 61 62 63

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

64
    const reload: any = inject('reload')
65 66 67 68 69 70 71 72 73
    const resourceListRef = ref()
    const folderShowRef = ref(false)
    const uploadShowRef = ref(false)
    const renameShowRef = ref(false)
    const serachRef = ref()

    const renameInfo = reactive({
      id: -1,
      name: '',
74
      description: ''
75 76 77 78 79 80
    })

    const paginationReactive = reactive({
      page: 1,
      pageSize: 10,
      itemCount: 0,
81
      pageSizes: [10, 30, 50]
82 83 84 85 86 87 88 89
    })

    const handleUpdatePage = (page: number) => {
      paginationReactive.page = page
      resourceListRef.value = getResourceListState(
        fileId.value,
        serachRef.value,
        paginationReactive.page,
90
        paginationReactive.pageSize
91 92 93 94 95 96 97 98 99 100
      )
    }

    const handleUpdatePageSize = (pageSize: number) => {
      paginationReactive.page = 1
      paginationReactive.pageSize = pageSize
      resourceListRef.value = getResourceListState(
        fileId.value,
        serachRef.value,
        paginationReactive.page,
101
        paginationReactive.pageSize
102 103 104 105 106 107 108 109 110 111 112 113 114
      )
    }

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

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

    const { getResourceListState } = useFileState(setPagination)

115 116 117
    const handleConditions = () => {
      resourceListRef.value = getResourceListState(
        fileId.value,
118
        serachRef.value
119
      )
120 121 122 123 124 125 126 127 128 129 130 131
    }

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

    const handleCreateFile = () => {
      const name = fileId.value
        ? 'resource-subfile-create'
        : 'resource-file-create'
      router.push({
        name,
132
        params: { id: fileId.value }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      })
    }

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

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

    const updateList = () => {
      resourceListRef.value = getResourceListState(
        fileId.value,
150
        serachRef.value
151 152
      )
    }
153
    const fileStore = useFileStore()
154 155 156 157 158

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

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

174 175
    watch(
      () => router.currentRoute.value.params.id,
176
      // @ts-ignore
177 178 179 180 181 182 183 184 185 186 187 188 189 190
      () => {
        reload()
        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)
            }
          })
        }
      }
191 192
    )

193 194 195 196 197 198 199
    const initBreadcrumb = async (dirs: string[]) => {
      let index = 0
      for (let dir of dirs) {
        const newDir = dirs.slice(0, index + 1).join('/')
        if (newDir) {
          const id = 0
          let resource = await queryResourceById(
200 201 202 203 204 205
            {
              id,
              type: 'FILE',
              fullName: newDir
            },
            id
206
          )
207
          breadcrumbItemsRef.value?.push({ id: resource.id, fullName: dir })
208
        } else {
209
          breadcrumbItemsRef.value?.push({ id: 0, fullName: 'Root' })
210 211 212 213 214
        }
        index = index + 1
      }
    }

215 216 217 218 219 220 221 222 223
    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)
            }
224
          }
225 226 227
        })
      }
    })
228

229 230
    return {
      fileId,
231
      serachRef,
232 233 234 235 236 237 238 239 240 241 242 243 244 245
      folderShowRef,
      uploadShowRef,
      renameShowRef,
      handleShowModal,
      resourceListRef,
      updateList,
      handleConditions,
      handleCreateFolder,
      handleCreateFile,
      handleUploadFile,
      handleRenameFile,
      handleUpdatePage,
      handleUpdatePageSize,
      pagination: paginationReactive,
246
      renameInfo,
247
      breadcrumbItemsRef
248 249 250 251 252 253 254 255 256
    }
  },
  render() {
    const { t } = useI18n()
    const { columnsRef } = useTable(this.handleRenameFile, this.updateList)
    const {
      handleConditions,
      handleCreateFolder,
      handleCreateFile,
257
      handleUploadFile
258
    } = this
259

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