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 5.8KB

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