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