b977127086034c68fe12110a372de2f127eec2f2
[unfold.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 (function($){
14
15     var TestbedsPlugin = Plugin.extend({
16
17         /** XXX to check
18          * @brief Plugin constructor
19          * @param options : an associative array of setting values
20          * @param element : 
21          * @return : a jQuery collection of objects on which the plugin is
22          *     applied, which allows to maintain chainability of calls
23          */
24         init: function(options, element) {
25                 // for debugging tools
26                 this.classname="testbedsplugin";
27             // Call the parent constructor, see FAQ when forgotten
28             this._super(options, element);
29
30             /* Member variables */
31             this.filters = Array();
32
33             /* Plugin events */
34
35             /* Setup query and record handlers */
36
37             // Explain this will allow query events to be handled
38             // What happens when we don't define some events ?
39             // Some can be less efficient
40             this.listen_query(options.query_uuid);
41             this.listen_query(options.query_all_uuid, 'all');
42             this.listen_query(options.query_network_uuid, 'network');
43
44             /* GUI setup and event binding */
45             // call function
46
47         },
48
49         /* PLUGIN EVENTS */
50         // on_show like in querytable
51
52
53         /* GUI EVENTS */
54
55         // a function to bind events here: click change
56         // how to raise manifold events
57
58
59         /* GUI MANIPULATION */
60
61         // We advise you to write function to change behaviour of the GUI
62         // Will use naming helpers to access content _inside_ the plugin
63         // always refer to these functions in the remaining of the code
64
65         show_hide_button: function() 
66         {
67             // this.id, this.el, this.cl, this.elts
68             // same output as a jquery selector with some guarantees
69         },
70
71         /* TEMPLATES */
72
73         // see in the html template
74         // How to load a template, use of mustache
75
76         /* QUERY HANDLERS */
77
78         // How to make sure the plugin is not desynchronized
79         // He should manifest its interest in filters, fields or records
80         // functions triggered only if the proper listen is done
81
82         // no prefix
83
84         /* When a filter is added/removed, update the list of filters local to the plugin */
85         on_filter_added: function(filter)
86         {
87             this.filters.push(filter);
88             if(filter[0]=='network_hrn'){
89                 if(filter[1]=='included'){
90                     $.each(filter[2], function(value){
91                         $("#testbeds-filter_"+value).addClass("active");
92                     });
93                 }else if(filter[1]=='=' || filter[1]=='=='){
94                     $("#testbeds-filter_"+filter[2]).addClass("active");
95                 }
96             }
97         },
98         on_filter_removed: function(filter)
99         {
100             this.filters = $.grep(this.filters, function(x) {
101                 return x == filter;
102             });
103             if(filter[0]=='network_hrn'){
104                 if(filter[1]=='included'){
105                     $.each(filter[2], function(value){
106                         $("#testbeds-filter_"+value).removeClass("active");
107                     });
108                 }else if(filter[1]=='=' || filter[1]=='=='){
109                     $("#testbeds-filter_"+filter[2]).removeClass("active");
110                 }
111             }
112         },
113
114         // ... be sure to list all events here
115
116         /* RECORD HANDLERS */
117         on_network_new_record: function(record)
118         {
119             row  = '<a href="#" class="list-group-item sl-platform" id="testbeds-filter_'+record["network_hrn"]+'" data-platform="'+record["network_hrn"]+'">';
120             row += '<span class="list-group-item-heading">'+record["platform"]+'</span>';
121             //row += '<span class="list-group-item-heading">'+record["network_hrn"]+'</span></a>';
122             row += '<p class="list-group-item-text">'+record["network_hrn"]+'</p></a>';
123             $('#testbeds-filter').append(row);
124         },
125
126         /* When the network query is done, add the click event to the elements  */
127         on_network_query_done: function() {
128             var self = this;
129             console.log('query network DONE');
130             $("[id^='testbeds-filter_']").on('click',function(e) {
131                 $(this).toggleClass("active");
132
133                 // avoid multiple calls when an event is raised to manifold.js
134                 e.stopPropagation();
135
136                 value = this.dataset['platform'];
137                 // handle the hrn that include . in their name (has to be in sync with the data from SFA)
138                 //value = value.replace(/\./g,"\\.");
139                 key = "network_hrn";
140                 op = "included";
141                 return $(this).hasClass('active') ? self._addFilter(key, op, value) : self._removeFilter(key, op, value);
142             });
143            
144         },
145
146         /* INTERNAL FUNCTIONS */
147         _dummy: function() {
148             // only convention, not strictly enforced at the moment
149         },
150         _addFilter: function(key, op, value)
151         {
152             console.log("add "+value);
153             var self = this;
154             values = Array();
155             // get the previous list of values for this key, ex: [ple,nitos]
156             // remove the previous filter
157             network_filter = $.grep(this.filters, function(x) {
158                 return x[0] == "network_hrn";
159             });
160             if(network_filter.length > 0){
161                 $.each(network_filter, function(i,f){
162                     values = f[2];
163                     manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
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         _removeFilter: function(key, op, value)
173         {
174             console.log("remove "+value);
175             var self = this;
176             values = Array();
177             // get the previous list of values for this key, ex: [ple,nitos,wilab]
178             // remove the previous filter
179             network_filter = $.grep(this.filters, function(x) {
180                 return x[0] == "network_hrn";
181             });
182             if(network_filter.length > 0){
183                 $.each(network_filter, function(i,f){
184                     values = f[2];
185                     manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
186                 });
187             }
188
189             // remove the value from the list of values, ex: wilab
190             values = $.grep(values, function(x) {
191                 return x != value;
192             });
193             if(values.length>0){
194                 // Update the filter with the new list of values, ex: [ple,nitos]
195                 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
196             }
197         }
198
199     });
200
201     /* Plugin registration */
202     $.plugin('TestbedsPlugin', TestbedsPlugin);
203
204     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
205
206 })(jQuery);