fileactionsmenu.js 3.5 KB

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