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);
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 scope.toggle_textarea(false);
79 var container = Y.one('#comment-list-'+cid);
80 var result = scope.render([obj], true);
81 var newcomment = Y.Node.create(result.html);
82 container.appendChild(newcomment);
84 var linktext = Y.one('#comment-link-text-'+cid);
86 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
90 color: { to: '#06e' },
91 backgroundColor: { to: '#FFE390' }
93 var anim = new Y.YUI2.util.ColorAnim(ids[i], attributes);
96 scope.register_pagination();
97 scope.register_delete_buttons();
102 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
104 var anim = new Y.YUI2.util.ColorAnim('dlg-content-'+cid, attributes);
108 request: function(args, noloading) {
112 scope = args['scope'];
114 //params['page'] = args.page?args.page:'';
115 // the form element only accept certain file types
116 params['sesskey'] = M.cfg.sesskey;
117 params['action'] = args.action?args.action:'';
118 params['client_id'] = this.client_id;
119 params['itemid'] = this.itemid;
120 params['area'] = this.commentarea;
121 params['courseid'] = this.courseid;
122 params['contextid'] = this.contextid;
123 params['component'] = this.component;
124 if (args['params']) {
125 for (i in args['params']) {
126 params[i] = args['params'][i];
132 complete: function(id,o,p) {
137 var data = Y.JSON.parse(o.responseText);
139 if (data.error == 'require_login') {
140 args.callback(id,data,p);
146 args.callback(id,data,p);
155 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
157 data: build_querystring(params)
160 cfg.form = args.form;
167 render: function(list, newcmt) {
170 var template = Y.one('#cmt-tmpl');
173 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
174 var val = template.get('innerHTML');
175 if (list[i].profileurl) {
176 val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
178 val = val.replace('___name___', list[i].fullname);
180 if (list[i]['delete']||newcmt) {
181 list[i].content = '<div class="comment-delete"><a href="#" id ="comment-delete-'+this.client_id+'-'+list[i].id+'" title="'+M.str.moodle.deletecomment+'"><img alt="" src="'+M.util.image_url('t/delete', 'core')+'" /></a></div>' + list[i].content;
183 val = val.replace('___time___', list[i].time);
184 val = val.replace('___picture___', list[i].avatar);
185 val = val.replace('___content___', list[i].content);
186 val = '<li id="'+htmlid+'">'+val+'</li>';
187 ret.ids.push(htmlid);
193 load: function(page) {
195 var container = Y.one('#comment-ctrl-'+this.client_id);
203 callback: function(id, ret, args) {
204 var linktext = Y.one('#comment-link-text-'+scope.client_id);
205 if (ret.count && linktext) {
206 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
208 var container = Y.one('#comment-list-'+scope.client_id);
209 var pagination = Y.one('#comment-pagination-'+scope.client_id);
210 if (ret.pagination) {
211 pagination.set('innerHTML', ret.pagination);
214 pagination.set('innerHTML', '');
216 if (ret.error == 'require_login') {
218 result.html = M.str.moodle.commentsrequirelogin;
220 var result = scope.render(ret.list);
222 container.set('innerHTML', result.html);
223 var img = Y.one('#comment-img-'+scope.client_id);
225 img.set('src', M.util.image_url('t/expanded', 'core'));
227 args.scope.register_pagination();
228 args.scope.register_delete_buttons();
233 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
235 var params = {'commentid': id};
236 function remove_dom(type, anim, cmt) {
243 callback: function(id, resp, args) {
244 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
249 var cmt = Y.one('#'+htmlid);
250 cmt.setStyle('overflow', 'hidden');
251 var anim = new Y.YUI2.util.Anim(htmlid, attributes, 1, Y.YUI2.util.Easing.easeOut);
252 anim.onComplete.subscribe(remove_dom, cmt, this);
257 register_actions: function() {
259 var action_btn = Y.one('#comment-action-post-'+this.client_id);
261 action_btn.on('click', function(e) {
267 // cancel comment box
268 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
270 cancel.on('click', function(e) {
277 register_delete_buttons: function() {
280 Y.all('div.comment-delete a').each(
282 var theid = node.get('id');
283 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
284 var commentid = theid.match(parseid);
289 Y.Event.purgeElement('#'+theid, false, 'click');
291 node.on('click', function(e) {
294 scope.dodelete(commentid[1]);
297 // Also handle space/enter key.
298 node.on('key', function(e) {
301 scope.dodelete(commentid[1]);
304 // 13 and 32 are the keycodes for space and enter.
308 register_pagination: function() {
311 Y.all('#comment-pagination-'+this.client_id+' a').each(
313 node.on('click', function(e, node) {
315 var id = node.get('id');
316 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
317 var result = id.match(re);
318 this.load(result[1]);
323 view: function(page) {
324 var container = Y.one('#comment-ctrl-'+this.client_id);
325 var ta = Y.one('#dlg-content-'+this.client_id);
326 var img = Y.one('#comment-img-'+this.client_id);
327 var d = container.getStyle('display');
328 if (d=='none'||d=='') {
330 if (!this.autostart) {
333 this.register_delete_buttons();
334 this.register_pagination();
336 container.setStyle('display', 'block');
338 img.set('src', M.util.image_url('t/expanded', 'core'));
342 container.setStyle('display', 'none');
343 var collapsedimage = 't/collapsed'; // ltr mode
344 if ( Y.one(document.body).hasClass('dir-rtl') ) {
345 collapsedimage = 't/collapsed_rtl';
347 collapsedimage = 't/collapsed';
349 img.set('src', M.util.image_url(collapsedimage, 'core'));
355 //toggle_textarea.apply(ta, [false]);
356 //// reset textarea size
357 ta.on('focus', function() {
358 this.toggle_textarea(true);
360 //ta.onkeypress = function() {
361 //if (this.scrollHeight > this.clientHeight && !window.opera)
364 ta.on('blur', function() {
365 this.toggle_textarea(false);
368 this.register_actions();
371 toggle_textarea: function(focus) {
372 var t = Y.one('#dlg-content-'+this.client_id);
377 if (t.get('value') == M.str.moodle.addcomment) {
379 t.setStyle('color', 'black');
382 if (t.get('value') == '') {
383 t.set('value', M.str.moodle.addcomment);
384 t.setStyle('color','grey');
390 var container = Y.one('#comment-list-'+this.client_id);
391 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
395 new CommentHelper(options);
397 init_admin: function(Y) {
398 var select_all = Y.one('#comment_select_all');
399 select_all.on('click', function(e) {
400 var comments = document.getElementsByName('comments');
402 for (var i in comments) {
403 if (comments[i].checked) {
407 for (i in comments) {
408 comments[i].checked = !checked;
410 this.set('checked', !checked);
413 var comments_delete = Y.one('#comments_delete');
414 if (comments_delete) {
415 comments_delete.on('click', function(e) {
418 var comments = document.getElementsByName('comments');
419 for (var i in comments) {
420 if (typeof comments[i] == 'object' && comments[i].checked) {
421 list += (comments[i].value + '-');
428 args.message = M.str.admin.confirmdeletecomments;
429 args.callback = function() {
430 var url = M.cfg.wwwroot + '/comment/index.php';
434 'sesskey': M.cfg.sesskey,
440 complete: function(id,o,p) {
445 if (o.responseText == 'yes') {
454 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
456 data: build_querystring(data)
460 M.util.show_confirm_dialog(e, args);