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