1 define(function(require) {
4 /* globals Node, gettext */
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 View = function(node, user) {
17 this.dom = $(_.template(commentsTemplate)());
18 this.list = this.dom.find('.list');
19 this.textarea = this.dom.find('textarea');
20 this.addButton = this.dom.find('button.btnAdd');
21 this.cancelButton = this.dom.find('button.btnCancel');
23 this.textarea.on('input', function() {
24 this.addButton.attr('disabled', this.textarea.val() === '');
26 if (this.textarea.prop('scrollHeight') > this.textarea.prop('clientHeight')) {
27 this.textarea.height(this.textarea.prop('scrollHeight'));
32 this.addButton.hide();
33 this.cancelButton.hide();
34 this.textarea.on('focus', function() {
35 this.addButton.show();
36 this.cancelButton.show();
39 this.addButton.on('click', function() {
44 this.node.document.transaction(function() {
45 var commentNode = this.node.document.createDocumentNode({tagName: 'aside', attrs: {'class': 'comment'}}),
46 metadata = commentNode.getMetadata(),
50 creator = this.user.name;
52 creator += ' (' + this.user.email + ')';
55 creator = 'anonymous';
58 metadata.add({key: 'creator', value: creator});
59 metadata.add({key: 'date', value: datetime.currentStrfmt()});
60 commentNode.append({text: this.textarea.val()});
62 this.node.append(commentNode);
65 description: gettext('Add comment')
68 this.textarea.val('');
74 this.cancelButton.on('click', function() {
75 this.addButton.hide();
76 this.cancelButton.hide();
77 this.textarea.val('');
85 _.extend(View.prototype, {
89 // while(this.node.getTagName() === 'span' && this.node.parent()) {
90 // this.node = this.node.parent();
92 this.textarea.attr('placeholder', gettext('Comment'));
95 .filter(function(child) {
96 return child.nodeType === Node.ELEMENT_NODE && child.getTagName() === 'aside' && child.getClass() === 'comment';
97 //return child.is({tag: 'aside', klass: 'comment'});
100 .forEach(function(commentNode) {
101 var commentView = new CommentView(commentNode);
102 this.list.append(commentView.dom);
103 this.textarea.attr('placeholder', gettext('Respond') + '...');
107 onActivated: function() {
108 //if(this.list.find('.comment').length === 0) {
109 this.dom.find('.newComment').toggle(true);
114 onDeactivated: function() {
115 this.dom.find('.newComment').toggle(false);
116 this.addButton.hide();
117 this.cancelButton.hide();
122 getHeight: function() {
123 return this.dom.outerHeight();
129 var CommentView = function(commentNode) {
130 this.node = commentNode;
132 var metaData = this.node.getMetadata(),
135 metaData.some(function(row) {
136 author = row.getValue();
138 author = author.split(' ')[0];
143 metaData.some(function(row) {
144 date = row.getValue();
148 this.dom = $(_.template(commentTemplate)({
149 author: author ||'?',
151 content: this.node.object.getText() || '?'
154 this.contentElement = this.dom.find('.content');
155 this.editElement = this.dom.find('.edit');
157 this.dom.find('.remove-btn').on('click', function() {
158 this.node.document.transaction(function() {
162 description: gettext('Remove comment')
167 this.dom.find('.edit-start-btn').on('click', function() {
171 this.dom.find('.edit-save-btn').on('click', function() {
175 this.dom.find('.edit-cancel-btn').on('click', function() {
176 this.cancelEditing();
179 this.textarea = this.editElement.find('textarea');
180 this.textarea.on('input', function() {
181 this.dom.find('.edit-save-btn').attr('disabled', this.textarea.val() === '');
183 if (this.textarea.prop('scrollHeight') > this.textarea.prop('clientHeight')) {
184 this.textarea.height(this.textarea.prop('scrollHeight'));
191 $.extend(CommentView.prototype, {
192 startEditing: function() {
193 this.contentElement.hide();
194 this.editElement.show();
195 this.textarea.val(this.node.object.getText());
196 if(this.textarea.prop('scrollHeight') > this.textarea.prop('clientHeight')) {
197 this.textarea.height(this.textarea.prop('scrollHeight'));
199 this.textarea.focus();
201 saveEditing: function() {
202 var newContent = this.editElement.find('textarea').val();
203 this.node.document.transaction(function() {
204 this.node.object.setText(newContent);
207 description: gettext('Edit comment')
211 cancelEditing: function() {
212 this.contentElement.show();
213 this.editElement.find('textarea').val('');
214 this.editElement.hide();