web service MDL-13126 little change into create user description (it was not correct...
[moodle.git] / comment / comment.js
CommitLineData
adacb0fe
DC
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
1bcb7eb5 16/**
adacb0fe
DC
17 * Comment Helper
18 * @author Dongsheng Cai <dongsheng@moodle.com>
1bcb7eb5 19 */
ef502357
PS
20M.core_comment = {
21 /**
22 * Initialize commenting system
23 */
3b01539c 24 init: function(Y, options) {
ef502357
PS
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.courseid = args.courseid;
41 this.contextid = args.contextid;
76951100
DC
42 this.env = args.env;
43 // expand comments?
ef502357
PS
44 if (args.autostart) {
45 this.view(args.page);
46 }
76951100 47 // hide toggle link
ef502357
PS
48 if (args.notoggle) {
49 Y.one('#comment-link-'+this.client_id).setStyle('display', 'none');
50 }
51 // load comments
52 Y.one('#comment-link-'+this.client_id).on('click', function(e) {
53 e.preventDefault();
54 this.view(0);
55 return false;
56 }, this);
57 },
58 post: function() {
59 var ta = Y.one('#dlg-content-'+this.client_id);
60 var scope = this;
61 var value = ta.get('value');
2b728cb5 62 if (value && value != M.str.moodle.addcomment) {
ef502357
PS
63 var params = {'content': value};
64 this.request({
65 action: 'add',
66 scope: scope,
67 params: params,
68 callback: function(id, obj, args) {
69 var scope = args.scope;
70 var cid = scope.client_id;
71 var ta = Y.one('#dlg-content-'+cid);
72 ta.set('value', '');
73 var container = Y.one('#comment-list-'+cid);
74 var result = scope.render([obj], true);
75 var newcomment = Y.Node.create(result.html);
76 container.appendChild(newcomment);
77 var ids = result.ids;
78 var linktext = Y.one('#comment-link-text-'+cid);
2b728cb5 79 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
ef502357
PS
80 for(var i in ids) {
81 var attributes = {
82 color: { to: '#06e' },
83 backgroundColor: { to: '#FFE390' }
84 };
85 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
86 anim.animate();
87 }
76951100
DC
88 scope.register_pagination();
89 scope.register_delete_buttons();
adacb0fe 90 }
ef502357
PS
91 }, true);
92 } else {
93 var attributes = {
94 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
95 };
96 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
97 anim.animate();
98 }
99 },
100 request: function(args, noloading) {
101 var params = {};
102 var scope = this;
103 if (args['scope']) {
104 scope = args['scope'];
105 }
106 //params['page'] = args.page?args.page:'';
107 params['env'] = '';
108 // the form element only accept certain file types
109 params['sesskey'] = M.cfg.sesskey;
110 params['action'] = args.action?args.action:'';
111 params['client_id'] = this.client_id;
112 params['itemid'] = this.itemid;
113 params['area'] = this.commentarea;
114 params['courseid'] = this.courseid;
115 params['contextid'] = this.contextid;
116 if (args['params']) {
117 for (i in args['params']) {
118 params[i] = args['params'][i];
adacb0fe 119 }
adacb0fe 120 }
ef502357
PS
121 var cfg = {
122 method: 'POST',
123 on: {
124 complete: function(id,o,p) {
125 if (!o) {
126 alert('IO FATAL');
127 return;
128 }
384ab39a 129 var data = Y.JSON.parse(o.responseText);
ef502357
PS
130 if (data.error) {
131 alert(data.error);
132 return false;
133 } else {
134 args.callback(id,data,p);
135 return true;
136 }
7e7d2e64 137 }
ef502357
PS
138 },
139 arguments: {
140 scope: scope
141 },
142 headers: {
143 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
144 'User-Agent': 'MoodleComment/3.0'
145 },
146 data: build_querystring(params)
147 };
148 if (args.form) {
149 cfg.form = args.form;
adacb0fe 150 }
ef502357
PS
151 Y.io(this.api, cfg);
152 if (!noloading) {
153 this.wait();
154 }
155 },
156 render: function(list, newcmt) {
157 var ret = {};
158 ret.ids = [];
159 var template = Y.one('#cmt-tmpl');
160 var html = '';
161 for(var i in list) {
162 var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
163 var val = template.get('innerHTML');
164 val = val.replace('___name___', list[i].username);
165 if (list[i]['delete']||newcmt) {
2b728cb5 166 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;
adacb0fe 167 }
ef502357
PS
168 val = val.replace('___time___', list[i].time);
169 val = val.replace('___picture___', list[i].avatar);
170 val = val.replace('___content___', list[i].content);
171 val = '<li id="'+htmlid+'">'+val+'</li>';
172 ret.ids.push(htmlid);
173 html = (val+html);
adacb0fe 174 }
ef502357
PS
175 ret.html = html;
176 return ret;
177 },
178 load: function(page) {
179 var scope = this;
180 var container = Y.one('#comment-ctrl-'+this.client_id);
181 var params = {
182 'page': page,
1bcb7eb5 183 }
ef502357
PS
184 this.request({
185 scope: scope,
186 params: params,
187 callback: function(id, ret, args) {
188 var linktext = Y.one('#comment-link-text-'+scope.client_id);
2b728cb5 189 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
ef502357
PS
190 var container = Y.one('#comment-list-'+scope.client_id);
191 var pagination = Y.one('#comment-pagination-'+scope.client_id);
192 if (ret.pagination) {
193 pagination.set('innerHTML', ret.pagination);
194 } else {
195 //empty paging bar
196 pagination.set('innerHTML', '');
adacb0fe 197 }
ef502357
PS
198 var result = scope.render(ret.list);
199 container.set('innerHTML', result.html);
200 args.scope.register_pagination();
201 args.scope.register_delete_buttons();
202 }
203 });
204 },
4c508047 205
bdf69389 206 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
ef502357
PS
207 var scope = this;
208 var params = {'commentid': id};
209
210 function remove_dom(type, anmi, cmt) {
211 cmt.remove();
adacb0fe 212 }
ef502357
PS
213 this.request({
214 action: 'delete',
215 scope: scope,
216 params: params,
217 callback: function(id, resp, args) {
218 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
219 var attributes = {
220 width:{to:0},
221 height:{to:0}
222 };
223 var cmt = Y.one('#'+htmlid);
224 cmt.setStyle('overflow', 'hidden');
225 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
226 anim.onComplete.subscribe(remove_dom, cmt, this);
227 anim.animate();
228 }
229 }, true);
230 },
231 register_actions: function() {
232 // add new comment
233 Y.one('#comment-action-post-'+this.client_id).on('click', function(e) {
234 e.preventDefault();
235 this.post();
236 return false;
237 }, this);
238 // cancel comment box
239 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
240 if (cancel) {
241 cancel.on('click', function(e) {
242 e.preventDefault();
243 this.view(0);
244 return false;
245 }, this);
adacb0fe 246 }
ef502357
PS
247 },
248 register_delete_buttons: function() {
249 var scope = this;
250 // page buttons
251 Y.all('div.comment-content a').each(
252 function(node, id) {
2057b5ef
DC
253 var theid = node.get('id');
254 var re = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
255 var result = theid.match(re);
256 if (result[1]) {
257 Y.Event.purgeElement('#'+theid, false, 'click');
258 }
ef502357 259 node.on('click', function(e, node) {
ef502357 260 if (result[1]) {
38673bfe 261 this.dodelete(result[1]);
ef502357
PS
262 }
263 //this.load(result[1]);
264 }, scope, node);
265 }
266 );
267 },
268 register_pagination: function() {
269 var scope = this;
270 // page buttons
3e34183a 271 Y.all('#comment-pagination-'+this.client_id+' a').each(
ef502357
PS
272 function(node, id) {
273 node.on('click', function(e, node) {
274 var id = node.get('id');
275 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
276 var result = id.match(re);
277 this.load(result[1]);
278 }, scope, node);
279 }
280 );
281 },
282 view: function(page) {
283 var container = Y.one('#comment-ctrl-'+this.client_id);
284 var ta = Y.one('#dlg-content-'+this.client_id);
285 var img = Y.one('#comment-img-'+this.client_id);
286 var d = container.getStyle('display');
287 if (d=='none'||d=='') {
288 // show
76951100
DC
289 if (this.env != 'block_comments') {
290 this.load(page);
291 } else {
292 this.register_delete_buttons();
293 this.register_pagination();
294 }
ef502357 295 container.setStyle('display', 'block');
bca09754 296 img.src=M.util.image_url('t/expanded', 'core');
ef502357
PS
297 } else {
298 // hide
299 container.setStyle('display', 'none');
bca09754 300 img.src=M.util.image_url('t/collapsed', 'core');
ef502357 301 ta.set('value','');
adacb0fe 302 }
ef502357
PS
303 //toggle_textarea.apply(ta, [false]);
304 //// reset textarea size
305 ta.on('click', function() {
306 this.toggle_textarea(true);
307 }, this)
308 //ta.onkeypress = function() {
309 //if (this.scrollHeight > this.clientHeight && !window.opera)
310 //this.rows += 1;
311 //}
312 ta.on('blur', function() {
313 this.toggle_textarea(false);
314 }, this);
315 this.register_actions();
316 return false;
317 },
318 toggle_textarea: function(focus) {
319 var t = Y.one('#dlg-content-'+this.client_id);
320 if (focus) {
2b728cb5 321 if (t.get('value') == M.str.moodle.addcomment) {
ef502357
PS
322 t.set('value', '');
323 t.setStyle('color', 'black');
324 }
325 }else{
326 if (t.get('value') == '') {
2b728cb5 327 t.set('value', M.str.moodle.addcomment);
ef502357 328 t.setStyle('color','grey');
76951100 329 t.set('rows', 2);
ef502357 330 }
adacb0fe 331 }
ef502357
PS
332 },
333 wait: function() {
334 var container = Y.one('#comment-list-'+this.client_id);
5aaa76ea 335 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading', 'core')+'" /></div>');
adacb0fe 336 }
ef502357
PS
337 });
338
339 new CommentHelper(options);
340 }
341};