Testbed/Facility selection
[myslice.git] / plugins / testbeds / static / js / testbeds.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 // XXX Inherit from an AngularPlugin class ?
14 (function (ng, app) {
15
16     // Define our Controller constructor.
17     function Controller($scope) {
18         /* Contructor */
19
20         /* Plugin instance */
21         $scope.instance = null;
22
23         $scope.facility_names = Array();
24         $scope.testbed_names = new Object();
25
26         /* Models */
27         //$scope.testbeds = Array();
28         $scope._facility_active = new Object();
29         $scope._testbed_active  = new Object();
30
31         $scope.is_facility_active = function(facility)
32         {
33             return (($scope._facility_active[facility] === undefined) || $scope._facility_active[facility]);
34         };
35
36         $scope.is_testbed_active = function(facility, testbed)
37         {
38             return (($scope._testbed_active[facility] === undefined) || 
39                     ($scope._testbed_active[facility][testbed] === undefined) || 
40                     ($scope._testbed_active[facility][testbed]));
41         };
42
43         $scope.set_facility_active = function(facility, value)
44         {
45             $scope._facility_active[facility] = value;
46         };
47
48         $scope.set_testbed_active = function(facility, testbed, value)
49         {
50             if ($scope._testbed_active[facility] === undefined)
51                 $scope._testbed_active[facility] = new Object();
52             $scope._testbed_active[facility][testbed] = value;
53         };
54     
55         /* Click event */
56
57         $scope.select_facility = function(facility)
58         {
59             var selected, prev_selected, num, num_selected, num_prev_selected, filter;
60
61             // prev_selected = $.map($scope.facility_names, function(x, i) {
62                 // return $scope.is_facility_active(x) ? x : null;
63             // });
64
65             $scope.set_facility_active(facility, ! $scope.is_facility_active(facility));
66             
67             $.each($scope.testbed_names[facility], function(j, testbed_name) {
68                 $scope.select_testbed(facility, testbed_name);
69             });
70             console.log($scope);
71             // selected = $.map($scope.facility_names, function(x, i) {
72                 // return $scope.is_facility_active(x) ? x : null;
73             // });
74
75             // num = $scope.facility_names.length;
76             // prev_num_selected = prev_selected.length;
77             // num_selected = selected.length;
78
79             // if ((prev_num_selected != 0) && (prev_num_selected != num)) {
80                 // // Remove previous filter
81                 // filter = ['facility_name', 'included', prev_selected];
82                 // manifold.raise_event($scope.instance.options.query_uuid, FILTER_REMOVED, filter);
83             // }
84 // 
85             // if (num_selected != num) {
86                 // filter = ['facility_name', 'included', selected];
87                 // manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
88             // }
89         };
90
91         $scope.select_testbed = function(facility, testbed)
92         {
93             var selected, prev_selected, num, num_selected, num_prev_selected, filter;
94
95             prev_selected = Array();
96             $.each($scope.facility_names, function(i, facility_name) {
97                 $.each($scope.testbed_names[facility_name], function(j, testbed_name) {
98                     if ($scope.is_testbed_active(facility_name, testbed_name)) {
99                         // XXX We should have a joint facility/testbed filter
100                         prev_selected.push(testbed_name);
101                     }
102                 });
103
104             });
105
106             $scope.set_testbed_active(facility, testbed, ! $scope.is_testbed_active(facility, testbed));
107
108             selected = Array();
109             $.each($scope.facility_names, function(i, facility_name) {
110                 $.each($scope.testbed_names[facility_name], function(j, testbed_name) {
111                     if ($scope.is_testbed_active(facility_name, testbed_name)) {
112                         // XXX We should have a joint facility/testbed filter
113                         selected.push(testbed_name);
114                     }
115                 });
116
117             });
118
119             num = 0;
120             $.each($scope.facility_names, function(i, facility_name) {
121                 num += $scope.testbed_names[facility_name].length;
122             });
123             prev_num_selected = prev_selected.length;
124             num_selected = selected.length;
125
126             if ((prev_num_selected != 0) && (prev_num_selected != num)) {
127                 // Remove previous filter
128                 // XXX We should have a joint facility/testbed filter
129                 filter = ['testbed_name', 'included', prev_selected];
130                 manifold.raise_event($scope.instance.options.query_uuid, FILTER_REMOVED, filter);
131             }
132
133             if (num_selected != num) {
134                 // XXX We should have a joint facility/testbed filter
135                 filter = ['testbed_name', 'included', selected];
136                 manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
137             }
138         };
139
140         /* Return object reference */
141         return (this);
142     }
143
144     // Define the Controller as the constructor function.
145     app.controller("TestbedsCtrl", Controller);
146
147 })(angular, ManifoldApp);
148
149 (function($){
150     var TestbedsPlugin = Plugin.extend({
151
152         /** XXX to check
153          * @brief Plugin constructor
154          * @param options : an associative array of setting values
155          * @param element : 
156          * @return : a jQuery collection of objects on which the plugin is
157          *     applied, which allows to maintain chainability of calls
158          */
159         init: function(options, element) 
160         {
161             // Call the parent constructor, see FAQ when forgotten
162             this._super(options, element);
163
164             /* Member variables */
165             this.testbeds = Array();
166
167             this._get_scope().instance = this;
168
169             /* Handlers */
170             this.listen_query(options.query_uuid);
171             this.listen_query(options.query_networks_uuid, 'networks');
172         },
173
174
175         /* HANDLERS */
176
177         /* When a filter is added/removed, update the list of filters local to the plugin */
178         /*
179         on_filter_added: function(filter)
180         {
181             this.filters.push(filter);
182             if(filter[0]=='network_hrn'){
183                 if(filter[1]=='included'){
184                     $.each(filter[2], function(value){
185                         $("#testbeds-filter_"+value).addClass("active");
186                     });
187                 }else if(filter[1]=='=' || filter[1]=='=='){
188                     $("#testbeds-filter_"+filter[2]).addClass("active");
189                 }
190                 // XXX NAMING
191                 // XXX How to display unsupported filters
192                 // XXX Constants for operators
193             }
194         },
195         on_filter_removed: function(filter)
196         {
197             this.filters = $.grep(this.filters, function(x) {
198                 return x == filter;
199             });
200             if(filter[0]=='network_hrn'){
201                 if(filter[1]=='included'){
202                     $.each(filter[2], function(value){
203                         $("#testbeds-filter_"+value).removeClass("active");
204                     });
205                 }else if(filter[1]=='=' || filter[1]=='=='){
206                     $("#testbeds-filter_"+filter[2]).removeClass("active");
207                 }
208             }
209         },
210         */
211         // ... be sure to list all events here
212
213         on_query_done: function()
214         {
215             var scope, query_ext, resources;
216             scope = this._get_scope();
217             query_ext = manifold.query_store.find_analyzed_query_ext(this.options.query_uuid);
218             resources = query_ext.records.values();
219
220             $.each(resources, function(i, resource) {
221                 if ($.inArray(resource.facility_name, scope.facility_names) == -1)
222                     scope.facility_names.push(resource.facility_name);
223                 if (scope.testbed_names[resource.facility_name] === undefined)
224                     scope.testbed_names[resource.facility_name] = Array();
225                 if ($.inArray(resource.testbed_name, scope.testbed_names[resource.facility_name]) == -1)
226                     scope.testbed_names[resource.facility_name].push(resource.testbed_name);
227             });
228
229             scope.$apply();
230         },
231
232         /*
233         on_networks_query_done: function()
234         {
235             var scope = this._get_scope();
236             var query_ext = manifold.query_store.find_analyzed_query_ext(this.options.query_networks_uuid);
237             scope.testbeds = query_ext.records.values();
238             $.each(scope.testbeds, function(i, testbed) { testbed.active = true });
239             scope.$apply();
240         },
241 */
242
243         /* INTERNAL FUNCTIONS */
244
245         _get_scope : function()
246         {
247             return angular.element('[ng-controller=TestbedsCtrl]').scope();
248         },
249
250 /*
251         _addFilter: function(key, op, value)
252         {
253             values = Array();
254             // get the previous list of values for this key, ex: [ple,nitos]
255             // remove the previous filter
256             network_filter = $.grep(this.filters, function(x) {
257                 return x[0] == "network_hrn";
258             });
259             if(network_filter.length > 0){
260                 $.each(network_filter, function(i,f){
261                     values = f[2];
262                 });
263             }
264             // Add the new value to list of values, ex: wilab
265             values.push(value);
266             
267             // Update the filter with the new list of values, ex: [ple,nitos,wilab]
268             manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
269         },
270
271         _removeFilter: function(key, op, value)
272         {
273             console.log("remove "+value);
274             var self = this;
275             values = Array();
276             // get the previous list of values for this key, ex: [ple,nitos,wilab]
277             // remove the previous filter
278             network_filter = $.grep(this.filters, function(x) {
279                 return x[0] == "network_hrn";
280             });
281             if(network_filter.length > 0){
282                 $.each(network_filter, function(i,f){
283                     values = f[2];
284                     manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
285                 });
286             }
287
288             // remove the value from the list of values, ex: wilab
289             values = $.grep(values, function(x) {
290                 return x != value;
291             });
292             if(values.length>0){
293                 // Update the filter with the new list of values, ex: [ple,nitos]
294                 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
295             }
296         }
297 */
298     });
299
300     /* Plugin registration */
301     $.plugin('TestbedsPlugin', TestbedsPlugin);
302
303     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
304
305 })(jQuery);