9c1c244b13dad9a2ddb22a58102cef72b928ba6a
[myslice.git] / plugins / filter_status / static / js / filter_status.js
1 /**
2  * TestbedsPlugin: List of testbeds plugin
3  * Version:     0.1
4  * Description: TODO -> generalize to a list of possible filters
5  *              This file is part of the Manifold project 
6  * Requires:    js/plugin.js
7  * URL:         http://www.myslice.info
8  * Author:      Loïc Baron <loic.baron@lip6.fr>
9  * Copyright:   Copyright 2012-2013 UPMC Sorbonne Universités
10  * License:     GPLv3
11  */
12
13 (function($){
14
15     var FilterStatusPlugin = Plugin.extend({
16
17         /** 
18          * @brief Plugin constructor
19          * @param options : an associative array of setting values
20          * @param element : 
21          * @return : a jQuery collection of objects on which the plugin is
22          *     applied, which allows to maintain chainability of calls
23          */
24         init: function(options, element)
25         {
26             // Call the parent constructor, see FAQ when forgotten
27             this._super(options, element);
28
29             /* Setup query and record handlers */
30             this.listen_query(options.query_uuid);
31
32             /* Setup click handlers */
33             this.elts('list-group-item').click({'instance': this}, this._on_click);
34
35             this.prev_filter_status = null;
36
37             /* Initialize tooltips */
38             $("[rel='tooltip']").tooltip();
39
40         },
41
42     /**************************************************************************
43      *                            GUI MANAGEMENT                              *
44      **************************************************************************/
45
46         select_tab: function(tab)
47         {
48             this.elts('list-group-item').removeClass('active');
49             this.elmt(tab).addClass('active');
50         },
51
52     /**************************************************************************
53      *                            EVENT HANDLERS                              *
54      **************************************************************************/
55
56     // These functions are here to react on external filters, which we don't
57     // use at the moment
58
59     on_filter_added: function(filter) 
60     {
61         // XXX
62     },
63
64     on_filter_removed: function(filter) 
65     {
66         // XXX
67     },
68
69     on_field_state_changed: function(data) 
70     {
71         var query_ext;
72         
73         switch (data.state) {
74             case STATE_SET:
75             case STATE_WARNINGS:
76                 /* Get the number of pending / unconfigured resources */
77                 /* Let's store it in query_ext */
78                 query_ext = manifold.query_store.find_analyzed_query_ext(this.options.query_uuid);
79
80                 $('#badge-pending').text(query_ext.num_pending);
81                 if (query_ext.num_pending > 0) {
82                                         $('#badge-pending').show();
83                 } else {
84                                         $('#badge-pending').hide();
85                 }
86
87                 $('#badge-unconfigured').text(query_ext.num_unconfigured);
88                 if (query_ext.num_unconfigured > 0) {
89                                         $('#badge-unconfigured').show();
90                 } else {
91                                         $('#badge-unconfigured').hide();
92                 }
93             default:
94                 break;
95         }
96     },
97
98     /**************************************************************************
99      *                            PRIVATE METHODS                             *
100      **************************************************************************/
101
102         /**
103          * @brief : Click event handler
104          */
105         _on_click: function(e)
106         {
107             var filter_status;
108             var filter;
109
110             // A pointer to the plugin instance, since 'this' is overriden here
111             self = e.data.instance;
112
113
114             // Select the tab...
115             filter_status = this.dataset['status'];
116             self.select_tab(filter_status);
117
118             // ... and communicate the appropriate filters to the manager
119             // NOTE: we use the manifold namespace for internal filters 
120             if (self.prev_filter_status) {
121                 var filter = ['manifold:status', '==', self.prev_filter_status];
122                 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, filter);
123             }
124             // XXX The datatables will be refreshed twice !
125             if (filter_status != 'all') {
126                 // No filter for 'all'
127                 var filter = ['manifold:status', '==', filter_status];
128                 manifold.raise_event(self.options.query_uuid, FILTER_ADDED, filter);
129             }
130
131             self.prev_filter_status = filter_status;
132         },
133
134     });
135
136     /* Plugin registration */
137     $.plugin('FilterStatusPlugin', FilterStatusPlugin);
138
139 })(jQuery);