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