Filter status: All, Available, Unavailable, Unconfigured, Pending, Reserved
[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             this.listen_query(options.query_lease_uuid, 'leases');
32
33             /* Setup click handlers */
34             this.elts('list-group-item').click({'instance': this}, this._on_click);
35
36             this.prev_filter_status = null;
37
38             /* Initialize tooltips */
39             $("[rel='tooltip']").tooltip();
40
41         },
42
43     /**************************************************************************
44      *                            GUI MANAGEMENT                              *
45      **************************************************************************/
46
47         select_tab: function(tab)
48         {
49             this.elts('list-group-item').removeClass('active');
50             this.elmt(tab).addClass('active');
51         },
52
53     /**************************************************************************
54      *                            EVENT HANDLERS                              *
55      **************************************************************************/
56
57     // These functions are here to react on external filters, which we don't
58     // use at the moment
59
60     on_filter_added: function(filter) 
61     {
62         // XXX
63     },
64
65     on_filter_removed: function(filter) 
66     {
67         // XXX
68     },
69
70     on_leases_field_state_changed: function(data) 
71     {
72         console.log('leases_field_state_changed');
73         this.on_field_state_changed(data);
74     },
75     on_field_state_changed: function(data) 
76     {
77         var query_ext;
78         
79         switch (data.state) {
80             case STATE_SET:
81             case STATE_WARNINGS:
82                 /* Get the number of pending / unconfigured resources */
83                 /* Let's store it in query_ext */
84                 query_ext = manifold.query_store.find_analyzed_query_ext(this.options.query_uuid);
85
86                 $('#badge-pending').text(query_ext.num_pending);
87                 if (query_ext.num_pending > 0) {
88                                         $('#badge-pending').show();
89                 } else {
90                                         $('#badge-pending').hide();
91                 }
92
93                 $('#badge-unconfigured').text(query_ext.num_unconfigured);
94                 if (query_ext.num_unconfigured > 0) {
95                                         $('#badge-unconfigured').show();
96                 } else {
97                                         $('#badge-unconfigured').hide();
98                 }
99             default:
100                 break;
101         }
102     },
103
104     /**************************************************************************
105      *                            PRIVATE METHODS                             *
106      **************************************************************************/
107
108         /**
109          * @brief : Click event handler
110          */
111         _on_click: function(e)
112         {
113             var filter_status;
114             var filter;
115
116             // A pointer to the plugin instance, since 'this' is overriden here
117             self = e.data.instance;
118
119
120             // Select the tab...
121             filter_status = this.dataset['status'];
122             self.select_tab(filter_status);
123
124             // ... and communicate the appropriate filters to the manager
125             // NOTE: we use the manifold namespace for internal filters 
126             if (self.prev_filter_status) {
127                 var filter = ['manifold:status', '==', self.prev_filter_status];
128                 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, filter);
129             }
130             // XXX The datatables will be refreshed twice !
131             //if (filter_status != 'all') {
132             // No filter for 'all'
133             // Changed since we have available/unavailable status
134             var filter = ['manifold:status', '==', filter_status];
135             manifold.raise_event(self.options.query_uuid, FILTER_ADDED, filter);
136             //}
137
138             self.prev_filter_status = filter_status;
139         },
140
141     });
142
143     /* Plugin registration */
144     $.plugin('FilterStatusPlugin', FilterStatusPlugin);
145
146 })(jQuery);