2 * TestbedsPlugin: List of testbeds plugin
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
13 // XXX Inherit from an AngularPlugin class ?
16 // Define our Controller constructor.
17 function Controller($scope) {
21 $scope.instance = null;
23 $scope.facility_names = Array();
24 $scope.testbed_names = new Object();
27 //$scope.testbeds = Array();
28 $scope._facility_active = new Object();
29 $scope._testbed_active = new Object();
31 $scope.is_facility_active = function(facility)
33 return (($scope._facility_active[facility] === undefined) || $scope._facility_active[facility]);
36 $scope.is_testbed_active = function(facility, testbed)
38 return (($scope._testbed_active[facility] === undefined) ||
39 ($scope._testbed_active[facility][testbed] === undefined) ||
40 ($scope._testbed_active[facility][testbed]));
43 $scope.set_facility_active = function(facility, value)
45 $scope._facility_active[facility] = value;
48 $scope.set_testbed_active = function(facility, testbed, value)
50 if ($scope._testbed_active[facility] === undefined)
51 $scope._testbed_active[facility] = new Object();
52 $scope._testbed_active[facility][testbed] = value;
57 $scope.select_facility = function(facility)
59 var selected, prev_selected, num, num_selected, num_prev_selected, filter;
61 // prev_selected = $.map($scope.facility_names, function(x, i) {
62 // return $scope.is_facility_active(x) ? x : null;
65 $scope.set_facility_active(facility, ! $scope.is_facility_active(facility));
67 $.each($scope.testbed_names[facility], function(j, testbed_name) {
68 $scope.select_testbed(facility, testbed_name);
71 // selected = $.map($scope.facility_names, function(x, i) {
72 // return $scope.is_facility_active(x) ? x : null;
75 // num = $scope.facility_names.length;
76 // prev_num_selected = prev_selected.length;
77 // num_selected = selected.length;
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);
85 // if (num_selected != num) {
86 // filter = ['facility_name', 'included', selected];
87 // manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
91 $scope.select_testbed = function(facility, testbed)
93 var selected, prev_selected, num, num_selected, num_prev_selected, filter;
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);
106 $scope.set_testbed_active(facility, testbed, ! $scope.is_testbed_active(facility, testbed));
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);
120 $.each($scope.facility_names, function(i, facility_name) {
121 num += $scope.testbed_names[facility_name].length;
123 prev_num_selected = prev_selected.length;
124 num_selected = selected.length;
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);
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);
140 /* Return object reference */
144 // Define the Controller as the constructor function.
145 app.controller("TestbedsCtrl", Controller);
147 })(angular, ManifoldApp);
150 var TestbedsPlugin = Plugin.extend({
153 * @brief Plugin constructor
154 * @param options : an associative array of setting values
156 * @return : a jQuery collection of objects on which the plugin is
157 * applied, which allows to maintain chainability of calls
159 init: function(options, element)
161 // Call the parent constructor, see FAQ when forgotten
162 this._super(options, element);
164 /* Member variables */
165 this.testbeds = Array();
167 this._get_scope().instance = this;
170 this.listen_query(options.query_uuid);
171 this.listen_query(options.query_networks_uuid, 'networks');
177 /* When a filter is added/removed, update the list of filters local to the plugin */
179 on_filter_added: function(filter)
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");
187 }else if(filter[1]=='=' || filter[1]=='=='){
188 $("#testbeds-filter_"+filter[2]).addClass("active");
191 // XXX How to display unsupported filters
192 // XXX Constants for operators
195 on_filter_removed: function(filter)
197 this.filters = $.grep(this.filters, function(x) {
200 if(filter[0]=='network_hrn'){
201 if(filter[1]=='included'){
202 $.each(filter[2], function(value){
203 $("#testbeds-filter_"+value).removeClass("active");
205 }else if(filter[1]=='=' || filter[1]=='=='){
206 $("#testbeds-filter_"+filter[2]).removeClass("active");
211 // ... be sure to list all events here
213 on_query_done: function()
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();
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);
233 on_networks_query_done: function()
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 });
243 /* INTERNAL FUNCTIONS */
245 _get_scope : function()
247 return angular.element('[ng-controller=TestbedsCtrl]').scope();
251 _addFilter: function(key, op, value)
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";
259 if(network_filter.length > 0){
260 $.each(network_filter, function(i,f){
264 // Add the new value to list of values, ex: wilab
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]);
271 _removeFilter: function(key, op, value)
273 console.log("remove "+value);
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";
281 if(network_filter.length > 0){
282 $.each(network_filter, function(i,f){
284 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
288 // remove the value from the list of values, ex: wilab
289 values = $.grep(values, function(x) {
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]);
300 /* Plugin registration */
301 $.plugin('TestbedsPlugin', TestbedsPlugin);
303 // TODO Here use cases for instanciating plugins in different ways like in the pastie.