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) {
60 // Also handle space/enter key.
61 handle.on('key', function(e) {
67 scope.toggle_textarea(false);
70 var ta = Y.one('#dlg-content-'+this.client_id);
72 var value = ta.get('value');
73 if (value && value != M.util.get_string('addcomment', 'moodle')) {
74 ta.set('disabled', true);
76 'backgroundImage': 'url(' + M.util.image_url('i/loading_small', 'core') + ')',
77 'backgroundRepeat': 'no-repeat',
78 'backgroundPosition': 'center center'
80 var params = {'content': value};
85 callback: async function(id, obj, args) {
86 var scope = args.scope;
87 var cid = scope.client_id;
88 var ta = Y.one('#dlg-content-'+cid);
90 ta.set('disabled', false);
91 ta.setStyle('backgroundImage', 'none');
92 scope.toggle_textarea(false);
93 var container = Y.one('#comment-list-'+cid);
94 var result = await scope.render([obj], true);
95 var newcomment = Y.Node.create(result.html);
96 container.appendChild(newcomment);
98 var linkText = Y.one('#comment-link-text-' + cid);
100 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', obj.count));
104 color: { to: '#06e' },
105 backgroundColor: { to: '#FFE390' }
107 var anim = new Y.YUI2.util.ColorAnim(ids[i], attributes);
110 scope.register_pagination();
111 scope.register_delete_buttons();
116 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
118 var anim = new Y.YUI2.util.ColorAnim('dlg-content-'+cid, attributes);
122 request: function(args, noloading) {
126 scope = args['scope'];
128 //params['page'] = args.page?args.page:'';
129 // the form element only accept certain file types
130 params['sesskey'] = M.cfg.sesskey;
131 params['action'] = args.action?args.action:'';
132 params['client_id'] = this.client_id;
133 params['itemid'] = this.itemid;
134 params['area'] = this.commentarea;
135 params['courseid'] = this.courseid;
136 params['contextid'] = this.contextid;
137 params['component'] = this.component;
138 if (args['params']) {
139 for (i in args['params']) {
140 params[i] = args['params'][i];
146 complete: function(id,o,p) {
151 var data = Y.JSON.parse(o.responseText);
153 if (data.error == 'require_login') {
154 args.callback(id,data,p);
160 args.callback(id,data,p);
169 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
171 data: build_querystring(params)
174 cfg.form = args.form;
181 render: async function(list, newcmt) {
184 var template = Y.one('#cmt-tmpl');
187 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
188 var val = template.get('innerHTML');
189 if (list[i].profileurl) {
190 val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
192 val = val.replace('___name___', list[i].fullname);
194 if (list[i].delete || newcmt) {
195 list[i].clientid = this.client_id;
196 list[i].content += await this.renderDeleteIcon(list[i]);
198 val = val.replace('___time___', list[i].time);
199 val = val.replace('___picture___', list[i].avatar);
200 val = val.replace('___content___', list[i].content);
201 val = '<li id="'+htmlid+'">'+val+'</li>';
202 ret.ids.push(htmlid);
208 renderDeleteIcon: async function(list) {
209 return new Promise(function(resolve) {
210 require(['core/templates', 'core/str'], (Templates, Str) => {
211 return Str.get_string('deletecommentbyon', 'moodle', {
214 }).then(function(deleteStr) {
215 return Templates.renderPix('t/delete', 'core', deleteStr).then(function(deleteIcon) {
216 var deleteDiv = document.createElement('div');
217 deleteDiv.className = 'comment-delete';
219 var deleteLink = document.createElement('a');
220 deleteLink.href = '#';
221 deleteLink.role = 'button';
222 deleteLink.title = deleteStr;
223 deleteLink.id = `comment-delete-${list.clientid}-${list.id}`;
224 deleteLink.innerHTML = deleteIcon;
226 deleteDiv.appendChild(deleteLink);
228 resolve(deleteDiv.outerHTML);
236 load: function(page) {
238 var container = Y.one('#comment-ctrl-'+this.client_id);
246 callback: async function(id, ret, args) {
247 var linkText = Y.one('#comment-link-text-' + scope.client_id);
248 if (ret.count && linkText) {
249 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', ret.count));
251 var container = Y.one('#comment-list-'+scope.client_id);
252 var pagination = Y.one('#comment-pagination-'+scope.client_id);
253 if (ret.pagination) {
254 pagination.set('innerHTML', ret.pagination);
257 pagination.set('innerHTML', '');
259 if (ret.error == 'require_login') {
261 result.html = M.util.get_string('commentsrequirelogin', 'moodle');
263 var result = await scope.render(ret.list);
265 container.set('innerHTML', result.html);
266 var img = Y.one('#comment-img-'+scope.client_id);
268 img.set('src', M.util.image_url('t/expanded', 'core'));
270 args.scope.register_pagination();
271 args.scope.register_delete_buttons();
276 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
278 cid = scope.client_id,
279 params = {'commentid': id};
280 function remove_dom(type, anim, cmt) {
282 var linkText = Y.one('#comment-link-text-' + cid),
283 comments = Y.all('#comment-list-' + cid + ' li');
284 if (linkText && comments) {
285 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', comments.size()));
292 callback: function(id, resp, args) {
293 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
298 var cmt = Y.one('#'+htmlid);
299 cmt.setStyle('overflow', 'hidden');
300 var anim = new Y.YUI2.util.Anim(htmlid, attributes, 1, Y.YUI2.util.Easing.easeOut);
301 anim.onComplete.subscribe(remove_dom, cmt, this);
306 register_actions: function() {
308 var action_btn = Y.one('#comment-action-post-'+this.client_id);
310 action_btn.on('click', function(e) {
316 // cancel comment box
317 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
319 cancel.on('click', function(e) {
326 register_delete_buttons: function() {
329 Y.all('div.comment-delete a').each(
331 var theid = node.get('id');
332 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
333 var commentid = theid.match(parseid);
338 Y.Event.purgeElement('#'+theid, false, 'click');
340 node.on('click', function(e) {
343 scope.dodelete(commentid[1]);
346 // Also handle space/enter key.
347 node.on('key', function(e) {
350 scope.dodelete(commentid[1]);
353 // 13 and 32 are the keycodes for space and enter.
357 register_pagination: function() {
360 Y.all('#comment-pagination-'+this.client_id+' a').each(
362 node.on('click', function(e, node) {
364 var id = node.get('id');
365 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
366 var result = id.match(re);
367 this.load(result[1]);
372 view: function(page) {
373 var commenttoggler = Y.one('#comment-link-' + this.client_id);
374 var container = Y.one('#comment-ctrl-'+this.client_id);
375 var ta = Y.one('#dlg-content-'+this.client_id);
376 var img = Y.one('#comment-img-'+this.client_id);
377 var d = container.getStyle('display');
378 if (d=='none'||d=='') {
380 if (!this.autostart) {
383 this.register_delete_buttons();
384 this.register_pagination();
386 container.setStyle('display', 'block');
388 img.set('src', M.util.image_url('t/expanded', 'core'));
390 if (commenttoggler) {
391 commenttoggler.setAttribute('aria-expanded', 'true');
395 container.setStyle('display', 'none');
396 var collapsedimage = 't/collapsed'; // ltr mode
397 if ( Y.one(document.body).hasClass('dir-rtl') ) {
398 collapsedimage = 't/collapsed_rtl';
400 collapsedimage = 't/collapsed';
403 img.set('src', M.util.image_url(collapsedimage, 'core'));
408 if (commenttoggler) {
409 commenttoggler.setAttribute('aria-expanded', 'false');
413 //toggle_textarea.apply(ta, [false]);
414 //// reset textarea size
415 ta.on('focus', function() {
416 this.toggle_textarea(true);
418 //ta.onkeypress = function() {
419 //if (this.scrollHeight > this.clientHeight && !window.opera)
422 ta.on('blur', function() {
423 this.toggle_textarea(false);
426 this.register_actions();
429 toggle_textarea: function(focus) {
430 var t = Y.one('#dlg-content-'+this.client_id);
435 if (t.get('value') == M.util.get_string('addcomment', 'moodle')) {
437 t.setStyle('color', 'black');
440 if (t.get('value') == '') {
441 t.set('value', M.util.get_string('addcomment', 'moodle'));
442 t.setStyle('color','grey');
448 var container = Y.one('#comment-list-'+this.client_id);
449 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
453 new CommentHelper(options);