You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilesListVirtual.vue 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <!--
  2. - @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
  3. -
  4. - @author John Molakvoæ <skjnldsv@protonmail.com>
  5. -
  6. - @license AGPL-3.0-or-later
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <VirtualList ref="table"
  24. :data-component="userConfig.grid_view ? FileEntryGrid : FileEntry"
  25. :data-key="'source'"
  26. :data-sources="nodes"
  27. :grid-mode="userConfig.grid_view"
  28. :extra-props="{
  29. isMtimeAvailable,
  30. isSizeAvailable,
  31. nodes,
  32. filesListWidth,
  33. }"
  34. :scroll-to-index="scrollToIndex"
  35. :caption="caption">
  36. <template v-if="!isNoneSelected" #header-overlay>
  37. <span class="files-list__selected">{{ t('files', '{count} selected', { count: selectedNodes.length }) }}</span>
  38. <FilesListTableHeaderActions :current-view="currentView"
  39. :selected-nodes="selectedNodes" />
  40. </template>
  41. <template #before>
  42. <!-- Headers -->
  43. <FilesListHeader v-for="header in sortedHeaders"
  44. :key="header.id"
  45. :current-folder="currentFolder"
  46. :current-view="currentView"
  47. :header="header" />
  48. </template>
  49. <!-- Thead-->
  50. <template #header>
  51. <!-- Table header and sort buttons -->
  52. <FilesListTableHeader ref="thead"
  53. :files-list-width="filesListWidth"
  54. :is-mtime-available="isMtimeAvailable"
  55. :is-size-available="isSizeAvailable"
  56. :nodes="nodes" />
  57. </template>
  58. <!-- Tfoot-->
  59. <template #footer>
  60. <FilesListTableFooter :files-list-width="filesListWidth"
  61. :is-mtime-available="isMtimeAvailable"
  62. :is-size-available="isSizeAvailable"
  63. :nodes="nodes"
  64. :summary="summary" />
  65. </template>
  66. </VirtualList>
  67. </template>
  68. <script lang="ts">
  69. import type { Node as NcNode } from '@nextcloud/files'
  70. import type { PropType } from 'vue'
  71. import type { UserConfig } from '../types'
  72. import { getFileListHeaders, Folder, View, getFileActions, FileType } from '@nextcloud/files'
  73. import { showError } from '@nextcloud/dialogs'
  74. import { loadState } from '@nextcloud/initial-state'
  75. import { translate as t, translatePlural as n } from '@nextcloud/l10n'
  76. import { defineComponent } from 'vue'
  77. import { action as sidebarAction } from '../actions/sidebarAction.ts'
  78. import { getSummaryFor } from '../utils/fileUtils'
  79. import { useSelectionStore } from '../store/selection.js'
  80. import { useUserConfigStore } from '../store/userconfig.ts'
  81. import FileEntry from './FileEntry.vue'
  82. import FileEntryGrid from './FileEntryGrid.vue'
  83. import FilesListHeader from './FilesListHeader.vue'
  84. import FilesListTableFooter from './FilesListTableFooter.vue'
  85. import FilesListTableHeader from './FilesListTableHeader.vue'
  86. import filesListWidthMixin from '../mixins/filesListWidth.ts'
  87. import VirtualList from './VirtualList.vue'
  88. import logger from '../logger.js'
  89. import FilesListTableHeaderActions from './FilesListTableHeaderActions.vue'
  90. export default defineComponent({
  91. name: 'FilesListVirtual',
  92. components: {
  93. FilesListHeader,
  94. FilesListTableFooter,
  95. FilesListTableHeader,
  96. VirtualList,
  97. FilesListTableHeaderActions,
  98. },
  99. mixins: [
  100. filesListWidthMixin,
  101. ],
  102. props: {
  103. currentView: {
  104. type: View,
  105. required: true,
  106. },
  107. currentFolder: {
  108. type: Folder,
  109. required: true,
  110. },
  111. nodes: {
  112. type: Array as PropType<NcNode[]>,
  113. required: true,
  114. },
  115. },
  116. setup() {
  117. const userConfigStore = useUserConfigStore()
  118. const selectionStore = useSelectionStore()
  119. return {
  120. userConfigStore,
  121. selectionStore,
  122. }
  123. },
  124. data() {
  125. return {
  126. FileEntry,
  127. FileEntryGrid,
  128. headers: getFileListHeaders(),
  129. scrollToIndex: 0,
  130. openFileId: null as number|null,
  131. }
  132. },
  133. computed: {
  134. userConfig(): UserConfig {
  135. return this.userConfigStore.userConfig
  136. },
  137. fileId() {
  138. return parseInt(this.$route.params.fileid) || null
  139. },
  140. /**
  141. * If the current `fileId` should be opened
  142. * The state of the `openfile` query param
  143. */
  144. openFile() {
  145. return !!this.$route.query.openfile
  146. },
  147. summary() {
  148. return getSummaryFor(this.nodes)
  149. },
  150. isMtimeAvailable() {
  151. // Hide mtime column on narrow screens
  152. if (this.filesListWidth < 768) {
  153. return false
  154. }
  155. return this.nodes.some(node => node.mtime !== undefined)
  156. },
  157. isSizeAvailable() {
  158. // Hide size column on narrow screens
  159. if (this.filesListWidth < 768) {
  160. return false
  161. }
  162. return this.nodes.some(node => node.attributes.size !== undefined)
  163. },
  164. sortedHeaders() {
  165. if (!this.currentFolder || !this.currentView) {
  166. return []
  167. }
  168. return [...this.headers].sort((a, b) => a.order - b.order)
  169. },
  170. caption() {
  171. const defaultCaption = t('files', 'List of files and folders.')
  172. const viewCaption = this.currentView.caption || defaultCaption
  173. const sortableCaption = t('files', 'Column headers with buttons are sortable.')
  174. const virtualListNote = t('files', 'This list is not fully rendered for performance reasons. The files will be rendered as you navigate through the list.')
  175. return `${viewCaption}\n${sortableCaption}\n${virtualListNote}`
  176. },
  177. selectedNodes() {
  178. return this.selectionStore.selected
  179. },
  180. isNoneSelected() {
  181. return this.selectedNodes.length === 0
  182. },
  183. },
  184. watch: {
  185. fileId(fileId) {
  186. this.scrollToFile(fileId, false)
  187. },
  188. openFile(open: boolean) {
  189. if (open) {
  190. this.$nextTick(() => this.handleOpenFile(this.fileId))
  191. }
  192. },
  193. },
  194. mounted() {
  195. // Add events on parent to cover both the table and DragAndDrop notice
  196. const mainContent = window.document.querySelector('main.app-content') as HTMLElement
  197. mainContent.addEventListener('dragover', this.onDragOver)
  198. // handle initially opening a given file
  199. const { id } = loadState<{ id?: number }>('files', 'openFileInfo', {})
  200. this.scrollToFile(id ?? this.fileId)
  201. this.openSidebarForFile(id ?? this.fileId)
  202. this.handleOpenFile(id ?? null)
  203. },
  204. beforeDestroy() {
  205. const mainContent = window.document.querySelector('main.app-content') as HTMLElement
  206. mainContent.removeEventListener('dragover', this.onDragOver)
  207. },
  208. methods: {
  209. // Open the file sidebar if we have the room for it
  210. // but don't open the sidebar for the current folder
  211. openSidebarForFile(fileId) {
  212. if (document.documentElement.clientWidth > 1024 && this.currentFolder.fileid !== fileId) {
  213. // Open the sidebar for the given URL fileid
  214. // iif we just loaded the app.
  215. const node = this.nodes.find(n => n.fileid === fileId) as NcNode
  216. if (node && sidebarAction?.enabled?.([node], this.currentView)) {
  217. logger.debug('Opening sidebar on file ' + node.path, { node })
  218. sidebarAction.exec(node, this.currentView, this.currentFolder.path)
  219. }
  220. }
  221. },
  222. scrollToFile(fileId: number|null, warn = true) {
  223. if (fileId) {
  224. const index = this.nodes.findIndex(node => node.fileid === fileId)
  225. if (warn && index === -1 && fileId !== this.currentFolder.fileid) {
  226. showError(this.t('files', 'File not found'))
  227. }
  228. this.scrollToIndex = Math.max(0, index)
  229. }
  230. },
  231. /**
  232. * Handle opening a file (e.g. by ?openfile=true)
  233. * @param fileId File to open
  234. */
  235. handleOpenFile(fileId: number|null) {
  236. if (fileId === null || this.openFileId === fileId) {
  237. return
  238. }
  239. const node = this.nodes.find(n => n.fileid === fileId) as NcNode
  240. if (node === undefined || node.type === FileType.Folder) {
  241. return
  242. }
  243. logger.debug('Opening file ' + node.path, { node })
  244. this.openFileId = fileId
  245. getFileActions()
  246. .filter(action => !action.enabled || action.enabled([node], this.currentView))
  247. .sort((a, b) => (a.order || 0) - (b.order || 0))
  248. .filter(action => !!action?.default)[0].exec(node, this.currentView, this.currentFolder.path)
  249. },
  250. getFileId(node) {
  251. return node.fileid
  252. },
  253. onDragOver(event: DragEvent) {
  254. // Detect if we're only dragging existing files or not
  255. const isForeignFile = event.dataTransfer?.types.includes('Files')
  256. if (isForeignFile) {
  257. // Only handle uploading of existing Nextcloud files
  258. // See DragAndDropNotice for handling of foreign files
  259. return
  260. }
  261. event.preventDefault()
  262. event.stopPropagation()
  263. const tableTop = this.$refs.table.$el.getBoundingClientRect().top
  264. const tableBottom = tableTop + this.$refs.table.$el.getBoundingClientRect().height
  265. // If reaching top, scroll up. Using 100 because of the floating header
  266. if (event.clientY < tableTop + 100) {
  267. this.$refs.table.$el.scrollTop = this.$refs.table.$el.scrollTop - 25
  268. return
  269. }
  270. // If reaching bottom, scroll down
  271. if (event.clientY > tableBottom - 50) {
  272. this.$refs.table.$el.scrollTop = this.$refs.table.$el.scrollTop + 25
  273. }
  274. },
  275. t,
  276. },
  277. })
  278. </script>
  279. <style scoped lang="scss">
  280. .files-list {
  281. --row-height: 55px;
  282. --cell-margin: 14px;
  283. --checkbox-padding: calc((var(--row-height) - var(--checkbox-size)) / 2);
  284. --checkbox-size: 24px;
  285. --clickable-area: 44px;
  286. --icon-preview-size: 32px;
  287. overflow: auto;
  288. height: 100%;
  289. will-change: scroll-position;
  290. & :deep() {
  291. // Table head, body and footer
  292. tbody {
  293. will-change: padding;
  294. contain: layout paint style;
  295. display: flex;
  296. flex-direction: column;
  297. width: 100%;
  298. // Necessary for virtual scrolling absolute
  299. position: relative;
  300. /* Hover effect on tbody lines only */
  301. tr {
  302. contain: strict;
  303. &:hover,
  304. &:focus {
  305. background-color: var(--color-background-dark);
  306. }
  307. }
  308. }
  309. // Before table and thead
  310. .files-list__before {
  311. display: flex;
  312. flex-direction: column;
  313. }
  314. .files-list__selected {
  315. padding-right: 12px;
  316. white-space: nowrap;
  317. }
  318. .files-list__table {
  319. display: block;
  320. &.files-list__table--with-thead-overlay {
  321. // Hide the table header below the overlay
  322. margin-top: calc(-1 * var(--row-height));
  323. }
  324. }
  325. .files-list__thead-overlay {
  326. // Pinned on top when scrolling
  327. position: sticky;
  328. top: 0;
  329. // Save space for a row checkbox
  330. margin-left: var(--row-height);
  331. // More than .files-list__thead
  332. z-index: 20;
  333. display: flex;
  334. align-items: center;
  335. // Reuse row styles
  336. background-color: var(--color-main-background);
  337. border-bottom: 1px solid var(--color-border);
  338. height: var(--row-height);
  339. }
  340. .files-list__thead,
  341. .files-list__tfoot {
  342. display: flex;
  343. flex-direction: column;
  344. width: 100%;
  345. background-color: var(--color-main-background);
  346. }
  347. // Table header
  348. .files-list__thead {
  349. // Pinned on top when scrolling
  350. position: sticky;
  351. z-index: 10;
  352. top: 0;
  353. }
  354. // Table footer
  355. .files-list__tfoot {
  356. min-height: 300px;
  357. }
  358. tr {
  359. position: relative;
  360. display: flex;
  361. align-items: center;
  362. width: 100%;
  363. user-select: none;
  364. border-bottom: 1px solid var(--color-border);
  365. box-sizing: border-box;
  366. user-select: none;
  367. height: var(--row-height);
  368. }
  369. td, th {
  370. display: flex;
  371. align-items: center;
  372. flex: 0 0 auto;
  373. justify-content: left;
  374. width: var(--row-height);
  375. height: var(--row-height);
  376. margin: 0;
  377. padding: 0;
  378. color: var(--color-text-maxcontrast);
  379. border: none;
  380. // Columns should try to add any text
  381. // node wrapped in a span. That should help
  382. // with the ellipsis on overflow.
  383. span {
  384. overflow: hidden;
  385. white-space: nowrap;
  386. text-overflow: ellipsis;
  387. }
  388. }
  389. .files-list__row--failed {
  390. position: absolute;
  391. display: block;
  392. top: 0;
  393. left: 0;
  394. right: 0;
  395. bottom: 0;
  396. opacity: .1;
  397. z-index: -1;
  398. background: var(--color-error);
  399. }
  400. .files-list__row-checkbox {
  401. justify-content: center;
  402. .checkbox-radio-switch {
  403. display: flex;
  404. justify-content: center;
  405. --icon-size: var(--checkbox-size);
  406. label.checkbox-radio-switch__label {
  407. width: var(--clickable-area);
  408. height: var(--clickable-area);
  409. margin: 0;
  410. padding: calc((var(--clickable-area) - var(--checkbox-size)) / 2);
  411. }
  412. .checkbox-radio-switch__icon {
  413. margin: 0 !important;
  414. }
  415. }
  416. }
  417. .files-list__row {
  418. &:hover, &:focus, &:active, &--active, &--dragover {
  419. // WCAG AA compliant
  420. background-color: var(--color-background-hover);
  421. // text-maxcontrast have been designed to pass WCAG AA over
  422. // a white background, we need to adjust then.
  423. --color-text-maxcontrast: var(--color-main-text);
  424. > * {
  425. --color-border: var(--color-border-dark);
  426. }
  427. // Hover state of the row should also change the favorite markers background
  428. .favorite-marker-icon svg path {
  429. stroke: var(--color-background-hover);
  430. }
  431. }
  432. &--dragover * {
  433. // Prevent dropping on row children
  434. pointer-events: none;
  435. }
  436. }
  437. // Entry preview or mime icon
  438. .files-list__row-icon {
  439. position: relative;
  440. display: flex;
  441. overflow: visible;
  442. align-items: center;
  443. // No shrinking or growing allowed
  444. flex: 0 0 var(--icon-preview-size);
  445. justify-content: center;
  446. width: var(--icon-preview-size);
  447. height: 100%;
  448. // Show same padding as the checkbox right padding for visual balance
  449. margin-right: var(--checkbox-padding);
  450. color: var(--color-primary-element);
  451. // Icon is also clickable
  452. * {
  453. cursor: pointer;
  454. }
  455. & > span {
  456. justify-content: flex-start;
  457. &:not(.files-list__row-icon-favorite) svg {
  458. width: var(--icon-preview-size);
  459. height: var(--icon-preview-size);
  460. }
  461. // Slightly increase the size of the folder icon
  462. &.folder-icon,
  463. &.folder-open-icon {
  464. margin: -3px;
  465. svg {
  466. width: calc(var(--icon-preview-size) + 6px);
  467. height: calc(var(--icon-preview-size) + 6px);
  468. }
  469. }
  470. }
  471. &-preview {
  472. overflow: hidden;
  473. width: var(--icon-preview-size);
  474. height: var(--icon-preview-size);
  475. border-radius: var(--border-radius);
  476. // Center and contain the preview
  477. object-fit: contain;
  478. object-position: center;
  479. /* Preview not loaded animation effect */
  480. &:not(.files-list__row-icon-preview--loaded) {
  481. background: var(--color-loading-dark);
  482. // animation: preview-gradient-fade 1.2s ease-in-out infinite;
  483. }
  484. }
  485. &-favorite {
  486. position: absolute;
  487. top: 0px;
  488. right: -10px;
  489. }
  490. // File and folder overlay
  491. &-overlay {
  492. position: absolute;
  493. max-height: calc(var(--icon-preview-size) * 0.5);
  494. max-width: calc(var(--icon-preview-size) * 0.5);
  495. color: var(--color-primary-element-text);
  496. // better alignment with the folder icon
  497. margin-top: 2px;
  498. // Improve icon contrast with a background for files
  499. &--file {
  500. color: var(--color-main-text);
  501. background: var(--color-main-background);
  502. border-radius: 100%;
  503. }
  504. }
  505. }
  506. // Entry link
  507. .files-list__row-name {
  508. // Prevent link from overflowing
  509. overflow: hidden;
  510. // Take as much space as possible
  511. flex: 1 1 auto;
  512. a {
  513. display: flex;
  514. align-items: center;
  515. // Fill cell height and width
  516. width: 100%;
  517. height: 100%;
  518. // Necessary for flex grow to work
  519. min-width: 0;
  520. // Already added to the inner text, see rule below
  521. &:focus-visible {
  522. outline: none;
  523. }
  524. // Keyboard indicator a11y
  525. &:focus .files-list__row-name-text {
  526. outline: 2px solid var(--color-main-text) !important;
  527. border-radius: 20px;
  528. }
  529. &:focus:not(:focus-visible) .files-list__row-name-text {
  530. outline: none !important;
  531. }
  532. }
  533. .files-list__row-name-text {
  534. color: var(--color-main-text);
  535. // Make some space for the outline
  536. padding: 5px 10px;
  537. margin-left: -10px;
  538. // Align two name and ext
  539. display: inline-flex;
  540. }
  541. .files-list__row-name-ext {
  542. color: var(--color-text-maxcontrast);
  543. // always show the extension
  544. overflow: visible;
  545. }
  546. }
  547. // Rename form
  548. .files-list__row-rename {
  549. width: 100%;
  550. max-width: 600px;
  551. input {
  552. width: 100%;
  553. // Align with text, 0 - padding - border
  554. margin-left: -8px;
  555. padding: 2px 6px;
  556. border-width: 2px;
  557. &:invalid {
  558. // Show red border on invalid input
  559. border-color: var(--color-error);
  560. color: red;
  561. }
  562. }
  563. }
  564. .files-list__row-actions {
  565. // take as much space as necessary
  566. width: auto;
  567. // Add margin to all cells after the actions
  568. & ~ td,
  569. & ~ th {
  570. margin: 0 var(--cell-margin);
  571. }
  572. button {
  573. .button-vue__text {
  574. // Remove bold from default button styling
  575. font-weight: normal;
  576. }
  577. }
  578. }
  579. .files-list__row-action--inline {
  580. margin-right: 7px;
  581. }
  582. .files-list__row-mtime,
  583. .files-list__row-size {
  584. color: var(--color-text-maxcontrast);
  585. }
  586. .files-list__row-size {
  587. width: calc(var(--row-height) * 1.5);
  588. // Right align content/text
  589. justify-content: flex-end;
  590. }
  591. .files-list__row-mtime {
  592. width: calc(var(--row-height) * 2);
  593. }
  594. .files-list__row-column-custom {
  595. width: calc(var(--row-height) * 2);
  596. }
  597. }
  598. }
  599. </style>
  600. <style lang="scss">
  601. // Grid mode
  602. tbody.files-list__tbody.files-list__tbody--grid {
  603. --half-clickable-area: calc(var(--clickable-area) / 2);
  604. --row-width: 160px;
  605. // We use half of the clickable area as visual balance margin
  606. --row-height: calc(var(--row-width) - var(--half-clickable-area));
  607. --icon-preview-size: calc(var(--row-width) - var(--clickable-area));
  608. --checkbox-padding: 0px;
  609. display: grid;
  610. grid-template-columns: repeat(auto-fill, var(--row-width));
  611. grid-gap: 15px;
  612. row-gap: 15px;
  613. align-content: center;
  614. align-items: center;
  615. justify-content: space-around;
  616. justify-items: center;
  617. tr {
  618. width: var(--row-width);
  619. height: calc(var(--row-height) + var(--clickable-area));
  620. border: none;
  621. border-radius: var(--border-radius);
  622. }
  623. // Checkbox in the top left
  624. .files-list__row-checkbox {
  625. position: absolute;
  626. z-index: 9;
  627. top: 0;
  628. left: 0;
  629. overflow: hidden;
  630. width: var(--clickable-area);
  631. height: var(--clickable-area);
  632. border-radius: var(--half-clickable-area);
  633. }
  634. // Star icon in the top right
  635. .files-list__row-icon-favorite {
  636. position: absolute;
  637. top: 0;
  638. right: 0;
  639. display: flex;
  640. align-items: center;
  641. justify-content: center;
  642. width: var(--clickable-area);
  643. height: var(--clickable-area);
  644. }
  645. .files-list__row-name {
  646. display: grid;
  647. justify-content: stretch;
  648. width: 100%;
  649. height: 100%;
  650. grid-auto-rows: var(--row-height) var(--clickable-area);
  651. span.files-list__row-icon {
  652. width: 100%;
  653. height: 100%;
  654. // Visual balance, we use half of the clickable area
  655. // as a margin around the preview
  656. padding-top: var(--half-clickable-area);
  657. }
  658. a.files-list__row-name-link {
  659. // Minus action menu
  660. width: calc(100% - var(--clickable-area));
  661. height: var(--clickable-area);
  662. }
  663. .files-list__row-name-text {
  664. margin: 0;
  665. padding-right: 0;
  666. }
  667. }
  668. .files-list__row-actions {
  669. position: absolute;
  670. right: 0;
  671. bottom: 0;
  672. width: var(--clickable-area);
  673. height: var(--clickable-area);
  674. }
  675. }
  676. </style>