navigation MDL-21418 Changed regex to replace all characters not just a-zA-Z0-9 thank...
[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) {
9598d578 15 list[i].content = '<div class="comment-delete"><a href="###" title="'+mstr.moodle.deletecomment+'" onclick="delete_comment(\''+client_id+'\',\''+list[i].id+'\')"><img src="'+M.cfg.wwwroot+'/pix/t/delete.gif" /></a></div>' + list[i].content;
1bcb7eb5 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);
9598d578 29 container.innerHTML = '<div style="text-align:center"><img src="'+M.cfg.wwwroot+'/pix/i/loading.gif'+'" /></div>';
1bcb7eb5 30}
31function get_comments(client_id, area, itemid, page) {
9598d578 32 var url = M.cfg.wwwroot + '/comment/comment_ajax.php';
1bcb7eb5 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,
9598d578 40 'sesskey': M.cfg.sesskey
1bcb7eb5 41 }
42 this.cb = {
43 success: function(o) {
da964d22 44 var ret = json_decode(o.responseText);
1bcb7eb5 45 if (!comment_check_response(ret)) {
46 return;
47 }
b9f6ca70 48 var linktext = document.getElementById('comment-link-text-'+ret.client_id);
49 linktext.innerHTML = mstr.moodle.comments + ' ('+ret.count+')';
1bcb7eb5 50 var container = document.getElementById('comment-list-'+ret.client_id);
51 var pagination = document.getElementById('comment-pagination-'+ret.client_id);
52 if (ret.pagination) {
53 pagination.innerHTML = ret.pagination;
54 } else {
55 //empty paging bar
56 pagination.innerHTML = '';
57 }
58 var result = cmt_replace(ret.client_id, ret.list);
59 container.innerHTML = result.html;
60 }
61 }
62 cmt_load(client_id);
63 var trans = YAHOO.util.Connect.asyncRequest('POST',
64 url+'?action=get', this.cb, build_querystring(data));
65}
66function post_comment(cid) {
67 this.cb = {
68 success: function(o) {
da964d22 69 var resp = json_decode(o.responseText);
1bcb7eb5 70 if (!comment_check_response(resp)) {
71 return;
72 }
73 if(resp) {
74 var cid = resp.client_id;
75 var ta = document.getElementById('dlg-content-'+cid);
76 ta.value = '';
77 var container = document.getElementById('comment-list-'+cid);
78 var result = cmt_replace(cid,[resp], true);
79 container.innerHTML += result.html;
80 var ids = result.ids;
b9f6ca70 81 var linktext = document.getElementById('comment-link-text-'+resp.client_id);
82 linktext.innerHTML = mstr.moodle.comments + ' ('+resp.count+')';
1bcb7eb5 83 for(var i in ids) {
84 var attributes = {
85 color: { to: '#06e' },
86 backgroundColor: { to: '#FFE390' }
87 };
88 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
6f5e0852 89 anim.animate();
1bcb7eb5 90 }
91 }
92 }
93 }
94 var ta = document.getElementById('dlg-content-'+cid);
95 if (ta.value && ta.value != mstr.moodle.addcomment) {
9598d578 96 var url = M.cfg.wwwroot + '/comment/comment_ajax.php';
1bcb7eb5 97 var formObject = document.getElementById('comment-form-'+cid);
98 YAHOO.util.Connect.setForm(formObject);
99 var trans = YAHOO.util.Connect.asyncRequest('POST', url+'?action=add', this.cb);
100 } else {
101 var attributes = {
102 backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
103 };
104 var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
6f5e0852 105 anim.animate();
1bcb7eb5 106 }
107}
108function delete_comment(client_id, comment_id) {
9598d578 109 var url = M.cfg.wwwroot + '/comment/comment_ajax.php';
1bcb7eb5 110 var data = {
111 'courseid': comment_params.courseid,
112 'contextid': comment_params.contextid,
113 'commentid': comment_id,
114 'client_id': client_id,
9598d578 115 'sesskey': M.cfg.sesskey
1bcb7eb5 116 }
117 this.cb = {
118 success: function(o) {
da964d22 119 var resp = json_decode(o.responseText);
1bcb7eb5 120 if (!comment_check_response(resp)) {
121 return;
122 }
123 var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
124 this.el = document.getElementById(htmlid);
125 this.el.style.overflow = 'hidden';
126 var attributes = {
127 width:{to:0},
128 height:{to:0}
129 };
130 var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
6f5e0852
PS
131 anim.onComplete.subscribe(this.remove_dom, [], this);
132 anim.animate();
1bcb7eb5 133 },
134 remove_dom: function() {
6f5e0852 135 this.el.parentNode.removeChild(this.el);
1bcb7eb5 136 }
137 }
138 var trans = YAHOO.util.Connect.asyncRequest('POST',
139 url+'?action=delete', this.cb, build_querystring(data));
140}
141function view_comments(client_id, area, itemid, page) {
142 var container = document.getElementById('comment-ctrl-'+client_id);
143 var ta = document.getElementById('dlg-content-'+client_id);
144 var img = document.getElementById('comment-img-'+client_id);
145 if (container.style.display=='none'||container.style.display=='') {
146 // show
147 get_comments(client_id, area, itemid, page);
148 container.style.display = 'block';
9598d578 149 img.src=M.cfg.wwwroot+'/pix/t/expanded.png';
1bcb7eb5 150 } else {
151 // hide
152 container.style.display = 'none';
9598d578 153 img.src=M.cfg.wwwroot+'/pix/t/collapsed.png';
1bcb7eb5 154 ta.value = '';
155 }
156 toggle_textarea.apply(ta, [false]);
157 // reset textarea size
158 ta.onclick = function() {
159 toggle_textarea.apply(this, [true]);
160 }
161 ta.onkeypress = function() {
162 if (this.scrollHeight > this.clientHeight && !window.opera)
163 this.rows += 1;
164 }
165 ta.onblur = function() {
166 toggle_textarea.apply(this, [false]);
167 }
168 return false;
169}
170function comment_hide_link(cid) {
171 var link = document.getElementById('comment-link-'+cid);
172 if(link){
173 link.style.display='none';
174 } else {
1bcb7eb5 175 }
176}
177function toggle_textarea(focus) {
178 if (focus) {
179 if (this.value == mstr.moodle.addcomment) {
180 this.value = '';
181 this.style.color = 'black';
182 }
183 }else{
184 if (this.value == '') {
185 this.value = mstr.moodle.addcomment;
186 this.style.color = 'grey';
187 this.rows = 1;
188 }
189 }
190}
191function comment_check_response(data) {
192 if (data.error) {
193 alert(data.error);
194 return false;
195 }
196 return true;
197}