global helper functions need to have grep-able names, i.e. a little bit longer
[myslice.git] / plugins / active_filters / static / js / active_filters.js
1 /**
2  * Description: ActiveFilters plugin
3  * Copyright (c) 2012-2013 UPMC Sorbonne Universite
4  * License: GPLv3
5  */
6
7 // NOTE: We are not making use of element, but this.el() instead...
8
9 (function($){
10
11     var ActiveFilters = Plugin.extend({
12
13         init: function(options, element) {
14             this._super(options, element);
15
16             this.elts('closeButton').click(function() {
17                 manifold.raise_event(options.query_uuid, FILTER_REMOVED, filter);
18             });
19
20             this.el('clearFilters').click(function () {
21                 manifold.raise_event(options.query_uuid, CLEAR_FILTERS);
22             });
23             this.check_and_hide_clear_button();
24
25             this.listen_query(options.query_uuid);
26
27         },
28
29         // This should be provided in the API
30         // make_id_from_filter, etc
31         getOperatorLabel: function(op)
32         {
33             if (op == "=" || op == "==") {
34                 return 'eq';
35             } else if (op == "!=") {
36                 return "ne";
37             } else if (op == ">") {
38                 return "gt";
39             } else if (op == ">=") {
40                 return "ge";
41             } else if (op == "<") {
42                 return "lt";
43             } else if (op == "<=") {
44                 return "le";
45             } else {
46                 return false;
47             }
48         },
49
50         show_clear_button: function()
51         {
52             this.el('clearFilters').show();
53         },
54
55         hide_clear_button: function()
56         {
57             this.el('clearFilters').hide();
58         },
59
60         check_and_hide_clear_button: function()
61         {
62             // Count the number of filter _inside_ the current plugin
63             var count = this.elts('filterButton').length;
64             if (count == 1) { // Including the template
65                 this.el('clearFilters').hide();
66             }
67         },
68
69         clear_filters: function() 
70         {
71             // XXX We need to remove all filter but template
72             this.hide_clear_button();
73         },
74
75         add_filter: function(filter)
76         {
77             var template = this.el('template').html();
78             var ctx = {
79                 id:   this.id(this.id_from_filter(filter, false)),
80                 span: this.str_from_filter(filter)
81             };
82             var output = Mustache.render(template, ctx);
83
84             this.el('myActiveFilters').append(output);
85
86             // Add an event on click on the close button, call function removeFilter
87             var self = this;
88             this.elts('closeButton').last().click(function() {
89                 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, filter);
90             });
91
92             this.show_clear_button();
93         },
94         
95         remove_filter: function(filter)
96         {
97             this.el(this.id_from_filter(filter, false)).remove();
98             this.check_and_hide_clear_button();
99         },
100
101         /* Events */
102
103         on_filter_added: function(filter) {
104             this.add_filter(filter);
105         },
106
107         on_filter_removed: function(filter) {
108             this.remove_filter(filter);
109         },
110
111         on_filter_clear: function(filter) {
112             this.clear_filters();
113         },
114     });
115
116     $.plugin('ActiveFilters', ActiveFilters);
117
118 })(jQuery);