/* * NOTE: the /mod/chat/gui_header_js/ is not a real plugin, * ideally this code should be in /mod/chat/module.js */ /** * @namespace M.mod_chat_ajax */ M.mod_chat_ajax = M.mod_chat_ajax || {}; /** * Init ajax based Chat UI. * @namespace M.mod_chat_ajax * @function * @param {YUI} Y * @param {Object} cfg configuration data */ M.mod_chat_ajax.init = function(Y, cfg) { var gui_ajax = { // Properties. api : M.cfg.wwwroot + '/mod/chat/chat_ajax.php?sesskey=' + M.cfg.sesskey, // The path to the ajax callback script. cfg : {}, // A configuration variable. interval : null, // The interval object for refreshes. layout : null, // A reference to the layout used in this module. messages : [], // An array of messages. scrollable : true, // True is scrolling should occur. thememenu : null, // A reference to the menu for changing themes. // Elements messageinput : null, sendbutton : null, messagebox : null, init : function(cfg) { this.cfg = cfg; this.cfg.req_count = this.cfg.req_count || 0; participantswidth = 180; if (Y.one('#input-message').get('docWidth') < 640) { participantswidth = 120; } this.layout = new Y.YUI2.widget.Layout({ units : [ {position: 'right', width: participantswidth, resize: true, gutter: '1px', scroll: true, body: 'chat-userlist', animate: false}, {position: 'bottom', height: 42, resize: false, body: 'chat-input-area', gutter: '1px', collapse: false, resize: false}, {position: 'center', body: 'chat-messages', gutter: '0px', scroll: true} ] }); this.layout.on('render', function() { var unit = this.getUnitByPosition('right'); if (unit) { unit.on('close', function() { closeLeft(); }); } }, this.layout); this.layout.render(); // Gather the general elements. this.messageinput = Y.one('#input-message'); this.sendbutton = Y.one('#button-send'); this.messagebox = Y.one('#chat-messages'); // Set aria attributes to messagebox and chat-userlist. this.messagebox.set('role', 'log'); this.messagebox.set('aria-live', 'polite'); var userlist = Y.one('#chat-userlist'); userlist.set('aria-live', 'polite'); userlist.set('aria-relevant', 'all'); // Attach the default events for this module. this.sendbutton.on('click', this.send, this); this.messagebox.on('mouseenter', function() { this.scrollable = false; }, this); this.messagebox.on('mouseleave', function() { this.scrollable = true; }, this); // Send the message when the enter key is pressed. Y.on('key', this.send, this.messageinput, 'press:13', this); document.title = this.cfg.chatroom_name; // Prepare and execute the first AJAX request of information. Y.io(this.api,{ method : 'POST', data : build_querystring({ action : 'init', chat_init : 1, chat_sid : this.cfg.sid, theme : this.theme }), on : { success : function(tid, outcome) { this.messageinput.removeAttribute('disabled'); this.messageinput.set('value', ''); this.messageinput.focus(); try { var data = Y.JSON.parse(outcome.responseText); } catch (ex) { return; } this.update_users(data.users); } }, context : this }); var scope = this; this.interval = setInterval(function() { scope.update_messages(); }, this.cfg.timer, this); // Create and initalise theme changing menu. this.thememenu = new Y.YUI2.widget.Menu('basicmenu', {xy:[0,0]}); this.thememenu.addItems([ {text: M.util.get_string('bubble', 'mod_chat'), url: this.cfg.chaturl + '&theme=bubble'}, {text: M.util.get_string('compact', 'mod_chat'), url: this.cfg.chaturl + '&theme=compact'}, {text: M.util.get_string('coursetheme', 'mod_chat'), url: this.cfg.chaturl + '&theme=course_theme'} ]); this.thememenu.render(document.body); Y.one('#choosetheme').on('click', function(e) { this.moveTo((e.pageX - 20), (e.pageY - 20)); this.show(); }, this.thememenu); }, append_message : function(key, message, row) { var item = Y.Node.create('
' + users[i].picture + ' |