rss MDL-23734 fixed rss block link back to item parents ie the forum itself
[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) {
88 this.messageinput.focus();
5be94998 89 this.messageinput.removeAttribute('disabled');
579a4976
SH
90 this.messageinput.set('value', '');
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
102 this.interval = setInterval(function(me){
103 me.update_messages();
104 }, this.cfg.timer, this);
105
106 // Create and initalise theme changing menu
107 /*
108 this.thememenu = new Y.Overlay({
109 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>',
110 visible : false,
111 zIndex : 2,
112 align : {
113 node : '#choosetheme',
114 points : [Y.WidgetPositionExt.BL, Y.WidgetPositionExt.BR]
115 }
116 });
117 this.thememenu.render(document.body);
118 Y.one('#choosetheme').on('click', function(e){
119 this.show();
120 this.get('boundingBox').setStyle('visibility', 'visible');
121 }, this.thememenu);
122
123 return;
124 */
125 this.thememenu = new YAHOO.widget.Menu('basicmenu', {xy:[0,0]});
126 this.thememenu.addItems([
127 {text: "Bubble", url: this.cfg.chaturl+'&theme=bubble'},
128 {text: "Compact", url: this.cfg.chaturl+'&theme=compact'}
129 ]);
130 this.thememenu.render(document.body);
131 Y.one('#choosetheme').on('click', function(e){
132 this.moveTo((e.pageX-20), (e.pageY-20));
133 this.show();
134 }, this.thememenu);
135 },
136
137 append_message : function(key, message, row) {
138 var item = Y.Node.create('<li id="mdl-chat-entry-'+key+'">'+message.message+'</li>');
139 item.addClass((message.mymessage)?'mdl-chat-my-entry':'mdl-chat-entry');
140 Y.one('#messages-list').append(item);
141 if (message.type && message.type == 'beep') {
142 Y.one('#chat-notify').setContent('<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />');
143 }
144 },
145
146 send : function(e, beep) {
147 this.sendbutton.set('value', M.str.chat.sending);
148
149 var data = {
2daed78a
PS
150 chat_message : (!beep)?this.messageinput.get('value'):'',
151 chat_sid : this.cfg.sid,
152 theme : this.cfg.theme
153 };
154 if (beep) {
155 data.beep = beep
156 }
579a4976
SH
157
158 Y.io(this.api+'?action=chat', {
2daed78a
PS
159 method : 'POST',
160 data : build_querystring(data),
161 on : {
162 success : this.send_callback
163 },
164 context : this
579a4976
SH
165 });
166 },
167
168 send_callback : function(tid, outcome, args) {
2daed78a 169 if (outcome.responseText == 200) {
579a4976
SH
170 this.sendbutton.set('value', M.str.chat.send);
171 this.messageinput.set('value', '');
172 }
173 clearInterval(this.interval);
174 this.update_messages();
2daed78a 175 this.interval = setInterval(function(me) {
579a4976
SH
176 me.update_messages();
177 }, this.cfg.timer, this);
178 },
179
e8a4928d
DC
180 talkto: function (e, name) {
181 console.info(name);
579a4976
SH
182 this.messageinput.set('value', "To "+name+": ");
183 this.messageinput.focus();
184 },
185
186 update_messages : function() {
187 this.cfg.req_count++;
188 Y.io(this.api+'?action=update', {
189 method : 'POST',
190 data : build_querystring({
191 chat_lastrow : this.cfg.chat_lastrow || false,
192 chat_lasttime : this.cfg.chat_lasttime,
193 chat_sid : this.cfg.sid,
194 theme : this.cfg.theme
195 }),
196 on : {
197 success : this.update_messages_callback
198 },
199 context : this
200 });
201 },
202
203 update_messages_callback : function(tid, outcome) {
204 try {
205 var data = Y.JSON.parse(outcome.responseText);
206 } catch (ex) {
207 return;
208 }
209 if (data.error) {
210 if (data.error.level == 'ERROR'){
211 clearInterval(this.interval);
212 window.location = this.cfg.home;
213 }
214 }
215 this.cfg.chat_lasttime = data.lasttime;
216 this.cfg.chat_lastrow = data.lastrow;
217 // Update messages
218 for (var key in data.msgs){
2daed78a 219 if (!M.util.in_array(key, this.messages)) {
579a4976
SH
220 this.messages.push(key);
221 this.append_message(key, data.msgs[key], data.lastrow);
222 }
223 }
224 // Update users
225 this.update_users(data.users);
226 // Scroll to the bottom of the message list
227 if (this.scrollable) {
228 Y.Node.getDOMNode(this.messagebox).parentNode.scrollTop+=500;
229 }
230 this.messageinput.focus();
231 },
232
233 update_users : function(users) {
2daed78a 234 if (!users) {
579a4976
SH
235 return;
236 }
237 var list = Y.one('#users-list');
238 list.get('children').remove();
239 for (var i in users) {
240 var li = Y.Node.create('<li><table><tr><td>'+users[i].picture+'</td><td></td></tr></table></li>');
241 if (users[i].id == this.cfg.userid) {
242 li.all('td').item(1).append(Y.Node.create('<strong><a target="_blank" href="'+users[i].url+'">'+ users[i].name+'</a></strong>'));
243 } else {
244 li.all('td').item(1).append(Y.Node.create('<div><a target="_blank" href="'+users[i].url+'">'+users[i].name+'</a></div>'));
e8a4928d
DC
245 var talk = Y.Node.create('<a href="###">'+M.str.chat.talk+'</a>&nbsp;');
246 talk.on('click', this.talkto, this, users[i].name);
247 var beep = Y.Node.create('<a href="###">'+M.str.chat.beep+'</a>');
248 beep.on('click', this.send, this, users[i].id);
579a4976
SH
249 li.all('td').item(1).append(Y.Node.create('<div></div>').append(talk).append(beep));
250 }
251 list.append(li);
252 }
253 }
2daed78a
PS
254
255 };
256
257 gui_ajax.init(cfg);
258};