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