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.

Navigation.ts 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @copyright Copyright (c) 2022 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 isSvg from 'is-svg'
  25. import logger from '../logger.js'
  26. export type ContentsWithRoot = {
  27. folder: Folder,
  28. contents: Node[]
  29. }
  30. export interface Column {
  31. /** Unique column ID */
  32. id: string
  33. /** Translated column title */
  34. title: string
  35. /**
  36. * Property key from Node main or additional attributes.
  37. * Will be used if no custom sort function is provided.
  38. * Sorting will be done by localCompare
  39. */
  40. property: string
  41. /** Special function used to sort Nodes between them */
  42. sortFunction?: (nodeA: Node, nodeB: Node) => number;
  43. /** Custom summary of the column to display at the end of the list.
  44. Will not be displayed if nothing is provided */
  45. summary?: (node: Node[]) => string
  46. }
  47. export interface Navigation {
  48. /** Unique view ID */
  49. id: string
  50. /** Translated view name */
  51. name: string
  52. /**
  53. * Method return the content of the provided path
  54. * This ideally should be a cancellable promise.
  55. * promise.cancel(reason) will be called when the directory
  56. * change and the promise is not resolved yet.
  57. * You _must_ also return the current directory
  58. * information alongside with its content.
  59. */
  60. getContents: (path: string) => Promise<ContentsWithRoot[]>
  61. /** The view icon as an inline svg */
  62. icon: string
  63. /** The view order */
  64. order: number
  65. /** This view column(s). Name and actions are
  66. by default always included */
  67. columns?: Column[]
  68. /** The empty view element to render your empty content into */
  69. emptyView?: (div: HTMLDivElement) => void
  70. /** The parent unique ID */
  71. parent?: string
  72. /** This view is sticky (sent at the bottom) */
  73. sticky?: boolean
  74. /** This view has children and is expanded or not */
  75. expanded?: boolean
  76. /**
  77. * This view is sticky a legacy view.
  78. * Here until all the views are migrated to Vue.
  79. * @deprecated It will be removed in a near future
  80. */
  81. legacy?: boolean
  82. /**
  83. * An icon class.
  84. * @deprecated It will be removed in a near future
  85. */
  86. iconClass?: string
  87. }
  88. export default class {
  89. private _views: Navigation[] = []
  90. private _currentView: Navigation | null = null
  91. constructor() {
  92. logger.debug('Navigation service initialized')
  93. }
  94. register(view: Navigation) {
  95. try {
  96. isValidNavigation(view)
  97. isUniqueNavigation(view, this._views)
  98. } catch (e) {
  99. if (e instanceof Error) {
  100. logger.error(e.message, { view })
  101. }
  102. throw e
  103. }
  104. if (view.legacy) {
  105. logger.warn('Legacy view detected, please migrate to Vue')
  106. }
  107. if (view.iconClass) {
  108. view.legacy = true
  109. }
  110. this._views.push(view)
  111. }
  112. get views(): Navigation[] {
  113. return this._views
  114. }
  115. setActive(view: Navigation | null) {
  116. this._currentView = view
  117. }
  118. get active(): Navigation | null {
  119. return this._currentView
  120. }
  121. }
  122. /**
  123. * Make sure the given view is unique
  124. * and not already registered.
  125. */
  126. const isUniqueNavigation = function(view: Navigation, views: Navigation[]): boolean {
  127. if (views.find(search => search.id === view.id)) {
  128. throw new Error(`Navigation id ${view.id} is already registered`)
  129. }
  130. return true
  131. }
  132. /**
  133. * Typescript cannot validate an interface.
  134. * Please keep in sync with the Navigation interface requirements.
  135. */
  136. const isValidNavigation = function(view: Navigation): boolean {
  137. if (!view.id || typeof view.id !== 'string') {
  138. throw new Error('Navigation id is required and must be a string')
  139. }
  140. if (!view.name || typeof view.name !== 'string') {
  141. throw new Error('Navigation name is required and must be a string')
  142. }
  143. /**
  144. * Legacy handle their content and icon differently
  145. * TODO: remove when support for legacy views is removed
  146. */
  147. if (!view.legacy) {
  148. if (!view.getContents || typeof view.getContents !== 'function') {
  149. throw new Error('Navigation getContents is required and must be a function')
  150. }
  151. if (!view.icon || typeof view.icon !== 'string' || !isSvg(view.icon)) {
  152. throw new Error('Navigation icon is required and must be a valid svg string')
  153. }
  154. }
  155. if (!('order' in view) || typeof view.order !== 'number') {
  156. throw new Error('Navigation order is required and must be a number')
  157. }
  158. // Optional properties
  159. if (view.columns) {
  160. view.columns.forEach(isValidColumn)
  161. }
  162. if (view.emptyView && typeof view.emptyView !== 'function') {
  163. throw new Error('Navigation emptyView must be a function')
  164. }
  165. if (view.parent && typeof view.parent !== 'string') {
  166. throw new Error('Navigation parent must be a string')
  167. }
  168. if ('sticky' in view && typeof view.sticky !== 'boolean') {
  169. throw new Error('Navigation sticky must be a boolean')
  170. }
  171. if ('expanded' in view && typeof view.expanded !== 'boolean') {
  172. throw new Error('Navigation expanded must be a boolean')
  173. }
  174. return true
  175. }
  176. /**
  177. * Typescript cannot validate an interface.
  178. * Please keep in sync with the Column interface requirements.
  179. */
  180. const isValidColumn = function(column: Column): boolean {
  181. if (!column.id || typeof column.id !== 'string') {
  182. throw new Error('Column id is required')
  183. }
  184. if (!column.title || typeof column.title !== 'string') {
  185. throw new Error('Column title is required')
  186. }
  187. if (!column.property || typeof column.property !== 'string') {
  188. throw new Error('Column property is required')
  189. }
  190. // Optional properties
  191. if (column.sortFunction && typeof column.sortFunction !== 'function') {
  192. throw new Error('Column sortFunction must be a function')
  193. }
  194. if (column.summary && typeof column.summary !== 'function') {
  195. throw new Error('Column summary must be a function')
  196. }
  197. return true
  198. }