includes/clientside/static/autofill.js
author Dan
Mon, 06 Jul 2009 11:27:02 -0400
changeset 1037 61354f877e06
parent 787 b0d0d429d8cf
child 1046 cfc6c30c5e2d
permissions -rw-r--r--
History: Fixed missing $q in fetchrow()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
d74ff822acc9 Replaced autocompleting username with a much more efficient algorithm and caching system
Dan
parents:
diff changeset
     1
/**
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
     2
 * Javascript auto-completion for form fields. jQuery based in 1.1.5.
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
     3
 * Different types of auto-completion fields can be defined (e.g. with different data sets). For each one, a schema
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
     4
 * can be created describing how to draw each row.
184
d74ff822acc9 Replaced autocompleting username with a much more efficient algorithm and caching system
Dan
parents:
diff changeset
     5
 */
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
     6
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
     7
var autofill_schemas = window.autofill_schemas || {};
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
     8
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
     9
/**
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    10
 * SCHEMA - GENERIC
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    11
 */
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    12
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    13
autofill_schemas.generic = {
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    14
  init: function(element, fillclass, params)
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    15
  {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    16
    $(element).autocomplete(makeUrlNS('Special', 'Autofill', 'type=' + fillclass) + '&userinput=', {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    17
        minChars: 3
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    18
    });
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    19
  }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    20
}
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    21
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    22
/**
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    23
 * SCHEMA - USERNAME
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    24
 */
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    25
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    26
autofill_schemas.username = {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    27
  init: function(element, fillclass, params)
184
d74ff822acc9 Replaced autocompleting username with a much more efficient algorithm and caching system
Dan
parents:
diff changeset
    28
  {
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    29
    params = params || {};
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    30
    var allow_anon = params.allow_anon ? '1' : '0';
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    31
    $(element).autocomplete(makeUrlNS('Special', 'Autofill', 'type=' + fillclass + '&allow_anon=' + allow_anon) + '&userinput=', {
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    32
        minChars: 3,
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    33
        formatItem: function(row, _, __)
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    34
        {
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    35
          var html = row.name_highlight + ' – ';
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    36
          html += '<span style="' + row.rank_style + '">' + row.rank_title + '</span>';
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    37
          return html;
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    38
        },
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    39
        tableHeader: '<tr><th>' + $lang.get('user_autofill_heading_suggestions') + '</th></tr>',
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    40
        showWhenNoResults: true,
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    41
        noResultsHTML: '<tr><td class="row1" style="font-size: smaller;">' + $lang.get('user_autofill_msg_no_suggestions') + '</td></tr>',
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    42
    });
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    43
  }
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    44
}
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    45
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    46
autofill_schemas.page = {
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    47
  init: function(element, fillclass, params)
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    48
  {
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    49
    $(element).autocomplete(makeUrlNS('Special', 'Autofill', 'type=' + fillclass) + '&userinput=', {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    50
        minChars: 3,
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    51
        formatItem: function(row, _, __)
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    52
        {
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    53
          var html = '<u>' + row.name_highlight + '</u>';
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    54
          html += ' &ndash; ' + row.pid_highlight;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    55
          return html;
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    56
        },
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    57
        showWhenNoResults: true,
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    58
        noResultsHTML: '<tr><td class="row1" style="font-size: smaller;">' + $lang.get('user_autofill_msg_no_suggestions') + '</td></tr>',
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    59
    });
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    60
  }
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    61
}
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    62
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    63
window.autofill_onload = function()
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    64
{
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    65
  if ( this.loaded )
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    66
  {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    67
    return true;
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    68
  }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    69
  
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    70
  var inputs = document.getElementsByClassName('input', 'autofill');
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    71
  
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    72
  if ( inputs.length > 0 )
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    73
  {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    74
    // we have at least one input that needs to be made an autofill element.
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    75
    // is spry data loaded?
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    76
    load_component('l10n');
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    77
  }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    78
  
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    79
  this.loaded = true;
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    80
  
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    81
  for ( var i = 0; i < inputs.length; i++ )
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    82
  {
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    83
    autofill_init_element(inputs[i]);
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    84
  }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
    85
}
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    86
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    87
window.autofill_init_element = function(element, params)
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    88
{
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    89
  if ( element.af_initted )
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    90
    return false;
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
    91
  
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    92
  params = params || {};
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    93
  // assign an ID if it doesn't have one yet
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    94
  if ( !element.id )
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    95
  {
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    96
    element.id = 'autofill_' + Math.floor(Math.random() * 100000);
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    97
  }
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    98
  var id = element.id;
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
    99
  
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   100
  // get the fill type
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   101
  var fillclass = element.className;
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   102
  fillclass = fillclass.split(' ');
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   103
  fillclass = fillclass[1];
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   104
  
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   105
  var schema = ( autofill_schemas[fillclass] ) ? autofill_schemas[fillclass] : autofill_schemas['generic'];
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   106
  if ( typeof(schema.init) != 'function' )
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   107
  {
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   108
    schema.init = autofill_schemas.generic.init;
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   109
  }
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   110
  schema.init(element, fillclass, params);
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   111
  
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   112
  element.af_initted = true;
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   113
}
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   114
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   115
window.AutofillUsername = function(el, allow_anon)
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   116
{
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   117
  el.onkeyup = null;
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   118
  el.className = 'autofill username';
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   119
  autofill_init_element(el, { allow_anon: allow_anon });
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   120
}
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   121
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   122
window.AutofillPage = function(el)
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   123
{
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   124
  el.onkeyup = null;
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   125
  el.className = 'autofill page';
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   126
  autofill_init_element(el, {});
581
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   127
}
5e8fd89c02ea Initial progress towards converting auto-completion framework to Spry. Not currently in a very working state.
Dan
parents: 550
diff changeset
   128
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   129
window.autofill_init = function()
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   130
{
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   131
  load_component(['l10n', 'jquery', 'jquery-ui']);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   132
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   133
  if ( !window.jQuery )
184
d74ff822acc9 Replaced autocompleting username with a much more efficient algorithm and caching system
Dan
parents:
diff changeset
   134
  {
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   135
    throw('jQuery didn\'t load properly. Aborting auto-complete init.');
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   136
  }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   137
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   138
  jQuery.autocomplete = function(input, options) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   139
    // Create a link to self
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   140
    var me = this;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   141
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   142
    // Create jQuery object for input element
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   143
    var $input = $(input).attr("autocomplete", "off");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   144
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   145
    // Apply inputClass if necessary
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   146
    if (options.inputClass) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   147
      $input.addClass(options.inputClass);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   148
    }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   149
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   150
    // Create results
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   151
    var results = document.createElement("div");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   152
    $(results).addClass('tblholder').css('z-index', getHighestZ() + 1).css('margin-top', 0);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   153
    $(results).css('clip', 'rect(0px,auto,auto,0px)').css('overflow', 'auto').css('max-height', '300px');
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   154
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   155
    // Create jQuery object for results
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   156
    // var $results = $(results);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   157
    var $results = $(results).hide().addClass(options.resultsClass).css("position", "absolute");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   158
    if( options.width > 0 ) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   159
      $results.css("width", options.width);
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   160
    }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   161
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   162
    // Add to body element
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   163
    $("body").append(results);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   164
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   165
    input.autocompleter = me;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   166
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   167
    var timeout = null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   168
    var prev = "";
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   169
    var active = -1;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   170
    var cache = {};
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   171
    var keyb = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   172
    // hasFocus was false by default, see if making it true helps
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   173
    var hasFocus = true;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   174
    var hasNoResults = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   175
    var lastKeyPressCode = null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   176
    var mouseDownOnSelect = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   177
    var hidingResults = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   178
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   179
    // flush cache
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   180
    function flushCache(){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   181
      cache = {};
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   182
      cache.data = {};
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   183
      cache.length = 0;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   184
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   185
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   186
    // flush cache
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   187
    flushCache();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   188
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   189
    // if there is a data array supplied
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   190
    if( options.data != null ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   191
      var sFirstChar = "", stMatchSets = {}, row = [];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   192
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   193
      // no url was specified, we need to adjust the cache length to make sure it fits the local data store
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   194
      if( typeof options.url != "string" ) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   195
        options.cacheLength = 1;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   196
      }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   197
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   198
      // loop through the array and create a lookup structure
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   199
      for( var i=0; i < options.data.length; i++ ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   200
        // if row is a string, make an array otherwise just reference the array
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   201
        row = ((typeof options.data[i] == "string") ? [options.data[i]] : options.data[i]);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   202
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   203
        // if the length is zero, don't add to list
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   204
        if( row[0].length > 0 ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   205
          // get the first character
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   206
          sFirstChar = row[0].substring(0, 1).toLowerCase();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   207
          // if no lookup array for this character exists, look it up now
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   208
          if( !stMatchSets[sFirstChar] ) stMatchSets[sFirstChar] = [];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   209
          // if the match is a string
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   210
          stMatchSets[sFirstChar].push(row);
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   211
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   212
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   213
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   214
      // add the data items to the cache
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   215
      if ( options.cacheLength )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   216
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   217
        for( var k in stMatchSets ) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   218
          // increase the cache size
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   219
          options.cacheLength++;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   220
          // add to the cache
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   221
          addToCache(k, stMatchSets[k]);
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   222
        }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   223
      }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   224
    }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   225
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   226
    $input
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   227
    .keydown(function(e) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   228
      // track last key pressed
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   229
      lastKeyPressCode = e.keyCode;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   230
      switch(e.keyCode) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   231
        case 38: // up
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   232
          e.preventDefault();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   233
          moveSelect(-1);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   234
          break;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   235
        case 40: // down
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   236
          e.preventDefault();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   237
          moveSelect(1);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   238
          break;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   239
        case 9:  // tab
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   240
        case 13: // return
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   241
          if( selectCurrent() ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   242
            // make sure to blur off the current field
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   243
            // (Enano edit - why do we want this, again?)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   244
            // $input.get(0).blur();
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   245
            e.preventDefault();
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   246
          }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   247
          break;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   248
        default:
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   249
          active = -1;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   250
          if (timeout) clearTimeout(timeout);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   251
          timeout = setTimeout(function(){onChange();}, options.delay);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   252
          break;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   253
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   254
    })
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   255
    .focus(function(){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   256
      // track whether the field has focus, we shouldn't process any results if the field no longer has focus
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   257
      hasFocus = true;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   258
    })
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   259
    .blur(function() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   260
      // track whether the field has focus
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   261
      hasFocus = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   262
      if (!mouseDownOnSelect) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   263
        hideResults();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   264
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   265
    });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   266
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   267
    hideResultsNow();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   268
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   269
    function onChange() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   270
      // ignore if the following keys are pressed: [del] [shift] [capslock]
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   271
      if( lastKeyPressCode == 46 || (lastKeyPressCode > 8 && lastKeyPressCode < 32) ) return $results.hide();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   272
      var v = $input.val();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   273
      if (v == prev) return;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   274
      prev = v;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   275
      if (v.length >= options.minChars) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   276
        $input.addClass(options.loadingClass);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   277
        requestData(v);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   278
      } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   279
        $input.removeClass(options.loadingClass);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   280
        $results.hide();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   281
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   282
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   283
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   284
    function moveSelect(step) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   285
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   286
      var lis = $("td", results);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   287
      if (!lis || hasNoResults) return;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   288
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   289
      active += step;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   290
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   291
      if (active < 0) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   292
        active = 0;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   293
      } else if (active >= lis.size()) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   294
        active = lis.size() - 1;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   295
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   296
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   297
      lis.removeClass("row2");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   298
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   299
      $(lis[active]).addClass("row2");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   300
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   301
      // scroll the results div
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   302
      // are we going up or down?
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   303
      var td_top = $dynano(lis[active]).Top() - $dynano(results).Top();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   304
      var td_height = $dynano(lis[active]).Height();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   305
      var td_bottom = td_top + td_height;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   306
      var visibleTopBoundary = getScrollOffset(results);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   307
      var results_height = $dynano(results).Height();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   308
      var visibleBottomBoundary = visibleTopBoundary + results_height;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   309
      var scrollTo = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   310
      if ( td_top < visibleTopBoundary && step < 0 )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   311
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   312
        // going up: scroll the results div to just higher than the result we're trying to see
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   313
        scrollTo = td_top - 7;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   314
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   315
      else if ( td_bottom > visibleBottomBoundary && step > 0 )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   316
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   317
        // going down is a little harder, we want the result to be at the bottom
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   318
        scrollTo = td_top - results_height + td_height + 7;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   319
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   320
      if ( scrollTo )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   321
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   322
        results.scrollTop = scrollTo;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   323
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   324
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   325
      // Weird behaviour in IE
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   326
      // if (lis[active] && lis[active].scrollIntoView) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   327
      // 	lis[active].scrollIntoView(false);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   328
      // }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   329
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   330
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   331
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   332
    function selectCurrent() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   333
      var li = $("td.row2", results)[0];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   334
      if (!li) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   335
        var $li = $("td", results);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   336
        if (options.selectOnly) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   337
          if ($li.length == 1) li = $li[0];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   338
        } else if (options.selectFirst) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   339
          li = $li[0];
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   340
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   341
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   342
      if (li) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   343
        selectItem(li);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   344
        return true;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   345
      } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   346
        return false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   347
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   348
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   349
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   350
    function selectItem(li) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   351
      if (!li) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   352
        li = document.createElement("li");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   353
        li.extra = [];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   354
        li.selectValue = "";
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   355
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   356
      var v = $.trim(li.selectValue ? li.selectValue : li.innerHTML);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   357
      input.lastSelected = v;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   358
      prev = v;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   359
      $results.html("");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   360
      $input.val(v);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   361
      hideResultsNow();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   362
      if (options.onItemSelect) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   363
        setTimeout(function() { options.onItemSelect(li) }, 1);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   364
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   365
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   366
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   367
    // selects a portion of the input string
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   368
    function createSelection(start, end){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   369
      // get a reference to the input element
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   370
      var field = $input.get(0);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   371
      if( field.createTextRange ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   372
        var selRange = field.createTextRange();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   373
        selRange.collapse(true);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   374
        selRange.moveStart("character", start);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   375
        selRange.moveEnd("character", end);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   376
        selRange.select();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   377
      } else if( field.setSelectionRange ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   378
        field.setSelectionRange(start, end);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   379
      } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   380
        if( field.selectionStart ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   381
          field.selectionStart = start;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   382
          field.selectionEnd = end;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   383
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   384
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   385
      field.focus();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   386
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   387
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   388
    // fills in the input box w/the first match (assumed to be the best match)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   389
    function autoFill(sValue){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   390
      // if the last user key pressed was backspace, don't autofill
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   391
      if( lastKeyPressCode != 8 ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   392
        // fill in the value (keep the case the user has typed)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   393
        $input.val($input.val() + sValue.substring(prev.length));
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   394
        // select the portion of the value not typed by the user (so the next character will erase)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   395
        createSelection(prev.length, sValue.length);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   396
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   397
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   398
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   399
    function showResults() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   400
      // get the position of the input field right now (in case the DOM is shifted)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   401
      var pos = findPos(input);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   402
      // either use the specified width, or autocalculate based on form element
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   403
      var iWidth = (options.width > 0) ? options.width : $input.width();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   404
      // reposition
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   405
      $results.css({
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   406
        width: parseInt(iWidth) + "px",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   407
        top: (pos.y + input.offsetHeight) + "px",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   408
        left: pos.x + "px"
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   409
      });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   410
      if ( !$results.is(":visible") )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   411
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   412
        $results.show("blind", {}, 200);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   413
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   414
      else
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   415
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   416
        $results.show();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   417
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   418
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   419
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   420
    function hideResults() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   421
      if (timeout) clearTimeout(timeout);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   422
      timeout = setTimeout(hideResultsNow, 200);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   423
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   424
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   425
    function hideResultsNow() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   426
      if (hidingResults) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   427
        return;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   428
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   429
      hidingResults = true;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   430
    
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   431
      if (timeout) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   432
        clearTimeout(timeout);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   433
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   434
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   435
      var v = $input.removeClass(options.loadingClass).val();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   436
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   437
      if ($results.is(":visible")) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   438
        $results.hide();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   439
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   440
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   441
      if (options.mustMatch) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   442
        if (!input.lastSelected || input.lastSelected != v) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   443
          selectItem(null);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   444
        }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   445
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   446
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   447
      hidingResults = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   448
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   449
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   450
    function receiveData(q, data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   451
      if (data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   452
        $input.removeClass(options.loadingClass);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   453
        results.innerHTML = "";
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   454
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   455
        // if the field no longer has focus or if there are no matches, do not display the drop down
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   456
        if( !hasFocus )
704
077887be639d More work on auto-completion - it auto-scrolls now and limits result divs to 300px height
Dan
parents: 701
diff changeset
   457
        {
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   458
          return hideResultsNow();
704
077887be639d More work on auto-completion - it auto-scrolls now and limits result divs to 300px height
Dan
parents: 701
diff changeset
   459
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   460
        if ( data.length == 0 && !options.showWhenNoResults )
704
077887be639d More work on auto-completion - it auto-scrolls now and limits result divs to 300px height
Dan
parents: 701
diff changeset
   461
        {
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   462
          return hideResultsNow();
704
077887be639d More work on auto-completion - it auto-scrolls now and limits result divs to 300px height
Dan
parents: 701
diff changeset
   463
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   464
        hasNoResults = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   465
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   466
        if ($.browser.msie) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   467
          // we put a styled iframe behind the calendar so HTML SELECT elements don't show through
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   468
          $results.append(document.createElement('iframe'));
704
077887be639d More work on auto-completion - it auto-scrolls now and limits result divs to 300px height
Dan
parents: 701
diff changeset
   469
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   470
        results.appendChild(dataToDom(data));
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   471
        // autofill in the complete box w/the first match as long as the user hasn't entered in more data
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   472
        if( options.autoFill && ($input.val().toLowerCase() == q.toLowerCase()) ) autoFill(data[0][0]);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   473
        showResults();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   474
      } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   475
        hideResultsNow();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   476
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   477
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   478
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   479
    function parseData(data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   480
      if (!data) return null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   481
      var parsed = parseJSON(data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   482
      return parsed;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   483
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   484
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   485
    function dataToDom(data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   486
      var ul = document.createElement("table");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   487
      $(ul).attr("border", "0").attr("cellspacing", "1").attr("cellpadding", "3");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   488
      var num = data.length;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   489
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   490
      if ( options.tableHeader )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   491
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   492
        ul.innerHTML = options.tableHeader;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   493
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   494
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   495
      if ( num == 0 )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   496
      {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   497
        // not showing any results
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   498
        if ( options.noResultsHTML )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   499
          ul.innerHTML += options.noResultsHTML;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   500
        
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   501
        hasNoResults = true;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   502
        return ul;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   503
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   504
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   505
      // limited results to a max number
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   506
      if( (options.maxItemsToShow > 0) && (options.maxItemsToShow < num) ) num = options.maxItemsToShow;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   507
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   508
      for (var i=0; i < num; i++) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   509
        var row = data[i];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   510
        if (!row) continue;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   511
        
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   512
        if ( typeof(row[0]) != 'string' )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   513
        {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   514
          // last ditch resort if it's a 1.1.4 autocomplete plugin that doesn't provide an automatic result.
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   515
          // hopefully this doesn't slow it down a lot.
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   516
          for ( var i in row )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   517
          {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   518
            if ( i == "0" || i == 0 )
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   519
              break;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   520
            row[0] = row[i];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   521
            break;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   522
          }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   523
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   524
        
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   525
        var li = document.createElement("tr");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   526
        var td = document.createElement("td");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   527
        td.selectValue = row[0];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   528
        $(td).addClass('row1');
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   529
        $(td).css("font-size", "smaller");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   530
        
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   531
        if ( options.formatItem )
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   532
        {
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   533
          td.innerHTML = options.formatItem(row, i, num);
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   534
        }
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   535
        else
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   536
        {
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   537
          td.innerHTML = row[0];
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   538
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   539
        li.appendChild(td);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   540
        ul.appendChild(li);
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   541
        
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   542
        $(td).hover(
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   543
          function() { $("tr", ul).removeClass("row2"); $(this).addClass("row2"); active = $("tr", ul).indexOf($(this).get(0)); },
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   544
          function() { $(this).removeClass("row2"); }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   545
        ).click(function(e) { 
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   546
          e.preventDefault();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   547
          e.stopPropagation();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   548
          selectItem(this)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   549
        });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   550
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   551
      
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   552
      $(ul).mousedown(function() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   553
        mouseDownOnSelect = true;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   554
      }).mouseup(function() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   555
        mouseDownOnSelect = false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   556
      });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   557
      return ul;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   558
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   559
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   560
    function requestData(q) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   561
      if (!options.matchCase) q = q.toLowerCase();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   562
      var data = options.cacheLength ? loadFromCache(q) : null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   563
      // recieve the cached data
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   564
      if (data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   565
        receiveData(q, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   566
      // if an AJAX url has been supplied, try loading the data now
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   567
      } else if( (typeof options.url == "string") && (options.url.length > 0) ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   568
        $.get(makeUrl(q), function(data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   569
          data = parseData(data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   570
          addToCache(q, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   571
          receiveData(q, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   572
        });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   573
      // if there's been no data found, remove the loading class
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   574
      } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   575
        $input.removeClass(options.loadingClass);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   576
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   577
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   578
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   579
    function makeUrl(q) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   580
      var sep = options.url.indexOf('?') == -1 ? '?' : '&'; 
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   581
      var url = options.url + encodeURI(q);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   582
      for (var i in options.extraParams) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   583
        url += "&" + i + "=" + encodeURI(options.extraParams[i]);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   584
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   585
      return url;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   586
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   587
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   588
    function loadFromCache(q) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   589
      if (!q) return null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   590
      if (cache.data[q]) return cache.data[q];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   591
      if (options.matchSubset) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   592
        for (var i = q.length - 1; i >= options.minChars; i--) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   593
          var qs = q.substr(0, i);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   594
          var c = cache.data[qs];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   595
          if (c) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   596
            var csub = [];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   597
            for (var j = 0; j < c.length; j++) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   598
              var x = c[j];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   599
              var x0 = x[0];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   600
              if (matchSubset(x0, q)) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   601
                csub[csub.length] = x;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   602
              }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   603
            }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   604
            return csub;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   605
          }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   606
        }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   607
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   608
      return null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   609
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   610
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   611
    function matchSubset(s, sub) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   612
      if (!options.matchCase) s = s.toLowerCase();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   613
      var i = s.indexOf(sub);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   614
      if (i == -1) return false;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   615
      return i == 0 || options.matchContains;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   616
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   617
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   618
    this.flushCache = function() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   619
      flushCache();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   620
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   621
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   622
    this.setExtraParams = function(p) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   623
      options.extraParams = p;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   624
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   625
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   626
    this.findValue = function(){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   627
      var q = $input.val();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   628
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   629
      if (!options.matchCase) q = q.toLowerCase();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   630
      var data = options.cacheLength ? loadFromCache(q) : null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   631
      if (data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   632
        findValueCallback(q, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   633
      } else if( (typeof options.url == "string") && (options.url.length > 0) ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   634
        $.get(makeUrl(q), function(data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   635
          data = parseData(data)
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   636
          addToCache(q, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   637
          findValueCallback(q, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   638
        });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   639
      } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   640
        // no matches
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   641
        findValueCallback(q, null);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   642
      }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   643
    }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   644
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   645
    function findValueCallback(q, data){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   646
      if (data) $input.removeClass(options.loadingClass);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   647
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   648
      var num = (data) ? data.length : 0;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   649
      var li = null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   650
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   651
      for (var i=0; i < num; i++) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   652
        var row = data[i];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   653
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   654
        if( row[0].toLowerCase() == q.toLowerCase() ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   655
          li = document.createElement("li");
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   656
          if (options.formatItem) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   657
            li.innerHTML = options.formatItem(row, i, num);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   658
            li.selectValue = row[0];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   659
          } else {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   660
            li.innerHTML = row[0];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   661
            li.selectValue = row[0];
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   662
          }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   663
          var extra = null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   664
          if( row.length > 1 ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   665
            extra = [];
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   666
            for (var j=1; j < row.length; j++) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   667
              extra[extra.length] = row[j];
701
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   668
            }
dd80cde96a6c Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents: 699
diff changeset
   669
          }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   670
          li.extra = extra;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   671
        }
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   672
      }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   673
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   674
      if( options.onFindValue ) setTimeout(function() { options.onFindValue(li) }, 1);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   675
    }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   676
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   677
    function addToCache(q, data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   678
      if (!data || !q || !options.cacheLength) return;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   679
      if (!cache.length || cache.length > options.cacheLength) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   680
        flushCache();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   681
        cache.length++;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   682
      } else if (!cache[q]) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   683
        cache.length++;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   684
      }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   685
      cache.data[q] = data;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   686
    };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   687
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   688
    function findPos(obj) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   689
      var curleft = obj.offsetLeft || 0;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   690
      var curtop = obj.offsetTop || 0;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   691
      while (obj = obj.offsetParent) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   692
        curleft += obj.offsetLeft
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   693
        curtop += obj.offsetTop
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   694
      }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   695
      return {x:curleft,y:curtop};
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   696
    }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   697
  }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   698
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   699
  jQuery.fn.autocomplete = function(url, options, data) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   700
    // Make sure options exists
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   701
    options = options || {};
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   702
    // Set url as option
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   703
    options.url = url;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   704
    // set some bulk local data
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   705
    options.data = ((typeof data == "object") && (data.constructor == Array)) ? data : null;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   706
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   707
    // Set default values for required options
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   708
    options = $.extend({
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   709
      inputClass: "ac_input",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   710
      resultsClass: "ac_results",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   711
      lineSeparator: "\n",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   712
      cellSeparator: "|",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   713
      minChars: 1,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   714
      delay: 400,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   715
      matchCase: 0,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   716
      matchSubset: 1,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   717
      matchContains: 0,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   718
      cacheLength: false,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   719
      mustMatch: 0,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   720
      extraParams: {},
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   721
      loadingClass: "ac_loading",
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   722
      selectFirst: false,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   723
      selectOnly: false,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   724
      maxItemsToShow: -1,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   725
      autoFill: false,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   726
      showWhenNoResults: false,
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   727
      width: 0
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   728
    }, options);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   729
    options.width = parseInt(options.width, 10);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   730
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   731
    this.each(function() {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   732
      var input = this;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   733
      new jQuery.autocomplete(input, options);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   734
    });
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   735
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   736
    // Don't break the chain
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   737
    return this;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   738
  }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   739
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   740
  jQuery.fn.autocompleteArray = function(data, options) {
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   741
    return this.autocomplete(null, options, data);
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   742
  }
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   743
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   744
  jQuery.fn.indexOf = function(e){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   745
    for( var i=0; i<this.length; i++ ){
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   746
      if( this[i] == e ) return i;
699
c7d737202d59 Removed Adobe Spry and replaced with jQuery. Please report any new bugs on the forums or via IRC. In a related note, auto-completion should work now at least for usernames. Still hacking away at page name completion...
Dan
parents: 687
diff changeset
   747
    }
787
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   748
    return -1;
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   749
  };
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   750
  
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   751
  autofill_onload();
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   752
};
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   753
b0d0d429d8cf Fixed: autocomplete got broken somehow
Dan
parents: 779
diff changeset
   754
addOnloadHook(autofill_init);