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);
44 // Fail fast if the comments element cannot be found, such as in embedded-type views where blocks may be loaded
46 if (!Y.one('#comment-ctrl-'+this.client_id)) {
54 var handle = Y.one('#comment-link-'+this.client_id);
58 handle.setStyle('display', 'none');
60 handle.on('click', function(e) {
65 // Also handle space/enter key.
66 handle.on('key', function(e) {
72 scope.toggle_textarea(false);
75 var ta = Y.one('#dlg-content-'+this.client_id);
77 var value = ta.get('value');
78 if (value && value != M.util.get_string('addcomment', 'moodle')) {
79 ta.set('disabled', true);
81 'backgroundImage': 'url(' + M.util.image_url('i/loading_small', 'core') + ')',
82 'backgroundRepeat': 'no-repeat',
83 'backgroundPosition': 'center center'
85 var params = {'content': value};
90 callback: async function(id, obj, args) {
91 var scope = args.scope;
92 var cid = scope.client_id;
93 var ta = Y.one('#dlg-content-'+cid);
95 ta.set('disabled', false);
96 ta.setStyle('backgroundImage', 'none');
97 scope.toggle_textarea(false);
98 var container = Y.one('#comment-list-'+cid);
99 var result = await scope.render([obj], true);
100 var newcomment = Y.Node.create(result.html);
101 container.appendChild(newcomment);
102 var ids = result.ids;
103 var linkText = Y.one('#comment-link-text-' + cid);
105 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', obj.count));
109 color: { to: '#06e' },
110 backgroundColor: { to: '#FFE390' }
112 var anim = new Y.YUI2.util.ColorAnim(ids[i], attributes);
115 scope.register_pagination();
116 scope.register_delete_buttons();
121 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
123 var anim = new Y.YUI2.util.ColorAnim('dlg-content-'+cid, attributes);
127 request: function(args, noloading) {
131 scope = args['scope'];
133 //params['page'] = args.page?args.page:'';
134 // the form element only accept certain file types
135 params['sesskey'] = M.cfg.sesskey;
136 params['action'] = args.action?args.action:'';
137 params['client_id'] = this.client_id;
138 params['itemid'] = this.itemid;
139 params['area'] = this.commentarea;
140 params['courseid'] = this.courseid;
141 params['contextid'] = this.contextid;
142 params['component'] = this.component;
143 if (args['params']) {
144 for (i in args['params']) {
145 params[i] = args['params'][i];
151 complete: function(id,o,p) {
156 var data = Y.JSON.parse(o.responseText);
158 if (data.error == 'require_login') {
159 args.callback(id,data,p);
165 args.callback(id,data,p);
174 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
176 data: build_querystring(params)
179 cfg.form = args.form;
186 render: async function(list, newcmt) {
189 var template = Y.one('#cmt-tmpl');
192 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
193 var val = template.get('innerHTML');
194 if (list[i].profileurl) {
195 val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
197 val = val.replace('___name___', list[i].fullname);
199 if (list[i].delete || newcmt) {
200 list[i].clientid = this.client_id;
201 list[i].content += await this.renderDeleteIcon(list[i]);
203 val = val.replace('___time___', list[i].time);
204 val = val.replace('___picture___', list[i].avatar);
205 val = val.replace('___content___', list[i].content);
206 val = '<li id="'+htmlid+'">'+val+'</li>';
207 ret.ids.push(htmlid);
213 renderDeleteIcon: async function(list) {
214 return new Promise(function(resolve) {
215 require(['core/templates', 'core/str'], (Templates, Str) => {
216 return Str.get_string('deletecommentbyon', 'moodle', {
219 }).then(function(deleteStr) {
220 return Templates.renderPix('t/delete', 'core', deleteStr).then(function(deleteIcon) {
221 var deleteDiv = document.createElement('div');
222 deleteDiv.className = 'comment-delete';
224 var deleteLink = document.createElement('a');
225 deleteLink.href = '#';
226 deleteLink.role = 'button';
227 deleteLink.title = deleteStr;
228 deleteLink.id = `comment-delete-${list.clientid}-${list.id}`;
229 deleteLink.innerHTML = deleteIcon;
231 deleteDiv.appendChild(deleteLink);
233 resolve(deleteDiv.outerHTML);
241 load: function(page) {
243 var container = Y.one('#comment-ctrl-'+this.client_id);
251 callback: async function(id, ret, args) {
252 var linkText = Y.one('#comment-link-text-' + scope.client_id);
253 if (ret.count && linkText) {
254 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', ret.count));
256 var container = Y.one('#comment-list-'+scope.client_id);
257 var pagination = Y.one('#comment-pagination-'+scope.client_id);
258 if (ret.pagination) {
259 pagination.set('innerHTML', ret.pagination);
262 pagination.set('innerHTML', '');
264 if (ret.error == 'require_login') {
266 result.html = M.util.get_string('commentsrequirelogin', 'moodle');
268 var result = await scope.render(ret.list);
270 container.set('innerHTML', result.html);
271 var img = Y.one('#comment-img-'+scope.client_id);
273 img.set('src', M.util.image_url('t/expanded', 'core'));
275 args.scope.register_pagination();
276 args.scope.register_delete_buttons();
281 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
283 cid = scope.client_id,
284 params = {'commentid': id};
285 function remove_dom(type, anim, cmt) {
287 var linkText = Y.one('#comment-link-text-' + cid),
288 comments = Y.all('#comment-list-' + cid + ' li');
289 if (linkText && comments) {
290 linkText.set('innerHTML', M.util.get_string('commentscount', 'moodle', comments.size()));
297 callback: function(id, resp, args) {
298 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
303 var cmt = Y.one('#'+htmlid);
304 cmt.setStyle('overflow', 'hidden');
305 var anim = new Y.YUI2.util.Anim(htmlid, attributes, 1, Y.YUI2.util.Easing.easeOut);
306 anim.onComplete.subscribe(remove_dom, cmt, this);
311 register_actions: function() {
313 var action_btn = Y.one('#comment-action-post-'+this.client_id);
315 action_btn.on('click', function(e) {
321 // cancel comment box
322 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
324 cancel.on('click', function(e) {
331 register_delete_buttons: function() {
334 Y.all('div.comment-delete a').each(
336 var theid = node.get('id');
337 var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
338 var commentid = theid.match(parseid);
343 Y.Event.purgeElement('#'+theid, false, 'click');
345 node.on('click', function(e) {
348 scope.dodelete(commentid[1]);
351 // Also handle space/enter key.
352 node.on('key', function(e) {
355 scope.dodelete(commentid[1]);
358 // 13 and 32 are the keycodes for space and enter.
362 register_pagination: function() {
365 Y.all('#comment-pagination-'+this.client_id+' a').each(
367 node.on('click', function(e, node) {
369 var id = node.get('id');
370 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
371 var result = id.match(re);
372 this.load(result[1]);
377 view: function(page) {
378 var commenttoggler = Y.one('#comment-link-' + this.client_id);
379 var container = Y.one('#comment-ctrl-'+this.client_id);
380 var ta = Y.one('#dlg-content-'+this.client_id);
381 var img = Y.one('#comment-img-'+this.client_id);
382 var d = container.getStyle('display');
383 if (d=='none'||d=='') {
385 if (!this.autostart) {
388 this.register_delete_buttons();
389 this.register_pagination();
391 container.setStyle('display', 'block');
393 img.set('src', M.util.image_url('t/expanded', 'core'));
395 if (commenttoggler) {
396 commenttoggler.setAttribute('aria-expanded', 'true');
400 container.setStyle('display', 'none');
401 var collapsedimage = 't/collapsed'; // ltr mode
402 if ( Y.one(document.body).hasClass('dir-rtl') ) {
403 collapsedimage = 't/collapsed_rtl';
405 collapsedimage = 't/collapsed';
408 img.set('src', M.util.image_url(collapsedimage, 'core'));
413 if (commenttoggler) {
414 commenttoggler.setAttribute('aria-expanded', 'false');
418 //toggle_textarea.apply(ta, [false]);
419 //// reset textarea size
420 ta.on('focus', function() {
421 this.toggle_textarea(true);
423 //ta.onkeypress = function() {
424 //if (this.scrollHeight > this.clientHeight && !window.opera)
427 ta.on('blur', function() {
428 this.toggle_textarea(false);
431 this.register_actions();
434 toggle_textarea: function(focus) {
435 var t = Y.one('#dlg-content-'+this.client_id);
440 if (t.get('value') == M.util.get_string('addcomment', 'moodle')) {
442 t.setStyle('color', 'black');
445 if (t.get('value') == '') {
446 t.set('value', M.util.get_string('addcomment', 'moodle'));
447 t.setStyle('color','grey');
453 var container = Y.one('#comment-list-'+this.client_id);
454 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
458 new CommentHelper(options);