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.

files.ts 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* eslint-disable */
  23. import type { Folder, Node } from '@nextcloud/files'
  24. import Vue from 'vue'
  25. import type { FileStore, RootStore, RootOptions, Service } from '../types'
  26. const state = {
  27. files: {} as FileStore,
  28. roots: {} as RootStore,
  29. }
  30. const getters = {
  31. /**
  32. * Get a file or folder by id
  33. */
  34. getNode: (state) => (id: number): Node|undefined => state.files[id],
  35. /**
  36. * Get a list of files or folders by their IDs
  37. * Does not return undefined values
  38. */
  39. getNodes: (state) => (ids: number[]): Node[] => ids
  40. .map(id => state.files[id])
  41. .filter(Boolean),
  42. /**
  43. * Get a file or folder by id
  44. */
  45. getRoot: (state) => (service: Service): Folder|undefined => state.roots[service],
  46. }
  47. const mutations = {
  48. updateNodes: (state, nodes: Node[]) => {
  49. nodes.forEach(node => {
  50. if (!node.attributes.fileid) {
  51. return
  52. }
  53. Vue.set(state.files, node.attributes.fileid, node)
  54. // state.files = {
  55. // ...state.files,
  56. // [node.attributes.fileid]: node,
  57. // }
  58. })
  59. },
  60. setRoot: (state, { service, root }: RootOptions) => {
  61. state.roots = {
  62. ...state.roots,
  63. [service]: root,
  64. }
  65. }
  66. }
  67. const actions = {
  68. /**
  69. * Insert valid nodes into the store.
  70. * Roots (that does _not_ have a fileid) should
  71. * be defined in the roots store
  72. */
  73. addNodes: (context, nodes: Node[]) => {
  74. context.commit('updateNodes', nodes)
  75. },
  76. /**
  77. * Set the root of a service
  78. */
  79. setRoot(context, { service, root }: RootOptions) {
  80. context.commit('setRoot', { service, root })
  81. }
  82. }
  83. export default {
  84. namespaced: true,
  85. state,
  86. getters,
  87. mutations,
  88. actions,
  89. }