enrol flatfile MDL-23892 the flatfile plugin now processes future enrolment start...
[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
24 api : M.cfg.wwwroot+'/mod/chat/chat_ajax.php', // The path to the ajax callback script
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;
40 this.layout = new YAHOO.widget.Layout({
41 units : [
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}
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
63 // Attach the default events for this module
64 this.sendbutton.on('click', this.send, this);
2daed78a
PS
65 this.messagebox.on('mouseenter', function() {
66 this.scrollable = false;
67 }, this);
68 this.messagebox.on('mouseleave', function() {
69 this.scrollable = true;
70 }, this);
579a4976
SH
71
72 // Send the message when the enter key is pressed
73 Y.on('key', this.send, this.messageinput, 'press:13', this);
74
75 document.title = this.cfg.chatroom_name;
76
77 // Prepare and execute the first AJAX request of information
78 Y.io(this.api,{
79 method : 'POST',
80 data : build_querystring({
81 action : 'init',
82 chat_init : 1,
83 chat_sid : this.cfg.sid,
84 theme : this.theme
85 }),
86 on : {
87 success : function(tid, outcome) {
5be94998 88 this.messageinput.removeAttribute('disabled');
579a4976 89 this.messageinput.set('value', '');
c67a139c 90 this.messageinput.focus();
579a4976
SH
91 try {
92 var data = Y.JSON.parse(outcome.responseText);
93 } catch (ex) {
94 return;
95 }
96 this.update_users(data.users);
97 }
98 },
99 context : this
100 });
101
c67a139c
DC
102 var scope = this;
103 this.interval = setInterval(function() {
104 scope.update_messages();
579a4976
SH
105 }, this.cfg.timer, this);
106
107 // Create and initalise theme changing menu
108 /*
109 this.thememenu = new Y.Overlay({
110 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>',
111 visible : false,
112 zIndex : 2,
113 align : {
114 node : '#choosetheme',
115 points : [Y.WidgetPositionExt.BL, Y.WidgetPositionExt.BR]
116 }
117 });
118 this.thememenu.render(document.body);
119 Y.one('#choosetheme').on('click', function(e){
120 this.show();
121 this.get('boundingBox').setStyle('visibility', 'visible');
122 }, this.thememenu);
123
124 return;
125 */
126 this.thememenu = new YAHOO.widget.Menu('basicmenu', {xy:[0,0]});
127 this.thememenu.addItems([
128 {text: "Bubble", url: this.cfg.chaturl+'&theme=bubble'},
129 {text: "Compact", url: this.cfg.chaturl+'&theme=compact'}
130 ]);
131 this.thememenu.render(document.body);
132 Y.one('#choosetheme').on('click', function(e){
133 this.moveTo((e.pageX-20), (e.pageY-20));
134 this.show();
135 }, this.thememenu);
136 },
137
138 append_message : function(key, message, row) {
139 var item = Y.Node.create('<li id="mdl-chat-entry-'+key+'">'+message.message+'</li>');
140 item.addClass((message.mymessage)?'mdl-chat-my-entry':'mdl-chat-entry');
141 Y.one('#messages-list').append(item);
142 if (message.type && message.type == 'beep') {
143 Y.one('#chat-notify').setContent('<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />');
144 }
145 },
146
147 send : function(e, beep) {
148 this.sendbutton.set('value', M.str.chat.sending);
149
150 var data = {
2daed78a
PS
151 chat_message : (!beep)?this.messageinput.get('value'):'',
152 chat_sid : this.cfg.sid,
153 theme : this.cfg.theme
154 };
155 if (beep) {
156 data.beep = beep
157 }
579a4976
SH
158
159 Y.io(this.api+'?action=chat', {
2daed78a
PS
160 method : 'POST',
161 data : build_querystring(data),
162 on : {
163 success : this.send_callback
164 },
165 context : this
579a4976
SH
166 });
167 },
168
169 send_callback : function(tid, outcome, args) {
2daed78a 170 if (outcome.responseText == 200) {
579a4976
SH
171 this.sendbutton.set('value', M.str.chat.send);
172 this.messageinput.set('value', '');
173 }
174 clearInterval(this.interval);
175 this.update_messages();
58f3f058
DC
176 var scope = this;
177 this.interval = setInterval(function() {
178 scope.update_messages();
579a4976
SH
179 }, this.cfg.timer, this);
180 },
181
e8a4928d
DC
182 talkto: function (e, name) {
183 console.info(name);
579a4976
SH
184 this.messageinput.set('value', "To "+name+": ");
185 this.messageinput.focus();
186 },
187
188 update_messages : function() {
189 this.cfg.req_count++;
190 Y.io(this.api+'?action=update', {
191 method : 'POST',
192 data : build_querystring({
193 chat_lastrow : this.cfg.chat_lastrow || false,
194 chat_lasttime : this.cfg.chat_lasttime,
195 chat_sid : this.cfg.sid,
196 theme : this.cfg.theme
197 }),
198 on : {
199 success : this.update_messages_callback
200 },
201 context : this
202 });
203 },
204
205 update_messages_callback : function(tid, outcome) {
206 try {
207 var data = Y.JSON.parse(outcome.responseText);
208 } catch (ex) {
209 return;
210 }
211 if (data.error) {
212 if (data.error.level == 'ERROR'){
213 clearInterval(this.interval);
214 window.location = this.cfg.home;
215 }
216 }
217 this.cfg.chat_lasttime = data.lasttime;
218 this.cfg.chat_lastrow = data.lastrow;
219 // Update messages
220 for (var key in data.msgs){
2daed78a 221 if (!M.util.in_array(key, this.messages)) {
579a4976
SH
222 this.messages.push(key);
223 this.append_message(key, data.msgs[key], data.lastrow);
224 }
225 }
226 // Update users
227 this.update_users(data.users);
228 // Scroll to the bottom of the message list
229 if (this.scrollable) {
230 Y.Node.getDOMNode(this.messagebox).parentNode.scrollTop+=500;
231 }
232 this.messageinput.focus();
233 },
234
235 update_users : function(users) {
2daed78a 236 if (!users) {
579a4976
SH
237 return;
238 }
239 var list = Y.one('#users-list');
240 list.get('children').remove();
241 for (var i in users) {
242 var li = Y.Node.create('<li><table><tr><td>'+users[i].picture+'</td><td></td></tr></table></li>');
243 if (users[i].id == this.cfg.userid) {
244 li.all('td').item(1).append(Y.Node.create('<strong><a target="_blank" href="'+users[i].url+'">'+ users[i].name+'</a></strong>'));
245 } else {
246 li.all('td').item(1).append(Y.Node.create('<div><a target="_blank" href="'+users[i].url+'">'+users[i].name+'</a></div>'));
e8a4928d
DC
247 var talk = Y.Node.create('<a href="###">'+M.str.chat.talk+'</a>&nbsp;');
248 talk.on('click', this.talkto, this, users[i].name);
249 var beep = Y.Node.create('<a href="###">'+M.str.chat.beep+'</a>');
250 beep.on('click', this.send, this, users[i].id);
579a4976
SH
251 li.all('td').item(1).append(Y.Node.create('<div></div>').append(talk).append(beep));
252 }
253 list.append(li);
254 }
255 }
2daed78a
PS
256
257 };
258
259 gui_ajax.init(cfg);
260};