fileactionsmenu.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. var TEMPLATE_MENU =
  12. '<ul>' +
  13. '{{#each items}}' +
  14. '<li>' +
  15. '<a href="#" class="menuitem action action-{{nameLowerCase}} permanent" data-action="{{name}}">' +
  16. '{{#if icon}}<img class="icon" src="{{icon}}"/>' +
  17. '{{else}}'+
  18. '{{#if iconClass}}' +
  19. '<span class="icon {{iconClass}}"></span>' +
  20. '{{else}}' +
  21. '<span class="no-icon"></span>' +
  22. '{{/if}}' +
  23. '{{/if}}' +
  24. '<span>{{displayName}}</span></a>' +
  25. '</li>' +
  26. '{{/each}}' +
  27. '</ul>';
  28. /**
  29. * Construct a new FileActionsMenu instance
  30. * @constructs FileActionsMenu
  31. * @memberof OCA.Files
  32. */
  33. var FileActionsMenu = OC.Backbone.View.extend({
  34. tagName: 'div',
  35. className: 'fileActionsMenu popovermenu bubble hidden open menu',
  36. /**
  37. * Current context
  38. *
  39. * @type OCA.Files.FileActionContext
  40. */
  41. _context: null,
  42. events: {
  43. 'click a.action': '_onClickAction'
  44. },
  45. template: function(data) {
  46. if (!OCA.Files.FileActionsMenu._TEMPLATE) {
  47. OCA.Files.FileActionsMenu._TEMPLATE = Handlebars.compile(TEMPLATE_MENU);
  48. }
  49. return OCA.Files.FileActionsMenu._TEMPLATE(data);
  50. },
  51. /**
  52. * Event handler whenever an action has been clicked within the menu
  53. *
  54. * @param {Object} event event object
  55. */
  56. _onClickAction: function(event) {
  57. var $target = $(event.target);
  58. if (!$target.is('a')) {
  59. $target = $target.closest('a');
  60. }
  61. var fileActions = this._context.fileActions;
  62. var actionName = $target.attr('data-action');
  63. var actions = fileActions.getActions(
  64. fileActions.getCurrentMimeType(),
  65. fileActions.getCurrentType(),
  66. fileActions.getCurrentPermissions()
  67. );
  68. var actionSpec = actions[actionName];
  69. var fileName = this._context.$file.attr('data-file');
  70. event.stopPropagation();
  71. event.preventDefault();
  72. OC.hideMenus();
  73. actionSpec.action(
  74. fileName,
  75. this._context
  76. );
  77. },
  78. /**
  79. * Renders the menu with the currently set items
  80. */
  81. render: function() {
  82. var self = this;
  83. var fileActions = this._context.fileActions;
  84. var actions = fileActions.getActions(
  85. fileActions.getCurrentMimeType(),
  86. fileActions.getCurrentType(),
  87. fileActions.getCurrentPermissions()
  88. );
  89. var defaultAction = fileActions.getDefaultFileAction(
  90. fileActions.getCurrentMimeType(),
  91. fileActions.getCurrentType(),
  92. fileActions.getCurrentPermissions()
  93. );
  94. var items = _.filter(actions, function(actionSpec) {
  95. return (
  96. actionSpec.type === OCA.Files.FileActions.TYPE_DROPDOWN &&
  97. (!defaultAction || actionSpec.name !== defaultAction.name)
  98. );
  99. });
  100. items = _.map(items, function(item) {
  101. if (_.isFunction(item.displayName)) {
  102. item = _.extend({}, item);
  103. item.displayName = item.displayName(self._context);
  104. }
  105. return item;
  106. });
  107. items = items.sort(function(actionA, actionB) {
  108. var orderA = actionA.order || 0;
  109. var orderB = actionB.order || 0;
  110. if (orderB === orderA) {
  111. return OC.Util.naturalSortCompare(actionA.displayName, actionB.displayName);
  112. }
  113. return orderA - orderB;
  114. });
  115. items = _.map(items, function(item) {
  116. item.nameLowerCase = item.name.toLowerCase();
  117. return item;
  118. });
  119. this.$el.html(this.template({
  120. items: items
  121. }));
  122. },
  123. /**
  124. * Displays the menu under the given element
  125. *
  126. * @param {OCA.Files.FileActionContext} context context
  127. * @param {Object} $trigger trigger element
  128. */
  129. show: function(context) {
  130. this._context = context;
  131. this.render();
  132. this.$el.removeClass('hidden');
  133. OC.showMenu(null, this.$el);
  134. }
  135. });
  136. OCA.Files.FileActionsMenu = FileActionsMenu;
  137. })();