e21446cfe02117127a44e7ecd47f33f396fd5c13
[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         /* Models */
24         $scope.testbeds = Array();
25
26         /* Click event */
27         $scope.select = function(testbed)
28         {
29             var selected, prev_selected, num, num_selected, num_prev_selected, filter;
30
31             prev_selected = $.map($scope.testbeds, function(x, i) {
32                 return x.active ? x.network_hrn : null;
33             });
34
35             testbed.active = !testbed.active;
36
37             selected = $.map($scope.testbeds, function(x, i) {
38                 return x.active ? x.network_hrn : null;
39             });
40
41             num = $scope.testbeds.length;
42             prev_num_selected = prev_selected.length;
43             num_selected = selected.length;
44
45             
46             if ((prev_num_selected != 0) && (prev_num_selected != num)) {
47                 // Remove previous filter
48                 filter = ['network_hrn', 'included', prev_selected];
49                 manifold.raise_event($scope.instance.options.query_uuid, FILTER_REMOVED, filter);
50             }
51
52             if (num_selected != num) {
53                 filter = ['network_hrn', 'included', selected];
54                 manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
55             }
56         };
57
58         /* Return object reference */
59         return (this);
60     }
61
62     // Define the Controller as the constructor function.
63     app.controller("TestbedsCtrl", Controller);
64
65 })(angular, ManifoldApp);
66
67 (function($){
68     var TestbedsPlugin = Plugin.extend({
69
70         /** XXX to check
71          * @brief Plugin constructor
72          * @param options : an associative array of setting values
73          * @param element : 
74          * @return : a jQuery collection of objects on which the plugin is
75          *     applied, which allows to maintain chainability of calls
76          */
77         init: function(options, element) 
78         {
79             // Call the parent constructor, see FAQ when forgotten
80             this._super(options, element);
81
82             /* Member variables */
83             this.filters = Array();
84             this.testbeds = Array();
85
86             this._get_scope().instance = this;
87
88             /* Handlers */
89             this.listen_query(options.query_uuid);
90             this.listen_query(options.query_networks_uuid, 'networks');
91         },
92
93
94         /* HANDLERS */
95
96         /* When a filter is added/removed, update the list of filters local to the plugin */
97         on_filter_added: function(filter)
98         {
99             this.filters.push(filter);
100             if(filter[0]=='network_hrn'){
101                 if(filter[1]=='included'){
102                     $.each(filter[2], function(value){
103                         $("#testbeds-filter_"+value).addClass("active");
104                     });
105                 }else if(filter[1]=='=' || filter[1]=='=='){
106                     $("#testbeds-filter_"+filter[2]).addClass("active");
107                 }
108                 // XXX NAMING
109                 // XXX How to display unsupported filters
110                 // XXX Constants for operators
111             }
112         },
113         on_filter_removed: function(filter)
114         {
115             this.filters = $.grep(this.filters, function(x) {
116                 return x == filter;
117             });
118             if(filter[0]=='network_hrn'){
119                 if(filter[1]=='included'){
120                     $.each(filter[2], function(value){
121                         $("#testbeds-filter_"+value).removeClass("active");
122                     });
123                 }else if(filter[1]=='=' || filter[1]=='=='){
124                     $("#testbeds-filter_"+filter[2]).removeClass("active");
125                 }
126             }
127         },
128
129         // ... be sure to list all events here
130
131         on_networks_query_done: function()
132         {
133              this.set_networks();
134         },
135
136         /* INTERNAL FUNCTIONS */
137
138         set_networks: function()
139         {
140             var scope = this._get_scope();
141             var query_ext = manifold.query_store.find_analyzed_query_ext(this.options.query_networks_uuid);
142             scope.testbeds = query_ext.records.values();
143             $.each(scope.testbeds, function(i, testbed) { testbed.active = true });
144             scope.$apply();
145         },
146
147         _get_scope : function()
148         {
149             return angular.element('[ng-controller=TestbedsCtrl]').scope()
150         },
151
152 /*
153         _addFilter: function(key, op, value)
154         {
155             values = Array();
156             // get the previous list of values for this key, ex: [ple,nitos]
157             // remove the previous filter
158             network_filter = $.grep(this.filters, function(x) {
159                 return x[0] == "network_hrn";
160             });
161             if(network_filter.length > 0){
162                 $.each(network_filter, function(i,f){
163                     values = f[2];
164                 });
165             }
166             // Add the new value to list of values, ex: wilab
167             values.push(value);
168             
169             // Update the filter with the new list of values, ex: [ple,nitos,wilab]
170             manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
171         },
172
173         _removeFilter: function(key, op, value)
174         {
175             console.log("remove "+value);
176             var self = this;
177             values = Array();
178             // get the previous list of values for this key, ex: [ple,nitos,wilab]
179             // remove the previous filter
180             network_filter = $.grep(this.filters, function(x) {
181                 return x[0] == "network_hrn";
182             });
183             if(network_filter.length > 0){
184                 $.each(network_filter, function(i,f){
185                     values = f[2];
186                     manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
187                 });
188             }
189
190             // remove the value from the list of values, ex: wilab
191             values = $.grep(values, function(x) {
192                 return x != value;
193             });
194             if(values.length>0){
195                 // Update the filter with the new list of values, ex: [ple,nitos]
196                 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
197             }
198         }
199 */
200     });
201
202     /* Plugin registration */
203     $.plugin('TestbedsPlugin', TestbedsPlugin);
204
205     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
206
207 })(jQuery);