1
0

filesplugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2016 Vincent Petry <pvince81@owncloud.com>
  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. _.extend(OC.Files.Client, {
  13. PROPERTY_COMMENTS_UNREAD: '{' + OC.Files.Client.NS_OWNCLOUD + '}comments-unread'
  14. });
  15. var TEMPLATE_COMMENTS_UNREAD =
  16. '<a class="action action-comment permanent" title="{{countMessage}}" href="#">' +
  17. '<img class="svg" src="{{iconUrl}}"/>' +
  18. '</a>';
  19. OCA.Comments = _.extend({}, OCA.Comments);
  20. if (!OCA.Comments) {
  21. /**
  22. * @namespace
  23. */
  24. OCA.Comments = {};
  25. }
  26. /**
  27. * @namespace
  28. */
  29. OCA.Comments.FilesPlugin = {
  30. ignoreLists: [
  31. 'files_trashbin',
  32. 'files.public'
  33. ],
  34. _formatCommentCount: function(count) {
  35. if (!this._commentsUnreadTemplate) {
  36. this._commentsUnreadTemplate = Handlebars.compile(TEMPLATE_COMMENTS_UNREAD);
  37. }
  38. return this._commentsUnreadTemplate({
  39. count: count,
  40. countMessage: n('comments', '%n unread comment', '%n unread comments', count),
  41. iconUrl: OC.imagePath('core', 'actions/comment')
  42. });
  43. },
  44. attach: function(fileList) {
  45. var self = this;
  46. if (this.ignoreLists.indexOf(fileList.id) >= 0) {
  47. return;
  48. }
  49. fileList.registerTabView(new OCA.Comments.CommentsTabView('commentsTabView'));
  50. var oldGetWebdavProperties = fileList._getWebdavProperties;
  51. fileList._getWebdavProperties = function() {
  52. var props = oldGetWebdavProperties.apply(this, arguments);
  53. props.push(OC.Files.Client.PROPERTY_COMMENTS_UNREAD);
  54. return props;
  55. };
  56. fileList.filesClient.addFileInfoParser(function(response) {
  57. var data = {};
  58. var props = response.propStat[0].properties;
  59. var commentsUnread = props[OC.Files.Client.PROPERTY_COMMENTS_UNREAD];
  60. if (!_.isUndefined(commentsUnread) && commentsUnread !== '') {
  61. data.commentsUnread = parseInt(commentsUnread, 10);
  62. }
  63. return data;
  64. });
  65. fileList.$el.addClass('has-comments');
  66. var oldCreateRow = fileList._createRow;
  67. fileList._createRow = function(fileData) {
  68. var $tr = oldCreateRow.apply(this, arguments);
  69. if (fileData.commentsUnread) {
  70. $tr.attr('data-comments-unread', fileData.commentsUnread);
  71. }
  72. return $tr;
  73. };
  74. // register "comment" action for reading comments
  75. fileList.fileActions.registerAction({
  76. name: 'Comment',
  77. displayName: t('comments', 'Comment'),
  78. mime: 'all',
  79. permissions: OC.PERMISSION_READ,
  80. type: OCA.Files.FileActions.TYPE_INLINE,
  81. render: function(actionSpec, isDefault, context) {
  82. var $file = context.$file;
  83. var unreadComments = $file.data('comments-unread');
  84. if (unreadComments) {
  85. var $actionLink = $(self._formatCommentCount(unreadComments));
  86. context.$file.find('a.name>span.fileactions').append($actionLink);
  87. return $actionLink;
  88. }
  89. return '';
  90. },
  91. actionHandler: function(fileName, context) {
  92. context.$file.find('.action-comment').tooltip('hide');
  93. // open sidebar in comments section
  94. context.fileList.showDetailsView(fileName, 'commentsTabView');
  95. }
  96. });
  97. // add attribute to "elementToFile"
  98. var oldElementToFile = fileList.elementToFile;
  99. fileList.elementToFile = function($el) {
  100. var fileInfo = oldElementToFile.apply(this, arguments);
  101. var commentsUnread = $el.data('comments-unread');
  102. if (commentsUnread) {
  103. fileInfo.commentsUnread = commentsUnread;
  104. }
  105. return fileInfo;
  106. };
  107. }
  108. };
  109. })();
  110. OC.Plugins.register('OCA.Files.FileList', OCA.Comments.FilesPlugin);