author | Dan |
Wed, 26 Aug 2009 13:05:17 -0400 | |
changeset 1106 | 01315acbc22b |
parent 1053 | bdbb49cf6f1b |
child 1227 | bdac73ed481e |
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, |
1053
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
41 |
noResultsHTML: '<tr><td class="row1" style="font-size: smaller;">' + $lang.get('user_autofill_msg_no_suggestions') + '</td></tr>' |
701
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, |
1053
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
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 |
{ |
|
1053
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
494 |
// fails in IE6 |
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
495 |
try |
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
496 |
{ |
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
497 |
ul.innerHTML = options.tableHeader; |
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
498 |
} |
bdbb49cf6f1b
One word: Internet Explorer 6. This includes a rewrite of $paths->parseAdminTree() that encodes to JSON instead of manually generating JS, so good-bye to stupid parser problems I hope.
Dan
parents:
1046
diff
changeset
|
499 |
catch ( e ) {}; |
787 | 500 |
} |
501 |
||
502 |
if ( num == 0 ) |
|
503 |
{ |
|
504 |
// not showing any results |
|
505 |
if ( options.noResultsHTML ) |
|
506 |
ul.innerHTML += options.noResultsHTML; |
|
507 |
||
508 |
hasNoResults = true; |
|
509 |
return ul; |
|
510 |
} |
|
511 |
||
512 |
// limited results to a max number |
|
513 |
if( (options.maxItemsToShow > 0) && (options.maxItemsToShow < num) ) num = options.maxItemsToShow; |
|
514 |
||
515 |
for (var i=0; i < num; i++) { |
|
516 |
var row = data[i]; |
|
517 |
if (!row) continue; |
|
518 |
||
519 |
if ( typeof(row[0]) != 'string' ) |
|
520 |
{ |
|
521 |
// last ditch resort if it's a 1.1.4 autocomplete plugin that doesn't provide an automatic result. |
|
522 |
// hopefully this doesn't slow it down a lot. |
|
523 |
for ( var i in row ) |
|
524 |
{ |
|
525 |
if ( i == "0" || i == 0 ) |
|
526 |
break; |
|
527 |
row[0] = row[i]; |
|
528 |
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
|
529 |
} |
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
|
530 |
} |
787 | 531 |
|
532 |
var li = document.createElement("tr"); |
|
533 |
var td = document.createElement("td"); |
|
534 |
td.selectValue = row[0]; |
|
535 |
$(td).addClass('row1'); |
|
536 |
$(td).css("font-size", "smaller"); |
|
537 |
||
538 |
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
|
539 |
{ |
787 | 540 |
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
|
541 |
} |
dd80cde96a6c
Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents:
699
diff
changeset
|
542 |
else |
dd80cde96a6c
Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents:
699
diff
changeset
|
543 |
{ |
787 | 544 |
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
|
545 |
} |
787 | 546 |
li.appendChild(td); |
547 |
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
|
548 |
|
787 | 549 |
$(td).hover( |
550 |
function() { $("tr", ul).removeClass("row2"); $(this).addClass("row2"); active = $("tr", ul).indexOf($(this).get(0)); }, |
|
551 |
function() { $(this).removeClass("row2"); } |
|
552 |
).click(function(e) { |
|
553 |
e.preventDefault(); |
|
554 |
e.stopPropagation(); |
|
555 |
selectItem(this) |
|
556 |
}); |
|
557 |
} |
|
558 |
||
559 |
$(ul).mousedown(function() { |
|
560 |
mouseDownOnSelect = true; |
|
561 |
}).mouseup(function() { |
|
562 |
mouseDownOnSelect = false; |
|
563 |
}); |
|
564 |
return ul; |
|
565 |
}; |
|
566 |
||
567 |
function requestData(q) { |
|
568 |
if (!options.matchCase) q = q.toLowerCase(); |
|
569 |
var data = options.cacheLength ? loadFromCache(q) : null; |
|
570 |
// recieve the cached data |
|
571 |
if (data) { |
|
572 |
receiveData(q, data); |
|
573 |
// if an AJAX url has been supplied, try loading the data now |
|
574 |
} else if( (typeof options.url == "string") && (options.url.length > 0) ){ |
|
575 |
$.get(makeUrl(q), function(data) { |
|
576 |
data = parseData(data); |
|
577 |
addToCache(q, data); |
|
578 |
receiveData(q, data); |
|
579 |
}); |
|
580 |
// if there's been no data found, remove the loading class |
|
581 |
} else { |
|
582 |
$input.removeClass(options.loadingClass); |
|
583 |
} |
|
584 |
}; |
|
585 |
||
586 |
function makeUrl(q) { |
|
587 |
var sep = options.url.indexOf('?') == -1 ? '?' : '&'; |
|
588 |
var url = options.url + encodeURI(q); |
|
589 |
for (var i in options.extraParams) { |
|
590 |
url += "&" + i + "=" + encodeURI(options.extraParams[i]); |
|
591 |
} |
|
592 |
return url; |
|
593 |
}; |
|
594 |
||
595 |
function loadFromCache(q) { |
|
596 |
if (!q) return null; |
|
597 |
if (cache.data[q]) return cache.data[q]; |
|
598 |
if (options.matchSubset) { |
|
599 |
for (var i = q.length - 1; i >= options.minChars; i--) { |
|
600 |
var qs = q.substr(0, i); |
|
601 |
var c = cache.data[qs]; |
|
602 |
if (c) { |
|
603 |
var csub = []; |
|
604 |
for (var j = 0; j < c.length; j++) { |
|
605 |
var x = c[j]; |
|
606 |
var x0 = x[0]; |
|
607 |
if (matchSubset(x0, q)) { |
|
608 |
csub[csub.length] = x; |
|
609 |
} |
|
610 |
} |
|
611 |
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
|
612 |
} |
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
|
613 |
} |
787 | 614 |
} |
615 |
return null; |
|
616 |
}; |
|
617 |
||
618 |
function matchSubset(s, sub) { |
|
619 |
if (!options.matchCase) s = s.toLowerCase(); |
|
620 |
var i = s.indexOf(sub); |
|
621 |
if (i == -1) return false; |
|
622 |
return i == 0 || options.matchContains; |
|
623 |
}; |
|
624 |
||
625 |
this.flushCache = function() { |
|
626 |
flushCache(); |
|
627 |
}; |
|
628 |
||
629 |
this.setExtraParams = function(p) { |
|
630 |
options.extraParams = p; |
|
631 |
}; |
|
632 |
||
633 |
this.findValue = function(){ |
|
634 |
var q = $input.val(); |
|
635 |
||
636 |
if (!options.matchCase) q = q.toLowerCase(); |
|
637 |
var data = options.cacheLength ? loadFromCache(q) : null; |
|
638 |
if (data) { |
|
639 |
findValueCallback(q, data); |
|
640 |
} else if( (typeof options.url == "string") && (options.url.length > 0) ){ |
|
641 |
$.get(makeUrl(q), function(data) { |
|
642 |
data = parseData(data) |
|
643 |
addToCache(q, data); |
|
644 |
findValueCallback(q, data); |
|
645 |
}); |
|
646 |
} else { |
|
647 |
// no matches |
|
648 |
findValueCallback(q, null); |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
function findValueCallback(q, data){ |
|
653 |
if (data) $input.removeClass(options.loadingClass); |
|
654 |
||
655 |
var num = (data) ? data.length : 0; |
|
656 |
var li = null; |
|
657 |
||
658 |
for (var i=0; i < num; i++) { |
|
659 |
var row = data[i]; |
|
660 |
||
661 |
if( row[0].toLowerCase() == q.toLowerCase() ){ |
|
662 |
li = document.createElement("li"); |
|
663 |
if (options.formatItem) { |
|
664 |
li.innerHTML = options.formatItem(row, i, num); |
|
665 |
li.selectValue = row[0]; |
|
666 |
} else { |
|
667 |
li.innerHTML = row[0]; |
|
668 |
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
|
669 |
} |
787 | 670 |
var extra = null; |
671 |
if( row.length > 1 ){ |
|
672 |
extra = []; |
|
673 |
for (var j=1; j < row.length; j++) { |
|
674 |
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
|
675 |
} |
dd80cde96a6c
Autocomplete further stabilized. Made Special:PasswordReset and Special:Register prevent use if logged in.
Dan
parents:
699
diff
changeset
|
676 |
} |
787 | 677 |
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
|
678 |
} |
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
|
679 |
} |
787 | 680 |
|
681 |
if( options.onFindValue ) setTimeout(function() { options.onFindValue(li) }, 1); |
|
682 |
} |
|
683 |
||
684 |
function addToCache(q, data) { |
|
685 |
if (!data || !q || !options.cacheLength) return; |
|
686 |
if (!cache.length || cache.length > options.cacheLength) { |
|
687 |
flushCache(); |
|
688 |
cache.length++; |
|
689 |
} else if (!cache[q]) { |
|
690 |
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
|
691 |
} |
787 | 692 |
cache.data[q] = data; |
693 |
}; |
|
694 |
||
695 |
function findPos(obj) { |
|
696 |
var curleft = obj.offsetLeft || 0; |
|
697 |
var curtop = obj.offsetTop || 0; |
|
698 |
while (obj = obj.offsetParent) { |
|
699 |
curleft += obj.offsetLeft |
|
700 |
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
|
701 |
} |
787 | 702 |
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
|
703 |
} |
787 | 704 |
} |
705 |
||
706 |
jQuery.fn.autocomplete = function(url, options, data) { |
|
707 |
// Make sure options exists |
|
708 |
options = options || {}; |
|
709 |
// Set url as option |
|
710 |
options.url = url; |
|
711 |
// set some bulk local data |
|
712 |
options.data = ((typeof data == "object") && (data.constructor == Array)) ? data : null; |
|
713 |
||
714 |
// Set default values for required options |
|
715 |
options = $.extend({ |
|
716 |
inputClass: "ac_input", |
|
717 |
resultsClass: "ac_results", |
|
718 |
lineSeparator: "\n", |
|
719 |
cellSeparator: "|", |
|
720 |
minChars: 1, |
|
721 |
delay: 400, |
|
722 |
matchCase: 0, |
|
723 |
matchSubset: 1, |
|
724 |
matchContains: 0, |
|
725 |
cacheLength: false, |
|
726 |
mustMatch: 0, |
|
727 |
extraParams: {}, |
|
728 |
loadingClass: "ac_loading", |
|
729 |
selectFirst: false, |
|
730 |
selectOnly: false, |
|
731 |
maxItemsToShow: -1, |
|
732 |
autoFill: false, |
|
733 |
showWhenNoResults: false, |
|
734 |
width: 0 |
|
735 |
}, options); |
|
736 |
options.width = parseInt(options.width, 10); |
|
737 |
||
738 |
this.each(function() { |
|
739 |
var input = this; |
|
740 |
new jQuery.autocomplete(input, options); |
|
741 |
}); |
|
742 |
||
743 |
// Don't break the chain |
|
744 |
return this; |
|
745 |
} |
|
746 |
||
747 |
jQuery.fn.autocompleteArray = function(data, options) { |
|
748 |
return this.autocomplete(null, options, data); |
|
749 |
} |
|
750 |
||
751 |
jQuery.fn.indexOf = function(e){ |
|
752 |
for( var i=0; i<this.length; i++ ){ |
|
753 |
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
|
754 |
} |
787 | 755 |
return -1; |
756 |
}; |
|
757 |
||
758 |
autofill_onload(); |
|
759 |
}; |
|
760 |
||
761 |
addOnloadHook(autofill_init); |