commentsmodifymenu.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2018
  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. /* global Handlebars */
  11. (function() {
  12. var TEMPLATE_MENU =
  13. '<ul>' +
  14. '{{#each items}}' +
  15. '<li>' +
  16. '<a href="#" class="menuitem action {{name}} permanent" data-action="{{name}}">' +
  17. '{{#if iconClass}}' +
  18. '<span class="icon {{iconClass}}"></span>' +
  19. '{{else}}' +
  20. '<span class="no-icon"></span>' +
  21. '{{/if}}' +
  22. '<span>{{displayName}}</span>' +
  23. '</li>' +
  24. '{{/each}}' +
  25. '</ul>';
  26. /**
  27. * Construct a new CommentsModifyMenuinstance
  28. * @constructs CommentsModifyMenu
  29. * @memberof OC.Comments
  30. */
  31. var CommentsModifyMenu = OC.Backbone.View.extend({
  32. tagName: 'div',
  33. className: 'commentsModifyMenu popovermenu bubble menu',
  34. _scopes: [
  35. {
  36. name: 'edit',
  37. displayName: t('comments', 'Edit comment'),
  38. iconClass: 'icon-rename'
  39. },
  40. {
  41. name: 'delete',
  42. displayName: t('comments', 'Delete comment'),
  43. iconClass: 'icon-delete'
  44. }
  45. ],
  46. initialize: function() {
  47. },
  48. events: {
  49. 'click a.action': '_onClickAction'
  50. },
  51. template: Handlebars.compile(TEMPLATE_MENU),
  52. /**
  53. * Event handler whenever an action has been clicked within the menu
  54. *
  55. * @param {Object} event event object
  56. */
  57. _onClickAction: function(event) {
  58. var $target = $(event.currentTarget);
  59. if (!$target.hasClass('menuitem')) {
  60. $target = $target.closest('.menuitem');
  61. }
  62. OC.hideMenus();
  63. this.trigger('select:menu-item-clicked', event, $target.data('action'));
  64. },
  65. /**
  66. * Renders the menu with the currently set items
  67. */
  68. render: function() {
  69. this.$el.html(this.template({
  70. items: this._scopes
  71. }));
  72. },
  73. /**
  74. * Displays the menu
  75. */
  76. show: function(context) {
  77. this._context = context;
  78. for(var i in this._scopes) {
  79. this._scopes[i].active = false;
  80. }
  81. var $el = $(context.target);
  82. var offsetIcon = $el.offset();
  83. var offsetContainer = $el.closest('.authorRow').offset();
  84. // adding some extra top offset to push the menu below the button.
  85. var position = {
  86. top: offsetIcon.top - offsetContainer.top + 48,
  87. left: '',
  88. right: ''
  89. };
  90. position.left = offsetIcon.left - offsetContainer.left;
  91. if (position.left > 200) {
  92. // we need to position the menu to the right.
  93. position.left = '';
  94. position.right = this.$el.closest('.comment').find('.date').width();
  95. this.$el.removeClass('menu-left').addClass('menu-right');
  96. } else {
  97. this.$el.removeClass('menu-right').addClass('menu-left');
  98. }
  99. this.$el.css(position);
  100. this.render();
  101. this.$el.removeClass('hidden');
  102. OC.showMenu(null, this.$el);
  103. }
  104. });
  105. OCA.Comments = OCA.Comments || {};
  106. OCA.Comments.CommentsModifyMenu = CommentsModifyMenu;
  107. })(OC, OCA);