fileactionsmenu.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. );
  48. var actionSpec = actions[actionName];
  49. var fileName = this._context.$file.attr('data-file');
  50. event.stopPropagation();
  51. event.preventDefault();
  52. OC.hideMenus();
  53. actionSpec.action(
  54. fileName,
  55. this._context
  56. );
  57. },
  58. /**
  59. * Renders the menu with the currently set items
  60. */
  61. render: function() {
  62. var self = this;
  63. var fileActions = this._context.fileActions;
  64. var actions = fileActions.getActions(
  65. fileActions.getCurrentMimeType(),
  66. fileActions.getCurrentType(),
  67. fileActions.getCurrentPermissions()
  68. );
  69. var defaultAction = fileActions.getDefaultFileAction(
  70. fileActions.getCurrentMimeType(),
  71. fileActions.getCurrentType(),
  72. fileActions.getCurrentPermissions()
  73. );
  74. var items = _.filter(actions, function(actionSpec) {
  75. return (
  76. actionSpec.type === OCA.Files.FileActions.TYPE_DROPDOWN &&
  77. (!defaultAction || actionSpec.name !== defaultAction.name)
  78. );
  79. });
  80. items = _.map(items, function(item) {
  81. if (_.isFunction(item.displayName)) {
  82. item = _.extend({}, item);
  83. item.displayName = item.displayName(self._context);
  84. }
  85. if (_.isFunction(item.iconClass)) {
  86. var fileName = self._context.$file.attr('data-file');
  87. item = _.extend({}, item);
  88. item.iconClass = item.iconClass(fileName, self._context);
  89. }
  90. return item;
  91. });
  92. items = items.sort(function(actionA, actionB) {
  93. var orderA = actionA.order || 0;
  94. var orderB = actionB.order || 0;
  95. if (orderB === orderA) {
  96. return OC.Util.naturalSortCompare(actionA.displayName, actionB.displayName);
  97. }
  98. return orderA - orderB;
  99. });
  100. items = _.map(items, function(item) {
  101. item.nameLowerCase = item.name.toLowerCase();
  102. return item;
  103. });
  104. this.$el.html(this.template({
  105. items: items
  106. }));
  107. },
  108. /**
  109. * Displays the menu under the given element
  110. *
  111. * @param {OCA.Files.FileActionContext} context context
  112. * @param {Object} $trigger trigger element
  113. */
  114. show: function(context) {
  115. this._context = context;
  116. this.render();
  117. this.$el.removeClass('hidden');
  118. OC.showMenu(null, this.$el);
  119. }
  120. });
  121. OCA.Files.FileActionsMenu = FileActionsMenu;
  122. })();