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