Update to new librarian api for html, txt.
[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             if (this.element.autocomplete('instance') !== undefined) this.element.autocomplete('instance')._renderItem = __bind(this, this.render_item_2022);
24             if (this.element.data('autocomplete') !== undefined) 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             var $label = $("<li><a><div></div><span></span></a></li>");
50             if (item.img) {
51                 $('div', $label).append($('<img>').attr('src', item.img));
52             }
53             if (item.author) {
54                 label = '<cite>' + item.label + '</cite>, ' + item['author'];
55             } else {
56                 label = item.label;
57             }
58             $('span', $label).html(label);
59             $label.addClass('type-' + item.type);
60             $label.appendTo(ul);
61             return $label;
62         },
63
64         destroy: function() {
65         }
66     });
67
68 })(jQuery);