Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * tabcompletion, emotes and usernames
- * also overwrite native tabcompletion
- */
- $('#chatline').unbind('keydown',tabSuggestions || function(){});
- var tabSuggestions = (function(){
- var chatline = document.getElementById('chatline'),
- suggestionsDiv = document.getElementById('suggestionsDiv') || document.createElement('div'),
- lastMatchNum = 0,
- lastMatch;
- suggestionsDiv.id = 'suggestionsDiv',
- suggestionsDiv.style.position = 'absolute',
- suggestionsDiv.style.zIndex = '999',
- suggestionsDiv.style.backgroundColor = '#161a20',
- chatline.parentNode.appendChild(suggestionsDiv)
- function emoteImg(emote){
- var i = document.createElement('img')
- i.className = 'channel-emote',
- i.src = emote.image,
- i.title = emote.name,
- i.onclick = function(e){
- clearBorders();
- chatline.value = chatline.value.replace(/\/[a-z0-9?!&]*$/i, emote.name),
- // als er geen spatie of dubbele punt voor de /emote staat, voeg een spatie toe (anders werkt de emote niet)
- chatline.value.lastIndexOf('/') !== 0 && // check eerst of de / niet het eerste teken van de string is
- [' ',':'].indexOf(chatline.value[chatline.value.lastIndexOf('/')-1]) === -1 && // staat er iets anders dan een spatie of : voor de /emote ?
- (chatline.value = chatline.value.replace(/\/[a-z0-9?!&]*$/i, ' ' + emote.name)), // zet een spatie voor de /emote
- this.style.border = '2px solid white',
- chatline.focus();
- var i = 0, a = this;
- while (a = a.previousSibling) i++;
- lastMatchNum = i
- }
- return i
- }
- function clearBorders(){
- for (var i = 0; i < suggestionsDiv.children.length; i++)
- suggestionsDiv.children[i].style.border = 'none'
- }
- function levenshtein(a, b) {
- if (a.length === 0) return b.length
- if (b.length === 0) return a.length
- let tmp, i, j, prev, val, row
- // swap to save some memory O(min(a,b)) instead of O(a)
- if (a.length > b.length) {
- tmp = a
- a = b
- b = tmp
- }
- row = Array(a.length + 1)
- // init the row
- for (i = 0; i <= a.length; i++) {
- row[i] = i
- }
- // fill in the rest
- for (i = 1; i <= b.length; i++) {
- prev = i
- for (j = 1; j <= a.length; j++) {
- if (b[i - 1] === a[j - 1]) {
- val = row[j - 1] // match
- } else {
- val = Math.min(row[j - 1] + 1, // substitution
- Math.min(prev + 1, // insertion
- row[j] + 1)) // deletion
- }
- row[j - 1] = prev
- prev = val
- }
- row[a.length] = prev
- }
- return row[a.length]
- }
- return function(e){
- if (e.keyCode != 9 && !e.shiftKey)
- suggestionsDiv.innerHTML = '',
- lastMatch = null
- else if (e.keyCode == 9){
- //emotes
- if (lastMatch) {
- //toggle matches
- if (Object.prototype.toString.call(lastMatch) === '[object Array]')
- //usernames
- chatline.value = chatline.value.replace(/[a-z0-9-]+$/i, lastMatch[++lastMatchNum % lastMatch.length])
- else
- //emotes
- e.shiftKey ? lastMatchNum = (--lastMatchNum + suggestionsDiv.children.length) % suggestionsDiv.children.length :
- lastMatchNum = ++lastMatchNum % suggestionsDiv.children.length,
- clearBorders(),suggestionsDiv.children[lastMatchNum] && suggestionsDiv.children[lastMatchNum].onclick()
- } else {
- var emoteStr = chatline.value.match(/\/[a-z0-9?!&]*$/i)
- if (emoteStr){
- //show emotesuggestions
- var emoteList = CHANNEL.emotes.slice(),
- suggestions = []
- //find matches based on levenshtein distance
- emoteStr = emoteStr[0].replace(/\?/g,'\\?')
- for (var i = 0, emoteStr_withoutslash = emoteStr.replace('/',''); i < emoteList.length; i++)
- if (emoteList[i].name.match(emoteStr_withoutslash) || levenshtein(emoteStr, emoteList[i].name) < 3)
- suggestions.push(new emoteImg(emoteList[i]))
- //sort suggestions
- suggestions.sort(function(a, b){
- var indexofa = a.title.indexOf(emoteStr),
- indexofb = b.title.indexOf(emoteStr)
- if (indexofa === 0 && indexofb !== 0)
- return -1
- else if (indexofb === 0 && indexofa !== 0)
- return 1
- else {
- //sort aphabetically with respect for numbers at the end
- var i = 0;
- while (a.title.charAt(i) === b.title.charAt(i) && i < a.title.length && i < b.title.length)
- i++;
- if (a.title.length !== b.title.length &&
- (i == a.title.length - 1 || i == b.title.length - 1) &&
- !isNaN(parseInt(a.title.charAt(i))) && !isNaN(parseInt(b.title.charAt(i))))
- return a.title.charAt(i) < b.title.charAt(i) ? 1 : -1
- return a.title.charAt(i) < b.title.charAt(i) ? -1 : 1
- }
- })
- //populate suggestionsDiv
- suggestionsDiv.innerHTML = ''
- for (var i=0; i<suggestions.length; i++)
- suggestionsDiv.appendChild(suggestions[i])
- lastMatchNum = 0,
- lastMatch = emoteStr,
- suggestionsDiv.children[0] && (suggestionsDiv.children[0].style.border = '2px solid white') && suggestionsDiv.children[lastMatchNum].onclick()
- } else {
- //usernames
- var userStr = chatline.value.match(/[a-z0-9-]+$/i),
- userlistElems = $('#userlist .userlist_item');//document.getElementById("userlist").children
- lastMatch = [],
- lastMatchNum = 0
- for (var i = 0; i < userlistElems.length; i++)
- if (userlistElems[i].children[1].textContent != CLIENT.name &&
- userlistElems[i].children[1].textContent.match(new RegExp('^' + userStr, 'i')))
- lastMatch.push(userlistElems[i].children[1].textContent);
- if (lastMatch.length != 0)
- chatline.value = chatline.value.replace(/[a-z0-9-]+$/i, lastMatch[0])
- else
- lastMatch = null
- }
- }
- }
- }
- })()
- chatTabComplete = function(){return false},
- $("#chatline").keydown(tabSuggestions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement