presence/app/assets/javascripts/presence_utilities.js in social_stream-0.26.1 vs presence/app/assets/javascripts/presence_utilities.js in social_stream-0.26.2

- old
+ new

@@ -1,237 +1,262 @@ //////////////////// -//Debug functions +//SOCIAL STREAM PRESENCE: UTILITIES //////////////////// -var sspresence_debugging=false; -function log(msg) { - if(sspresence_debugging){ - console.log(msg) - } -} +PRESENCE.UTILITIES = (function(P,$,undefined){ + var init = function(){ + initFocusListeners(); + } -//////////////////// -//Blink page title when focus lost on new messages -//////////////////// - -var chatFocus; - -function onChatBlur() { - chatFocus = false; -}; - -function onChatFocus(){ - stopBlink(); - titles = []; //Remove titles after StopBlink! - chatFocus = true; -}; - -function initFocusListeners(){ - if (/*@cc_on!@*/false) { // check for Internet Explorer - document.onfocusin = onFocus; - document.onfocusout = onBlur; - } else { - window.onfocus = onChatFocus; - window.onblur = onChatBlur; - } -} - - -var blinkTimer; -var titles=[]; - -function blinkTitle(titles,index){ - $(document).attr("title", titles[index]); - index = (index+1)%titles.length - blinkTimer=setTimeout(function(){blinkTitle(titles,index)}, 2000); -} - -function stopBlink(){ - clearTimeout(blinkTimer); - if (titles.length > 0) { - $(document).attr("title", titles[0]); - } -} - -function blinkTitleOnMessage(username){ - if (!chatFocus){ - if (titles.length==0){ - titles.push($(document).attr("title")) - } - if (titles.indexOf(username) == -1){ - titles.push(username + " says...") - } - stopBlink(); - blinkTitle(titles,titles.length-1); - } -} - - - -//////////////////// -//Control user data input on the chatbox -//////////////////// - -//Return true to allow user to send data to the chatbox. -function userChatDataInputControl(){ - var floodControlBoolean = floodControl(); - var offlineDataSendControlBoolean = offlineDataSendControl(); - return (floodControlBoolean && offlineDataSendControlBoolean); -} - - - -//////////////////// -//Antiflood -//////////////////// - -var lastMessageTimes = new Array(); -//lastMessageTimes['slug'] = ["timeOfLastMessage",["msgID1","msgID2"]]; - -var timeBetweenMessages = 500; //mseconds - -//Return true when detects a text storm and control the text flood: -//timeBetweenMessages is the minimum time that must elapse between the messages of the same contact. -function antifloodControl(from_slug,msg,msgID) { + //////////////////// + //Debug functions + //////////////////// + var debugging=false; - if( from_slug in lastMessageTimes){ - - } else { - lastMessageTimes[from_slug] = [,[]]; - } + var log = function(msg) { + if(debugging){ + console.log(msg) + } + } - if (msgID==null){ - msgID = generateMessageID(); + //////////////////// + //Blink page title when focus lost on new messages + //////////////////// + var chatFocus; + + var onChatBlur = function() { + chatFocus = false; + }; + + var onChatFocus = function(){ + stopBlink(); + titles = []; //Remove titles after StopBlink! + chatFocus = true; + }; + + var initFocusListeners = function(){ + if (/*@cc_on!@*/false) { // check for Internet Explorer + document.onfocusin = PRESENCE.UTILITIES.onChatFocus; + document.onfocusout = PRESENCE.UTILITIES.onChatBlur; + } else { + window.onfocus = PRESENCE.UTILITIES.onChatFocus; + window.onblur = PRESENCE.UTILITIES.onChatBlur; + } } - var lastMessageTime = lastMessageTimes[from_slug][0]; + var getChatFocus = function(){ + return chatFocus; + } - var t = (new Date()).getTime(); - if(t - lastMessageTime < timeBetweenMessages) { - //Flood detected - return retryToShowMessage(from_slug,msg,msgID); - } - //Check if is the first message of this user to be send. - //var messageQueue = lastMessageTimes[from_slug][1]; + var blinkTimer; + var titles=[]; - if (lastMessageTimes[from_slug][1].length>0){ - if((lastMessageTimes[from_slug][1])[0]==msgID){ - //Message is the first on the queue: Show it and remove from the queue - lastMessageTimes[from_slug][1].splice(0,1); - } else { - //Message is not the first on the queue - return retryToShowMessage(from_slug,msg,msgID); - } + var blinkTitle = function(titles,index){ + $(document).attr("title", titles[index]); + index = (index+1)%titles.length + blinkTimer=setTimeout(function(){PRESENCE.UTILITIES.blinkTitle(titles,index)}, 2000); } - //Message can be send - lastMessageTimes[from_slug][0] = t; - return false; -}; + var stopBlink = function (){ + clearTimeout(blinkTimer); + if (titles.length > 0) { + $(document).attr("title", titles[0]); + } + } + + var blinkTitleOnMessage = function (username){ + if (!chatFocus){ + if (titles.length==0){ + titles.push($(document).attr("title")) + } + if (titles.indexOf(username) == -1){ + titles.push(username + " says...") + } + stopBlink(); + blinkTitle(titles,titles.length-1); + } + } + + + + //////////////////// + //Control user data input on the chatbox + //////////////////// + + //Return true to allow user to send data to the chatbox. + var userChatDataInputControl = function(){ + var floodControlBoolean = floodControl(); + var offlineDataSendControlBoolean = offlineDataSendControl(); + return (floodControlBoolean && offlineDataSendControlBoolean); + } + + + + //////////////////// + //Antiflood + //////////////////// + + var lastMessageTimes = new Array(); + //lastMessageTimes['slug'] = ["timeOfLastMessage",["msgID1","msgID2"]]; + + var timeBetweenMessages = 500; //mseconds + + //Return true when detects a text storm and control the text flood: + //timeBetweenMessages is the minimum time that must elapse between the messages of the same contact. + var antifloodControl = function(from_slug,msg,msgID) { + if( from_slug in lastMessageTimes){ + + } else { + lastMessageTimes[from_slug] = [,[]]; + } + + if (msgID==null){ + msgID = generateMessageID(); + } + + var lastMessageTime = lastMessageTimes[from_slug][0]; + + var t = (new Date()).getTime(); + if(t - lastMessageTime < timeBetweenMessages) { + //Flood detected + return retryToShowMessage(from_slug,msg,msgID); + } + + //Check if is the first message of this user to be send. + //var messageQueue = lastMessageTimes[from_slug][1]; + + if (lastMessageTimes[from_slug][1].length>0){ + if((lastMessageTimes[from_slug][1])[0]==msgID){ + //Message is the first on the queue: Show it and remove from the queue + lastMessageTimes[from_slug][1].splice(0,1); + } else { + //Message is not the first on the queue + return retryToShowMessage(from_slug,msg,msgID); + } + } + + //Message can be send + lastMessageTimes[from_slug][0] = t; + return false; + }; + + + var rootMessageID=1; + var generateMessageID = function(){ + return (++rootMessageID); + } + + + var retryToShowMessage = function(from_slug,msg,msgID){ + //Enque the message if isn't in the queue + if (lastMessageTimes[from_slug][1].indexOf(msgID)==-1){ + lastMessageTimes[from_slug][1].push(msgID); + } + + setTimeout(function(){PRESENCE.UIMANAGER.afterReceivedChatMessage(from_slug,msg,msgID)}, timeBetweenMessages); + return true; + } -var rootMessageID=1; -function generateMessageID(){ - return (++rootMessageID); -} - - -function retryToShowMessage(from_slug,msg,msgID){ - //Enque the message if isn't in the queue - if (lastMessageTimes[from_slug][1].indexOf(msgID)==-1){ - lastMessageTimes[from_slug][1].push(msgID); - } - - setTimeout(function(){afterReceivedChatMessage(from_slug,msg,msgID)}, timeBetweenMessages); - return true; -} - - - - - -//////////////////// -//Controlflood -//////////////////// -var timeBetweenOwnMessages = 500; //mseconds -var lastMessageSentTime=null; - -function floodControl() { - var t = (new Date()).getTime(); - if(lastMessageSentTime==null){ - lastMessageSentTime = t; - return true; - } + //////////////////// + //Controlflood + //////////////////// + var timeBetweenOwnMessages = 500; //mseconds + var lastMessageSentTime=null; - if (t - lastMessageSentTime < timeBetweenOwnMessages) { - return false; - } else { - lastMessageSentTime = t; - return true; - } -}; + var floodControl = function() { + var t = (new Date()).getTime(); + + if(lastMessageSentTime==null){ + lastMessageSentTime = t; + return true; + } + + if (t - lastMessageSentTime < timeBetweenOwnMessages) { + return false; + } else { + lastMessageSentTime = t; + return true; + } + }; -//////////////////// -//Bounce chatbox control -//////////////////// -var lastBounceTimes = new Array(); -var timeBetweenBounces = 5000; //mseconds - -function mustBounceBoxForChatWindow(jqueryUIChatbox){ + //////////////////// + //Bounce chatbox control + //////////////////// + var lastBounceTimes = new Array(); + var timeBetweenBounces = 5000; //mseconds - var slug = jqueryUIChatbox.elem.uiChatbox.find(".ui-chatbox-content").find(".ui-chatbox-log").attr("id") + var mustBounceBoxForChatWindow = function(jqueryUIChatbox){ + + var slug = jqueryUIChatbox.elem.uiChatbox.find(".ui-chatbox-content").find(".ui-chatbox-log").attr("id") + + if (typeof slug == 'undefined') { + return false; + } + + if((slug in contactsInfo)&&(contactsInfo[slug].videoChatStatus!="disconnected")){ + return false; + } - if (typeof slug == 'undefined') { - return false; + var t = (new Date()).getTime(); + + if(!(slug in lastBounceTimes)){ + lastBounceTimes[slug] = t; + return true; + } + + var lastBounceTime = lastBounceTimes[slug]; + + if (t - lastBounceTime < timeBetweenBounces) { + return false; + } else { + lastBounceTimes[slug] = t; + return true; + } + } - if((slug in contactsInfo)&&(contactsInfo[slug].videoChatStatus!="disconnected")){ - return false; + + + //////////////////// + //Prevent user to send data to the chatbox when he is offline. + //////////////////// + + var offlineDataSendControl = function(){ + return ((!PRESENCE.XMPPClient.getDisconnectionFlag()) && (PRESENCE.XMPPClient.isStropheConnected())); } - - var t = (new Date()).getTime(); - if(!(slug in lastBounceTimes)){ - lastBounceTimes[slug] = t; - return true; - } - - var lastBounceTime = lastBounceTimes[slug]; - if (t - lastBounceTime < timeBetweenBounces) { - return false; - } else { - lastBounceTimes[slug] = t; - return true; - } + //////////////////// + //Special slugs management + //////////////////// + var isAdminSlug = function(slug){ + return (slug == '<%=SocialStream::Presence.social_stream_presence_username%>'); + } -} + + //////////////////// + //Next features... + //////////////////// + //[...] + return { + init: init, + log : log, + onChatFocus : onChatFocus, + onChatBlur : onChatBlur, + blinkTitleOnMessage : blinkTitleOnMessage, + blinkTitle : blinkTitle, + userChatDataInputControl : userChatDataInputControl, + antifloodControl : antifloodControl, + mustBounceBoxForChatWindow : mustBounceBoxForChatWindow, + isAdminSlug : isAdminSlug, + getChatFocus : getChatFocus + }; -//////////////////// -//Prevent user to send data to the chatbox when he is offline. -//////////////////// -function offlineDataSendControl(){ - return ((!disconnectionFlag) && (isStropheConnected())); -} - - -//////////////////// -//Special slugs management -//////////////////// -function isAdminSlug(slug){ - return (slug == '<%=SocialStream::Presence.social_stream_presence_username%>'); -} - - - -//////////////////// -//Next features... -//////////////////// - - +}) (PRESENCE, jQuery);