forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
259 lines (234 loc) · 10.2 KB
/
module.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* NOTE: the /mod/chat/gui_header_js/ is not a real plugin,
* ideally this code should be in /mod/chat/module.js
*/
/**
* @namespace M.mod_chat_ajax
*/
M.mod_chat_ajax = M.mod_chat_ajax || {};
/**
* Init ajax based Chat UI.
* @namespace M.mod_chat_ajax
* @function
* @param {YUI} Y
* @param {Object} cfg configuration data
*/
M.mod_chat_ajax.init = function(Y, cfg) {
var gui_ajax = {
// Properties.
api : M.cfg.wwwroot + '/mod/chat/chat_ajax.php?sesskey=' + M.cfg.sesskey, // The path to the ajax callback script.
cfg : {}, // A configuration variable.
interval : null, // The interval object for refreshes.
layout : null, // A reference to the layout used in this module.
messages : [], // An array of messages.
scrollable : true, // True is scrolling should occur.
thememenu : null, // A reference to the menu for changing themes.
// Elements
messageinput : null,
sendbutton : null,
messagebox : null,
init : function(cfg) {
this.cfg = cfg;
this.cfg.req_count = this.cfg.req_count || 0;
participantswidth = 180;
if (Y.one('#input-message').get('docWidth') < 640) {
participantswidth = 120;
}
this.layout = new Y.YUI2.widget.Layout({
units : [
{position: 'right', width: participantswidth, resize: true, gutter: '1px', scroll: true, body: 'chat-userlist', animate: false},
{position: 'bottom', height: 42, resize: false, body: 'chat-input-area', gutter: '1px', collapse: false, resize: false},
{position: 'center', body: 'chat-messages', gutter: '0px', scroll: true}
]
});
this.layout.on('render', function() {
var unit = this.getUnitByPosition('right');
if (unit) {
unit.on('close', function() {
closeLeft();
});
}
}, this.layout);
this.layout.render();
// Gather the general elements.
this.messageinput = Y.one('#input-message');
this.sendbutton = Y.one('#button-send');
this.messagebox = Y.one('#chat-messages');
// Set aria attributes to messagebox and chat-userlist.
this.messagebox.set('role', 'log');
this.messagebox.set('aria-live', 'polite');
var userlist = Y.one('#chat-userlist');
userlist.set('aria-live', 'polite');
userlist.set('aria-relevant', 'all');
// Attach the default events for this module.
this.sendbutton.on('click', this.send, this);
this.messagebox.on('mouseenter', function() {
this.scrollable = false;
}, this);
this.messagebox.on('mouseleave', function() {
this.scrollable = true;
}, this);
// Send the message when the enter key is pressed.
Y.on('key', this.send, this.messageinput, 'press:13', this);
document.title = this.cfg.chatroom_name;
// Prepare and execute the first AJAX request of information.
Y.io(this.api,{
method : 'POST',
data : build_querystring({
action : 'init',
chat_init : 1,
chat_sid : this.cfg.sid,
theme : this.theme
}),
on : {
success : function(tid, outcome) {
this.messageinput.removeAttribute('disabled');
this.messageinput.set('value', '');
this.messageinput.focus();
try {
var data = Y.JSON.parse(outcome.responseText);
} catch (ex) {
return;
}
this.update_users(data.users);
}
},
context : this
});
var scope = this;
this.interval = setInterval(function() {
scope.update_messages();
}, this.cfg.timer, this);
// Create and initalise theme changing menu.
this.thememenu = new Y.YUI2.widget.Menu('basicmenu', {xy:[0,0]});
this.thememenu.addItems([
{text: M.util.get_string('bubble', 'mod_chat'), url: this.cfg.chaturl + '&theme=bubble'},
{text: M.util.get_string('compact', 'mod_chat'), url: this.cfg.chaturl + '&theme=compact'}
]);
if (this.cfg.showcoursetheme == 1) {
this.thememenu.addItem({text: M.util.get_string('coursetheme', 'mod_chat'), url: this.cfg.chaturl + '&theme=course_theme'});
}
this.thememenu.render(document.body);
Y.one('#choosetheme').on('click', function(e) {
this.moveTo((e.pageX - 20), (e.pageY - 20));
this.show();
}, this.thememenu);
},
append_message : function(key, message, row) {
var item = Y.Node.create('<li id="mdl-chat-entry-' + key + '">' + message.message + '</li>');
item.addClass((message.mymessage) ? 'mdl-chat-my-entry' : 'mdl-chat-entry');
Y.one('#messages-list').append(item);
if (message.type && message.type == 'beep') {
Y.one('#chat-notify').setContent('<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />');
}
},
send : function(e, beep) {
if((this.messageinput.get('value') != '') || (typeof beep != 'undefined')) {
this.sendbutton.set('value', M.util.get_string('sending', 'chat'));
var data = {
chat_message : (!beep) ? this.messageinput.get('value') : '',
chat_sid : this.cfg.sid,
theme : this.cfg.theme
};
if (beep) {
data.beep = beep
}
data.action = 'chat';
Y.io(this.api, {
method : 'POST',
data : build_querystring(data),
on : {
success : this.send_callback
},
context : this
});
}
},
send_callback : function(tid, outcome, args) {
try {
var data = Y.JSON.parse(outcome.responseText);
} catch (ex) {
return;
}
this.sendbutton.set('value', M.util.get_string('send', 'chat'));
this.messageinput.set('value', '');
clearInterval(this.interval);
this.update_messages();
var scope = this;
this.interval = setInterval(function() {
scope.update_messages();
}, this.cfg.timer, this);
},
talkto: function (e, name) {
this.messageinput.set('value', "To " + name + ": ");
this.messageinput.focus();
},
update_messages : function() {
this.cfg.req_count++;
Y.io(this.api, {
method : 'POST',
data : build_querystring({
action: 'update',
chat_lastrow : this.cfg.chat_lastrow || false,
chat_lasttime : this.cfg.chat_lasttime,
chat_sid : this.cfg.sid,
theme : this.cfg.theme
}),
on : {
success : this.update_messages_callback
},
context : this
});
},
update_messages_callback : function(tid, outcome) {
try {
var data = Y.JSON.parse(outcome.responseText);
} catch (ex) {
return;
}
if (data.error) {
clearInterval(this.interval);
alert(data.error);
window.location = this.cfg.home;
}
this.cfg.chat_lasttime = data.lasttime;
this.cfg.chat_lastrow = data.lastrow;
// Update messages.
for (var key in data.msgs){
if (!M.util.in_array(key, this.messages)) {
this.messages.push(key);
this.append_message(key, data.msgs[key], data.lastrow);
}
}
// Update users.
this.update_users(data.users);
// Scroll to the bottom of the message list
if (this.scrollable) {
Y.Node.getDOMNode(this.messagebox).parentNode.scrollTop += 500;
}
this.messageinput.focus();
},
update_users : function(users) {
if (!users) {
return;
}
var list = Y.one('#users-list');
list.get('children').remove();
for (var i in users) {
var li = Y.Node.create('<li><table><tr><td>' + users[i].picture + '</td><td></td></tr></table></li>');
if (users[i].id == this.cfg.userid) {
li.all('td').item(1).append(Y.Node.create('<strong><a target="_blank" href="' + users[i].url + '">' + users[i].name + '</a></strong>'));
} else {
li.all('td').item(1).append(Y.Node.create('<div><a target="_blank" href="' + users[i].url + '">' + users[i].name + '</a></div>'));
var talk = Y.Node.create('<a href="###">' + M.util.get_string('talk', 'chat') + '</a>');
talk.on('click', this.talkto, this, users[i].name);
var beep = Y.Node.create('<a href="###">' + M.util.get_string('beep', 'chat') + '</a>');
beep.on('click', this.send, this, users[i].id);
li.all('td').item(1).append(Y.Node.create('<div></div>').append(talk).append(' ').append(beep));
}
list.append(li);
}
}
};
gui_ajax.init(cfg);
};