rating MDL-21657 put default scale in a constant
[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;
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');
2b728cb5 59 if (value && value != M.str.moodle.addcomment) {
ef502357
PS
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);
2b728cb5 76 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
ef502357
PS
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 }
384ab39a 124 var data = Y.JSON.parse(o.responseText);
ef502357
PS
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) {
2b728cb5 161 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 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);
2b728cb5 184 linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
ef502357
PS
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 },
4c508047 200
bdf69389 201 dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
ef502357
PS
202 var scope = this;
203 var params = {'commentid': id};
204
205 function remove_dom(type, anmi, cmt) {
206 cmt.remove();
adacb0fe 207 }
ef502357
PS
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;
214 var attributes = {
215 width:{to:0},
216 height:{to:0}
217 };
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);
222 anim.animate();
223 }
224 }, true);
225 },
226 register_actions: function() {
227 // add new comment
228 Y.one('#comment-action-post-'+this.client_id).on('click', function(e) {
229 e.preventDefault();
230 this.post();
231 return false;
232 }, this);
233 // cancel comment box
234 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
235 if (cancel) {
236 cancel.on('click', function(e) {
237 e.preventDefault();
238 this.view(0);
239 return false;
240 }, this);
adacb0fe 241 }
ef502357
PS
242 },
243 register_delete_buttons: function() {
244 var scope = this;
245 // page buttons
246 Y.all('div.comment-content 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-delete-"+this.client_id+"-(\\d+)", "i");
251 var result = id.match(re);
252 if (result[1]) {
38673bfe 253 this.dodelete(result[1]);
ef502357
PS
254 }
255 //this.load(result[1]);
256 }, scope, node);
257 }
258 );
259 },
260 register_pagination: function() {
261 var scope = this;
262 // page buttons
263 Y.all('#comment-paging-'+this.client_id+' a').each(
264 function(node, id) {
265 node.on('click', function(e, node) {
266 var id = node.get('id');
267 var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
268 var result = id.match(re);
269 this.load(result[1]);
270 }, scope, node);
271 }
272 );
273 },
274 view: function(page) {
275 var container = Y.one('#comment-ctrl-'+this.client_id);
276 var ta = Y.one('#dlg-content-'+this.client_id);
277 var img = Y.one('#comment-img-'+this.client_id);
278 var d = container.getStyle('display');
279 if (d=='none'||d=='') {
280 // show
281 this.load(page);
282 container.setStyle('display', 'block');
bca09754 283 img.src=M.util.image_url('t/expanded', 'core');
ef502357
PS
284 } else {
285 // hide
286 container.setStyle('display', 'none');
bca09754 287 img.src=M.util.image_url('t/collapsed', 'core');
ef502357 288 ta.set('value','');
adacb0fe 289 }
ef502357
PS
290 //toggle_textarea.apply(ta, [false]);
291 //// reset textarea size
292 ta.on('click', function() {
293 this.toggle_textarea(true);
294 }, this)
295 //ta.onkeypress = function() {
296 //if (this.scrollHeight > this.clientHeight && !window.opera)
297 //this.rows += 1;
298 //}
299 ta.on('blur', function() {
300 this.toggle_textarea(false);
301 }, this);
302 this.register_actions();
303 return false;
304 },
305 toggle_textarea: function(focus) {
306 var t = Y.one('#dlg-content-'+this.client_id);
307 if (focus) {
2b728cb5 308 if (t.get('value') == M.str.moodle.addcomment) {
ef502357
PS
309 t.set('value', '');
310 t.setStyle('color', 'black');
311 }
312 }else{
313 if (t.get('value') == '') {
2b728cb5 314 t.set('value', M.str.moodle.addcomment);
ef502357
PS
315 t.setStyle('color','grey');
316 t.set('rows', 1);
317 }
adacb0fe 318 }
ef502357
PS
319 },
320 wait: function() {
321 var container = Y.one('#comment-list-'+this.client_id);
bca09754 322 container.set('innerHTML', '<div style="text-align:center"><img src="'+M.util.image_url('i/loading', 'core')+'" /></div>');
adacb0fe 323 }
ef502357
PS
324 });
325
326 new CommentHelper(options);
327 }
328};