"MDL-19118, comments api"
[moodle.git] / comment / comment.js
CommitLineData
1bcb7eb5 1/**
2 * Javascript for comments 2.0
3 */
4
5function cmt_replace(client_id,list,newcmt) {
6 var ret = {};
7 ret.ids = [];
8 var template = document.getElementById('cmt-tmpl');
9 var html = '';
10 for(var i in list) {
11 var htmlid = 'comment-'+list[i].id+'-'+client_id;
12 var val = template.innerHTML;
13 val = val.replace('___name___', list[i].username);
14 if (list[i]['delete']||newcmt) {
15 list[i].time += ' <a href="###" title="'+mstr.moodle.delete+'" onclick="delete_comment(\''+client_id+'\',\''+list[i].id+'\')"><img src="'+moodle_cfg.wwwroot+'/pix/t/delete.gif" /></a>';
16 }
17 val = val.replace('___time___', list[i].time);
18 val = val.replace('___picture___', list[i].avatar);
19 val = val.replace('___content___', list[i].content);
20 val = '<li id="'+htmlid+'">'+val+'</li>';
21 ret.ids.push(htmlid);
22 html = (val+html);
23 }
24 ret.html = html;
25 return ret;
26}
27function cmt_load(cid) {
28 var container = document.getElementById('comment-list-'+cid);
29 container.innerHTML = '<div style="text-align:center"><img src="'+moodle_cfg.wwwroot+'/pix/i/loading.gif'+'" /></div>';
30}
31function get_comments(client_id, area, itemid, page) {
32 var url = moodle_cfg.wwwroot + '/comment/comment_ajax.php';
33 var data = {
34 'courseid': comment_params.courseid,
35 'contextid': comment_params.contextid,
36 'area': area,
37 'itemid': itemid,
38 'page': page,
39 'client_id': client_id,
40 'sesskey': moodle_cfg.sesskey
41 }
42 this.cb = {
43 success: function(o) {
44 try {
45 var ret = YAHOO.lang.JSON.parse(o.responseText);
46 } catch(e) {
47 alert("JSON ERROR: "+o.responseText);
48 }
49 if (!comment_check_response(ret)) {
50 return;
51 }
52 var container = document.getElementById('comment-list-'+ret.client_id);
53 var pagination = document.getElementById('comment-pagination-'+ret.client_id);
54 if (ret.pagination) {
55 pagination.innerHTML = ret.pagination;
56 } else {
57 //empty paging bar
58 pagination.innerHTML = '';
59 }
60 var result = cmt_replace(ret.client_id, ret.list);
61 container.innerHTML = result.html;
62 }
63 }
64 cmt_load(client_id);
65 var trans = YAHOO.util.Connect.asyncRequest('POST',
66 url+'?action=get', this.cb, build_querystring(data));
67}
68function post_comment(cid) {
69 this.cb = {
70 success: function(o) {
71 try {
72 var resp = YAHOO.lang.JSON.parse(o.responseText);
73 } catch(e) {
74 alert("JSON ERROR: "+o.responseText);
75 }
76 if (!comment_check_response(resp)) {
77 return;
78 }
79 if(resp) {
80 var cid = resp.client_id;
81 var ta = document.getElementById('dlg-content-'+cid);
82 ta.value = '';
83 var container = document.getElementById('comment-list-'+cid);
84 var result = cmt_replace(cid,[resp], true);
85 container.innerHTML += result.html;
86 var ids = result.ids;
87 for(var i in ids) {
88 var attributes = {
89 color: { to: '#06e' },
90 backgroundColor: { to: '#FFE390' }
91 };
92 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
93 anim.animate();
94 }
95 }
96 }
97 }
98 var ta = document.getElementById('dlg-content-'+cid);
99 if (ta.value && ta.value != mstr.moodle.addcomment) {
100 var url = moodle_cfg.wwwroot + '/comment/comment_ajax.php';
101 var formObject = document.getElementById('comment-form-'+cid);
102 YAHOO.util.Connect.setForm(formObject);
103 var trans = YAHOO.util.Connect.asyncRequest('POST', url+'?action=add', this.cb);
104 } else {
105 var attributes = {
106 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
107 };
108 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
109 anim.animate();
110 }
111}
112function delete_comment(client_id, comment_id) {
113 var url = moodle_cfg.wwwroot + '/comment/comment_ajax.php';
114 var data = {
115 'courseid': comment_params.courseid,
116 'contextid': comment_params.contextid,
117 'commentid': comment_id,
118 'client_id': client_id,
119 'sesskey': moodle_cfg.sesskey
120 }
121 this.cb = {
122 success: function(o) {
123 try {
124 var resp = YAHOO.lang.JSON.parse(o.responseText);
125 } catch(e) {
126 alert("JSON ERROR: "+o.responseText);
127 }
128 if (!comment_check_response(resp)) {
129 return;
130 }
131 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
132 this.el = document.getElementById(htmlid);
133 this.el.style.overflow = 'hidden';
134 var attributes = {
135 width:{to:0},
136 height:{to:0}
137 };
138 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
139 anim.onComplete.subscribe(this.remove_dom, [], this);
140 anim.animate();
141 },
142 remove_dom: function() {
143 this.el.parentNode.removeChild(this.el);
144 }
145 }
146 var trans = YAHOO.util.Connect.asyncRequest('POST',
147 url+'?action=delete', this.cb, build_querystring(data));
148}
149function view_comments(client_id, area, itemid, page) {
150 var container = document.getElementById('comment-ctrl-'+client_id);
151 var ta = document.getElementById('dlg-content-'+client_id);
152 var img = document.getElementById('comment-img-'+client_id);
153 if (container.style.display=='none'||container.style.display=='') {
154 // show
155 get_comments(client_id, area, itemid, page);
156 container.style.display = 'block';
157 img.src=moodle_cfg.wwwroot+'/pix/t/expanded.png';
158 } else {
159 // hide
160 container.style.display = 'none';
161 img.src=moodle_cfg.wwwroot+'/pix/t/collapsed.png';
162 ta.value = '';
163 }
164 toggle_textarea.apply(ta, [false]);
165 // reset textarea size
166 ta.onclick = function() {
167 toggle_textarea.apply(this, [true]);
168 }
169 ta.onkeypress = function() {
170 if (this.scrollHeight > this.clientHeight && !window.opera)
171 this.rows += 1;
172 }
173 ta.onblur = function() {
174 toggle_textarea.apply(this, [false]);
175 }
176 return false;
177}
178function comment_hide_link(cid) {
179 var link = document.getElementById('comment-link-'+cid);
180 if(link){
181 link.style.display='none';
182 } else {
183 alert('wront');
184 }
185}
186function toggle_textarea(focus) {
187 if (focus) {
188 if (this.value == mstr.moodle.addcomment) {
189 this.value = '';
190 this.style.color = 'black';
191 }
192 }else{
193 if (this.value == '') {
194 this.value = mstr.moodle.addcomment;
195 this.style.color = 'grey';
196 this.rows = 1;
197 }
198 }
199}
200function comment_check_response(data) {
201 if (data.error) {
202 alert(data.error);
203 return false;
204 }
205 return true;
206}