1 define(function(require) {
7 var $ = require('libs/jquery'),
8 _ = require('libs/underscore'),
9 datetime = require('fnpjs/datetime'),
10 commentsTemplate = require('libs/text!./comments.html'),
11 commentTemplate = require('libs/text!./comment.html');
14 var makeAutoResizable = function(textarea) {
15 textarea.on('input', function() {
20 var resize = function(textarea) {
21 if(textarea.prop('scrollHeight') > textarea.prop('clientHeight')) {
22 textarea.height(textarea.prop('scrollHeight'));
27 var CommentsView = function(node, user) {
30 this.dom = $(_.template(commentsTemplate)());
31 this.list = this.dom.find('.list');
32 this.textarea = this.dom.find('textarea');
33 this.addButton = this.dom.find('button.btnAdd');
34 this.cancelButton = this.dom.find('button.btnCancel');
36 this.textarea.on('input', function() {
37 this.addButton.attr('disabled', this.textarea.val() === '');
39 makeAutoResizable(this.textarea);
41 this.addButton.hide();
42 this.cancelButton.hide();
43 this.textarea.on('focus', function() {
44 this.addButton.show();
45 this.cancelButton.show();
48 this.addButton.on('click', function() {
53 this.node.document.transaction(function() {
54 var commentNode = this.node.document.createDocumentNode({tagName: 'aside', attrs: {'class': 'comment'}}),
55 metadata = commentNode.getMetadata(),
59 creator = this.user.name;
61 creator += ' (' + this.user.email + ')';
64 creator = 'anonymous';
67 metadata.add({key: 'creator', value: creator});
68 metadata.add({key: 'date', value: datetime.currentStrfmt()});
69 commentNode.append({text: this.textarea.val()});
71 this.node.append(commentNode);
74 description: gettext('Add comment')
77 this.textarea.val('');
83 this.cancelButton.on('click', function() {
84 this.addButton.hide();
85 this.cancelButton.hide();
86 this.textarea.val('');
94 _.extend(CommentsView.prototype, {
97 this.textarea.attr('placeholder', gettext('Comment'));
100 .filter(function(child) {
101 return child.is({tag: 'aside', klass: 'comment'});
103 .forEach(function(commentNode) {
104 var commentView = new CommentView(commentNode);
105 this.list.append(commentView.dom);
106 this.textarea.attr('placeholder', gettext('Respond') + '...');
109 onActivated: function() {
110 this.dom.find('.newComment').toggle(true);
112 onDeactivated: function() {
113 this.dom.find('.newComment').toggle(false);
114 this.addButton.hide();
115 this.cancelButton.hide();
120 var CommentView = function(commentNode) {
121 this.node = commentNode;
123 var metaData = this.node.getMetadata(),
126 metaData.some(function(row) {
127 author = row.getValue();
129 author = author.split(' ')[0];
134 metaData.some(function(row) {
135 date = row.getValue();
136 if(/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/g.test(date)) {
137 date = date.split(':');
139 date = date.join(':');
144 this.dom = $(_.template(commentTemplate)({
145 author: author || '',
147 content: this.node.object.getText() || '?'
150 this.contentElement = this.dom.find('.content');
151 this.editElement = this.dom.find('.edit');
152 this.deleteDialogElement = this.dom.find('.deleteDialog');
154 this.dom.find('.remove-btn').on('click', function() {
155 this.deleteDialogElement.show();
158 this.dom.find('.deleteDialog-confirm').on('click', function() {
159 this.node.document.transaction(function() {
163 description: gettext('Remove comment')
168 this.dom.find('.deleteDialog-cancel').on('click', function() {
169 this.deleteDialogElement.hide();
172 this.dom.find('.edit-start-btn').on('click', function() {
176 this.dom.find('.edit-save-btn').on('click', function() {
180 this.dom.find('.edit-cancel-btn').on('click', function() {
181 this.cancelEditing();
184 this.textarea = this.editElement.find('textarea');
185 this.textarea.on('input', function() {
186 this.dom.find('.edit-save-btn').attr('disabled', this.textarea.val() === '');
188 makeAutoResizable(this.textarea);
191 $.extend(CommentView.prototype, {
192 startEditing: function() {
193 this.contentElement.hide();
194 this.editElement.show();
195 this.textarea.val(this.node.object.getText());
196 resize(this.textarea);
197 this.textarea.focus();
199 saveEditing: function() {
200 var newContent = this.editElement.find('textarea').val();
201 this.node.document.transaction(function() {
202 this.node.object.setText(newContent);
205 description: gettext('Edit comment')
209 cancelEditing: function() {
210 this.contentElement.show();
211 this.editElement.find('textarea').val('');
212 this.editElement.hide();