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