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