diff --git a/js/qvitter.js b/js/qvitter.js index ff0fb86..181d632 100644 --- a/js/qvitter.js +++ b/js/qvitter.js @@ -3381,121 +3381,66 @@ $('body').on('keyup', 'div.queet-box-syntax', function(e) { }); }); -// check for user mentions window.lastMention = new Object(); -$('body').on('keyup', 'div.queet-box-syntax', function(e) { - - var queetBox = $(this); - var cursorPosArray = getSelectionInElement(queetBox[0]); - var cursorPos = cursorPosArray[0]; - - // add space before linebreaks (to separate mentions in beginning of new lines when .text():ing later) - if(e.keyCode == '13') { - e.preventDefault(); - var range = createRangeFromCharacterIndices(queetBox[0], cursorPos, cursorPos); - range.insertNode(document.createTextNode(" \n")); - } - else if(e.keyCode != '40' && e.keyCode != '38' && e.keyCode != '13' && e.keyCode != '9') { - var contents = queetBox.text().substring(0,cursorPos); - var mentionPos = contents.lastIndexOf('@'); - var check_contents = contents.substring(mentionPos - 1, cursorPos); - var regex = /(^|\s|\.|\n)(@)[a-zA-Z0-9]+/; - var match = check_contents.match(regex); - if (contents.indexOf('@') >= 0 && match) { - - if(contents.lastIndexOf('@') > 1) { - match[0] = match[0].substring(1,match[0].length); - } - if((contents.lastIndexOf('@')+match[0].length) == cursorPos) { - - queetBox.siblings('.mentions-suggestions').children('.user-suggestion').remove(); - queetBox.siblings('.mentions-suggestions').css('top',(queetBox.height()+20) + 'px'); - var term = match[0].substring(match[0].lastIndexOf('@')+1, match[0].length).toLowerCase(); - window.lastMention.mentionPos = mentionPos; - window.lastMention.cursorPos = cursorPos; - - - // see if anyone we're following matches - var suggestionsToShow = []; - var suggestionsUsernameCount = {}; - suggestionsUsernameCount[window.loggedIn.screen_name] = 1; // any suggestions with the same screen name as mine will get their server url added - $.each(window.following,function(){ - var userregex = new RegExp(term); - if(this.username.toLowerCase().match(userregex) || this.name.toLowerCase().match(userregex)) { - suggestionsToShow.push({avatar:this.avatar, name:this.name, username:this.username,url:this.url}); - - // count the usernames to see if we need to show the server for any of them - if(typeof suggestionsUsernameCount[this.username] != 'undefined') { - suggestionsUsernameCount[this.username] = suggestionsUsernameCount[this.username] + 1; - } - else { - suggestionsUsernameCount[this.username] = 1; - } - } - }); - - // show matches - $.each(suggestionsToShow,function(){ - var serverHtml = ''; - if(suggestionsUsernameCount[this.username]>1 && this.url !== false) { - serverHtml = '@' + this.url; - } - queetBox.siblings('.mentions-suggestions').append('
' + this.name + ' @' + this.username + serverHtml + '
') - }); - - } - else { - queetBox.siblings('.mentions-suggestions').children('.user-suggestion').remove(); - } - - } - else { - queetBox.siblings('.mentions-suggestions').children('.user-suggestion').remove(); - } - } - }); - - +// check for user mentions +$('body').on('keyup', 'div.queet-box-syntax', function(e) { checkMentions(e, true);}); // check for group mentions -$('body').on('keyup', 'div.queet-box-syntax', function(e) { +$('body').on('keyup', 'div.queet-box-syntax', function(e) { checkMentions(e, false);}); - var queetBox = $(this); +/** + * check for user/group mentions + * + * @param {Object} e: Event object + * @param {boolean} isUser + */ +function checkMentions(e, isUser) { + var mark = '!'; + var prefix = 'group'; + if (isUser) { + mark = '@'; + prefix = 'user'; + } + + // var queetBox = $(this); + var queetBox = $('body div.queet-box-syntax'); var cursorPosArray = getSelectionInElement(queetBox[0]); var cursorPos = cursorPosArray[0]; - // add space before linebreaks (to separate mentions in beginning of new lines when .text():ing later) - if(e.keyCode == '13') { - e.preventDefault(); - var range = createRangeFromCharacterIndices(queetBox[0], cursorPos, cursorPos); - range.insertNode(document.createTextNode(" \n")); - } - else if(e.keyCode != '40' && e.keyCode != '38' && e.keyCode != '13' && e.keyCode != '9') { + if(e.keyCode != '40' && e.keyCode != '38' && e.keyCode != '13' && e.keyCode != '9') { var contents = queetBox.text().substring(0,cursorPos); - var mentionPos = contents.lastIndexOf('!'); + var mentionPos = contents.lastIndexOf(mark); var check_contents = contents.substring(mentionPos - 1, cursorPos); - var regex = /(^|\s|\.|\n)(!)[a-zA-Z0-9]+/; + var regex = new RegExp('(^|\s|\.|\n)(' + mark + ')[a-zA-Z0-9]+'); var match = check_contents.match(regex); - if (contents.indexOf('!') >= 0 && match) { + if (contents.indexOf(mark) >= 0 && match) { - if(contents.lastIndexOf('!') > 1) { + if(contents.lastIndexOf(mark) > 1) { match[0] = match[0].substring(1,match[0].length); } - if((contents.lastIndexOf('!')+match[0].length) == cursorPos) { + if((contents.lastIndexOf(mark)+match[0].length) == cursorPos) { - queetBox.siblings('.mentions-suggestions').children('.group-suggestion').remove(); + queetBox.siblings('.mentions-suggestions').children('.' + prefix + '-suggestion').remove(); queetBox.siblings('.mentions-suggestions').css('top',(queetBox.height()+20) + 'px'); - var term = match[0].substring(match[0].lastIndexOf('!')+1, match[0].length).toLowerCase(); + var term = match[0].substring(match[0].lastIndexOf(mark)+1, match[0].length).toLowerCase(); window.lastMention.mentionPos = mentionPos; window.lastMention.cursorPos = cursorPos; - // see if any group we're member of matches + // see if any user/group we're following matches var suggestionsToShow = []; var suggestionsUsernameCount = {}; - $.each(window.groupMemberships,function(){ + if (isUser) { + suggestionsUsernameCount[window.loggedIn.screen_name] = 1; // any suggestions with the same screen name as mine will get their server url added + } + var targets = isUser ? (window.following) : (window.groupMemberships); + $.each(targets, function(){ var userregex = new RegExp(term); if(this.username.toLowerCase().match(userregex) || this.name.toLowerCase().match(userregex)) { - suggestionsToShow.push({id:this.id, avatar:this.avatar, name:this.name, username:this.username,url:this.url}); + var suggestion = {avatar:this.avatar, name:this.name, username:this.username,url:this.url}; + if (!isUser) { + suggestion.id = this.id; + } + suggestionsToShow.push(suggestion); // count the usernames to see if we need to show the server for any of them if(typeof suggestionsUsernameCount[this.username] != 'undefined') { @@ -3511,23 +3456,27 @@ $('body').on('keyup', 'div.queet-box-syntax', function(e) { $.each(suggestionsToShow,function(){ var serverHtml = ''; if(suggestionsUsernameCount[this.username]>1 && this.url !== false) { - serverHtml = this.url + '/group/'; + serverHtml = isUser ? ('@' + this.url) : (this.url + '/group/'); + } + if (isUser) { + queetBox.siblings('.mentions-suggestions').append('
' + this.name + ' @' + this.username + serverHtml + '
') + } + else { + queetBox.siblings('.mentions-suggestions').append('
' + this.name + ' !' + this.username + '
') } - queetBox.siblings('.mentions-suggestions').append('
' + this.name + ' !' + this.username + '
') }); } else { - queetBox.siblings('.mentions-suggestions').children('.group-suggestion').remove(); + queetBox.siblings('.mentions-suggestions').children('.' + prefix + '-suggestion').remove(); } } else { - queetBox.siblings('.mentions-suggestions').children('.group-suggestion').remove(); + queetBox.siblings('.mentions-suggestions').children('.' + prefix + '-suggestion').remove(); } } - }); - +} /* · ·