fileactionsmenu.js 3.5 KB

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