7f2a69856999d05f46c5489f66089d385e6308ac
[wolnelektury.git] / src / wolnelektury / static / js / search.js
1
2 var __bind = function (self, fn) {
3     return function() { return 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             this.element.autocomplete('instance')._renderItem = __bind(this, this.render_item_2022);
24             this.element.data('autocomplete')._renderItem = __bind(this, this.render_item);;
25         },
26
27         enter: function(event, ui) {
28             if (ui.item.url !== undefined) {
29             location.href = this.options.host+ui.item.url;
30             } else {
31             this.element.closest('form').submit();
32             }
33         },
34
35         render_item: function (ul, item) {
36             var label;
37             if (item['author']) {
38                 label = '<cite>' + item.label + '</cite>, ' + item['author'];
39             } else {
40                 label = item.label;
41             }
42             return $("<li></li>").data('item.autocomplete', item)
43             .append('<a href="'+this.options.host+item.url+'"><span class="search-hint-label">'+label+'</span>')
44             .appendTo(ul);
45         },
46
47         render_item_2022: function (ul, item) {
48             var label;
49             if (item['author']) {
50                 label = '<cite>' + item.label + '</cite>, ' + item['author'];
51             } else {
52                 label = item.label;
53             }
54             return $("<li></li>")
55             .append('<a href="'+this.options.host+item.url+'">'+label+'</a>')
56             .appendTo(ul);
57         },
58
59         destroy: function() {
60         }
61     });
62
63 })(jQuery);