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