Merge branch 'wip-MDL-48239-28' of git://github.com/abgreeve/moodle into MOODLE_28_STABLE
[moodle.git] / comment / comment.js
blob90c767d2f669ecb19369a4c3698f4e9e7f8d4305
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.autostart = (args.autostart);
44                 // expand comments?
45                 if (this.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                 scope.toggle_textarea(false);
62             },
63             post: function() {
64                 var ta = Y.one('#dlg-content-'+this.client_id);
65                 var scope = this;
66                 var value = ta.get('value');
67                 if (value && value != M.util.get_string('addcomment', 'moodle')) {
68                     var params = {'content': value};
69                     this.request({
70                         action: 'add',
71                         scope: scope,
72                         params: params,
73                         callback: function(id, obj, args) {
74                             var scope = args.scope;
75                             var cid = scope.client_id;
76                             var ta = Y.one('#dlg-content-'+cid);
77                             ta.set('value', '');
78                             scope.toggle_textarea(false);
79                             var container = Y.one('#comment-list-'+cid);
80                             var result = scope.render([obj], true);
81                             var newcomment = Y.Node.create(result.html);
82                             container.appendChild(newcomment);
83                             var ids = result.ids;
84                             var linkText = Y.one('#comment-link-text-' + cid);
85                             if (linkText) {
86                                 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', obj.count));
87                             }
88                             for(var i in ids) {
89                                 var attributes = {
90                                     color: { to: '#06e' },
91                                     backgroundColor: { to: '#FFE390' }
92                                 };
93                                 var anim = new Y.YUI2.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 Y.YUI2.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                 // the form element only accept certain file types
116                 params['sesskey']   = M.cfg.sesskey;
117                 params['action']    = args.action?args.action:'';
118                 params['client_id'] = this.client_id;
119                 params['itemid']    = this.itemid;
120                 params['area']      = this.commentarea;
121                 params['courseid']  = this.courseid;
122                 params['contextid'] = this.contextid;
123                 params['component'] = this.component;
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                                 if (data.error == 'require_login') {
140                                     args.callback(id,data,p);
141                                     return true;
142                                 }
143                                 alert(data.error);
144                                 return false;
145                             } else {
146                                 args.callback(id,data,p);
147                                 return true;
148                             }
149                         }
150                     },
151                     arguments: {
152                         scope: scope
153                     },
154                     headers: {
155                         'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
156                     },
157                     data: build_querystring(params)
158                 };
159                 if (args.form) {
160                     cfg.form = args.form;
161                 }
162                 Y.io(this.api, cfg);
163                 if (!noloading) {
164                     this.wait();
165                 }
166             },
167             render: function(list, newcmt) {
168                 var ret = {};
169                 ret.ids = [];
170                 var template = Y.one('#cmt-tmpl');
171                 var html = '';
172                 for(var i in list) {
173                     var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
174                     var val = template.get('innerHTML');
175                     if (list[i].profileurl) {
176                         val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
177                     } else {
178                         val = val.replace('___name___', list[i].fullname);
179                     }
180                     if (list[i]['delete']||newcmt) {
181                         list[i].content = '<div class="comment-delete"><a href="#" id ="comment-delete-'+this.client_id+'-'+list[i].id+'" title="'+M.util.get_string('deletecomment', 'moodle')+'"><img alt="" src="'+M.util.image_url('t/delete', 'core')+'" /></a></div>' + list[i].content;
182                     }
183                     val = val.replace('___time___', list[i].time);
184                     val = val.replace('___picture___', list[i].avatar);
185                     val = val.replace('___content___', list[i].content);
186                     val = '<li id="'+htmlid+'">'+val+'</li>';
187                     ret.ids.push(htmlid);
188                     html = (val+html);
189                 }
190                 ret.html = html;
191                 return ret;
192             },
193             load: function(page) {
194                 var scope = this;
195                 var container = Y.one('#comment-ctrl-'+this.client_id);
196                 var params = {
197                     'action': 'get',
198                     'page': page
199                 };
200                 this.request({
201                     scope: scope,
202                     params: params,
203                     callback: function(id, ret, args) {
204                         var linkText = Y.one('#comment-link-text-' + scope.client_id);
205                         if (ret.count && linkText) {
206                             linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', ret.count));
207                         }
208                         var container = Y.one('#comment-list-'+scope.client_id);
209                         var pagination = Y.one('#comment-pagination-'+scope.client_id);
210                         if (ret.pagination) {
211                             pagination.set('innerHTML', ret.pagination);
212                         } else {
213                             //empty paging bar
214                             pagination.set('innerHTML', '');
215                         }
216                         if (ret.error == 'require_login') {
217                             var result = {};
218                             result.html = M.util.get_string('commentsrequirelogin', 'moodle');
219                         } else {
220                             var result = scope.render(ret.list);
221                         }
222                         container.set('innerHTML', result.html);
223                         var img = Y.one('#comment-img-'+scope.client_id);
224                         if (img) {
225                             img.set('src', M.util.image_url('t/expanded', 'core'));
226                         }
227                         args.scope.register_pagination();
228                         args.scope.register_delete_buttons();
229                     }
230                 });
231             },
233             dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
234                 var scope = this,
235                     cid = scope.client_id,
236                     params = {'commentid': id};
237                 function remove_dom(type, anim, cmt) {
238                     cmt.remove();
239                     var linkText = Y.one('#comment-link-text-' + cid),
240                         comments = Y.all('#comment-list-' + cid + ' li');
241                     if (linkText && comments) {
242                         linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', comments.size()));
243                     }
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 Y.YUI2.util.Anim(htmlid, attributes, 1, Y.YUI2.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) {
298                             e.preventDefault();
299                             if (commentid[1]) {
300                                 scope.dodelete(commentid[1]);
301                             }
302                         });
303                         // Also handle space/enter key.
304                         node.on('key', function(e) {
305                             e.preventDefault();
306                             if (commentid[1]) {
307                                 scope.dodelete(commentid[1]);
308                             }
309                         }, '13,32');
310                         // 13 and 32 are the keycodes for space and enter.
311                     }
312                 );
313             },
314             register_pagination: function() {
315                 var scope = this;
316                 // page buttons
317                 Y.all('#comment-pagination-'+this.client_id+' a').each(
318                     function(node, id) {
319                         node.on('click', function(e, node) {
320                             e.preventDefault();
321                             var id = node.get('id');
322                             var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
323                             var result = id.match(re);
324                             this.load(result[1]);
325                         }, scope, node);
326                     }
327                 );
328             },
329             view: function(page) {
330                 var container = Y.one('#comment-ctrl-'+this.client_id);
331                 var ta = Y.one('#dlg-content-'+this.client_id);
332                 var img = Y.one('#comment-img-'+this.client_id);
333                 var d = container.getStyle('display');
334                 if (d=='none'||d=='') {
335                     // show
336                     if (!this.autostart) {
337                         this.load(page);
338                     } else {
339                         this.register_delete_buttons();
340                         this.register_pagination();
341                     }
342                     container.setStyle('display', 'block');
343                     if (img) {
344                         img.set('src', M.util.image_url('t/expanded', 'core'));
345                     }
346                 } else {
347                     // hide
348                     container.setStyle('display', 'none');
349                     var collapsedimage = 't/collapsed'; // ltr mode
350                     if ( Y.one(document.body).hasClass('dir-rtl') ) {
351                         collapsedimage = 't/collapsed_rtl';
352                     } else {
353                         collapsedimage = 't/collapsed';
354                     }
355                     img.set('src', M.util.image_url(collapsedimage, 'core'));
356                     if (ta) {
357                         ta.set('value','');
358                     }
359                 }
360                 if (ta) {
361                     //toggle_textarea.apply(ta, [false]);
362                     //// reset textarea size
363                     ta.on('focus', function() {
364                         this.toggle_textarea(true);
365                     }, this);
366                     //ta.onkeypress = function() {
367                         //if (this.scrollHeight > this.clientHeight && !window.opera)
368                             //this.rows += 1;
369                     //}
370                     ta.on('blur', function() {
371                         this.toggle_textarea(false);
372                     }, this);
373                 }
374                 this.register_actions();
375                 return false;
376             },
377             toggle_textarea: function(focus) {
378                 var t = Y.one('#dlg-content-'+this.client_id);
379                 if (!t) {
380                     return false;
381                 }
382                 if (focus) {
383                     if (t.get('value') == M.util.get_string('addcomment', 'moodle')) {
384                         t.set('value', '');
385                         t.setStyle('color', 'black');
386                     }
387                 }else{
388                     if (t.get('value') == '') {
389                         t.set('value', M.util.get_string('addcomment', 'moodle'));
390                         t.setStyle('color','grey');
391                         t.set('rows', 2);
392                     }
393                 }
394             },
395             wait: function() {
396                 var container = Y.one('#comment-list-'+this.client_id);
397                 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
398             }
399         });
401         new CommentHelper(options);
402     },
403     init_admin: function(Y) {
404         var select_all = Y.one('#comment_select_all');
405         select_all.on('click', function(e) {
406             var comments = document.getElementsByName('comments');
407             var checked = false;
408             for (var i in comments) {
409                 if (comments[i].checked) {
410                     checked=true;
411                 }
412             }
413             for (i in comments) {
414                 comments[i].checked = !checked;
415             }
416             this.set('checked', !checked);
417         });
419         var comments_delete = Y.one('#comments_delete');
420         if (comments_delete) {
421             comments_delete.on('click', function(e) {
422                 e.preventDefault();
423                 var list = '';
424                 var comments = document.getElementsByName('comments');
425                 for (var i in comments) {
426                     if (typeof comments[i] == 'object' && comments[i].checked) {
427                         list += (comments[i].value + '-');
428                     }
429                 }
430                 if (!list) {
431                     return;
432                 }
433                 var args = {};
434                 args.message = M.util.get_string('confirmdeletecomments', 'admin');
435                 args.callback = function() {
436                     var url = M.cfg.wwwroot + '/comment/index.php';
438                     var data = {
439                         'commentids': list,
440                         'sesskey': M.cfg.sesskey,
441                         'action': 'delete'
442                     };
443                     var cfg = {
444                         method: 'POST',
445                         on: {
446                             complete: function(id,o,p) {
447                                 if (!o) {
448                                     alert('IO FATAL');
449                                     return;
450                                 }
451                                 if (o.responseText == 'yes') {
452                                     location.reload();
453                                 }
454                             }
455                         },
456                         arguments: {
457                             scope: this
458                         },
459                         headers: {
460                             'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
461                         },
462                         data: build_querystring(data)
463                     };
464                     Y.io(url, cfg);
465                 };
466                 M.util.show_confirm_dialog(e, args);
467             });
468         }
469     }