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