filesplugin.js 3.4 KB

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