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