forked from flaptor/indextank-jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.indextank.instantlinks.js
115 lines (93 loc) · 4.17 KB
/
jquery.indextank.instantlinks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(function($){
if(!$.Indextank){
$.Indextank = new Object();
};
$.Indextank.InstantLinks = function(el, options){
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("Indextank.InstantLinks", base);
base.options = $.extend({},$.Indextank.InstantLinks.defaultOptions, options);
base.init = function(){
// Put your initialization code here
var ize = $(base.el.form).data("Indextank.Ize");
base.$el.autocomplete({
select: function( event, ui ) {
window.location.href = ui.item[base.options.url];
},
source: function ( request, responseCallback ) {
$.ajax( {
url: ize.apiurl + "/v1/indexes/" + ize.indexName + "/instantlinks",
dataType: "jsonp",
data: { query: request.term, field: base.options.name, fetch: base.options.fields },
success: function( data ) {
// augment results, so that they contain the matched query
var results = $.map(data.results, function(r) {
r.queryTerm = request.term;
return r;
});
responseCallback(results);
}
} );
},
minLength: base.options.minLength,
delay: base.options.delay
})
.data( "autocomplete" )._renderItem = function( ul, item) {
// create the list entry
var $li = $("<li/>").addClass("result").data("item", item);
// append a formatted item.
$li.append(base.options.format(item, base.options));
// put the li back on the ul
return $li.appendTo( ul );
};
};
// Run initializer
base.init();
};
$.Indextank.InstantLinks.defaultOptions = {
name: "name",
url: "url",
thumbnail: "thumbnail",
description: "description",
fields: "name,url,thumbnail,description",
minLength: 2,
delay: 100,
format: function( item , options ) {
function hl(text, query){
rx = new RegExp(query,'ig');
bolds = $.map(text.match(rx) || [], function(i) { return "<span class='highlighted'>"+i+"</span>";});
regulars = $( $.map(text.split(rx), function(i){ return $("<span></span>").addClass("regular").text(i).get(0);}));
return $.each(regulars, function(i, v) {
$(v).append(bolds[i] || "");
});
};
var name = item[options.name];
var highlightedName = hl(name, item.queryTerm);
var l = $("<a></a>").attr("href", item[options.url]);
// only display images for those documents that have one
if (item[options.thumbnail]) {
l.addClass("with-thumbnail");
l.append( $("<img />")
.attr("src", item[options.thumbnail])
.css( { "max-width": "50px", "max-height": "50px"} ) ) ;
}
l.append( $("<span/>").addClass("name").append(highlightedName) );
// only add description for those documents that have one
if (item[options.description]) {
l.addClass("with-description");
l.append( $("<span/>").addClass("description").text(item[options.description]));
}
return l;
}
};
$.fn.indextank_InstantLinks = function(options){
return this.each(function(){
(new $.Indextank.InstantLinks(this, options));
});
};
})(jQuery);