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