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;
43 this.autostart = (args.autostart);
49 var handle = Y.one('#comment-link-'+this.client_id);
53 handle.setStyle('display', 'none');
55 handle.on('click', function(e) {
61 scope.toggle_textarea(false);
62 CommentHelper.confirmoverlay = new Y.Overlay({
63 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>',
66 CommentHelper.confirmoverlay.render(document.body);
69 var ta = Y.one('#dlg-content-'+this.client_id);
71 var value = ta.get('value');
72 if (value && value != M.str.moodle.addcomment) {
73 var params = {'content': value};
78 callback: function(id, obj, args) {
79 var scope = args.scope;
80 var cid = scope.client_id;
81 var ta = Y.one('#dlg-content-'+cid);
83 scope.toggle_textarea(false);
84 var container = Y.one('#comment-list-'+cid);
85 var result = scope.render([obj], true);
86 var newcomment = Y.Node.create(result.html);
87 container.appendChild(newcomment);
89 var linktext = Y.one('#comment-link-text-'+cid);
91 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
95 color: { to: '#06e' },
96 backgroundColor: { to: '#FFE390' }
98 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
101 scope.register_pagination();
102 scope.register_delete_buttons();
107 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
109 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
113 request: function(args, noloading) {
117 scope = args['scope'];
119 //params['page'] = args.page?args.page:'';
120 // the form element only accept certain file types
121 params['sesskey'] = M.cfg.sesskey;
122 params['action'] = args.action?args.action:'';
123 params['client_id'] = this.client_id;
124 params['itemid'] = this.itemid;
125 params['area'] = this.commentarea;
126 params['courseid'] = this.courseid;
127 params['contextid'] = this.contextid;
128 params['component'] = this.component;
129 if (args['params']) {
130 for (i in args['params']) {
131 params[i] = args['params'][i];
137 complete: function(id,o,p) {
142 var data = Y.JSON.parse(o.responseText);
144 if (data.error == 'require_login') {
145 args.callback(id,data,p);
151 args.callback(id,data,p);
160 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
161 'User-Agent': 'MoodleComment/3.0'
163 data: build_querystring(params)
166 cfg.form = args.form;
173 render: function(list, newcmt) {
176 var template = Y.one('#cmt-tmpl');
179 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
180 var val = template.get('innerHTML');
181 if (list[i].profileurl) {
182 val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
184 val = val.replace('___name___', list[i].fullname);
186 if (list[i]['delete']||newcmt) {
187 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;
189 val = val.replace('___time___', list[i].time);
190 val = val.replace('___picture___', list[i].avatar);
191 val = val.replace('___content___', list[i].content);
192 val = '<li id="'+htmlid+'">'+val+'</li>';
193 ret.ids.push(htmlid);
199 load: function(page) {
201 var container = Y.one('#comment-ctrl-'+this.client_id);
209 callback: function(id, ret, args) {
210 var linktext = Y.one('#comment-link-text-'+scope.client_id);
211 if (ret.count && linktext) {
212 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
214 var container = Y.one('#comment-list-'+scope.client_id);
215 var pagination = Y.one('#comment-pagination-'+scope.client_id);
216 if (ret.pagination) {
217 pagination.set('innerHTML', ret.pagination);
220 pagination.set('innerHTML', '');
222 if (ret.error == 'require_login') {
224 result.html = M.str.moodle.commentsrequirelogin;
226 var result = scope.render(ret.list);
228 container.set('innerHTML', result.html);
229 var img = Y.one('#comment-img-'+scope.client_id);
231 img.set('src', M.util.image_url('t/expanded', 'core'));
233 args.scope.register_pagination();
234 args.scope.register_delete_buttons();
239 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
241 var params = {'commentid': id};
242 scope.cancel_delete();
243 function remove_dom(type, anim, cmt) {
250 callback: function(id, resp, args) {
251 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
256 var cmt = Y.one('#'+htmlid);
257 cmt.setStyle('overflow', 'hidden');
258 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
259 anim.onComplete.subscribe(remove_dom, cmt, this);
264 register_actions: function() {
266 var action_btn = Y.one('#comment-action-post-'+this.client_id);
268 action_btn.on('click', function(e) {
274 // cancel comment box
275 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
277 cancel.on('click', function(e) {
284 register_delete_buttons: function() {
287 Y.all('div.comment-delete a').each(
289 var theid = node.get('id');
290 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
291 var commentid = theid.match(parseid);
296 Y.Event.purgeElement('#'+theid, false, 'click');
298 node.on('click', function(e, node) {
300 var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
301 var re = new RegExp("(\\d+).*", "i");
302 var result = width.match(re);
304 width = Number(result[1]);
308 //CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY]);
309 CommentHelper.confirmoverlay.set('xy', [e.pageX-width-5, e.pageY]);
310 CommentHelper.confirmoverlay.set('visible', true);
311 Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
313 scope.cancel_delete();
315 Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
316 Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
319 scope.dodelete(commentid[1]);
326 cancel_delete: function() {
327 CommentHelper.confirmoverlay.set('visible', false);
329 register_pagination: function() {
332 Y.all('#comment-pagination-'+this.client_id+' a').each(
334 node.on('click', function(e, node) {
336 var id = node.get('id');
337 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
338 var result = id.match(re);
339 this.load(result[1]);
344 view: function(page) {
345 var container = Y.one('#comment-ctrl-'+this.client_id);
346 var ta = Y.one('#dlg-content-'+this.client_id);
347 var img = Y.one('#comment-img-'+this.client_id);
348 var d = container.getStyle('display');
349 if (d=='none'||d=='') {
351 if (!this.autostart) {
354 this.register_delete_buttons();
355 this.register_pagination();
357 container.setStyle('display', 'block');
359 img.set('src', M.util.image_url('t/expanded', 'core'));
363 container.setStyle('display', 'none');
364 img.set('src', M.util.image_url('t/collapsed', 'core'));
370 //toggle_textarea.apply(ta, [false]);
371 //// reset textarea size
372 ta.on('click', function() {
373 this.toggle_textarea(true);
375 //ta.onkeypress = function() {
376 //if (this.scrollHeight > this.clientHeight && !window.opera)
379 ta.on('blur', function() {
380 this.toggle_textarea(false);
383 this.register_actions();
386 toggle_textarea: function(focus) {
387 var t = Y.one('#dlg-content-'+this.client_id);
392 if (t.get('value') == M.str.moodle.addcomment) {
394 t.setStyle('color', 'black');
397 if (t.get('value') == '') {
398 t.set('value', M.str.moodle.addcomment);
399 t.setStyle('color','grey');
405 var container = Y.one('#comment-list-'+this.client_id);
406 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
410 new CommentHelper(options);
412 init_admin: function(Y) {
413 var select_all = Y.one('#comment_select_all');
414 select_all.on('click', function(e) {
415 var comments = document.getElementsByName('comments');
417 for (var i in comments) {
418 if (comments[i].checked) {
422 for (i in comments) {
423 comments[i].checked = !checked;
425 this.set('checked', !checked);
428 var comments_delete = Y.one('#comments_delete');
429 if (comments_delete) {
430 comments_delete.on('click', function(e) {
433 var comments = document.getElementsByName('comments');
434 for (var i in comments) {
435 if (typeof comments[i] == 'object' && comments[i].checked) {
436 list += (comments[i].value + '-');
443 args.message = M.str.admin.confirmdeletecomments;
444 args.callback = function() {
445 var url = M.cfg.wwwroot + '/comment/index.php';
449 'sesskey': M.cfg.sesskey,
455 complete: function(id,o,p) {
460 if (o.responseText == 'yes') {
469 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
470 'User-Agent': 'MoodleComment/3.0'
472 data: build_querystring(data)
476 M.util.show_confirm_dialog(e, args);