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