-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjquery.indextank.sorting.js
82 lines (65 loc) · 2.76 KB
/
jquery.indextank.sorting.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
(function($){
if(!$.Indextank){
$.Indextank = new Object();
};
$.Indextank.Sorting = 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.Sorting", base);
base.init = function(){
base.options = $.extend({},$.Indextank.Sorting.defaultOptions, options);
// react to result sets ..
base.$el.bind( "Indextank.AjaxSearch.success", base.trackQuery);
// create the control
var control = $("<div/>").append("Sort by ");
$.each( base.options.labels, function (name, fn){
var btn = $("<span/>").text(name);
btn.click( function(event){
control.children().removeClass("selected");
$(this).addClass("selected");
if (base.query) {
query = base.query.clone().withScoringFunction(fn);
// remember to start from scratch
query.withStart(0);
base.searcher.trigger("Indextank.AjaxSearch.runQuery", [query]);
}
});
control.append(btn);
});
// add separator
control.children(":not(:first)").prepend(" ", base.options.separator, " ");
// and make it appear
base.$el.append(control);
};
// tracks the latest query, so sorting buttons can run it again, changing the scoring function
base.trackQuery = function(event, data) {
// keep a copy of the query, so we can re-run it.
base.query = data.query;
// keep a pointer to the event trigger .. HACKY way
base.searcher = data.searcher;
};
// clean up
base.destroy = function(){
base.$el.unbind( "Indextank.AjaxSearch.success", base.trackQuery);
base.$el.removeData("Indextank.Sorting");
};
// Run initializer
base.init();
};
$.Indextank.Sorting.defaultOptions = {
// you should REALLY provide it, as the default is useless
labels: { 'newest' : 0 },
// the separator for possible sorting names. just for formatting
separator : "|"
};
$.fn.indextank_Sorting = function(options){
return this.each(function(){
(new $.Indextank.Sorting(this, options));
});
};
})(jQuery);