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