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 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);
89 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
93 color: { to: '#06e' },
94 backgroundColor: { to: '#FFE390' }
96 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
99 scope.register_pagination();
100 scope.register_delete_buttons();
105 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
107 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
111 request: function(args, noloading) {
115 scope = args['scope'];
117 //params['page'] = args.page?args.page:'';
118 // the form element only accept certain file types
119 params['sesskey'] = M.cfg.sesskey;
120 params['action'] = args.action?args.action:'';
121 params['client_id'] = this.client_id;
122 params['itemid'] = this.itemid;
123 params['area'] = this.commentarea;
124 params['courseid'] = this.courseid;
125 params['contextid'] = this.contextid;
126 params['component'] = this.component;
127 if (args['params']) {
128 for (i in args['params']) {
129 params[i] = args['params'][i];
135 complete: function(id,o,p) {
140 var data = Y.JSON.parse(o.responseText);
142 if (data.error == 'require_login') {
143 args.callback(id,data,p);
149 args.callback(id,data,p);
158 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
159 'User-Agent': 'MoodleComment/3.0'
161 data: build_querystring(params)
164 cfg.form = args.form;
171 render: function(list, newcmt) {
174 var template = Y.one('#cmt-tmpl');
177 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
178 var val = template.get('innerHTML');
179 if (list[i].profileurl) {
180 val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
182 val = val.replace('___name___', list[i].fullname);
184 if (list[i]['delete']||newcmt) {
185 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;
187 val = val.replace('___time___', list[i].time);
188 val = val.replace('___picture___', list[i].avatar);
189 val = val.replace('___content___', list[i].content);
190 val = '<li id="'+htmlid+'">'+val+'</li>';
191 ret.ids.push(htmlid);
197 load: function(page) {
199 var container = Y.one('#comment-ctrl-'+this.client_id);
207 callback: function(id, ret, args) {
208 var linktext = Y.one('#comment-link-text-'+scope.client_id);
209 if (ret.count && linktext) {
210 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
212 var container = Y.one('#comment-list-'+scope.client_id);
213 var pagination = Y.one('#comment-pagination-'+scope.client_id);
214 if (ret.pagination) {
215 pagination.set('innerHTML', ret.pagination);
218 pagination.set('innerHTML', '');
220 if (ret.error == 'require_login') {
222 result.html = M.str.moodle.commentsrequirelogin;
224 var result = scope.render(ret.list);
226 container.set('innerHTML', result.html);
227 var img = Y.one('#comment-img-'+scope.client_id);
229 img.set('src', M.util.image_url('t/expanded', 'core'));
231 args.scope.register_pagination();
232 args.scope.register_delete_buttons();
237 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
239 var params = {'commentid': id};
240 scope.cancel_delete();
241 function remove_dom(type, anim, cmt) {
248 callback: function(id, resp, args) {
249 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
254 var cmt = Y.one('#'+htmlid);
255 cmt.setStyle('overflow', 'hidden');
256 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
257 anim.onComplete.subscribe(remove_dom, cmt, this);
262 register_actions: function() {
264 var action_btn = Y.one('#comment-action-post-'+this.client_id);
266 action_btn.on('click', function(e) {
272 // cancel comment box
273 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
275 cancel.on('click', function(e) {
282 register_delete_buttons: function() {
285 Y.all('div.comment-delete a').each(
287 var theid = node.get('id');
288 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
289 var commentid = theid.match(parseid);
294 Y.Event.purgeElement('#'+theid, false, 'click');
296 node.on('click', function(e, node) {
298 var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
299 var re = new RegExp("(\\d+).*", "i");
300 var result = width.match(re);
302 width = Number(result[1]);
306 //CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY]);
307 CommentHelper.confirmoverlay.set('xy', [e.pageX-width-5, e.pageY]);
308 CommentHelper.confirmoverlay.set('visible', true);
309 Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
311 scope.cancel_delete();
313 Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
314 Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
317 scope.dodelete(commentid[1]);
324 cancel_delete: function() {
325 CommentHelper.confirmoverlay.set('visible', false);
327 register_pagination: function() {
330 Y.all('#comment-pagination-'+this.client_id+' a').each(
332 node.on('click', function(e, node) {
334 var id = node.get('id');
335 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
336 var result = id.match(re);
337 this.load(result[1]);
342 view: function(page) {
343 var container = Y.one('#comment-ctrl-'+this.client_id);
344 var ta = Y.one('#dlg-content-'+this.client_id);
345 var img = Y.one('#comment-img-'+this.client_id);
346 var d = container.getStyle('display');
347 if (d=='none'||d=='') {
349 if (!this.autostart) {
352 this.register_delete_buttons();
353 this.register_pagination();
355 container.setStyle('display', 'block');
357 img.set('src', M.util.image_url('t/expanded', 'core'));
361 container.setStyle('display', 'none');
362 img.set('src', M.util.image_url('t/collapsed', 'core'));
368 //toggle_textarea.apply(ta, [false]);
369 //// reset textarea size
370 ta.on('click', function() {
371 this.toggle_textarea(true);
373 //ta.onkeypress = function() {
374 //if (this.scrollHeight > this.clientHeight && !window.opera)
377 ta.on('blur', function() {
378 this.toggle_textarea(false);
381 this.register_actions();
384 toggle_textarea: function(focus) {
385 var t = Y.one('#dlg-content-'+this.client_id);
387 if (t.get('value') == M.str.moodle.addcomment) {
389 t.setStyle('color', 'black');
392 if (t.get('value') == '') {
393 t.set('value', M.str.moodle.addcomment);
394 t.setStyle('color','grey');
400 var container = Y.one('#comment-list-'+this.client_id);
401 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
405 new CommentHelper(options);
407 init_admin: function(Y) {
408 var select_all = Y.one('#comment_select_all');
409 select_all.on('click', function(e) {
410 var comments = document.getElementsByName('comments');
412 for (var i in comments) {
413 if (comments[i].checked) {
417 for (i in comments) {
418 comments[i].checked = !checked;
420 this.set('checked', !checked);
423 var comments_delete = Y.one('#comments_delete');
424 if (comments_delete) {
425 comments_delete.on('click', function(e) {
428 var comments = document.getElementsByName('comments');
429 for (var i in comments) {
430 if (typeof comments[i] == 'object' && comments[i].checked) {
431 list += (comments[i].value + '-');
438 args.message = M.str.admin.confirmdeletecomments;
439 args.callback = function() {
440 var url = M.cfg.wwwroot + '/comment/index.php';
444 'sesskey': M.cfg.sesskey,
450 complete: function(id,o,p) {
455 if (o.responseText == 'yes') {
464 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
465 'User-Agent': 'MoodleComment/3.0'
467 data: build_querystring(data)
471 M.util.show_confirm_dialog(e, args);