-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjquery.indextank.statsrenderer.js
61 lines (47 loc) · 2 KB
/
jquery.indextank.statsrenderer.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
(function($){
if(!$.Indextank){
$.Indextank = new Object();
};
$.Indextank.StatsRenderer = 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.StatsRenderer", base);
base.init = function(){
base.options = $.extend({},$.Indextank.StatsRenderer.defaultOptions, options);
base.$el.bind( "Indextank.AjaxSearch.success", function (event, data) {
base.$el.show();
base.$el.html("");
var stats = base.options.format(data);
stats.appendTo(base.$el);
});
base.$el.bind( "Indextank.AjaxSearch.noResults", function (event, query) {
base.$el.show();
base.$el.html("");
});
};
// Run initializer
base.init();
};
$.Indextank.StatsRenderer.defaultOptions = {
format: function (data) {
var r = $("<div></div>")
.append( $("<strong></strong>").text(data.matches) )
.append( $("<span></span>").text(" " + (data.matches == 1 ? "result":"results" )+ " for ") )
.append( $("<strong></strong>").text(data.query.queryString) )
.append( $("<span></span>").text(" in ") )
.append( $("<strong></strong>").text(data.search_time) )
.append( $("<span></span>").text(" seconds.") );
return r;
}
};
$.fn.indextank_StatsRenderer = function(options){
return this.each(function(){
(new $.Indextank.StatsRenderer(this, options));
});
};
})(jQuery);