1
0

fileactionsmenu.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015-2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. (function() {
  7. /**
  8. * Construct a new FileActionsMenu instance
  9. * @constructs FileActionsMenu
  10. * @memberof OCA.Files
  11. */
  12. var FileActionsMenu = OC.Backbone.View.extend({
  13. tagName: 'div',
  14. className: 'fileActionsMenu popovermenu bubble hidden open menu',
  15. /**
  16. * Current context
  17. *
  18. * @type OCA.Files.FileActionContext
  19. */
  20. _context: null,
  21. events: {
  22. 'click a.action': '_onClickAction'
  23. },
  24. template: function(data) {
  25. return OCA.Files.Templates['fileactionsmenu'](data);
  26. },
  27. /**
  28. * Event handler whenever an action has been clicked within the menu
  29. *
  30. * @param {Object} event event object
  31. */
  32. _onClickAction: function(event) {
  33. var $target = $(event.target);
  34. if (!$target.is('a')) {
  35. $target = $target.closest('a');
  36. }
  37. var fileActions = this._context.fileActions;
  38. var actionName = $target.attr('data-action');
  39. var actions = fileActions.getActions(
  40. fileActions.getCurrentMimeType(),
  41. fileActions.getCurrentType(),
  42. fileActions.getCurrentPermissions(),
  43. fileActions.getCurrentFile()
  44. );
  45. var actionSpec = actions[actionName];
  46. var fileName = this._context.$file.attr('data-file');
  47. event.stopPropagation();
  48. event.preventDefault();
  49. OC.hideMenus();
  50. actionSpec.action(
  51. fileName,
  52. this._context
  53. );
  54. },
  55. /**
  56. * Renders the menu with the currently set items
  57. */
  58. render: function() {
  59. var self = this;
  60. var fileActions = this._context.fileActions;
  61. var actions = fileActions.getActions(
  62. fileActions.getCurrentMimeType(),
  63. fileActions.getCurrentType(),
  64. fileActions.getCurrentPermissions(),
  65. fileActions.getCurrentFile()
  66. );
  67. var defaultAction = fileActions.getCurrentDefaultFileAction();
  68. var items = _.filter(actions, function(actionSpec) {
  69. return !defaultAction || actionSpec.name !== defaultAction.name;
  70. });
  71. items = _.map(items, function(item) {
  72. if (_.isFunction(item.displayName)) {
  73. item = _.extend({}, item);
  74. item.displayName = item.displayName(self._context);
  75. }
  76. if (_.isFunction(item.iconClass)) {
  77. var fileName = self._context.$file.attr('data-file');
  78. item = _.extend({}, item);
  79. item.iconClass = item.iconClass(fileName, self._context);
  80. }
  81. if (_.isFunction(item.icon)) {
  82. var fileName = self._context.$file.attr('data-file');
  83. item = _.extend({}, item);
  84. item.icon = item.icon(fileName, self._context);
  85. }
  86. item.inline = item.type === OCA.Files.FileActions.TYPE_INLINE
  87. return item;
  88. });
  89. items = items.sort(function(actionA, actionB) {
  90. var orderA = actionA.order || 0;
  91. var orderB = actionB.order || 0;
  92. if (orderB === orderA) {
  93. return OC.Util.naturalSortCompare(actionA.displayName, actionB.displayName);
  94. }
  95. return orderA - orderB;
  96. });
  97. items = _.map(items, function(item) {
  98. item.nameLowerCase = item.name.toLowerCase();
  99. return item;
  100. });
  101. this.$el.html(this.template({
  102. items: items
  103. }));
  104. },
  105. /**
  106. * Displays the menu under the given element
  107. *
  108. * @param {OCA.Files.FileActionContext} context context
  109. * @param {Object} $trigger trigger element
  110. */
  111. show: function(context) {
  112. this._context = context;
  113. this.render();
  114. this.$el.removeClass('hidden');
  115. window._nc_event_bus.emit('files:action-menu:opened', {
  116. el: this.$el[0],
  117. context,
  118. })
  119. OC.showMenu(null, this.$el);
  120. }
  121. });
  122. OCA.Files.FileActionsMenu = FileActionsMenu;
  123. })();