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