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.getCurrentDefaultFileAction();
  70. var items = _.filter(actions, function(actionSpec) {
  71. return !defaultAction || actionSpec.name !== defaultAction.name;
  72. });
  73. items = _.map(items, function(item) {
  74. if (_.isFunction(item.displayName)) {
  75. item = _.extend({}, item);
  76. item.displayName = item.displayName(self._context);
  77. }
  78. if (_.isFunction(item.iconClass)) {
  79. var fileName = self._context.$file.attr('data-file');
  80. item = _.extend({}, item);
  81. item.iconClass = item.iconClass(fileName, self._context);
  82. }
  83. if (_.isFunction(item.icon)) {
  84. var fileName = self._context.$file.attr('data-file');
  85. item = _.extend({}, item);
  86. item.icon = item.icon(fileName, self._context);
  87. }
  88. item.inline = item.type === OCA.Files.FileActions.TYPE_INLINE
  89. return item;
  90. });
  91. items = items.sort(function(actionA, actionB) {
  92. var orderA = actionA.order || 0;
  93. var orderB = actionB.order || 0;
  94. if (orderB === orderA) {
  95. return OC.Util.naturalSortCompare(actionA.displayName, actionB.displayName);
  96. }
  97. return orderA - orderB;
  98. });
  99. items = _.map(items, function(item) {
  100. item.nameLowerCase = item.name.toLowerCase();
  101. return item;
  102. });
  103. this.$el.html(this.template({
  104. items: items
  105. }));
  106. },
  107. /**
  108. * Displays the menu under the given element
  109. *
  110. * @param {OCA.Files.FileActionContext} context context
  111. * @param {Object} $trigger trigger element
  112. */
  113. show: function(context) {
  114. this._context = context;
  115. this.render();
  116. this.$el.removeClass('hidden');
  117. OC.showMenu(null, this.$el);
  118. }
  119. });
  120. OCA.Files.FileActionsMenu = FileActionsMenu;
  121. })();