Mercurial > public > sg101
annotate core/widgets.py @ 628:c6292e46e617
For local testing, set wiki cookie domain to None.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 12 Nov 2012 16:40:54 -0600 |
parents | 678a1a2ef55a |
children |
rev | line source |
---|---|
gremmie@1 | 1 """ |
gremmie@1 | 2 Various useful widgets for the GPP application. |
gremmie@1 | 3 """ |
gremmie@1 | 4 |
gremmie@1 | 5 from django import forms |
gremmie@1 | 6 from django.utils.safestring import mark_safe |
gremmie@1 | 7 from django.core.urlresolvers import reverse |
gremmie@1 | 8 |
gremmie@1 | 9 |
gremmie@1 | 10 class AutoCompleteUserInput(forms.TextInput): |
gremmie@1 | 11 |
gremmie@1 | 12 def render(self, name, value, attrs=None): |
bgneal@149 | 13 url = reverse('core-ajax_users') |
gremmie@1 | 14 output = super(AutoCompleteUserInput, self).render(name, value, attrs) |
bgneal@186 | 15 return output + mark_safe(u"""\ |
gremmie@1 | 16 <script type="text/javascript"> |
bgneal@186 | 17 $(function() { |
bgneal@186 | 18 var cache = {}; |
bgneal@186 | 19 var cacheSize = 0; |
bgneal@186 | 20 $("#id_%s").autocomplete({ |
bgneal@186 | 21 delay: 400, |
bgneal@326 | 22 minLength: 1, |
bgneal@186 | 23 source: function(request, response) { |
bgneal@186 | 24 if (cache[request.term]) { |
bgneal@186 | 25 response(cache[request.term]); |
bgneal@186 | 26 return; |
bgneal@186 | 27 } |
bgneal@186 | 28 $.ajax({ |
bgneal@186 | 29 url: "%s", |
bgneal@186 | 30 type: "GET", |
bgneal@186 | 31 data: { |
bgneal@186 | 32 q: request.term, |
bgneal@326 | 33 limit: 15 |
bgneal@186 | 34 }, |
bgneal@186 | 35 dataType: "json", |
bgneal@186 | 36 success: function(data, textStatus) { |
bgneal@326 | 37 if (cacheSize >= 32) { |
bgneal@186 | 38 cache = {}; |
bgneal@186 | 39 cacheSize = 0; |
bgneal@186 | 40 } |
bgneal@186 | 41 cache[request.term] = data; |
bgneal@186 | 42 ++cacheSize; |
bgneal@186 | 43 response(data); |
bgneal@186 | 44 }, |
bgneal@186 | 45 error: function(xhr, textStatus, ex) { |
bgneal@186 | 46 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + |
bgneal@186 | 47 xhr.responseText); |
bgneal@186 | 48 } |
bgneal@186 | 49 }); |
bgneal@186 | 50 } |
bgneal@186 | 51 }); |
gremmie@1 | 52 }); |
bgneal@186 | 53 </script>""" % (name, url)) |
gremmie@1 | 54 |