From 0119cfa3a47579ab1786cad85e48a941c8b576aa Mon Sep 17 00:00:00 2001 From: franzz Date: Sat, 13 Sep 2014 17:52:21 +0200 Subject: [PATCH] fixing tab in chat --- jquery/common.js | 141 ++++++++++++++++++++++++++++++++++------------ masks/chat.html | 142 +++++++++++++++++++++++++++-------------------- masks/logon.html | 2 +- todo | 1 - 4 files changed, 189 insertions(+), 97 deletions(-) diff --git a/jquery/common.js b/jquery/common.js index 5d7a341..97019a2 100644 --- a/jquery/common.js +++ b/jquery/common.js @@ -1,23 +1,40 @@ /* jQuery functions */ (function($) { - $.fn.getCursorPosition = function() { - var input = this.get(0); - if (!input) return; // No (input) element found - if ('selectionStart' in input) { - // Standard-compliant browsers - return input.selectionStart; - } else if (document.selection) { - // IE - input.focus(); - var sel = document.selection.createRange(); - var selLen = document.selection.createRange().text.length; - sel.moveStart('character', -input.value.length); - return sel.text.length - selLen; - } - }; + $.fn.getCursorPosition = function() { + var input = this.get(0); + if (!input) return; // No (input) element found + if ('selectionStart' in input) { + // Standard-compliant browsers + return input.selectionStart; + } else if (document.selection) { + // IE + input.focus(); + var sel = document.selection.createRange(); + var selLen = document.selection.createRange().text.length; + sel.moveStart('character', -input.value.length); + return sel.text.length - selLen; + } + }; })(jQuery); +$.fn.setCursorPosition = function(pos) +{ + this.each(function(index, elem) + { + if(elem.setSelectionRange) elem.setSelectionRange(pos, pos); + else if(elem.createTextRange) + { + var range = elem.createTextRange(); + range.collapse(true); + range.moveEnd('character', pos); + range.moveStart('character', pos); + range.select(); + } + }); + return this; +}; + $.prototype.addButton = function(sType, sTitle, oClickLink, sId, sButtonClass, sTmpVar) { $This = $(this); @@ -120,29 +137,48 @@ $.prototype.checkForm = function(sSelector) /* Common functions */ String.prototype.stripVowelAccent = function() -{ - var sText = this; - var rExps= - [ - {re:/[\xC0-\xC6]/g, ch:'A'}, - {re:/[\xE0-\xE6]/g, ch:'a'}, - {re:/[\xC8-\xCB]/g, ch:'E'}, - {re:/[\xE8-\xEB]/g, ch:'e'}, - {re:/[\xCC-\xCF]/g, ch:'I'}, - {re:/[\xEC-\xEF]/g, ch:'i'}, - {re:/[\xD2-\xD6]/g, ch:'O'}, - {re:/[\xF2-\xF6]/g, ch:'o'}, - {re:/[\xD9-\xDC]/g, ch:'U'}, - {re:/[\xF9-\xFC]/g, ch:'u'}, - {re:/[\xD1]/g, ch:'N'}, - {re:/[\xF1]/g, ch:'n'} - ]; - for(var i=0, len=rExps.length; i 124) + { + for(var z=0;z
- +
@@ -84,6 +84,7 @@ databap.pageInit = function() self.tmp('get_news', false); self.tmp('last_message_id', '0'); self.tmp('scrolling', 'boolean'); + databap.tmp('tab_info', 'object'); //Main elements $MsgInput = databap.getMainElem('#message'); @@ -488,6 +489,7 @@ function quitChan(sChanKeyName) function add_message(e) { + if(e.keyCode != 9) databap.tmp('tab_info', {}); //See case 9, tab switch(e.keyCode) { case 13 : //enter @@ -535,67 +537,84 @@ function add_message(e) } break; case 9 : //tab - /* Action Plan - - Detect current caret position : $this.getCursorPosition() - - Find word wrapped around cursor : while loop until non alphanum char before and after caret position - - - */ - //Init - $this = databap.getMainElem('#message'); - var chat_message = $.trim($this.val()); - if(!e.altKey && chat_message.length>0) + $This = databap.getMainElem('#message'); + var sChatMsg = $This.val(); + var iChatMsgLen = sChatMsg.length; + + if(!e.altKey && iChatMsgLen>0) { - var user_name = ""; - var index = 0; - - //Finding last word - var last_word = (chat_message.lastIndexOf(" ")==-1)?chat_message:chat_message.substr(chat_message.lastIndexOf(" ") + 1); - //debug(last_word); - if(last_word.substr(0, 1)=='@') last_word = last_word.substr(1); - - var last_word_len = last_word.length; - var approx_nick = last_word.toLowerCase(); + var iCurPos, iFirstPos, iOffset, sWord, sWordLen; - //Tabbing through positive results - var existing_nick = false; - var userIndex = $.inArray(last_word, users_list); - if(userIndex==-1) + //Loading previous data + bLooping = !$.isEmptyObject(databap.tmp('tab_info')); + if(bLooping) { - //Recording first search keyword - //debug('buffering approx nick : "'+last_word+'"'); - prev_approx_nick = approx_nick; - prev_approx_nick_len = last_word_len; - userIndex = users_list.length - 1; + sChatMsg = databap.tmp(['tab_info', 'chat_msg']); + iCurPos = databap.tmp(['tab_info', 'cur_pos']); + iFirstPos = databap.tmp(['tab_info', 'first_pos']); + sWord = databap.tmp(['tab_info', 'word']); } - else existing_nick = true; - - //debug('last word : "'+last_word+'"'); - //debug('existing_nick : '+existing_nick); - - //Next nickname in the list - index = (userIndex+1)%users_list.length; - count = 0; - - //loop on the nickname list - while(count0) + { + //turn to safename for matching + var sSafeWord = getSafeNickname(sWord); + var iSafeWordLen = sSafeWord.length; + + //List of current channel users + var asUserList = databap.tmp(['users', getChanKeyName(currentChan())]); + var asSafeUserList = Object.keys(asUserList); + var iUserListLen = asSafeUserList.length; + + //Starting list where we left off + var iCount = 0; + var iIndex = 0; + if(bLooping) iIndex = (Number(array_search(databap.tmp(['tab_info', 'safe_nickname']), asSafeUserList)) + 1)%iUserListLen; + + //Looping on user names to find the one(s) starting with the searched word + while(iCount < iUserListLen) + { + //Checking for first letters of user names + if(sSafeWord == asSafeUserList[iIndex].substr(0, sSafeWord.length)) break; + + //Looping to the top of the list once finished + iIndex = (iIndex + 1)%iUserListLen; + iCount++; + } + + if(iCount !== iUserListLen) + { + //Replace text in message input box + var sBeforeWord = sChatMsg.substr(0, iFirstPos); + var sAfterWord = sChatMsg.substr(iFirstPos+sWordLen); + var sSafeNickName = asSafeUserList[iIndex]; + var sNickName = asUserList[sSafeNickName]; + $This.val(sBeforeWord+sNickName+sAfterWord); + $This.setCursorPosition((sBeforeWord+sNickName).length); + + //Save value for tab-loops + if(bLooping) databap.tmp(['tab_info', 'safe_nickname'], sSafeNickName); //not saving index in case of user list refresh + else databap.tmp(['tab_info'], {'chat_msg':sChatMsg, 'cur_pos':iCurPos, 'first_pos':iFirstPos, 'word':sWord, 'safe_nickname':sSafeNickName}); + } + else databap.feedback('warning', 'Aucun nickname commençant "'+sWord+'" par n\'a été trouvé'); + } } break; /*default:debug(e.keyCode);break;*/ @@ -809,7 +828,7 @@ function refresh_users() { //empty current users list databap.getMainElem('#connected_users').empty(); - users_list = []; + databap.tmp('users', {}); $.each ( @@ -821,8 +840,8 @@ function refresh_users() chan_info, function(key, user_info) { - var sNickName = user_info.nickname.replace('"', '\''); - users_list.push(sNickName); + var sNickName = user_info.nickname; + databap.tmp(['users', sChankeyName, getSafeNickname(sNickName)], sNickName); var profileLink = databap.getInternalLink('profil', user_info.id_user); var mission = 'Mission actuelle : '+(user_info.status || 'Aucune'); var pm = 'Cliquez pour lancer un channel privé avec '+user_info.name+' ('+user_info.company+')'; @@ -830,7 +849,7 @@ function refresh_users() .append($('', {'class':'connected_user_logo', href:profileLink, title:mission, target:'_blank'}) .append($('', {src:databap.consts.app_image_folder+user_info.logo})) .append((user_info.afk=='1')?$('', {class:'fa fa-c-afk afk'}):'')) - .append($('', {'class':'connected_user_name clickable '+sNickName, id:user_info.id_user, title:pm}).text(sNickName)); + .append($('', {'class':'connected_user_name clickable '+sNickName.replace('"', '\''), id:user_info.id_user, title:pm}).text(sNickName)); $user.find('.connected_user_name').click(joinPmChan); databap.getMainElem('#connected_users').append($user); } @@ -852,6 +871,11 @@ function refresh_users() } } +function getSafeNickname(sNickName) +{ + return sNickName.stripVowelAccent().toLowerCase(); +} + function setPm() { var name = $(this).text(); diff --git a/masks/logon.html b/masks/logon.html index fa87761..b4bf7cd 100755 --- a/masks/logon.html +++ b/masks/logon.html @@ -24,7 +24,7 @@ - + diff --git a/todo b/todo index 092e159..bf16090 100644 --- a/todo +++ b/todo @@ -8,7 +8,6 @@ Internal: - put feedback box in title Bug fix: -- [1.0.0] Tab dans chat - [1.0.1] Check le document type plutot que l'extension pour les /img - [1.0.1] Resize .gif - Fix les "xxx se déconnecte" intempestives
Nom & prénom
Mot de passe