autocomplete doesnt support jsonp, handler added
[wolnelektury.git] / apps / wolnelektury_core / static / js / search.js
1
2 var __bind = function (self, fn) {
3     return function() { fn.apply(self, arguments); };
4 };
5
6 (function($){
7     $.widget("wl.search", {
8         options: {
9           minLength: 0,
10           dataType: "json",
11         },
12
13         _create: function() {
14             var opts = { 
15                 minLength: this.options.minLength,
16                 select: __bind(this, this.enter),
17                 focus: function() { return false; },
18                 source: this.element.data('source'),
19             };
20
21             this.element.autocomplete($.extend(opts, this.options))
22                 .data("autocomplete")._renderItem = __bind(this, this.render_item);
23         },
24
25         enter: function(event, ui) {
26             if (ui.item.url != undefined) {
27                 location.href = ui.item.url;
28             } else {
29                 this.element.closest('form').submit();
30             }
31         },
32    
33         render_item: function (ul, item) {
34             return $("<li></li>").data('item.autocomplete', item)
35                 .append('<a href="'+item.url+'"><span class="search-hint-label">'+item.label+'</span>'+
36                         '<span class="search-hint-category mono">'+item.category+'</span></a>')
37                 .appendTo(ul);
38         }, 
39
40         destroy: function() {
41
42         },
43         
44
45     });
46
47
48 })(jQuery);