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