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