1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * @author Dongsheng Cai <dongsheng@moodle.com>
22 * Initialize commenting system
24 init: function(Y, options) {
25 var CommentHelper = function(args) {
26 CommentHelper.superclass.constructor.apply(this, arguments);
28 CommentHelper.NAME = "COMMENT";
29 CommentHelper.ATTRS = {
33 Y.extend(CommentHelper, Y.Base, {
34 api: M.cfg.wwwroot+'/comment/comment_ajax.php',
35 initializer: function(args) {
37 this.client_id = args.client_id;
38 this.itemid = args.itemid;
39 this.commentarea = args.commentarea;
40 this.component = args.component;
41 this.courseid = args.courseid;
42 this.contextid = args.contextid;
50 Y.one('#comment-link-'+this.client_id).setStyle('display', 'none');
53 var handle = Y.one('#comment-link-'+this.client_id);
55 handle.on('click', function(e) {
61 CommentHelper.confirmoverlay = new Y.Overlay({
62 bodyContent: '<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>',
65 CommentHelper.confirmoverlay.render(document.body);
68 var ta = Y.one('#dlg-content-'+this.client_id);
70 var value = ta.get('value');
71 if (value && value != M.str.moodle.addcomment) {
72 var params = {'content': value};
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);
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);
87 var linktext = Y.one('#comment-link-text-'+cid);
88 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
91 color: { to: '#06e' },
92 backgroundColor: { to: '#FFE390' }
94 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
97 scope.register_pagination();
98 scope.register_delete_buttons();
103 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
105 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
109 request: function(args, noloading) {
113 scope = args['scope'];
115 //params['page'] = args.page?args.page:'';
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;
125 params['component'] = this.component;
126 if (args['params']) {
127 for (i in args['params']) {
128 params[i] = args['params'][i];
134 complete: function(id,o,p) {
139 var data = Y.JSON.parse(o.responseText);
144 args.callback(id,data,p);
153 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
154 'User-Agent': 'MoodleComment/3.0'
156 data: build_querystring(params)
159 cfg.form = args.form;
166 render: function(list, newcmt) {
169 var template = Y.one('#cmt-tmpl');
172 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
173 var val = template.get('innerHTML');
174 if (list[i].profileurl) {
175 val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
177 val = val.replace('___name___', list[i].fullname);
179 if (list[i]['delete']||newcmt) {
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;
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);
192 load: function(page) {
194 var container = Y.one('#comment-ctrl-'+this.client_id);
201 callback: function(id, ret, args) {
202 var linktext = Y.one('#comment-link-text-'+scope.client_id);
203 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
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);
210 pagination.set('innerHTML', '');
212 var result = scope.render(ret.list);
213 container.set('innerHTML', result.html);
214 args.scope.register_pagination();
215 args.scope.register_delete_buttons();
220 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
222 var params = {'commentid': id};
223 scope.cancel_delete();
224 function remove_dom(type, anim, cmt) {
231 callback: function(id, resp, args) {
232 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
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);
245 register_actions: function() {
247 var action_btn = Y.one('#comment-action-post-'+this.client_id);
249 action_btn.on('click', function(e) {
255 // cancel comment box
256 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
258 cancel.on('click', function(e) {
265 register_delete_buttons: function() {
268 Y.all('div.comment-delete a').each(
270 var theid = node.get('id');
271 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
272 var commentid = theid.match(parseid);
277 Y.Event.purgeElement('#'+theid, false, 'click');
279 node.on('click', function(e, node) {
281 var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
282 var re = new RegExp("(\\d+).*", "i");
283 var result = width.match(re);
285 width = Number(result[1]);
289 //CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY]);
290 CommentHelper.confirmoverlay.set('xy', [e.pageX-width-5, e.pageY]);
291 CommentHelper.confirmoverlay.set('visible', true);
292 Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
294 scope.cancel_delete();
296 Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
297 Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
300 scope.dodelete(commentid[1]);
307 cancel_delete: function() {
308 CommentHelper.confirmoverlay.set('visible', false);
310 register_pagination: function() {
313 Y.all('#comment-pagination-'+this.client_id+' a').each(
315 node.on('click', function(e, node) {
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]);
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=='') {
332 if (this.env != 'block_comments') {
335 this.register_delete_buttons();
336 this.register_pagination();
338 container.setStyle('display', 'block');
339 img.src=M.util.image_url('t/expanded', 'core');
342 container.setStyle('display', 'none');
343 img.src=M.util.image_url('t/collapsed', 'core');
349 //toggle_textarea.apply(ta, [false]);
350 //// reset textarea size
351 ta.on('click', function() {
352 this.toggle_textarea(true);
354 //ta.onkeypress = function() {
355 //if (this.scrollHeight > this.clientHeight && !window.opera)
358 ta.on('blur', function() {
359 this.toggle_textarea(false);
362 this.register_actions();
365 toggle_textarea: function(focus) {
366 var t = Y.one('#dlg-content-'+this.client_id);
368 if (t.get('value') == M.str.moodle.addcomment) {
370 t.setStyle('color', 'black');
373 if (t.get('value') == '') {
374 t.set('value', M.str.moodle.addcomment);
375 t.setStyle('color','grey');
381 var container = Y.one('#comment-list-'+this.client_id);
382 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading', 'core')+'" /></div>');
386 new CommentHelper(options);
388 init_admin: function(Y) {
389 var select_all = Y.one('#comment_select_all');
390 select_all.on('click', function(e) {
391 var comments = document.getElementsByName('comments');
393 for (var i in comments) {
394 if (comments[i].checked) {
398 for (i in comments) {
399 comments[i].checked = !checked;
401 this.set('checked', !checked);
404 var comments_delete = Y.one('#comments_delete');
405 if (comments_delete) {
406 comments_delete.on('click', function(e) {
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 + '-');
419 args.message = M.str.admin.confirmdeletecomments;
420 args.callback = function() {
421 var url = M.cfg.wwwroot + '/comment/index.php';
425 'sesskey': M.cfg.sesskey,
431 complete: function(id,o,p) {
436 if (o.responseText == 'yes') {
445 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
446 'User-Agent': 'MoodleComment/3.0'
448 data: build_querystring(data)
452 M.util.show_confirm_dialog(e, args);