nextcloud/apps/comments/src/filesplugin.js

127 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-01-27 20:28:55 +03:00
/*
* Copyright (c) 2016 Vincent Petry <pvince81@owncloud.com>
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
(function() {
_.extend(OC.Files.Client, {
PROPERTY_COMMENTS_UNREAD: '{' + OC.Files.Client.NS_OWNCLOUD + '}comments-unread',
})
OCA.Comments = _.extend({}, OCA.Comments)
2016-01-27 20:28:55 +03:00
if (!OCA.Comments) {
/**
* @namespace
*/
OCA.Comments = {}
2016-01-27 20:28:55 +03:00
}
/**
* @namespace
*/
OCA.Comments.FilesPlugin = {
ignoreLists: [
'trashbin',
'files.public',
2016-01-27 20:28:55 +03:00
],
_formatCommentCount: function(count) {
return OCA.Comments.Templates['filesplugin']({
count: count,
countMessage: n('comments', '%n unread comment', '%n unread comments', count),
iconUrl: OC.imagePath('core', 'actions/comment'),
})
},
2016-01-27 20:28:55 +03:00
attach: function(fileList) {
const self = this
if (this.ignoreLists.indexOf(fileList.id) >= 0) {
return
2016-01-27 20:28:55 +03:00
}
fileList.registerTabView(new OCA.Comments.CommentsTabView('commentsTabView'))
const oldGetWebdavProperties = fileList._getWebdavProperties
fileList._getWebdavProperties = function() {
const props = oldGetWebdavProperties.apply(this, arguments)
props.push(OC.Files.Client.PROPERTY_COMMENTS_UNREAD)
return props
}
fileList.filesClient.addFileInfoParser(function(response) {
const data = {}
const props = response.propStat[0].properties
const commentsUnread = props[OC.Files.Client.PROPERTY_COMMENTS_UNREAD]
if (!_.isUndefined(commentsUnread) && commentsUnread !== '') {
data.commentsUnread = parseInt(commentsUnread, 10)
}
return data
})
fileList.$el.addClass('has-comments')
const oldCreateRow = fileList._createRow
fileList._createRow = function(fileData) {
const $tr = oldCreateRow.apply(this, arguments)
if (fileData.commentsUnread) {
$tr.attr('data-comments-unread', fileData.commentsUnread)
}
return $tr
}
// register "comment" action for reading comments
fileList.fileActions.registerAction({
name: 'Comment',
displayName: function(context) {
if (context && context.$file) {
const unread = parseInt(context.$file.data('comments-unread'), 10)
if (unread >= 0) {
return n('comments', '1 new comment', '{unread} new comments', unread, { unread: unread })
}
}
return t('comments', 'Comment')
},
mime: 'all',
order: -140,
iconClass: 'icon-comment',
permissions: OC.PERMISSION_READ,
type: OCA.Files.FileActions.TYPE_INLINE,
render: function(actionSpec, isDefault, context) {
const $file = context.$file
const unreadComments = $file.data('comments-unread')
if (unreadComments) {
const $actionLink = $(self._formatCommentCount(unreadComments))
context.$file.find('a.name>span.fileactions').append($actionLink)
return $actionLink
}
return ''
},
actionHandler: function(fileName, context) {
context.$file.find('.action-comment').tooltip('hide')
// open sidebar in comments section
context.fileList.showDetailsView(fileName, 'comments')
},
})
// add attribute to "elementToFile"
const oldElementToFile = fileList.elementToFile
fileList.elementToFile = function($el) {
const fileInfo = oldElementToFile.apply(this, arguments)
const commentsUnread = $el.data('comments-unread')
if (commentsUnread) {
fileInfo.commentsUnread = commentsUnread
}
return fileInfo
}
},
}
2016-01-27 20:28:55 +03:00
})()
2016-01-27 20:28:55 +03:00
OC.Plugins.register('OCA.Files.FileList', OCA.Comments.FilesPlugin)