comparison gpp/core/widgets.py @ 186:be3fff614b93

Implement #66; use jQuery UI autocomplete widget to replace obsolete jquery-autocomplete plugin. I implemented a very simple caching system.
author Brian Neal <bgneal@gmail.com>
date Tue, 30 Mar 2010 01:30:32 +0000
parents ab7830b067b3
children 5453aedf95fd
comparison
equal deleted inserted replaced
185:afb65fa947f1 186:be3fff614b93
7 from django.core.urlresolvers import reverse 7 from django.core.urlresolvers import reverse
8 from django.conf import settings 8 from django.conf import settings
9 9
10 10
11 class AutoCompleteUserInput(forms.TextInput): 11 class AutoCompleteUserInput(forms.TextInput):
12 class Media:
13 css = {
14 'all': settings.GPP_THIRD_PARTY_CSS['jquery-autocomplete'],
15 }
16 js = settings.GPP_THIRD_PARTY_JS['jquery-autocomplete']
17 12
18 def render(self, name, value, attrs=None): 13 def render(self, name, value, attrs=None):
19 url = reverse('core-ajax_users') 14 url = reverse('core-ajax_users')
20 output = super(AutoCompleteUserInput, self).render(name, value, attrs) 15 output = super(AutoCompleteUserInput, self).render(name, value, attrs)
21 return output + mark_safe(u'''\ 16 return output + mark_safe(u"""\
22 <script type="text/javascript"> 17 <script type="text/javascript">
23 jQuery("#id_%s").autocomplete("%s", { 18 $(function() {
24 width: 150, 19 var cache = {};
25 max: 10, 20 var cacheSize = 0;
26 highlight: false, 21 $("#id_%s").autocomplete({
27 multiple: false, 22 delay: 400,
28 scroll: true, 23 minLength: 2,
29 scrollHeight: 300, 24 source: function(request, response) {
30 matchContains: true, 25 if (cache[request.term]) {
31 autoFill: true 26 response(cache[request.term]);
27 return;
28 }
29 $.ajax({
30 url: "%s",
31 type: "GET",
32 data: {
33 q: request.term,
34 limit: 10
35 },
36 dataType: "json",
37 success: function(data, textStatus) {
38 if (cacheSize >= 16) {
39 cache = {};
40 cacheSize = 0;
41 }
42 cache[request.term] = data;
43 ++cacheSize;
44 response(data);
45 },
46 error: function(xhr, textStatus, ex) {
47 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
48 xhr.responseText);
49 }
50 });
51 }
52 });
32 }); 53 });
33 </script>''' % (name, url)) 54 </script>""" % (name, url))
34 55