ee4cc51b53ddbc834ff620b29eb087cfce4b3b4f
[wolnelektury.git] / wolnelektury / 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         },
11
12         _create: function() {
13             var opts = { 
14                 minLength: this.options.minLength,
15                 select: __bind(this, this.enter),
16                 focus: function() { return false; },
17                 source: this.element.data('source'),
18             };
19             this.element.autocomplete(opts).data("autocomplete")._renderItem = __bind(this, this.render_item);
20         },
21
22         enter: function(event, ui) {
23             if (ui.item.url != undefined) {
24                 location.href = ui.item.url;
25             } else {
26                 this.element.closest('form').submit();
27             }
28         },
29    
30         render_item: function (ul, item) {
31             return $("<li></li>").data('item.autocomplete', item)
32                 .append('<a href="'+item.url+'">'+item.label+ ' ('+item.category+')</a>')
33                 .appendTo(ul);
34         },
35
36         destroy: function() {
37
38         },
39         
40
41     });
42
43
44 })(jQuery);