| 1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
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.
|
|
|
7 |
//
|
|
|
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.
|
|
|
12 |
//
|
|
|
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/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Comment Helper
|
|
|
18 |
* @author Dongsheng Cai <dongsheng@moodle.com>
|
|
|
19 |
*/
|
|
|
20 |
M.core_comment = {
|
|
|
21 |
/**
|
|
|
22 |
* Initialize commenting system
|
|
|
23 |
*/
|
|
|
24 |
init: function(Y, options) {
|
|
|
25 |
var CommentHelper = function(args) {
|
|
|
26 |
CommentHelper.superclass.constructor.apply(this, arguments);
|
|
|
27 |
};
|
|
|
28 |
CommentHelper.NAME = "COMMENT";
|
|
|
29 |
CommentHelper.ATTRS = {
|
|
|
30 |
options: {},
|
|
|
31 |
lang: {}
|
|
|
32 |
};
|
|
|
33 |
Y.extend(CommentHelper, Y.Base, {
|
|
|
34 |
api: M.cfg.wwwroot+'/comment/comment_ajax.php',
|
|
|
35 |
initializer: function(args) {
|
|
|
36 |
var scope = this;
|
|
|
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
|
|
|
45 |
// then discarded.
|
|
|
46 |
if (!Y.one('#comment-ctrl-'+this.client_id)) {
|
|
|
47 |
return;
|
|
|
48 |
}
|
|
|
49 |
// expand comments?
|
|
|
50 |
if (this.autostart) {
|
|
|
51 |
this.view(args.page);
|
|
|
52 |
}
|
|
|
53 |
// load comments
|
|
|
54 |
var handle = Y.one('#comment-link-'+this.client_id);
|
|
|
55 |
// hide toggle link
|
|
|
56 |
if (handle) {
|
|
|
57 |
if (args.notoggle) {
|
|
|
58 |
handle.setStyle('display', 'none');
|
|
|
59 |
}
|
|
|
60 |
handle.on('click', function(e) {
|
|
|
61 |
e.preventDefault();
|
|
|
62 |
this.view(0);
|
|
|
63 |
return false;
|
|
|
64 |
}, this);
|
|
|
65 |
// Also handle space/enter key.
|
|
|
66 |
handle.on('key', function(e) {
|
|
|
67 |
e.preventDefault();
|
|
|
68 |
this.view(0);
|
|
|
69 |
return false;
|
|
|
70 |
}, '13,32', this);
|
|
|
71 |
}
|
|
|
72 |
scope.toggle_textarea(false);
|
|
|
73 |
},
|
|
|
74 |
post: function() {
|
| 1441 |
ariadna |
75 |
var container = Y.one('#comment-list-'+this.client_id);
|
| 1 |
efrain |
76 |
var ta = Y.one('#dlg-content-'+this.client_id);
|
|
|
77 |
var scope = this;
|
|
|
78 |
var value = ta.get('value');
|
|
|
79 |
if (value && value != M.util.get_string('addcomment', 'moodle')) {
|
|
|
80 |
ta.set('disabled', true);
|
| 1441 |
ariadna |
81 |
var spinner = M.util.add_spinner(Y, container);
|
|
|
82 |
spinner.show();
|
| 1 |
efrain |
83 |
var params = {'content': value};
|
|
|
84 |
this.request({
|
|
|
85 |
action: 'add',
|
|
|
86 |
scope: scope,
|
|
|
87 |
params: params,
|
|
|
88 |
callback: async function(id, obj, args) {
|
|
|
89 |
var scope = args.scope;
|
|
|
90 |
var cid = scope.client_id;
|
|
|
91 |
var ta = Y.one('#dlg-content-'+cid);
|
|
|
92 |
ta.set('value', '');
|
|
|
93 |
ta.set('disabled', false);
|
| 1441 |
ariadna |
94 |
spinner.remove();
|
| 1 |
efrain |
95 |
scope.toggle_textarea(false);
|
|
|
96 |
var container = Y.one('#comment-list-'+cid);
|
|
|
97 |
var result = await scope.render([obj], true);
|
|
|
98 |
var newcomment = Y.Node.create(result.html);
|
|
|
99 |
container.appendChild(newcomment);
|
|
|
100 |
var ids = result.ids;
|
| 11 |
efrain |
101 |
var linkTextCount = Y.one('#comment-link-text-' + cid + ' .comment-link-count');
|
|
|
102 |
if (linkTextCount) {
|
|
|
103 |
linkTextCount.set('innerHTML', obj.count);
|
| 1 |
efrain |
104 |
}
|
|
|
105 |
for(var i in ids) {
|
|
|
106 |
var attributes = {
|
|
|
107 |
color: { to: '#06e' },
|
|
|
108 |
backgroundColor: { to: '#FFE390' }
|
|
|
109 |
};
|
|
|
110 |
var anim = new Y.YUI2.util.ColorAnim(ids[i], attributes);
|
|
|
111 |
anim.animate();
|
|
|
112 |
}
|
|
|
113 |
scope.register_pagination();
|
|
|
114 |
scope.register_delete_buttons();
|
|
|
115 |
}
|
|
|
116 |
}, true);
|
|
|
117 |
} else {
|
|
|
118 |
var attributes = {
|
|
|
119 |
backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
|
|
|
120 |
};
|
|
|
121 |
var anim = new Y.YUI2.util.ColorAnim('dlg-content-'+cid, attributes);
|
|
|
122 |
anim.animate();
|
|
|
123 |
}
|
|
|
124 |
},
|
|
|
125 |
request: function(args, noloading) {
|
|
|
126 |
var params = {};
|
|
|
127 |
var scope = this;
|
|
|
128 |
if (args['scope']) {
|
|
|
129 |
scope = args['scope'];
|
|
|
130 |
}
|
|
|
131 |
//params['page'] = args.page?args.page:'';
|
|
|
132 |
// the form element only accept certain file types
|
|
|
133 |
params['sesskey'] = M.cfg.sesskey;
|
|
|
134 |
params['action'] = args.action?args.action:'';
|
|
|
135 |
params['client_id'] = this.client_id;
|
|
|
136 |
params['itemid'] = this.itemid;
|
|
|
137 |
params['area'] = this.commentarea;
|
|
|
138 |
params['courseid'] = this.courseid;
|
|
|
139 |
params['contextid'] = this.contextid;
|
|
|
140 |
params['component'] = this.component;
|
|
|
141 |
if (args['params']) {
|
|
|
142 |
for (i in args['params']) {
|
|
|
143 |
params[i] = args['params'][i];
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
var cfg = {
|
|
|
147 |
method: 'POST',
|
|
|
148 |
on: {
|
|
|
149 |
complete: function(id,o,p) {
|
|
|
150 |
if (!o) {
|
|
|
151 |
alert('IO FATAL');
|
|
|
152 |
return false;
|
|
|
153 |
}
|
|
|
154 |
var data = Y.JSON.parse(o.responseText);
|
|
|
155 |
if (data.error) {
|
|
|
156 |
if (data.error == 'require_login') {
|
|
|
157 |
args.callback(id,data,p);
|
|
|
158 |
return true;
|
|
|
159 |
}
|
|
|
160 |
alert(data.error);
|
|
|
161 |
return false;
|
|
|
162 |
} else {
|
|
|
163 |
args.callback(id,data,p);
|
|
|
164 |
return true;
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
},
|
|
|
168 |
arguments: {
|
|
|
169 |
scope: scope
|
|
|
170 |
},
|
|
|
171 |
headers: {
|
|
|
172 |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
173 |
},
|
|
|
174 |
data: build_querystring(params)
|
|
|
175 |
};
|
|
|
176 |
if (args.form) {
|
|
|
177 |
cfg.form = args.form;
|
|
|
178 |
}
|
|
|
179 |
Y.io(this.api, cfg);
|
|
|
180 |
if (!noloading) {
|
|
|
181 |
this.wait();
|
|
|
182 |
}
|
|
|
183 |
},
|
|
|
184 |
render: async function(list, newcmt) {
|
|
|
185 |
var ret = {};
|
|
|
186 |
ret.ids = [];
|
|
|
187 |
var template = Y.one('#cmt-tmpl');
|
|
|
188 |
var html = '';
|
|
|
189 |
for(var i in list) {
|
|
|
190 |
var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
|
|
|
191 |
var val = template.get('innerHTML');
|
|
|
192 |
if (list[i].profileurl) {
|
|
|
193 |
val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
|
|
|
194 |
} else {
|
|
|
195 |
val = val.replace('___name___', list[i].fullname);
|
|
|
196 |
}
|
|
|
197 |
if (list[i].delete || newcmt) {
|
|
|
198 |
list[i].clientid = this.client_id;
|
|
|
199 |
list[i].content += await this.renderDeleteIcon(list[i]);
|
|
|
200 |
}
|
|
|
201 |
val = val.replace('___time___', list[i].time);
|
|
|
202 |
val = val.replace('___picture___', list[i].avatar);
|
|
|
203 |
val = val.replace('___content___', list[i].content);
|
|
|
204 |
val = '<li id="'+htmlid+'">'+val+'</li>';
|
|
|
205 |
ret.ids.push(htmlid);
|
|
|
206 |
html = (val+html);
|
|
|
207 |
}
|
|
|
208 |
ret.html = html;
|
|
|
209 |
return ret;
|
|
|
210 |
},
|
|
|
211 |
renderDeleteIcon: async function(list) {
|
|
|
212 |
return new Promise(function(resolve) {
|
|
|
213 |
require(['core/templates', 'core/str'], (Templates, Str) => {
|
|
|
214 |
return Str.get_string('deletecommentbyon', 'moodle', {
|
|
|
215 |
user: list.fullname,
|
|
|
216 |
time: list.time
|
|
|
217 |
}).then(function(deleteStr) {
|
|
|
218 |
return Templates.renderPix('t/delete', 'core', deleteStr).then(function(deleteIcon) {
|
|
|
219 |
var deleteDiv = document.createElement('div');
|
|
|
220 |
deleteDiv.className = 'comment-delete';
|
|
|
221 |
|
|
|
222 |
var deleteLink = document.createElement('a');
|
|
|
223 |
deleteLink.href = '#';
|
|
|
224 |
deleteLink.role = 'button';
|
|
|
225 |
deleteLink.title = deleteStr;
|
|
|
226 |
deleteLink.id = `comment-delete-${list.clientid}-${list.id}`;
|
|
|
227 |
deleteLink.innerHTML = deleteIcon;
|
|
|
228 |
|
|
|
229 |
deleteDiv.appendChild(deleteLink);
|
|
|
230 |
|
|
|
231 |
resolve(deleteDiv.outerHTML);
|
|
|
232 |
|
|
|
233 |
return true;
|
|
|
234 |
});
|
|
|
235 |
});
|
|
|
236 |
});
|
|
|
237 |
});
|
|
|
238 |
},
|
|
|
239 |
load: function(page) {
|
|
|
240 |
var scope = this;
|
|
|
241 |
var container = Y.one('#comment-ctrl-'+this.client_id);
|
|
|
242 |
var params = {
|
|
|
243 |
'action': 'get',
|
|
|
244 |
'page': page
|
|
|
245 |
};
|
|
|
246 |
this.request({
|
|
|
247 |
scope: scope,
|
|
|
248 |
params: params,
|
|
|
249 |
callback: async function(id, ret, args) {
|
| 11 |
efrain |
250 |
var linkTextCount = Y.one('#comment-link-text-' + scope.client_id + ' .comment-link-count');
|
|
|
251 |
if (linkTextCount) {
|
|
|
252 |
linkTextCount.set('innerHTML', ret.count);
|
| 1 |
efrain |
253 |
}
|
|
|
254 |
var container = Y.one('#comment-list-'+scope.client_id);
|
|
|
255 |
var pagination = Y.one('#comment-pagination-'+scope.client_id);
|
|
|
256 |
if (ret.pagination) {
|
|
|
257 |
pagination.set('innerHTML', ret.pagination);
|
|
|
258 |
} else {
|
|
|
259 |
//empty paging bar
|
|
|
260 |
pagination.set('innerHTML', '');
|
|
|
261 |
}
|
|
|
262 |
if (ret.error == 'require_login') {
|
|
|
263 |
var result = {};
|
|
|
264 |
result.html = M.util.get_string('commentsrequirelogin', 'moodle');
|
|
|
265 |
} else {
|
|
|
266 |
var result = await scope.render(ret.list);
|
|
|
267 |
}
|
|
|
268 |
container.set('innerHTML', result.html);
|
|
|
269 |
var img = Y.one('#comment-img-'+scope.client_id);
|
|
|
270 |
if (img) {
|
|
|
271 |
img.set('src', M.util.image_url('t/expanded', 'core'));
|
|
|
272 |
}
|
|
|
273 |
args.scope.register_pagination();
|
|
|
274 |
args.scope.register_delete_buttons();
|
|
|
275 |
}
|
|
|
276 |
});
|
|
|
277 |
},
|
|
|
278 |
|
|
|
279 |
dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
|
|
|
280 |
var scope = this,
|
|
|
281 |
cid = scope.client_id,
|
|
|
282 |
params = {'commentid': id};
|
|
|
283 |
function remove_dom(type, anim, cmt) {
|
|
|
284 |
cmt.remove();
|
| 11 |
efrain |
285 |
var linkTextCount = Y.one('#comment-link-text-' + cid + ' .comment-link-count'),
|
| 1 |
efrain |
286 |
comments = Y.all('#comment-list-' + cid + ' li');
|
| 11 |
efrain |
287 |
if (linkTextCount) {
|
|
|
288 |
linkTextCount.set('innerHTML', comments.size());
|
| 1 |
efrain |
289 |
}
|
|
|
290 |
}
|
|
|
291 |
this.request({
|
|
|
292 |
action: 'delete',
|
|
|
293 |
scope: scope,
|
|
|
294 |
params: params,
|
|
|
295 |
callback: function(id, resp, args) {
|
|
|
296 |
var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
|
|
|
297 |
var attributes = {
|
|
|
298 |
width:{to:0},
|
|
|
299 |
height:{to:0}
|
|
|
300 |
};
|
|
|
301 |
var cmt = Y.one('#'+htmlid);
|
|
|
302 |
cmt.setStyle('overflow', 'hidden');
|
|
|
303 |
var anim = new Y.YUI2.util.Anim(htmlid, attributes, 1, Y.YUI2.util.Easing.easeOut);
|
|
|
304 |
anim.onComplete.subscribe(remove_dom, cmt, this);
|
|
|
305 |
anim.animate();
|
|
|
306 |
}
|
|
|
307 |
}, true);
|
|
|
308 |
},
|
|
|
309 |
register_actions: function() {
|
|
|
310 |
// add new comment
|
|
|
311 |
var action_btn = Y.one('#comment-action-post-'+this.client_id);
|
|
|
312 |
if (action_btn) {
|
|
|
313 |
action_btn.on('click', function(e) {
|
|
|
314 |
e.preventDefault();
|
|
|
315 |
this.post();
|
|
|
316 |
return false;
|
|
|
317 |
}, this);
|
|
|
318 |
}
|
|
|
319 |
// cancel comment box
|
|
|
320 |
var cancel = Y.one('#comment-action-cancel-'+this.client_id);
|
|
|
321 |
if (cancel) {
|
|
|
322 |
cancel.on('click', function(e) {
|
|
|
323 |
e.preventDefault();
|
|
|
324 |
this.view(0);
|
|
|
325 |
return false;
|
|
|
326 |
}, this);
|
|
|
327 |
}
|
|
|
328 |
},
|
|
|
329 |
register_delete_buttons: function() {
|
|
|
330 |
var scope = this;
|
|
|
331 |
// page buttons
|
|
|
332 |
Y.all('div.comment-delete a').each(
|
|
|
333 |
function(node, id) {
|
|
|
334 |
var theid = node.get('id');
|
|
|
335 |
var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
|
|
|
336 |
var commentid = theid.match(parseid);
|
|
|
337 |
if (!commentid) {
|
|
|
338 |
return;
|
|
|
339 |
}
|
|
|
340 |
if (commentid[1]) {
|
|
|
341 |
Y.Event.purgeElement('#'+theid, false, 'click');
|
|
|
342 |
}
|
|
|
343 |
node.on('click', function(e) {
|
|
|
344 |
e.preventDefault();
|
|
|
345 |
if (commentid[1]) {
|
|
|
346 |
scope.dodelete(commentid[1]);
|
|
|
347 |
}
|
|
|
348 |
});
|
|
|
349 |
// Also handle space/enter key.
|
|
|
350 |
node.on('key', function(e) {
|
|
|
351 |
e.preventDefault();
|
|
|
352 |
if (commentid[1]) {
|
|
|
353 |
scope.dodelete(commentid[1]);
|
|
|
354 |
}
|
|
|
355 |
}, '13,32');
|
|
|
356 |
// 13 and 32 are the keycodes for space and enter.
|
|
|
357 |
}
|
|
|
358 |
);
|
|
|
359 |
},
|
|
|
360 |
register_pagination: function() {
|
|
|
361 |
var scope = this;
|
|
|
362 |
// page buttons
|
|
|
363 |
Y.all('#comment-pagination-'+this.client_id+' a').each(
|
|
|
364 |
function(node, id) {
|
|
|
365 |
node.on('click', function(e, node) {
|
|
|
366 |
e.preventDefault();
|
|
|
367 |
var id = node.get('id');
|
|
|
368 |
var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
|
|
|
369 |
var result = id.match(re);
|
|
|
370 |
this.load(result[1]);
|
|
|
371 |
}, scope, node);
|
|
|
372 |
}
|
|
|
373 |
);
|
|
|
374 |
},
|
|
|
375 |
view: function(page) {
|
|
|
376 |
var commenttoggler = Y.one('#comment-link-' + this.client_id);
|
|
|
377 |
var container = Y.one('#comment-ctrl-'+this.client_id);
|
|
|
378 |
var ta = Y.one('#dlg-content-'+this.client_id);
|
|
|
379 |
var img = Y.one('#comment-img-'+this.client_id);
|
|
|
380 |
var d = container.getStyle('display');
|
|
|
381 |
if (d=='none'||d=='') {
|
|
|
382 |
// show
|
|
|
383 |
if (!this.autostart) {
|
|
|
384 |
this.load(page);
|
|
|
385 |
} else {
|
|
|
386 |
this.register_delete_buttons();
|
|
|
387 |
this.register_pagination();
|
|
|
388 |
}
|
|
|
389 |
container.setStyle('display', 'block');
|
|
|
390 |
if (img) {
|
|
|
391 |
img.set('src', M.util.image_url('t/expanded', 'core'));
|
|
|
392 |
}
|
|
|
393 |
if (commenttoggler) {
|
|
|
394 |
commenttoggler.setAttribute('aria-expanded', 'true');
|
|
|
395 |
}
|
|
|
396 |
} else {
|
|
|
397 |
// hide
|
|
|
398 |
container.setStyle('display', 'none');
|
|
|
399 |
var collapsedimage = 't/collapsed'; // ltr mode
|
|
|
400 |
if ( Y.one(document.body).hasClass('dir-rtl') ) {
|
|
|
401 |
collapsedimage = 't/collapsed_rtl';
|
|
|
402 |
}
|
|
|
403 |
if (img) {
|
|
|
404 |
img.set('src', M.util.image_url(collapsedimage, 'core'));
|
|
|
405 |
}
|
|
|
406 |
if (ta) {
|
|
|
407 |
ta.set('value','');
|
|
|
408 |
}
|
|
|
409 |
if (commenttoggler) {
|
|
|
410 |
commenttoggler.setAttribute('aria-expanded', 'false');
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
if (ta) {
|
|
|
414 |
//toggle_textarea.apply(ta, [false]);
|
|
|
415 |
//// reset textarea size
|
|
|
416 |
ta.on('focus', function() {
|
|
|
417 |
this.toggle_textarea(true);
|
|
|
418 |
}, this);
|
|
|
419 |
//ta.onkeypress = function() {
|
|
|
420 |
//if (this.scrollHeight > this.clientHeight && !window.opera)
|
|
|
421 |
//this.rows += 1;
|
|
|
422 |
//}
|
|
|
423 |
ta.on('blur', function() {
|
|
|
424 |
this.toggle_textarea(false);
|
|
|
425 |
}, this);
|
|
|
426 |
}
|
|
|
427 |
this.register_actions();
|
|
|
428 |
return false;
|
|
|
429 |
},
|
|
|
430 |
toggle_textarea: function(focus) {
|
|
|
431 |
var t = Y.one('#dlg-content-'+this.client_id);
|
|
|
432 |
if (!t) {
|
|
|
433 |
return false;
|
|
|
434 |
}
|
|
|
435 |
if (focus) {
|
|
|
436 |
if (t.get('value') == M.util.get_string('addcomment', 'moodle')) {
|
|
|
437 |
t.set('value', '');
|
|
|
438 |
t.setStyle('color', 'black');
|
|
|
439 |
}
|
|
|
440 |
}else{
|
|
|
441 |
if (t.get('value') == '') {
|
|
|
442 |
t.set('value', M.util.get_string('addcomment', 'moodle'));
|
|
|
443 |
t.setStyle('color','grey');
|
|
|
444 |
t.set('rows', 2);
|
|
|
445 |
}
|
|
|
446 |
}
|
|
|
447 |
},
|
|
|
448 |
wait: function() {
|
|
|
449 |
var container = Y.one('#comment-list-'+this.client_id);
|
| 1441 |
ariadna |
450 |
container.set('innerHTML', '');
|
|
|
451 |
M.util.add_spinner(Y, container).show();
|
| 1 |
efrain |
452 |
}
|
|
|
453 |
});
|
|
|
454 |
|
|
|
455 |
new CommentHelper(options);
|
|
|
456 |
}
|
|
|
457 |
};
|