jsonp
[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             console.log("dataType: " + this.options.dataType);
15             var url = this.element.data('source');
16             var opts = { 
17                 minLength: this.options.minLength,
18                 select: __bind(this, this.enter),
19                 focus: function() { return false; },
20                 source: function(req, cb) {
21                     $.ajax({url: url,
22                             dataType: "jsonp",
23                             type: "GET",
24                             success: function(data) {
25                                 cb(data);
26                             },
27                             error: function() { cb([]); }
28
29                 });
30                 },
31             };
32
33             this.element.autocomplete($.extend(opts, this.options))
34                 .data("autocomplete")._renderItem = __bind(this, this.render_item);
35         },
36
37         enter: function(event, ui) {
38             if (ui.item.url != undefined) {
39                 location.href = ui.item.url;
40             } else {
41                 this.element.closest('form').submit();
42             }
43         },
44    
45         render_item: function (ul, item) {
46             return $("<li></li>").data('item.autocomplete', item)
47                 .append('<a href="'+item.url+'"><span class="search-hint-label">'+item.label+'</span>'+
48                         '<span class="search-hint-category mono">'+item.category+'</span></a>')
49                 .appendTo(ul);
50         }, 
51
52         destroy: function() {
53
54         },
55         
56
57     });
58
59
60 })(jQuery);