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