+var CommentView = function(commentNode) {
+ this.node = commentNode;
+
+ var metaData = this.node.getMetadata(),
+ author, date;
+
+ metaData.some(function(row) {
+ author = row.getValue();
+ if(author) {
+ author = author.split(' ')[0];
+ }
+ return true;
+ }, 'creator');
+
+ metaData.some(function(row) {
+ date = row.getValue();
+ if(/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/g.test(date)) {
+ date = date.split(':');
+ date.pop();
+ date = date.join(':');
+ }
+ return true;
+ }, 'date');
+
+ this.dom = $(_.template(commentTemplate)({
+ author: author || '',
+ date: date || '',
+ content: this.node.object.getText() || '?'
+ }));
+
+ this.contentElement = this.dom.find('.content');
+ this.editElement = this.dom.find('.edit');
+ this.deleteDialogElement = this.dom.find('.deleteDialog');
+
+ this.dom.find('.remove-btn').on('click', function() {
+ this.deleteDialogElement.show();
+ }.bind(this));
+
+ this.dom.find('.deleteDialog-confirm').on('click', function() {
+ this.node.document.transaction(function() {
+ this.node.detach();
+ }.bind(this), {
+ metadata: {
+ description: gettext('Remove comment')
+ }
+ });
+ }.bind(this));
+
+ this.dom.find('.deleteDialog-cancel').on('click', function() {
+ this.deleteDialogElement.hide();
+ }.bind(this));
+
+ this.dom.find('.edit-start-btn').on('click', function() {
+ this.startEditing();
+ }.bind(this));
+
+ this.dom.find('.edit-save-btn').on('click', function() {
+ this.saveEditing();
+ }.bind(this));
+
+ this.dom.find('.edit-cancel-btn').on('click', function() {
+ this.cancelEditing();
+ }.bind(this));
+
+ this.textarea = this.editElement.find('textarea');
+ this.textarea.on('input', function() {
+ this.dom.find('.edit-save-btn').attr('disabled', this.textarea.val() === '');
+ }.bind(this));
+ makeAutoResizable(this.textarea);
+};
+
+$.extend(CommentView.prototype, {
+ startEditing: function() {
+ this.contentElement.hide();
+ this.editElement.show();
+ this.textarea.val(this.node.object.getText());
+ resize(this.textarea);
+ this.textarea.focus();
+ },
+ saveEditing: function() {
+ var newContent = this.editElement.find('textarea').val();
+ this.node.document.transaction(function() {
+ this.node.object.setText(newContent);
+ }.bind(this), {
+ metadata: {
+ description: gettext('Edit comment')
+ }
+ });
+ },
+ cancelEditing: function() {
+ this.contentElement.show();
+ this.editElement.find('textarea').val('');
+ this.editElement.hide();
+ },