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