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