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;
55 $scope.tolower = function(string) {
56 return string.toLowerCase(string);
61 $scope.select_facility = function(facility)
63 var selected, prev_selected, num, num_selected, num_prev_selected, filter;
65 // prev_selected = $.map($scope.facility_names, function(x, i) {
66 // return $scope.is_facility_active(x) ? x : null;
69 $scope.set_facility_active(facility, ! $scope.is_facility_active(facility));
71 $.each($scope.testbed_names[facility], function(j, testbed_name) {
72 $scope.select_testbed(facility, testbed_name);
75 // selected = $.map($scope.facility_names, function(x, i) {
76 // return $scope.is_facility_active(x) ? x : null;
79 // num = $scope.facility_names.length;
80 // prev_num_selected = prev_selected.length;
81 // num_selected = selected.length;
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);
89 // if (num_selected != num) {
90 // filter = ['facility_name', 'included', selected];
91 // manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
95 $scope.select_testbed = function(facility, testbed)
97 var selected, prev_selected, num, num_selected, num_prev_selected, filter;
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);
110 $scope.set_testbed_active(facility, testbed, ! $scope.is_testbed_active(facility, testbed));
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);
124 $.each($scope.facility_names, function(i, facility_name) {
125 num += $scope.testbed_names[facility_name].length;
127 prev_num_selected = prev_selected.length;
128 num_selected = selected.length;
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);
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);
144 /* Return object reference */
148 // Define the Controller as the constructor function.
149 app.controller("TestbedsCtrl", Controller);
151 })(angular, ManifoldApp);
154 var TestbedsPlugin = Plugin.extend({
157 * @brief Plugin constructor
158 * @param options : an associative array of setting values
160 * @return : a jQuery collection of objects on which the plugin is
161 * applied, which allows to maintain chainability of calls
163 init: function(options, element)
165 // Call the parent constructor, see FAQ when forgotten
166 this._super(options, element);
168 /* Member variables */
169 this.testbeds = Array();
171 this._get_scope().instance = this;
174 this.listen_query(options.query_uuid);
175 this.listen_query(options.query_networks_uuid, 'networks');
181 /* When a filter is added/removed, update the list of filters local to the plugin */
183 on_filter_added: function(filter)
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");
191 }else if(filter[1]=='=' || filter[1]=='=='){
192 $("#testbeds-filter_"+filter[2]).addClass("active");
195 // XXX How to display unsupported filters
196 // XXX Constants for operators
199 on_filter_removed: function(filter)
201 this.filters = $.grep(this.filters, function(x) {
204 if(filter[0]=='network_hrn'){
205 if(filter[1]=='included'){
206 $.each(filter[2], function(value){
207 $("#testbeds-filter_"+value).removeClass("active");
209 }else if(filter[1]=='=' || filter[1]=='=='){
210 $("#testbeds-filter_"+filter[2]).removeClass("active");
215 // ... be sure to list all events here
217 on_query_done: function()
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();
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);
237 on_networks_query_done: function()
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 });
247 /* INTERNAL FUNCTIONS */
249 _get_scope : function()
251 return angular.element('[ng-controller=TestbedsCtrl]').scope();
255 _addFilter: function(key, op, value)
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";
263 if(network_filter.length > 0){
264 $.each(network_filter, function(i,f){
268 // Add the new value to list of values, ex: wilab
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]);
275 _removeFilter: function(key, op, value)
277 console.log("remove "+value);
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";
285 if(network_filter.length > 0){
286 $.each(network_filter, function(i,f){
288 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
292 // remove the value from the list of values, ex: wilab
293 values = $.grep(values, function(x) {
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]);
304 /* Plugin registration */
305 $.plugin('TestbedsPlugin', TestbedsPlugin);
307 // TODO Here use cases for instanciating plugins in different ways like in the pastie.