Merge branch 'MDL-66273-MOODLE-311-v2' of https://github.com/golenkovm/moodle into...
[moodle.git] / mod / chat / gui_header_js / module.js
blob0b934f203257796bb13e96be5c145f14f44c6292
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  */
6 /**
7  * @namespace M.mod_chat_header
8  */
9 M.mod_chat_header = M.mod_chat_ajax || {};
11 /**
12  * Init header based Chat UI - frame input
13  *
14  * @namespace M.mod_chat_header
15  * @function
16  * @param {YUI} Y
17  * @param {Boolean} forcerefreshasap refresh users frame asap
18  */
19 M.mod_chat_header.init_insert = function(Y, forcerefreshasap) {
20     if (forcerefreshasap) {
21         parent.jsupdate.location.href = parent.jsupdate.document.anchors[0].href;
22     }
23     parent.input.enableForm();
26 /**
27  * Init header based Chat UI - frame input
28  *
29  * @namespace M.mod_chat_header
30  * @function
31  * @param {YUI} Y
32  */
33 M.mod_chat_header.init_input = function(Y) {
35     var inputframe = {
37         waitflag : false,       // True when a submission is in progress
39         /**
40          * Initialises the input frame
41          *
42          * @function
43          */
44         init : function() {
45             Y.one('#inputForm').on('submit', this.submit, this);
46         },
47         /**
48          * Enables the input form
49          * @this {M.mod_chat.js}
50          */
51         enable_form : function() {
52             var el = Y.one('#input_chat_message');
53             this.waitflag = false;
54             el.set('className', '');
55             el.focus();
56         },
57         /**
58          * Submits the entered message
59          * @param {Event} e
60          */
61         submit : function(e) {
62             e.halt();
63             if (this.waitflag) {
64                 return false;
65             }
66             this.waitflag = true;
67             var inputchatmessage = Y.one('#input_chat_message');
68             Y.one('#insert_chat_message').set('value', inputchatmessage.get('value'));
69             inputchatmessage.set('value', '');
70             inputchatmessage.addClass('wait');
71             Y.one('#sendForm').submit();
72             this.enable_form();
73             return false;
74         }
76     };
78     inputframe.init();
81 /**
82  * Init header based Chat UI - frame users
83  *
84  * @namespace M.mod_chat_header
85  * @function
86  * @param {YUI} Y
87  * @param {Array} users
88  */
89 M.mod_chat_header.init_users = function(Y, users) {
91     var usersframe = {
93         timer : null,           // Stores the timer object
94         timeout : 1,            // The seconds between updates
95         users : [],             // An array of users
97         /**
98          * Initialises the frame with list of users
99          *
100          * @function
101          * @this
102          * @param {Array|null} users
103          */
104         init : function(users) {
105             this.users = users;
106             this.start();
107             Y.one(document.body).on('unload', this.stop, this);
108         },
109         /**
110          * Starts the update timeout
111          *
112          * @function
113          * @this
114          */
115         start : function() {
116             this.timer = setTimeout(function(self) {
117                 self.update();
118             }, this.timeout * 1000, this);
119         },
120         /**
121          * Stops the update timeout
122          * @function
123          * @this
124          */
125         stop : function() {
126             clearTimeout(this.timer);
127         },
128         /**
129          * Updates the user information
130          *
131          * @function
132          * @this
133          */
134         update : function() {
135             for (var i in this.users) {
136                 var el  = Y.one('#uidle' + this.users[i]);
137                 if (el) {
138                     var parts = el.get('innerHTML').split(':');
139                     var time = this.timeout + (parseInt(parts[0], 10) * 60) + parseInt(parts[1], 10);
140                     var min = Math.floor(time / 60);
141                     var sec = time % 60;
142                     el.set('innerHTML', ((min < 10) ? "0" : "") + min + ":" + ((sec < 10) ? "0" : "") + sec);
143                 }
144             }
145             this.start();
146         }
147     };
149     usersframe.init(users);