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.courseid = args.courseid;
41 this.contextid = args.contextid;
49 Y.one('#comment-link-'+this.client_id).setStyle('display', 'none');
52 Y.one('#comment-link-'+this.client_id).on('click', function(e) {
57 CommentHelper.confirmoverlay = new Y.Overlay({
58 bodyContent: '<a href="#" id="confirmdelete-'+this.client_id+'">'+M.str.moodle.yes+'</a> <a href="#" id="canceldelete-'+this.client_id+'">'+M.str.moodle.no+'</a>',
61 CommentHelper.confirmoverlay.render(document.body);
64 var ta = Y.one('#dlg-content-'+this.client_id);
66 var value = ta.get('value');
67 if (value && value != M.str.moodle.addcomment) {
68 var params = {'content': value};
73 callback: function(id, obj, args) {
74 var scope = args.scope;
75 var cid = scope.client_id;
76 var ta = Y.one('#dlg-content-'+cid);
78 var container = Y.one('#comment-list-'+cid);
79 var result = scope.render([obj], true);
80 var newcomment = Y.Node.create(result.html);
81 container.appendChild(newcomment);
83 var linktext = Y.one('#comment-link-text-'+cid);
84 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
87 color: { to: '#06e' },
88 backgroundColor: { to: '#FFE390' }
90 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
93 scope.register_pagination();
94 scope.register_delete_buttons();
99 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
101 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
105 request: function(args, noloading) {
109 scope = args['scope'];
111 //params['page'] = args.page?args.page:'';
113 // the form element only accept certain file types
114 params['sesskey'] = M.cfg.sesskey;
115 params['action'] = args.action?args.action:'';
116 params['client_id'] = this.client_id;
117 params['itemid'] = this.itemid;
118 params['area'] = this.commentarea;
119 params['courseid'] = this.courseid;
120 params['contextid'] = this.contextid;
121 if (args['params']) {
122 for (i in args['params']) {
123 params[i] = args['params'][i];
129 complete: function(id,o,p) {
134 var data = Y.JSON.parse(o.responseText);
139 args.callback(id,data,p);
148 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
149 'User-Agent': 'MoodleComment/3.0'
151 data: build_querystring(params)
154 cfg.form = args.form;
161 render: function(list, newcmt) {
164 var template = Y.one('#cmt-tmpl');
167 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
168 var val = template.get('innerHTML');
169 val = val.replace('___name___', list[i].username);
170 if (list[i]['delete']||newcmt) {
171 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;
173 val = val.replace('___time___', list[i].time);
174 val = val.replace('___picture___', list[i].avatar);
175 val = val.replace('___content___', list[i].content);
176 val = '<li id="'+htmlid+'">'+val+'</li>';
177 ret.ids.push(htmlid);
183 load: function(page) {
185 var container = Y.one('#comment-ctrl-'+this.client_id);
192 callback: function(id, ret, args) {
193 var linktext = Y.one('#comment-link-text-'+scope.client_id);
194 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
195 var container = Y.one('#comment-list-'+scope.client_id);
196 var pagination = Y.one('#comment-pagination-'+scope.client_id);
197 if (ret.pagination) {
198 pagination.set('innerHTML', ret.pagination);
201 pagination.set('innerHTML', '');
203 var result = scope.render(ret.list);
204 container.set('innerHTML', result.html);
205 args.scope.register_pagination();
206 args.scope.register_delete_buttons();
211 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
213 var params = {'commentid': id};
214 scope.cancel_delete();
215 function remove_dom(type, anmi, cmt) {
222 callback: function(id, resp, args) {
223 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
228 var cmt = Y.one('#'+htmlid);
229 cmt.setStyle('overflow', 'hidden');
230 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
231 anim.onComplete.subscribe(remove_dom, cmt, this);
236 register_actions: function() {
238 Y.one('#comment-action-post-'+this.client_id).on('click', function(e) {
243 // cancel comment box
244 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
246 cancel.on('click', function(e) {
253 register_delete_buttons: function() {
256 Y.all('div.comment-delete a').each(
258 var theid = node.get('id');
259 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
260 var commentid = theid.match(parseid);
262 Y.Event.purgeElement('#'+theid, false, 'click');
264 node.on('click', function(e, node) {
266 var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
267 var re = new RegExp("(\\d+).*", "i");
268 var result = width.match(re);
270 width = Number(result[1]);
274 CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY+10]);
275 CommentHelper.confirmoverlay.set('visible', true);
276 // XXX: YUI3 bug, a temp workaround in firefox, still have problem on webkit
277 CommentHelper.confirmoverlay.bodyNode.setStyle('visibility', 'visible');
278 Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
280 scope.cancel_delete();
282 Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
283 Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
286 scope.dodelete(commentid[1]);
293 cancel_delete: function() {
294 CommentHelper.confirmoverlay.set('visible', false);
295 // XXX: YUI3 bug, a temp workaround in firefox, still have problem on webkit
296 CommentHelper.confirmoverlay.bodyNode.setStyle('visibility', 'hidden');
298 register_pagination: function() {
301 Y.all('#comment-pagination-'+this.client_id+' a').each(
303 node.on('click', function(e, node) {
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]);
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=='') {
320 if (this.env != 'block_comments') {
323 this.register_delete_buttons();
324 this.register_pagination();
326 container.setStyle('display', 'block');
327 img.src=M.util.image_url('t/expanded', 'core');
330 container.setStyle('display', 'none');
331 img.src=M.util.image_url('t/collapsed', 'core');
334 //toggle_textarea.apply(ta, [false]);
335 //// reset textarea size
336 ta.on('click', function() {
337 this.toggle_textarea(true);
339 //ta.onkeypress = function() {
340 //if (this.scrollHeight > this.clientHeight && !window.opera)
343 ta.on('blur', function() {
344 this.toggle_textarea(false);
346 this.register_actions();
349 toggle_textarea: function(focus) {
350 var t = Y.one('#dlg-content-'+this.client_id);
352 if (t.get('value') == M.str.moodle.addcomment) {
354 t.setStyle('color', 'black');
357 if (t.get('value') == '') {
358 t.set('value', M.str.moodle.addcomment);
359 t.setStyle('color','grey');
365 var container = Y.one('#comment-list-'+this.client_id);
366 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading', 'core')+'" /></div>');
370 new CommentHelper(options);
372 init_admin: function(Y) {
373 var select_all = Y.one('#comment_select_all');
374 select_all.on('click', function(e) {
375 var comments = document.getElementsByName('comments');
377 for (var i in comments) {
378 if (comments[i].checked) {
382 for (i in comments) {
383 comments[i].checked = !checked;
385 this.set('checked', !checked);
388 var comments_delete = Y.one('#comments_delete');
389 if (comments_delete) {
390 comments_delete.on('click', function(e) {
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 + '-');
403 args.message = M.str.admin.confirmdeletecomments;
404 args.callback = function() {
405 var url = M.cfg.wwwroot + '/comment/index.php';
409 'sesskey': M.cfg.sesskey,
415 complete: function(id,o,p) {
420 if (o.responseText == 'yes') {
429 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
430 'User-Agent': 'MoodleComment/3.0'
432 data: build_querystring(data)
436 M.util.show_confirm_dialog(e, args);