weekly release 2.6dev
[moodle.git] / mod / chat / gui_ajax / module.js
CommitLineData
2daed78a
PS
1
2/*
3 * NOTE: the /mod/chat/gui_header_js/ is not a real plugin,
4 * ideally this code should be in /mod/chat/module.js
5 */
6
7/**
8 * @namespace M.mod_chat_ajax
9 */
10M.mod_chat_ajax = M.mod_chat_ajax || {};
11
579a4976 12/**
2daed78a
PS
13 * Init ajax based Chat UI.
14 * @namespace M.mod_chat_ajax
15 * @function
16 * @param {YUI} Y
17 * @param {Object} cfg configuration data
579a4976 18 */
2daed78a
PS
19M.mod_chat_ajax.init = function(Y, cfg) {
20
21 var gui_ajax = {
579a4976
SH
22
23 // Properties
467902f2 24 api : M.cfg.wwwroot+'/mod/chat/chat_ajax.php?sesskey='+M.cfg.sesskey, // The path to the ajax callback script
579a4976
SH
25 cfg : {}, // A configuration variable
26 interval : null, // The interval object for refreshes
27 layout : null, // A reference to the layout used in this module
28 messages : [], // An array of messages
29 scrollable : true, // True is scrolling should occur
30 thememenu : null, // A reference to the menu for changing themes
31
32 // Elements
33 messageinput : null,
34 sendbutton : null,
35 messagebox : null,
36
2daed78a 37 init : function(cfg) {
579a4976
SH
38 this.cfg = cfg;
39 this.cfg.req_count = this.cfg.req_count || 0;
69ccc32d 40 this.layout = new Y.YUI2.widget.Layout({
579a4976 41 units : [
f68c3b0a
DP
42 {position: 'right', width: 180, resize: true, gutter: '5px', scroll: true, body: 'chat-userlist', animate: false},
43 {position: 'bottom', height: 42, resize: false, body: 'chat-input-area', gutter: '5px', collapse: false, resize: false},
44 {position: 'center', body: 'chat-messages', gutter: '5px', scroll: true}
579a4976
SH
45 ]
46 });
47
48 this.layout.on('render', function() {
7d2975c1
DC
49 var unit = this.getUnitByPosition('right');
50 if (unit) {
51 unit.on('close', function() {
52 closeLeft();
53 });
54 }
579a4976
SH
55 }, this.layout);
56 this.layout.render();
57
58 // Gather the general elements
59 this.messageinput = Y.one('#input-message');
60 this.sendbutton = Y.one('#button-send');
61 this.messagebox = Y.one('#chat-messages');
62
02a011ea
RT
63 // Set aria attributes to messagebox and chat-userlist
64 this.messagebox.set('role', 'log');
65 this.messagebox.set('aria-live', 'polite');
66 var userlist = Y.one('#chat-userlist');
67 userlist.set('aria-live', 'polite');
68 userlist.set('aria-relevant', 'all');
69
579a4976
SH
70 // Attach the default events for this module
71 this.sendbutton.on('click', this.send, this);
2daed78a
PS
72 this.messagebox.on('mouseenter', function() {
73 this.scrollable = false;
74 }, this);
75 this.messagebox.on('mouseleave', function() {
76 this.scrollable = true;
77 }, this);
579a4976
SH
78
79 // Send the message when the enter key is pressed
80 Y.on('key', this.send, this.messageinput, 'press:13', this);
81
82 document.title = this.cfg.chatroom_name;
83
84 // Prepare and execute the first AJAX request of information
85 Y.io(this.api,{
86 method : 'POST',
87 data : build_querystring({
88 action : 'init',
89 chat_init : 1,
90 chat_sid : this.cfg.sid,
91 theme : this.theme
92 }),
93 on : {
94 success : function(tid, outcome) {
5be94998 95 this.messageinput.removeAttribute('disabled');
579a4976 96 this.messageinput.set('value', '');
c67a139c 97 this.messageinput.focus();
579a4976
SH
98 try {
99 var data = Y.JSON.parse(outcome.responseText);
100 } catch (ex) {
101 return;
102 }
103 this.update_users(data.users);
104 }
105 },
106 context : this
107 });
108
c67a139c
DC
109 var scope = this;
110 this.interval = setInterval(function() {
111 scope.update_messages();
579a4976
SH
112 }, this.cfg.timer, this);
113
114 // Create and initalise theme changing menu
115 /*
116 this.thememenu = new Y.Overlay({
117 bodyContent : '<div class="menuitem"><a href="'+this.cfg.chaturl+'&theme=bubble">Bubble</a></div><div class="menuitem"><a href="'+this.cfg.chaturl+'&theme=compact">Compact</a></div>',
118 visible : false,
119 zIndex : 2,
120 align : {
121 node : '#choosetheme',
122 points : [Y.WidgetPositionExt.BL, Y.WidgetPositionExt.BR]
123 }
124 });
125 this.thememenu.render(document.body);
126 Y.one('#choosetheme').on('click', function(e){
127 this.show();
128 this.get('boundingBox').setStyle('visibility', 'visible');
129 }, this.thememenu);
130
131 return;
132 */
69ccc32d 133 this.thememenu = new Y.YUI2.widget.Menu('basicmenu', {xy:[0,0]});
579a4976
SH
134 this.thememenu.addItems([
135 {text: "Bubble", url: this.cfg.chaturl+'&theme=bubble'},
136 {text: "Compact", url: this.cfg.chaturl+'&theme=compact'}
137 ]);
138 this.thememenu.render(document.body);
139 Y.one('#choosetheme').on('click', function(e){
140 this.moveTo((e.pageX-20), (e.pageY-20));
141 this.show();
142 }, this.thememenu);
143 },
144
145 append_message : function(key, message, row) {
146 var item = Y.Node.create('<li id="mdl-chat-entry-'+key+'">'+message.message+'</li>');
147 item.addClass((message.mymessage)?'mdl-chat-my-entry':'mdl-chat-entry');
148 Y.one('#messages-list').append(item);
149 if (message.type && message.type == 'beep') {
150 Y.one('#chat-notify').setContent('<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />');
151 }
152 },
153
154 send : function(e, beep) {
155 this.sendbutton.set('value', M.str.chat.sending);
156
157 var data = {
2daed78a
PS
158 chat_message : (!beep)?this.messageinput.get('value'):'',
159 chat_sid : this.cfg.sid,
160 theme : this.cfg.theme
161 };
162 if (beep) {
163 data.beep = beep
164 }
467902f2 165 data.action = 'chat';
579a4976 166
467902f2 167 Y.io(this.api, {
2daed78a
PS
168 method : 'POST',
169 data : build_querystring(data),
170 on : {
171 success : this.send_callback
172 },
173 context : this
579a4976
SH
174 });
175 },
176
177 send_callback : function(tid, outcome, args) {
529817c8
DC
178 try {
179 var data = Y.JSON.parse(outcome.responseText);
180 } catch (ex) {
181 return;
579a4976 182 }
529817c8
DC
183 this.sendbutton.set('value', M.str.chat.send);
184 this.messageinput.set('value', '');
579a4976
SH
185 clearInterval(this.interval);
186 this.update_messages();
58f3f058
DC
187 var scope = this;
188 this.interval = setInterval(function() {
189 scope.update_messages();
579a4976
SH
190 }, this.cfg.timer, this);
191 },
192
e8a4928d 193 talkto: function (e, name) {
579a4976
SH
194 this.messageinput.set('value', "To "+name+": ");
195 this.messageinput.focus();
196 },
197
198 update_messages : function() {
199 this.cfg.req_count++;
467902f2 200 Y.io(this.api, {
579a4976
SH
201 method : 'POST',
202 data : build_querystring({
467902f2 203 action: 'update',
579a4976
SH
204 chat_lastrow : this.cfg.chat_lastrow || false,
205 chat_lasttime : this.cfg.chat_lasttime,
206 chat_sid : this.cfg.sid,
207 theme : this.cfg.theme
208 }),
209 on : {
210 success : this.update_messages_callback
211 },
212 context : this
213 });
214 },
215
216 update_messages_callback : function(tid, outcome) {
217 try {
218 var data = Y.JSON.parse(outcome.responseText);
219 } catch (ex) {
220 return;
221 }
222 if (data.error) {
a8e3b008
DC
223 clearInterval(this.interval);
224 alert(data.error);
225 window.location = this.cfg.home;
579a4976
SH
226 }
227 this.cfg.chat_lasttime = data.lasttime;
228 this.cfg.chat_lastrow = data.lastrow;
229 // Update messages
230 for (var key in data.msgs){
2daed78a 231 if (!M.util.in_array(key, this.messages)) {
579a4976
SH
232 this.messages.push(key);
233 this.append_message(key, data.msgs[key], data.lastrow);
234 }
235 }
236 // Update users
237 this.update_users(data.users);
238 // Scroll to the bottom of the message list
239 if (this.scrollable) {
240 Y.Node.getDOMNode(this.messagebox).parentNode.scrollTop+=500;
241 }
242 this.messageinput.focus();
243 },
244
245 update_users : function(users) {
2daed78a 246 if (!users) {
579a4976
SH
247 return;
248 }
249 var list = Y.one('#users-list');
250 list.get('children').remove();
251 for (var i in users) {
252 var li = Y.Node.create('<li><table><tr><td>'+users[i].picture+'</td><td></td></tr></table></li>');
253 if (users[i].id == this.cfg.userid) {
254 li.all('td').item(1).append(Y.Node.create('<strong><a target="_blank" href="'+users[i].url+'">'+ users[i].name+'</a></strong>'));
255 } else {
256 li.all('td').item(1).append(Y.Node.create('<div><a target="_blank" href="'+users[i].url+'">'+users[i].name+'</a></div>'));
9d3b8b24 257 var talk = Y.Node.create('<a href="###">'+M.str.chat.talk+'</a>');
e8a4928d
DC
258 talk.on('click', this.talkto, this, users[i].name);
259 var beep = Y.Node.create('<a href="###">'+M.str.chat.beep+'</a>');
260 beep.on('click', this.send, this, users[i].id);
9d3b8b24 261 li.all('td').item(1).append(Y.Node.create('<div></div>').append(talk).append('&nbsp;').append(beep));
579a4976
SH
262 }
263 list.append(li);
264 }
265 }
2daed78a
PS
266
267 };
268
269 gui_ajax.init(cfg);
270};