2016-01-27 17:28:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-02-17 14:19:42 +00:00
|
|
|
/* global Handlebars, escapeHTML */
|
2016-02-03 17:44:14 +00:00
|
|
|
|
2016-02-01 15:37:33 +00:00
|
|
|
(function(OC, OCA) {
|
2016-01-27 17:28:55 +00:00
|
|
|
var TEMPLATE =
|
2016-02-03 17:44:14 +00:00
|
|
|
'<ul class="comments">' +
|
|
|
|
'</ul>' +
|
2016-08-03 11:18:09 +00:00
|
|
|
'<div class="emptycontent hidden"><div class="icon-comment"></div>' +
|
|
|
|
'<p>{{emptyResultLabel}}</p></div>' +
|
2016-02-03 17:44:14 +00:00
|
|
|
'<input type="button" class="showMore hidden" value="{{moreLabel}}"' +
|
|
|
|
' name="show-more" id="show-more" />' +
|
|
|
|
'<div class="loading hidden" style="height: 50px"></div>';
|
|
|
|
|
|
|
|
var EDIT_COMMENT_TEMPLATE =
|
|
|
|
'<div class="newCommentRow comment" data-id="{{id}}">' +
|
2016-02-01 15:37:33 +00:00
|
|
|
' <div class="authorRow">' +
|
2016-02-03 17:44:14 +00:00
|
|
|
' <div class="avatar" data-username="{{actorId}}"></div>' +
|
|
|
|
' <div class="author">{{actorDisplayName}}</div>' +
|
|
|
|
'{{#if isEditMode}}' +
|
2016-02-03 19:32:37 +00:00
|
|
|
' <a href="#" class="action delete icon icon-delete has-tooltip" title="{{deleteTooltip}}"></a>' +
|
2016-02-03 17:44:14 +00:00
|
|
|
'{{/if}}' +
|
2016-02-01 15:37:33 +00:00
|
|
|
' </div>' +
|
|
|
|
' <form class="newCommentForm">' +
|
2016-09-14 12:20:26 +00:00
|
|
|
' <textarea rows="1" class="message" placeholder="{{newMessagePlaceholder}}">{{message}}</textarea>' +
|
2016-08-03 11:18:09 +00:00
|
|
|
' <input class="submit icon-confirm" type="submit" value="" />' +
|
2016-02-03 19:32:37 +00:00
|
|
|
'{{#if isEditMode}}' +
|
2016-08-18 07:07:03 +00:00
|
|
|
' <input class="cancel pull-right" type="button" value="{{cancelText}}" />' +
|
2016-02-03 19:32:37 +00:00
|
|
|
'{{/if}}' +
|
2016-02-02 11:06:12 +00:00
|
|
|
' <div class="submitLoading icon-loading-small hidden"></div>'+
|
2016-02-01 15:37:33 +00:00
|
|
|
' </form>' +
|
2016-02-03 17:44:14 +00:00
|
|
|
'</div>';
|
2016-01-27 17:28:55 +00:00
|
|
|
|
|
|
|
var COMMENT_TEMPLATE =
|
2016-02-04 17:20:16 +00:00
|
|
|
'<li class="comment{{#if isUnread}} unread{{/if}}{{#if isLong}} collapsed{{/if}}" data-id="{{id}}">' +
|
2016-02-01 15:37:33 +00:00
|
|
|
' <div class="authorRow">' +
|
2016-02-04 10:26:11 +00:00
|
|
|
' <div class="avatar" {{#if actorId}}data-username="{{actorId}}"{{/if}}> </div>' +
|
2016-02-01 15:37:33 +00:00
|
|
|
' <div class="author">{{actorDisplayName}}</div>' +
|
2016-02-03 17:44:14 +00:00
|
|
|
'{{#if isUserAuthor}}' +
|
|
|
|
' <a href="#" class="action edit icon icon-rename has-tooltip" title="{{editTooltip}}"></a>' +
|
|
|
|
'{{/if}}' +
|
2016-08-31 15:46:50 +00:00
|
|
|
' <div class="date has-tooltip live-relative-timestamp" data-timestamp="{{timestamp}}" title="{{altDate}}">{{date}}</div>' +
|
2016-02-01 15:37:33 +00:00
|
|
|
' </div>' +
|
2016-02-01 15:54:27 +00:00
|
|
|
' <div class="message">{{{formattedMessage}}}</div>' +
|
2016-02-04 17:20:16 +00:00
|
|
|
'{{#if isLong}}' +
|
|
|
|
' <div class="message-overlay"></div>' +
|
|
|
|
'{{/if}}' +
|
2016-01-27 17:28:55 +00:00
|
|
|
'</li>';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof OCA.Comments
|
|
|
|
*/
|
|
|
|
var CommentsTabView = OCA.Files.DetailTabView.extend(
|
|
|
|
/** @lends OCA.Comments.CommentsTabView.prototype */ {
|
|
|
|
id: 'commentsTabView',
|
|
|
|
className: 'tab commentsTabView',
|
|
|
|
|
|
|
|
events: {
|
2016-01-28 13:24:12 +00:00
|
|
|
'submit .newCommentForm': '_onSubmitComment',
|
2016-02-03 17:44:14 +00:00
|
|
|
'click .showMore': '_onClickShowMore',
|
|
|
|
'click .action.edit': '_onClickEditComment',
|
|
|
|
'click .action.delete': '_onClickDeleteComment',
|
2016-02-04 17:20:16 +00:00
|
|
|
'click .cancel': '_onClickCloseComment',
|
|
|
|
'click .comment': '_onClickComment'
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
2016-02-05 14:45:30 +00:00
|
|
|
_commentMaxLength: 1000,
|
|
|
|
|
2016-01-27 17:28:55 +00:00
|
|
|
initialize: function() {
|
|
|
|
OCA.Files.DetailTabView.prototype.initialize.apply(this, arguments);
|
2016-02-02 14:13:45 +00:00
|
|
|
this.collection = new OCA.Comments.CommentCollection();
|
2016-01-27 17:28:55 +00:00
|
|
|
this.collection.on('request', this._onRequest, this);
|
|
|
|
this.collection.on('sync', this._onEndRequest, this);
|
|
|
|
this.collection.on('add', this._onAddModel, this);
|
2016-02-01 15:37:33 +00:00
|
|
|
|
2016-02-05 14:45:30 +00:00
|
|
|
this._commentMaxThreshold = this._commentMaxLength * 0.9;
|
|
|
|
|
2016-01-27 17:28:55 +00:00
|
|
|
// TODO: error handling
|
2016-02-05 14:45:30 +00:00
|
|
|
_.bindAll(this, '_onTypeComment');
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
template: function(params) {
|
|
|
|
if (!this._template) {
|
|
|
|
this._template = Handlebars.compile(TEMPLATE);
|
|
|
|
}
|
2016-02-01 15:37:33 +00:00
|
|
|
var currentUser = OC.getCurrentUser();
|
2016-01-27 17:28:55 +00:00
|
|
|
return this._template(_.extend({
|
2016-02-03 17:44:14 +00:00
|
|
|
actorId: currentUser.uid,
|
|
|
|
actorDisplayName: currentUser.displayName
|
|
|
|
}, params));
|
|
|
|
},
|
|
|
|
|
|
|
|
editCommentTemplate: function(params) {
|
|
|
|
if (!this._editCommentTemplate) {
|
|
|
|
this._editCommentTemplate = Handlebars.compile(EDIT_COMMENT_TEMPLATE);
|
|
|
|
}
|
|
|
|
var currentUser = OC.getCurrentUser();
|
|
|
|
return this._editCommentTemplate(_.extend({
|
2016-02-03 19:32:37 +00:00
|
|
|
actorId: currentUser.uid,
|
|
|
|
actorDisplayName: currentUser.displayName,
|
2016-08-03 11:18:09 +00:00
|
|
|
newMessagePlaceholder: t('comments', 'New comment …'),
|
2016-02-03 19:32:37 +00:00
|
|
|
deleteTooltip: t('comments', 'Delete comment'),
|
|
|
|
submitText: t('comments', 'Post'),
|
|
|
|
cancelText: t('comments', 'Cancel')
|
2016-01-27 17:28:55 +00:00
|
|
|
}, params));
|
|
|
|
},
|
|
|
|
|
|
|
|
commentTemplate: function(params) {
|
|
|
|
if (!this._commentTemplate) {
|
|
|
|
this._commentTemplate = Handlebars.compile(COMMENT_TEMPLATE);
|
|
|
|
}
|
2016-02-04 10:26:11 +00:00
|
|
|
|
|
|
|
params = _.extend({
|
2016-02-03 17:44:14 +00:00
|
|
|
editTooltip: t('comments', 'Edit comment'),
|
2016-02-04 17:20:16 +00:00
|
|
|
isUserAuthor: OC.getCurrentUser().uid === params.actorId,
|
|
|
|
isLong: this._isLong(params.message)
|
2016-02-04 10:26:11 +00:00
|
|
|
}, params);
|
|
|
|
|
|
|
|
if (params.actorType === 'deleted_users') {
|
|
|
|
// makes the avatar a X
|
|
|
|
params.actorId = null;
|
|
|
|
params.actorDisplayName = t('comments', '[Deleted user]');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._commentTemplate(params);
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getLabel: function() {
|
|
|
|
return t('comments', 'Comments');
|
|
|
|
},
|
|
|
|
|
|
|
|
setFileInfo: function(fileInfo) {
|
|
|
|
if (fileInfo) {
|
2016-02-03 15:18:14 +00:00
|
|
|
this.model = fileInfo;
|
2016-01-27 17:28:55 +00:00
|
|
|
this.render();
|
|
|
|
this.collection.setObjectId(fileInfo.id);
|
|
|
|
// reset to first page
|
|
|
|
this.collection.reset([], {silent: true});
|
|
|
|
this.nextPage();
|
|
|
|
} else {
|
2016-02-03 15:18:14 +00:00
|
|
|
this.model = null;
|
2016-01-27 17:28:55 +00:00
|
|
|
this.render();
|
|
|
|
this.collection.reset();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
this.$el.html(this.template({
|
2016-08-03 11:18:09 +00:00
|
|
|
emptyResultLabel: t('comments', 'No comments yet, start the conversation!'),
|
|
|
|
moreLabel: t('comments', 'More comments …')
|
2016-01-27 17:28:55 +00:00
|
|
|
}));
|
2016-02-03 17:44:14 +00:00
|
|
|
this.$el.find('.comments').before(this.editCommentTemplate({}));
|
2016-01-27 17:28:55 +00:00
|
|
|
this.$el.find('.has-tooltip').tooltip();
|
|
|
|
this.$container = this.$el.find('ul.comments');
|
2017-02-13 23:49:05 +00:00
|
|
|
this.$el.find('.avatar').avatar(OC.getCurrentUser().uid, 32);
|
2016-01-27 17:28:55 +00:00
|
|
|
this.delegateEvents();
|
2016-08-03 11:18:09 +00:00
|
|
|
this.$el.find('.message').on('keydown input change', this._onTypeComment);
|
2016-09-14 12:20:26 +00:00
|
|
|
|
|
|
|
autosize(this.$el.find('.newCommentRow textarea'))
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_formatItem: function(commentModel) {
|
2016-01-28 10:47:23 +00:00
|
|
|
var timestamp = new Date(commentModel.get('creationDateTime')).getTime();
|
|
|
|
var data = _.extend({
|
2016-08-31 15:46:50 +00:00
|
|
|
timestamp: timestamp,
|
2016-01-28 10:47:23 +00:00
|
|
|
date: OC.Util.relativeModifiedDate(timestamp),
|
2016-02-01 15:54:27 +00:00
|
|
|
altDate: OC.Util.formatDate(timestamp),
|
2016-10-17 12:06:23 +00:00
|
|
|
formattedMessage: this._formatMessage(commentModel.get('message'), commentModel.get('mentions'))
|
2016-01-28 10:47:23 +00:00
|
|
|
}, commentModel.attributes);
|
|
|
|
return data;
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_toggleLoading: function(state) {
|
|
|
|
this._loading = state;
|
|
|
|
this.$el.find('.loading').toggleClass('hidden', !state);
|
|
|
|
},
|
|
|
|
|
2016-02-03 19:32:37 +00:00
|
|
|
_onRequest: function(type) {
|
|
|
|
if (type === 'REPORT') {
|
|
|
|
this._toggleLoading(true);
|
|
|
|
this.$el.find('.showMore').addClass('hidden');
|
|
|
|
}
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
2016-02-03 15:18:14 +00:00
|
|
|
_onEndRequest: function(type) {
|
|
|
|
var fileInfoModel = this.model;
|
2016-01-27 17:28:55 +00:00
|
|
|
this._toggleLoading(false);
|
2016-08-03 11:18:09 +00:00
|
|
|
this.$el.find('.emptycontent').toggleClass('hidden', !!this.collection.length);
|
2016-01-27 17:28:55 +00:00
|
|
|
this.$el.find('.showMore').toggleClass('hidden', !this.collection.hasMoreResults());
|
2016-02-03 15:18:14 +00:00
|
|
|
|
|
|
|
if (type !== 'REPORT') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find first unread comment
|
|
|
|
var firstUnreadComment = this.collection.findWhere({isUnread: true});
|
|
|
|
if (firstUnreadComment) {
|
|
|
|
// update read marker
|
|
|
|
this.collection.updateReadMarker(
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
success: function() {
|
|
|
|
fileInfoModel.set('commentsUnread', 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onAddModel: function(model, collection, options) {
|
|
|
|
var $el = $(this.commentTemplate(this._formatItem(model)));
|
|
|
|
if (!_.isUndefined(options.at) && collection.length > 1) {
|
|
|
|
this.$container.find('li').eq(options.at).before($el);
|
|
|
|
} else {
|
|
|
|
this.$container.append($el);
|
|
|
|
}
|
2016-02-01 15:37:33 +00:00
|
|
|
|
|
|
|
this._postRenderItem($el);
|
|
|
|
},
|
|
|
|
|
|
|
|
_postRenderItem: function($el) {
|
2016-01-28 10:47:23 +00:00
|
|
|
$el.find('.has-tooltip').tooltip();
|
2017-02-13 23:49:05 +00:00
|
|
|
$el.find('.avatar').each(function() {
|
|
|
|
var $this = $(this);
|
|
|
|
$this.avatar($this.attr('data-username'), 32);
|
|
|
|
});
|
2017-04-24 17:52:45 +00:00
|
|
|
|
|
|
|
var username = $el.find('.avatar').data('username');
|
|
|
|
if (username !== oc_current_user) {
|
|
|
|
$el.find('.authorRow .avatar, .authorRow .author').contactsMenu(
|
|
|
|
username, 0, $el.find('.authorRow'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var message = $el.find('.message');
|
|
|
|
message.find('.avatar').each(function() {
|
|
|
|
var avatar = $(this);
|
|
|
|
var strong = $(this).next();
|
|
|
|
var appendTo = $(this).parent();
|
|
|
|
|
|
|
|
$.merge(avatar, strong).contactsMenu(avatar.data('user'), 0, appendTo);
|
|
|
|
});
|
2016-01-27 17:28:55 +00:00
|
|
|
},
|
|
|
|
|
2016-02-01 15:54:27 +00:00
|
|
|
/**
|
|
|
|
* Convert a message to be displayed in HTML,
|
|
|
|
* converts newlines to <br> tags.
|
|
|
|
*/
|
2016-10-17 12:06:23 +00:00
|
|
|
_formatMessage: function(message, mentions) {
|
|
|
|
message = escapeHTML(message).replace(/\n/g, '<br/>');
|
|
|
|
|
|
|
|
for(var i in mentions) {
|
|
|
|
var mention = '@' + mentions[i].mentionId;
|
2016-10-17 16:19:51 +00:00
|
|
|
|
2017-02-13 23:49:05 +00:00
|
|
|
var avatar = '<div class="avatar" '
|
|
|
|
+ 'data-user="' + _.escape(mentions[i].mentionId) + '"'
|
|
|
|
+' data-user-display-name="'
|
|
|
|
+ _.escape(mentions[i].mentionDisplayName) + '"></div>';
|
2016-10-17 16:19:51 +00:00
|
|
|
|
|
|
|
// escape possible regex characters in the name
|
2016-10-17 12:06:23 +00:00
|
|
|
mention = mention.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
2017-04-24 17:52:45 +00:00
|
|
|
var displayName = ''
|
|
|
|
+ '<span class="avatar-name-wrapper">'
|
|
|
|
+ avatar + ' <strong>'+ _.escape(mentions[i].mentionDisplayName)+'</strong>'
|
|
|
|
+ '</span>';
|
2016-10-17 16:19:51 +00:00
|
|
|
|
|
|
|
// replace every mention either at the start of the input or after a whitespace
|
|
|
|
// followed by a non-word character.
|
2016-10-18 15:05:56 +00:00
|
|
|
message = message.replace(new RegExp("(^|\\s)(" + mention + ")\\b", 'g'),
|
2016-10-17 16:19:51 +00:00
|
|
|
function(match, p1) {
|
|
|
|
// to get number of whitespaces (0 vs 1) right
|
|
|
|
return p1+displayName;
|
|
|
|
}
|
|
|
|
);
|
2016-10-17 12:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
2016-02-01 15:54:27 +00:00
|
|
|
},
|
|
|
|
|
2016-01-27 17:28:55 +00:00
|
|
|
nextPage: function() {
|
|
|
|
if (this._loading || !this.collection.hasMoreResults()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.collection.fetchNext();
|
|
|
|
},
|
|
|
|
|
2016-02-03 17:44:14 +00:00
|
|
|
_onClickEditComment: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
var $comment = $(ev.target).closest('.comment');
|
|
|
|
var commentId = $comment.data('id');
|
|
|
|
var commentToEdit = this.collection.get(commentId);
|
|
|
|
var $formRow = $(this.editCommentTemplate(_.extend({
|
|
|
|
isEditMode: true,
|
|
|
|
submitText: t('comments', 'Save')
|
|
|
|
}, commentToEdit.attributes)));
|
|
|
|
|
2016-02-04 17:20:16 +00:00
|
|
|
$comment.addClass('hidden').removeClass('collapsed');
|
2016-02-03 17:44:14 +00:00
|
|
|
// spawn form
|
|
|
|
$comment.after($formRow);
|
|
|
|
$formRow.data('commentEl', $comment);
|
2016-05-03 10:22:46 +00:00
|
|
|
$formRow.find('textarea').on('keydown input change', this._onTypeComment);
|
2016-02-03 17:44:14 +00:00
|
|
|
|
|
|
|
// copy avatar element from original to avoid flickering
|
2016-10-21 20:57:06 +00:00
|
|
|
$formRow.find('.avatar:first').replaceWith($comment.find('.avatar:first').clone());
|
2016-02-03 19:32:37 +00:00
|
|
|
$formRow.find('.has-tooltip').tooltip();
|
2016-02-03 17:44:14 +00:00
|
|
|
|
2016-09-14 12:20:26 +00:00
|
|
|
// Enable autosize
|
|
|
|
autosize($formRow.find('textarea'));
|
|
|
|
|
2016-02-03 17:44:14 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2016-02-05 14:45:30 +00:00
|
|
|
_onTypeComment: function(ev) {
|
|
|
|
var $field = $(ev.target);
|
|
|
|
var len = $field.val().length;
|
|
|
|
var $submitButton = $field.data('submitButtonEl');
|
|
|
|
if (!$submitButton) {
|
|
|
|
$submitButton = $field.closest('form').find('.submit');
|
|
|
|
$field.data('submitButtonEl', $submitButton);
|
|
|
|
}
|
|
|
|
$field.tooltip('hide');
|
|
|
|
if (len > this._commentMaxThreshold) {
|
|
|
|
$field.attr('data-original-title', t('comments', 'Allowed characters {count} of {max}', {count: len, max: this._commentMaxLength}));
|
|
|
|
$field.tooltip({trigger: 'manual'});
|
|
|
|
$field.tooltip('show');
|
|
|
|
$field.addClass('error');
|
|
|
|
}
|
2016-02-17 14:19:42 +00:00
|
|
|
|
2016-02-05 14:45:30 +00:00
|
|
|
var limitExceeded = (len > this._commentMaxLength);
|
|
|
|
$field.toggleClass('error', limitExceeded);
|
|
|
|
$submitButton.prop('disabled', limitExceeded);
|
2016-05-02 17:52:13 +00:00
|
|
|
|
2016-05-03 10:22:46 +00:00
|
|
|
//submits form on ctrl+Enter or cmd+Enter
|
|
|
|
if (ev.keyCode === 13 && (ev.ctrlKey || ev.metaKey)) {
|
2016-05-02 17:52:13 +00:00
|
|
|
$submitButton.click();
|
2016-05-03 10:29:43 +00:00
|
|
|
}
|
2016-02-05 14:45:30 +00:00
|
|
|
},
|
|
|
|
|
2016-02-04 17:20:16 +00:00
|
|
|
_onClickComment: function(ev) {
|
|
|
|
var $row = $(ev.target);
|
|
|
|
if (!$row.is('.comment')) {
|
|
|
|
$row = $row.closest('.comment');
|
|
|
|
}
|
|
|
|
$row.removeClass('collapsed');
|
|
|
|
},
|
|
|
|
|
2016-02-03 17:44:14 +00:00
|
|
|
_onClickCloseComment: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
var $row = $(ev.target).closest('.comment');
|
|
|
|
$row.data('commentEl').removeClass('hidden');
|
|
|
|
$row.remove();
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onClickDeleteComment: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
var $comment = $(ev.target).closest('.comment');
|
|
|
|
var commentId = $comment.data('id');
|
2016-02-03 19:32:37 +00:00
|
|
|
var $loading = $comment.find('.submitLoading');
|
2016-02-03 17:44:14 +00:00
|
|
|
|
|
|
|
$comment.addClass('disabled');
|
2016-02-03 19:32:37 +00:00
|
|
|
$loading.removeClass('hidden');
|
2016-02-03 17:44:14 +00:00
|
|
|
this.collection.get(commentId).destroy({
|
|
|
|
success: function() {
|
2016-02-03 19:32:37 +00:00
|
|
|
$comment.data('commentEl').remove();
|
2016-02-03 17:44:14 +00:00
|
|
|
$comment.remove();
|
|
|
|
},
|
2016-06-24 08:10:10 +00:00
|
|
|
error: function() {
|
2016-02-03 19:32:37 +00:00
|
|
|
$loading.addClass('hidden');
|
2016-02-03 17:44:14 +00:00
|
|
|
$comment.removeClass('disabled');
|
2016-06-24 08:10:10 +00:00
|
|
|
OC.Notification.showTemporary(t('comments', 'Error occurred while retrieving comment with id {id}', {id: commentId}));
|
2016-02-03 17:44:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2016-01-28 13:24:12 +00:00
|
|
|
_onClickShowMore: function(ev) {
|
2016-01-27 17:28:55 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
this.nextPage();
|
|
|
|
},
|
|
|
|
|
2016-10-19 11:56:49 +00:00
|
|
|
/**
|
|
|
|
* takes care of updating comment elements after submit (either new
|
|
|
|
* comment or edit).
|
|
|
|
*
|
|
|
|
* @param {OC.Backbone.Model} model
|
|
|
|
* @param {jQuery} $form
|
|
|
|
* @param {string|undefined} commentId
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-17 12:06:23 +00:00
|
|
|
_onSubmitSuccess: function(model, $form, commentId) {
|
|
|
|
var self = this;
|
|
|
|
var $submit = $form.find('.submit');
|
|
|
|
var $loading = $form.find('.submitLoading');
|
|
|
|
var $textArea = $form.find('.message');
|
|
|
|
|
|
|
|
model.fetch({
|
|
|
|
success: function(model) {
|
|
|
|
$submit.removeClass('hidden');
|
|
|
|
$loading.addClass('hidden');
|
2016-10-19 11:56:49 +00:00
|
|
|
var $target;
|
|
|
|
|
2016-10-17 12:06:23 +00:00
|
|
|
if(!_.isUndefined(commentId)) {
|
|
|
|
var $row = $form.closest('.comment');
|
2016-10-19 11:56:49 +00:00
|
|
|
$target = $row.data('commentEl');
|
|
|
|
$target.removeClass('hidden');
|
2016-10-17 12:06:23 +00:00
|
|
|
$row.remove();
|
|
|
|
} else {
|
2016-10-19 11:56:49 +00:00
|
|
|
$target = $('.commentsTabView .comments').find('li:first');
|
2016-10-17 12:06:23 +00:00
|
|
|
$textArea.val('').prop('disabled', false);
|
|
|
|
}
|
2016-10-19 10:47:53 +00:00
|
|
|
|
2016-10-19 11:56:49 +00:00
|
|
|
$target.find('.message')
|
|
|
|
.html(self._formatMessage(model.get('message'), model.get('mentions')))
|
|
|
|
.find('.avatar')
|
|
|
|
.each(function () { $(this).avatar(); });
|
2016-10-17 12:06:23 +00:00
|
|
|
},
|
|
|
|
error: function () {
|
2016-10-19 11:56:49 +00:00
|
|
|
self._onSubmitError($form, commentId);
|
2016-10-17 12:06:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-01-27 17:28:55 +00:00
|
|
|
_onSubmitComment: function(e) {
|
2016-02-03 17:44:14 +00:00
|
|
|
var self = this;
|
2016-02-02 11:06:12 +00:00
|
|
|
var $form = $(e.target);
|
2016-02-03 17:44:14 +00:00
|
|
|
var commentId = $form.closest('.comment').data('id');
|
2016-02-01 15:37:33 +00:00
|
|
|
var currentUser = OC.getCurrentUser();
|
2016-02-02 11:06:12 +00:00
|
|
|
var $submit = $form.find('.submit');
|
|
|
|
var $loading = $form.find('.submitLoading');
|
2016-08-03 11:18:09 +00:00
|
|
|
var $textArea = $form.find('.message');
|
2016-02-02 11:06:12 +00:00
|
|
|
var message = $textArea.val().trim();
|
2016-01-27 17:28:55 +00:00
|
|
|
e.preventDefault();
|
2016-02-02 11:06:12 +00:00
|
|
|
|
2016-02-05 14:45:30 +00:00
|
|
|
if (!message.length || message.length > this._commentMaxLength) {
|
2016-02-02 11:06:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$textArea.prop('disabled', true);
|
|
|
|
$submit.addClass('hidden');
|
|
|
|
$loading.removeClass('hidden');
|
|
|
|
|
2016-02-03 17:44:14 +00:00
|
|
|
if (commentId) {
|
|
|
|
// edit mode
|
|
|
|
var comment = this.collection.get(commentId);
|
|
|
|
comment.save({
|
|
|
|
message: $textArea.val()
|
|
|
|
}, {
|
|
|
|
success: function(model) {
|
2016-10-17 12:06:23 +00:00
|
|
|
self._onSubmitSuccess(model, $form, commentId);
|
2016-02-03 17:44:14 +00:00
|
|
|
},
|
2016-06-24 08:10:10 +00:00
|
|
|
error: function() {
|
2016-10-19 11:56:49 +00:00
|
|
|
self._onSubmitError($form, commentId);
|
2016-02-03 17:44:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.collection.create({
|
|
|
|
actorId: currentUser.uid,
|
|
|
|
actorDisplayName: currentUser.displayName,
|
|
|
|
actorType: 'users',
|
|
|
|
verb: 'comment',
|
|
|
|
message: $textArea.val(),
|
|
|
|
creationDateTime: (new Date()).toUTCString()
|
|
|
|
}, {
|
|
|
|
at: 0,
|
|
|
|
// wait for real creation before adding
|
|
|
|
wait: true,
|
2016-10-17 12:06:23 +00:00
|
|
|
success: function(model) {
|
|
|
|
self._onSubmitSuccess(model, $form);
|
2016-02-03 17:44:14 +00:00
|
|
|
},
|
2016-06-24 08:10:10 +00:00
|
|
|
error: function() {
|
2016-10-19 11:56:49 +00:00
|
|
|
self._onSubmitError($form);
|
2016-02-03 17:44:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-01-27 17:28:55 +00:00
|
|
|
|
|
|
|
return false;
|
2016-02-04 17:20:16 +00:00
|
|
|
},
|
|
|
|
|
2016-10-19 11:56:49 +00:00
|
|
|
/**
|
|
|
|
* takes care of updating the UI after an error on submit (either new
|
|
|
|
* comment or edit).
|
|
|
|
*
|
|
|
|
* @param {jQuery} $form
|
|
|
|
* @param {string|undefined} commentId
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onSubmitError: function($form, commentId) {
|
|
|
|
$form.find('.submit').removeClass('hidden');
|
|
|
|
$form.find('.submitLoading').addClass('hidden');
|
|
|
|
$form.find('.message').prop('disabled', false);
|
|
|
|
|
|
|
|
if(!_.isUndefined(commentId)) {
|
2017-02-14 20:26:00 +00:00
|
|
|
OC.Notification.show(t('comments', 'Error occurred while updating comment with id {id}', {id: commentId}), {type: 'error'});
|
2016-10-19 11:56:49 +00:00
|
|
|
} else {
|
2017-02-14 20:26:00 +00:00
|
|
|
OC.Notification.show(t('comments', 'Error occurred while posting comment'), {type: 'error'});
|
2016-10-19 11:56:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-04 17:20:16 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the given message is long and needs
|
|
|
|
* collapsing
|
|
|
|
*/
|
|
|
|
_isLong: function(message) {
|
|
|
|
return message.length > 250 || (message.match(/\n/g) || []).length > 1;
|
2016-01-27 17:28:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
OCA.Comments.CommentsTabView = CommentsTabView;
|
2016-02-01 15:37:33 +00:00
|
|
|
})(OC, OCA);
|