User:Gary/disambiguation navigator.coffee

From Wikipedia, the free encyclopedia
 DISAMBIGUATION NAVIGATOR
 Description: Makes navigation of disambiguation pages easier by associating
   each article with a keyboard key. Click on a key, such as "5", to click on
   the fifth article in a disambiguation page. Works with all numbers and
   letters on the keyboard.

class DisambiguationLinks

 @init: =>
   disambigBoxes = $('#disambigbox')
   setIndexBox = $('#setindexbox')
   if (not disambigBoxes.length and not setIndexBox.length) or
     window.mw.config.get('wgCanonicalNamespace') isnt  or
     window.mw.config.get('wgAction') isnt 'view'
       return false
   @setCss()
   numbers = '1234567890'
   letters = 'abcdefghijklmnopqrstuvwxyz'
   # Loop through each disambiguation list item
   # Place either a number or letter next to each one
   @setKeyForItems(letters)
   # 49 to 57, 48, 65 to 90
   keys = {}
   j = 0
   k = 0
   i = 49
   while i <= 57
     keys[i] = numbers.charAt(j)
     i++
     j++
   keys[48] = numbers.charAt(j)
   i = 65
   while i <= 90
     keys[i] = letters.charAt(k)
     i++
     k++
   # keys contains numbers and letters
   $.merge(keys, letters)
   $('html').on 'keydown', (event) =>
     if not @isModifierKey(event) and not @isInInputField(event)
       @goToDisambigLink(event, keys)
       event.preventDefault()
 @goToDisambigLink: (e, keys) ->
   if not keys[e.which]
     return false
   id = $('#disambig-link-' + keys[e.which])
   href = id.attr('href')
   if not href
     return false
   # Remove highlight from other links, then highlight the link's line
   $('.selected-disambig-link').removeClass('selected-disambig-link')
   id.closest('li').addClass('selected-disambig-link')
   # Go to the link
   location.href = href
 @isInInputField: (event) ->
   if ['INPUT', 'TEXTAREA'].indexOf(event.target.nodeName) > -1
     true
   else
     false
 @isModifierKey: (event) ->
   if event.altKey or event.ctrlKey or event.metaKey or event.shiftKey
     true
   else
     false
 @setCss: ->
   window.mw.util.addCSS '
     .disambig-link { color: #777; display: inline-block; width: 20px; }
     .catlinks .disambig-link { display: none; }
     .selected-disambig-link { background-color: #ff9; }'
 @setKeyForItems: (letters) ->
   listIndex = 1
   charIndex = 0
   $('#bodyContent').find('li').each (index, element) ->
     article = $(element)
     # Don't use this LI if it's part of the TOC
     if article.closest('#toc').length
       return true
     links = article.find('a')
     styledLinks = article.find('b > a, i > a, u > a')
     # There are no links in the current line.
     if (not links.length or
       # The first link is wrapped in another tag.
       links.first().parent()[0] isnt article[0]) and
       # The same as the above two but for styled links.
       (not styledLinks.length or
       styledLinks.first().parent().parent()[0] isnt article[0])
         return true
     links.first().attr('id', 'disambig-link-' + listIndex)
     article.prepend('(' + listIndex +
       ') ')
     # Increment index. Numbers from 1 to 0, then a to z. Covers 36 links.
     if listIndex is 9
       listIndex = 0
     else if listIndex is 0 or typeof(listIndex) is 'string'
       listIndex = letters.charAt(charIndex)
       if not listIndex
         return false
       charIndex++
     else
       listIndex++
     if listIndex is 
       return false

$ ->

 DisambiguationLinks.init()