fixing tab in chat

This commit is contained in:
2014-09-13 17:52:21 +02:00
parent 9196953a48
commit 0119cfa3a4
4 changed files with 189 additions and 97 deletions

View File

@@ -24,7 +24,7 @@
</div>
<div id="chat_input" class="tiny_round">
<form id="chat_form" name="chat_form">
<i id="message_img" class="fa fa-fw fa-20 fa-c-comment"></i><input type="text" id="message" name="message" value="" maxlength="500" />
<i id="message_img" class="fa fa-fw fa-20 fa-c-comment"></i><input type="text" id="message" name="message" value="" maxlength="500" autofocus />
<input type="hidden" id="chan" name="chan" value="" maxlength="50" />
</form>
</div>
@@ -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(count<users_list.length)
{
//debug('index: '+index);
var nick = users_list[index];
var lower_nick = nick.toLowerCase();
if
(
!existing_nick && lower_nick.substr(0, last_word_len) == approx_nick ||
existing_nick && lower_nick.substr(0, prev_approx_nick_len) == prev_approx_nick
)
else
{
//Find word in chat message input box
iCurPos = $This.getCursorPosition();
iFirstPos = sChatMsg.substr(0, iCurPos).lastIndexOf(' ') + 1;
iOffset = sChatMsg.substr(iFirstPos).indexOf(' ');
sWord = sChatMsg.substr(iFirstPos, (iOffset==-1)?iChatMsgLen:iOffset);
//@ excluded
if(sWord.substr(0, 1)=='@')
{
user_name = nick;
break;
iFirstPos++;
sWord = sWord.substr(1);
}
index = (index+1)%users_list.length;
count++;
}
//debug('chat message : '+chat_message);
if(user_name!='') $this.val(chat_message.substr(0, chat_message.length - last_word_len)+user_name+' ');
sWordLen = sWord.length;
//No guess if not at least one letter
if(sWordLen>0)
{
//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($('<a>', {'class':'connected_user_logo', href:profileLink, title:mission, target:'_blank'})
.append($('<img>', {src:databap.consts.app_image_folder+user_info.logo}))
.append((user_info.afk=='1')?$('<i>', {class:'fa fa-c-afk afk'}):''))
.append($('<a>', {'class':'connected_user_name clickable '+sNickName, id:user_info.id_user, title:pm}).text(sNickName));
.append($('<a>', {'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();