1
0

filesplugin.js 3.2 KB

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