PageSourceSearch

https://chelmsfordclashdiary.org.uk/static/js/FilterManager.js

js chelmsfordclashdiary.org.uk collected 2026-09-27 15:25:52 UTC 2,997 bytes, 87 lines download raw bytes

1/****
2 * Filter manager for paged lists.
3 * 
4 * Handles persistence of list filters and parsing the filters compontent of the URI
5 * Also responsible for making the filter dropdowns work.
6 ****/
7
8FilterManager = function(instance_name, container){
9
10    var FM = this;
11
12    if(instance_name === undefined){ // Not specified
13        instance_name = "";
14    }else{
15        instance_name = instance_name + "_";
16    }
17    // Prevent dropdowns vanishing when clicking on elements within them.
18    $(container).find(".filter-editor").on("click", function(){ return false; });
19    $(container).find(".filter-editor").find("input").on("keypress", function(e){ 
20        if (e.keyCode == 13) {
21            return false;
22        }
23    });
24
25    // Handle Add filter buttons
26    $(container).find("*[data-add-filter]").on("click", function(){
27        attr = $(this).attr("data-add-filter");
28        FM.add_filter(attr, $("#newfilter_" + attr + "_comp").val(), $("#newfilter_" + attr + "_value").val());
29        FM.reload_with_filters();
30        return false;
31    });
32
33    $(container).find("*.new-filter-value").on("keyup", function(e){
34        var code = (e.keyCode ? e.keyCode : e.which);
35        if (code==13) {
36            e.preventDefault();
37            attr = $(this).attr("data-filter-attr");
38            FM.add_filter(attr, $("#newfilter_" + attr + "_comp").val(), $("#newfilter_" + attr + "_value").val());
39            FM.reload_with_filters();
40            return false;
41        }
42    });
43
44    $(container).find("*[data-remove-filter]").on("click", function(){
45        index = $(this).attr("data-remove-filter");
46        FM.remove_filter(index);
47        FM.reload_with_filters();
48        return false;
49    });
50
51    this.filters = new Array();
52    this._instance_name = instance_name;
53    this._original_uri = new URI(document.location.href);
54
55    var attrs = this._original_uri.query(true);
56    if(attrs[instance_name + "filters"] !== undefined){
57        var filters = JSON.parse(attrs[instance_name + "filters"]);
58        for(var i = 0; i < filters.length; i++){
59            if(filters[i] == "") continue;
60            var filter = filters[i];
61            this.add_filter(filter[0], filter[1], filter[2] );
62        }
63    }
64}
65
66FilterManager.prototype.add_filter = function(field, op, value){
67    this.filters.push( [ field, op, value ] )
68}
69
70FilterManager.prototype.remove_filter = function(index){
71    this.filters.splice( index, 1 )
72}
73
74FilterManager.prototype.reload_with_filters = function(){
75    this._original_uri = new URI(document.location.href);
76    this._original_uri.setSearch(this._instance_name + "filters", JSON.stringify(this.filters));
77    document.location.href = this._original_uri.toString();
78}
79
80FilterManager.create = function(selector, context){
81    var obj = $(selector);
82    if(context === undefined)
83        context = obj.attr("data-filter-context")
84    FilterManager._registrations.push( new FilterManager( context, $(selector) ) );
85}
86
87FilterManager._registrations = new Array();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.