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.

ExternalShareActions.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  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. export default class ExternalShareActions {
  23. _state;
  24. constructor() {
  25. // init empty state
  26. this._state = {}
  27. // init default values
  28. this._state.actions = []
  29. console.debug('OCA.Sharing.ExternalShareActions initialized')
  30. }
  31. /**
  32. * Get the state
  33. *
  34. * @readonly
  35. * @memberof ExternalLinkActions
  36. * @returns {Object} the data state
  37. */
  38. get state() {
  39. return this._state
  40. }
  41. /**
  42. * Register a new option/entry for the a given share type
  43. *
  44. * @param {Object} action new action component to register
  45. * @param {string} action.id unique action id
  46. * @param {Function} action.data data to bind the component to
  47. * @param {Array} action.shareType list of OC.Share.SHARE_XXX to be mounted on
  48. * @param {Object} action.handlers list of listeners
  49. * @returns {boolean}
  50. */
  51. registerAction(action) {
  52. // Validate action
  53. if (typeof action !== 'object'
  54. || typeof action.id !== 'string'
  55. || typeof action.data !== 'function' // () => {disabled: true}
  56. || !Array.isArray(action.shareType) // [OC.Share.SHARE_TYPE_LINK, ...]
  57. || typeof action.handlers !== 'object' // {click: () => {}, ...}
  58. || !Object.values(action.handlers).every(handler => typeof handler === 'function')) {
  59. console.error('Invalid action provided', action)
  60. return false
  61. }
  62. // Check duplicates
  63. const hasDuplicate = this._state.actions.findIndex(check => check.id === action.id) > -1
  64. if (hasDuplicate) {
  65. console.error(`An action with the same id ${action.id} already exists`, action)
  66. return false
  67. }
  68. this._state.actions.push(action)
  69. return true
  70. }
  71. }