8d13582be4304ebc3531e281f703a9ec280104fb
[moodle.git] / comment / comment.js
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Comment Helper
18  * @author Dongsheng Cai <dongsheng@moodle.com>
19  */
20 M.core_comment = {
21     /**
22      * Initialize commenting system
23      */
24     init: function(Y, options) {
25         var CommentHelper = function(args) {
26             CommentHelper.superclass.constructor.apply(this, arguments);
27         }
28         CommentHelper.NAME = "COMMENT";
29         CommentHelper.ATTRS = {
30             options: {},
31             lang: {}
32         };
33         Y.extend(CommentHelper, Y.Base, {
34             api: M.cfg.wwwroot+'/comment/comment_ajax.php',
35             initializer: function(args) {
36                 var scope = this;
37                 this.client_id = args.client_id;
38                 this.itemid = args.itemid;
39                 this.commentarea = args.commentarea;
40                 this.courseid = args.courseid;
41                 this.contextid = args.contextid;
42                 this.env = args.env;
43                 // expand comments?
44                 if (args.autostart) {
45                     this.view(args.page);
46                 }
47                 // hide toggle link
48                 if (args.notoggle) {
49                     Y.one('#comment-link-'+this.client_id).setStyle('display', 'none');
50                 }
51                 // load comments
52                 var handle = Y.one('#comment-link-'+this.client_id);
53                 if (handle) {
54                     handle.on('click', function(e) {
55                         e.preventDefault();
56                         this.view(0);
57                         return false;
58                     }, this);
59                 }
60                 CommentHelper.confirmoverlay = new Y.Overlay({
61 bodyContent: '<div class="comment-delete-confirm"><a href="#" id="confirmdelete-'+this.client_id+'">'+M.str.moodle.yes+'</a> <a href="#" id="canceldelete-'+this.client_id+'">'+M.str.moodle.no+'</a></div>',
62                                         visible: false
63                                         });
64                 CommentHelper.confirmoverlay.render(document.body);
65             },
66             post: function() {
67                 var ta = Y.one('#dlg-content-'+this.client_id);
68                 var scope = this;
69                 var value = ta.get('value');
70                 if (value && value != M.str.moodle.addcomment) {
71                     var params = {'content': value};
72                     this.request({
73                         action: 'add',
74                         scope: scope,
75                         params: params,
76                         callback: function(id, obj, args) {
77                             var scope = args.scope;
78                             var cid = scope.client_id;
79                             var ta = Y.one('#dlg-content-'+cid);
80                             ta.set('value', '');
81                             var container = Y.one('#comment-list-'+cid);
82                             var result = scope.render([obj], true);
83                             var newcomment = Y.Node.create(result.html);
84                             container.appendChild(newcomment);
85                             var ids = result.ids;
86                             var linktext = Y.one('#comment-link-text-'+cid);
87                             linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
88                             for(var i in ids) {
89                                 var attributes = {
90                                     color: { to: '#06e' },
91                                     backgroundColor: { to: '#FFE390' }
92                                 };
93                                 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
94                                 anim.animate();
95                             }
96                             scope.register_pagination();
97                             scope.register_delete_buttons();
98                         }
99                     }, true);
100                 } else {
101                     var attributes = {
102                         backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
103                     };
104                     var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
105                     anim.animate();
106                 }
107             },
108             request: function(args, noloading) {
109                 var params = {};
110                 var scope = this;
111                 if (args['scope']) {
112                     scope = args['scope'];
113                 }
114                 //params['page'] = args.page?args.page:'';
115                 params['env']       = '';
116                 // the form element only accept certain file types
117                 params['sesskey']   = M.cfg.sesskey;
118                 params['action']    = args.action?args.action:'';
119                 params['client_id'] = this.client_id;
120                 params['itemid']    = this.itemid;
121                 params['area']      = this.commentarea;
122                 params['courseid']  = this.courseid;
123                 params['contextid'] = this.contextid;
124                 if (args['params']) {
125                     for (i in args['params']) {
126                         params[i] = args['params'][i];
127                     }
128                 }
129                 var cfg = {
130                     method: 'POST',
131                     on: {
132                         complete: function(id,o,p) {
133                             if (!o) {
134                                 alert('IO FATAL');
135                                 return false;
136                             }
137                             var data = Y.JSON.parse(o.responseText);
138                             if (data.error) {
139                                 alert(data.error);
140                                 return false;
141                             } else {
142                                 args.callback(id,data,p);
143                                 return true;
144                             }
145                         }
146                     },
147                     arguments: {
148                         scope: scope
149                     },
150                     headers: {
151                         'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
152                         'User-Agent': 'MoodleComment/3.0'
153                     },
154                     data: build_querystring(params)
155                 };
156                 if (args.form) {
157                     cfg.form = args.form;
158                 }
159                 Y.io(this.api, cfg);
160                 if (!noloading) {
161                     this.wait();
162                 }
163             },
164             render: function(list, newcmt) {
165                 var ret = {};
166                 ret.ids = [];
167                 var template = Y.one('#cmt-tmpl');
168                 var html = '';
169                 for(var i in list) {
170                     var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
171                     var val = template.get('innerHTML');
172                     if (list[i].profileurl) {
173                         val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
174                     } else {
175                         val = val.replace('___name___', list[i].fullname);
176                     }
177                     if (list[i]['delete']||newcmt) {
178                         list[i].content = '<div class="comment-delete"><a href="#" id ="comment-delete-'+this.client_id+'-'+list[i].id+'" title="'+M.str.moodle.deletecomment+'"><img src="'+M.util.image_url('t/delete', 'core')+'" /></a></div>' + list[i].content;
179                     }
180                     val = val.replace('___time___', list[i].time);
181                     val = val.replace('___picture___', list[i].avatar);
182                     val = val.replace('___content___', list[i].content);
183                     val = '<li id="'+htmlid+'">'+val+'</li>';
184                     ret.ids.push(htmlid);
185                     html = (val+html);
186                 }
187                 ret.html = html;
188                 return ret;
189             },
190             load: function(page) {
191                 var scope = this;
192                 var container = Y.one('#comment-ctrl-'+this.client_id);
193                 var params = {
194                     'page': page
195                 }
196                 this.request({
197                     scope: scope,
198                     params: params,
199                     callback: function(id, ret, args) {
200                         var linktext = Y.one('#comment-link-text-'+scope.client_id);
201                         linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
202                         var container = Y.one('#comment-list-'+scope.client_id);
203                         var pagination = Y.one('#comment-pagination-'+scope.client_id);
204                         if (ret.pagination) {
205                             pagination.set('innerHTML', ret.pagination);
206                         } else {
207                             //empty paging bar
208                             pagination.set('innerHTML', '');
209                         }
210                         var result = scope.render(ret.list);
211                         container.set('innerHTML', result.html);
212                         args.scope.register_pagination();
213                         args.scope.register_delete_buttons();
214                     }
215                 });
216             },
218             dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
219                 var scope = this;
220                 var params = {'commentid': id};
221                 scope.cancel_delete();
222                 function remove_dom(type, anim, cmt) {
223                     cmt.remove();
224                 }
225                 this.request({
226                     action: 'delete',
227                     scope: scope,
228                     params: params,
229                     callback: function(id, resp, args) {
230                         var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
231                         var attributes = {
232                             width:{to:0},
233                             height:{to:0}
234                         };
235                         var cmt = Y.one('#'+htmlid);
236                         cmt.setStyle('overflow', 'hidden');
237                         var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
238                         anim.onComplete.subscribe(remove_dom, cmt, this);
239                         anim.animate();
240                     }
241                 }, true);
242             },
243             register_actions: function() {
244                 // add new comment
245                 var action_btn = Y.one('#comment-action-post-'+this.client_id);
246                 if (action_btn) {
247                     action_btn.on('click', function(e) {
248                         e.preventDefault();
249                         this.post();
250                         return false;
251                     }, this);
252                 }
253                 // cancel comment box
254                 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
255                 if (cancel) {
256                     cancel.on('click', function(e) {
257                         e.preventDefault();
258                         this.view(0);
259                         return false;
260                     }, this);
261                 }
262             },
263             register_delete_buttons: function() {
264                 var scope = this;
265                 // page buttons
266                 Y.all('div.comment-delete a').each(
267                     function(node, id) {
268                         var theid = node.get('id');
269                         var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
270                         var commentid = theid.match(parseid);
271                         if (!commentid) {
272                             return;
273                         }
274                         if (commentid[1]) {
275                             Y.Event.purgeElement('#'+theid, false, 'click');
276                         }
277                         node.on('click', function(e, node) {
278                             e.preventDefault();
279                             var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
280                             var re = new RegExp("(\\d+).*", "i");
281                             var result = width.match(re);
282                             if (result[1]) {
283                                 width = Number(result[1]);
284                             } else {
285                                 width = 0;
286                             }
287                             //CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY]);
288                             CommentHelper.confirmoverlay.set('xy', [e.pageX-width-5, e.pageY]);
289                             CommentHelper.confirmoverlay.set('visible', true);
290                             Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
291                                                                 e.preventDefault();
292                                 scope.cancel_delete();
293                                 });
294                             Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
295                             Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
296                                                                         e.preventDefault();
297                                     if (commentid[1]) {
298                                         scope.dodelete(commentid[1]);
299                                     }
300                                 });
301                         }, scope, node);
302                     }
303                 );
304             },
305             cancel_delete: function() {
306                 CommentHelper.confirmoverlay.set('visible', false);
307             },
308             register_pagination: function() {
309                 var scope = this;
310                 // page buttons
311                 Y.all('#comment-pagination-'+this.client_id+' a').each(
312                     function(node, id) {
313                         node.on('click', function(e, node) {
314                                                         e.preventDefault();
315                             var id = node.get('id');
316                             var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
317                             var result = id.match(re);
318                             this.load(result[1]);
319                         }, scope, node);
320                     }
321                 );
322             },
323             view: function(page) {
324                 var container = Y.one('#comment-ctrl-'+this.client_id);
325                 var ta = Y.one('#dlg-content-'+this.client_id);
326                 var img = Y.one('#comment-img-'+this.client_id);
327                 var d = container.getStyle('display');
328                 if (d=='none'||d=='') {
329                     // show
330                     if (this.env != 'block_comments') {
331                         this.load(page);
332                     } else {
333                         this.register_delete_buttons();
334                         this.register_pagination();
335                     }
336                     container.setStyle('display', 'block');
337                     img.src=M.util.image_url('t/expanded', 'core');
338                 } else {
339                     // hide
340                     container.setStyle('display', 'none');
341                     img.src=M.util.image_url('t/collapsed', 'core');
342                     if (ta) {
343                         ta.set('value','');
344                     }
345                 }
346                 if (ta) {
347                     //toggle_textarea.apply(ta, [false]);
348                     //// reset textarea size
349                     ta.on('click', function() {
350                         this.toggle_textarea(true);
351                     }, this)
352                     //ta.onkeypress = function() {
353                         //if (this.scrollHeight > this.clientHeight && !window.opera)
354                             //this.rows += 1;
355                     //}
356                     ta.on('blur', function() {
357                         this.toggle_textarea(false);
358                     }, this);
359                 }
360                 this.register_actions();
361                 return false;
362             },
363             toggle_textarea: function(focus) {
364                 var t = Y.one('#dlg-content-'+this.client_id);
365                 if (focus) {
366                     if (t.get('value') == M.str.moodle.addcomment) {
367                         t.set('value', '');
368                         t.setStyle('color', 'black');
369                     }
370                 }else{
371                     if (t.get('value') == '') {
372                         t.set('value', M.str.moodle.addcomment);
373                         t.setStyle('color','grey');
374                         t.set('rows', 2);
375                     }
376                 }
377             },
378             wait: function() {
379                 var container = Y.one('#comment-list-'+this.client_id);
380                 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading', 'core')+'" /></div>');
381             }
382         });
384         new CommentHelper(options);
385     },
386     init_admin: function(Y) {
387         var select_all = Y.one('#comment_select_all');
388         select_all.on('click', function(e) {
389             var comments = document.getElementsByName('comments');
390             var checked = false;
391             for (var i in comments) {
392                 if (comments[i].checked) {
393                     checked=true;
394                 }
395             }
396             for (i in comments) {
397                 comments[i].checked = !checked;
398             }
399             this.set('checked', !checked);
400         });
402         var comments_delete = Y.one('#comments_delete');
403         if (comments_delete) {
404             comments_delete.on('click', function(e) {
405                 e.preventDefault();
406                 var list = '';
407                 var comments = document.getElementsByName('comments');
408                 for (var i in comments) {
409                     if (typeof comments[i] == 'object' && comments[i].checked) {
410                         list += (comments[i].value + '-');
411                     }
412                 }
413                 if (!list) {
414                     return;
415                 }
416                 var args = {};
417                 args.message = M.str.admin.confirmdeletecomments;
418                 args.callback = function() {
419                     var url = M.cfg.wwwroot + '/comment/index.php';
421                     var data = {
422                         'commentids': list,
423                         'sesskey': M.cfg.sesskey,
424                         'action': 'delete'
425                     }
426                     var cfg = {
427                         method: 'POST',
428                         on: {
429                             complete: function(id,o,p) {
430                                 if (!o) {
431                                     alert('IO FATAL');
432                                     return;
433                                 }
434                                 if (o.responseText == 'yes') {
435                                     location.reload();
436                                 }
437                             }
438                         },
439                         arguments: {
440                             scope: this
441                         },
442                         headers: {
443                             'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
444                             'User-Agent': 'MoodleComment/3.0'
445                         },
446                         data: build_querystring(data)
447                     };
448                     Y.io(url, cfg);
449                 }
450                 M.util.show_confirm_dialog(e, args);
451             });
452         }
453     }
454 };