message MDLSITE-1039 improving the jabber message processor
[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
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
7e3b8265 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);
4ef81b6c
DC
214 var img = Y.one('#comment-img-'+scope.client_id);
215 img.set('src', M.util.image_url('t/expanded', 'core'));
ef502357
PS
216 args.scope.register_pagination();
217 args.scope.register_delete_buttons();
218 }
219 });
220 },
4c508047 221
bdf69389 222 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
ef502357
PS
223 var scope = this;
224 var params = {'commentid': id};
4b201bc6 225 scope.cancel_delete();
7deb5107 226 function remove_dom(type, anim, cmt) {
ef502357 227 cmt.remove();
adacb0fe 228 }
ef502357
PS
229 this.request({
230 action: 'delete',
231 scope: scope,
232 params: params,
233 callback: function(id, resp, args) {
234 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
235 var attributes = {
236 width:{to:0},
237 height:{to:0}
238 };
239 var cmt = Y.one('#'+htmlid);
240 cmt.setStyle('overflow', 'hidden');
241 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
242 anim.onComplete.subscribe(remove_dom, cmt, this);
243 anim.animate();
244 }
245 }, true);
246 },
247 register_actions: function() {
248 // add new comment
6b9be737
DC
249 var action_btn = Y.one('#comment-action-post-'+this.client_id);
250 if (action_btn) {
251 action_btn.on('click', function(e) {
252 e.preventDefault();
253 this.post();
254 return false;
255 }, this);
256 }
ef502357
PS
257 // cancel comment box
258 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
259 if (cancel) {
260 cancel.on('click', function(e) {
261 e.preventDefault();
262 this.view(0);
263 return false;
264 }, this);
adacb0fe 265 }
ef502357
PS
266 },
267 register_delete_buttons: function() {
268 var scope = this;
269 // page buttons
b45fde05 270 Y.all('div.comment-delete a').each(
ef502357 271 function(node, id) {
2057b5ef 272 var theid = node.get('id');
4b201bc6
DC
273 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
274 var commentid = theid.match(parseid);
6b9be737
DC
275 if (!commentid) {
276 return;
277 }
4b201bc6 278 if (commentid[1]) {
2057b5ef
DC
279 Y.Event.purgeElement('#'+theid, false, 'click');
280 }
ef502357 281 node.on('click', function(e, node) {
63eee7b4 282 e.preventDefault();
4b201bc6
DC
283 var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
284 var re = new RegExp("(\\d+).*", "i");
285 var result = width.match(re);
ef502357 286 if (result[1]) {
4b201bc6
DC
287 width = Number(result[1]);
288 } else {
289 width = 0;
ef502357 290 }
63eee7b4
DC
291 //CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY]);
292 CommentHelper.confirmoverlay.set('xy', [e.pageX-width-5, e.pageY]);
4b201bc6 293 CommentHelper.confirmoverlay.set('visible', true);
1150c1bd
DC
294 Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
295 e.preventDefault();
4b201bc6
DC
296 scope.cancel_delete();
297 });
298 Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
1150c1bd
DC
299 Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
300 e.preventDefault();
4b201bc6
DC
301 if (commentid[1]) {
302 scope.dodelete(commentid[1]);
303 }
304 });
ef502357
PS
305 }, scope, node);
306 }
307 );
308 },
4b201bc6
DC
309 cancel_delete: function() {
310 CommentHelper.confirmoverlay.set('visible', false);
4b201bc6 311 },
ef502357
PS
312 register_pagination: function() {
313 var scope = this;
314 // page buttons
3e34183a 315 Y.all('#comment-pagination-'+this.client_id+' a').each(
ef502357
PS
316 function(node, id) {
317 node.on('click', function(e, node) {
1150c1bd 318 e.preventDefault();
ef502357
PS
319 var id = node.get('id');
320 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
321 var result = id.match(re);
322 this.load(result[1]);
323 }, scope, node);
324 }
325 );
326 },
327 view: function(page) {
328 var container = Y.one('#comment-ctrl-'+this.client_id);
329 var ta = Y.one('#dlg-content-'+this.client_id);
330 var img = Y.one('#comment-img-'+this.client_id);
331 var d = container.getStyle('display');
332 if (d=='none'||d=='') {
333 // show
76951100
DC
334 if (this.env != 'block_comments') {
335 this.load(page);
336 } else {
337 this.register_delete_buttons();
338 this.register_pagination();
339 }
ef502357 340 container.setStyle('display', 'block');
4ef81b6c 341 img.set('src', M.util.image_url('t/expanded', 'core'));
ef502357
PS
342 } else {
343 // hide
344 container.setStyle('display', 'none');
4ef81b6c 345 img.set('src', M.util.image_url('t/collapsed', 'core'));
d37e440a
DC
346 if (ta) {
347 ta.set('value','');
348 }
adacb0fe 349 }
6b9be737
DC
350 if (ta) {
351 //toggle_textarea.apply(ta, [false]);
352 //// reset textarea size
353 ta.on('click', function() {
354 this.toggle_textarea(true);
7e3b8265 355 }, this);
6b9be737
DC
356 //ta.onkeypress = function() {
357 //if (this.scrollHeight > this.clientHeight && !window.opera)
358 //this.rows += 1;
359 //}
360 ta.on('blur', function() {
361 this.toggle_textarea(false);
362 }, this);
363 }
ef502357
PS
364 this.register_actions();
365 return false;
366 },
367 toggle_textarea: function(focus) {
368 var t = Y.one('#dlg-content-'+this.client_id);
369 if (focus) {
2b728cb5 370 if (t.get('value') == M.str.moodle.addcomment) {
ef502357
PS
371 t.set('value', '');
372 t.setStyle('color', 'black');
373 }
374 }else{
375 if (t.get('value') == '') {
2b728cb5 376 t.set('value', M.str.moodle.addcomment);
ef502357 377 t.setStyle('color','grey');
76951100 378 t.set('rows', 2);
ef502357 379 }
adacb0fe 380 }
ef502357
PS
381 },
382 wait: function() {
383 var container = Y.one('#comment-list-'+this.client_id);
9e9dd1a3 384 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
adacb0fe 385 }
ef502357
PS
386 });
387
388 new CommentHelper(options);
34e20eb4
DC
389 },
390 init_admin: function(Y) {
391 var select_all = Y.one('#comment_select_all');
1150c1bd 392 select_all.on('click', function(e) {
34e20eb4
DC
393 var comments = document.getElementsByName('comments');
394 var checked = false;
395 for (var i in comments) {
396 if (comments[i].checked) {
397 checked=true;
398 }
399 }
4b201bc6 400 for (i in comments) {
34e20eb4
DC
401 comments[i].checked = !checked;
402 }
403 this.set('checked', !checked);
404 });
405
406 var comments_delete = Y.one('#comments_delete');
407 if (comments_delete) {
408 comments_delete.on('click', function(e) {
409 e.preventDefault();
410 var list = '';
411 var comments = document.getElementsByName('comments');
412 for (var i in comments) {
413 if (typeof comments[i] == 'object' && comments[i].checked) {
414 list += (comments[i].value + '-');
415 }
416 }
417 if (!list) {
418 return;
419 }
420 var args = {};
421 args.message = M.str.admin.confirmdeletecomments;
422 args.callback = function() {
423 var url = M.cfg.wwwroot + '/comment/index.php';
424
425 var data = {
426 'commentids': list,
427 'sesskey': M.cfg.sesskey,
428 'action': 'delete'
7e3b8265 429 };
34e20eb4
DC
430 var cfg = {
431 method: 'POST',
432 on: {
433 complete: function(id,o,p) {
434 if (!o) {
435 alert('IO FATAL');
436 return;
437 }
438 if (o.responseText == 'yes') {
439 location.reload();
440 }
441 }
442 },
443 arguments: {
444 scope: this
445 },
446 headers: {
447 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
448 'User-Agent': 'MoodleComment/3.0'
449 },
450 data: build_querystring(data)
451 };
452 Y.io(url, cfg);
7e3b8265 453 };
34e20eb4
DC
454 M.util.show_confirm_dialog(e, args);
455 });
456 }
ef502357
PS
457 }
458};