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