PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/chat/gui_ajax/module.js

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