Testbeds list plugin with filters: key, included, [x,y,z]
[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 (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                 key = "network_hrn";
138                 op = "included";
139                 return $(this).hasClass('active') ? self._addFilter(key, op, value) : self._removeFilter(key, op, value);
140             });
141            
142         },
143
144         /* INTERNAL FUNCTIONS */
145         _dummy: function() {
146             // only convention, not strictly enforced at the moment
147         },
148         _addFilter: function(key, op, value)
149         {
150             console.log("add "+value);
151             var self = this;
152             values = Array();
153             // get the previous list of values for this key, ex: [ple,nitos]
154             // remove the previous filter
155             network_filter = $.grep(this.filters, function(x) {
156                 return x[0] == "network_hrn";
157             });
158             if(network_filter.length > 0){
159                 $.each(network_filter, function(i,f){
160                     values = f[2];
161                     manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
162                 });
163             }
164             // Add the new value to list of values, ex: wilab
165             values.push(value);
166             
167             // Update the filter with the new list of values, ex: [ple,nitos,wilab]
168             manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
169         },
170         _removeFilter: function(key, op, value)
171         {
172             console.log("remove "+value);
173             var self = this;
174             values = Array();
175             // get the previous list of values for this key, ex: [ple,nitos,wilab]
176             // remove the previous filter
177             network_filter = $.grep(this.filters, function(x) {
178                 return x[0] == "network_hrn";
179             });
180             if(network_filter.length > 0){
181                 $.each(network_filter, function(i,f){
182                     values = f[2];
183                     manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
184                 });
185             }
186
187             // remove the value from the list of values, ex: wilab
188             values = $.grep(values, function(x) {
189                 return x != value;
190             });
191             if(values.length>0){
192                 // Update the filter with the new list of values, ex: [ple,nitos]
193                 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
194             }
195         }
196
197     });
198
199     /* Plugin registration */
200     $.plugin('TestbedsPlugin', TestbedsPlugin);
201
202     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
203
204 })(jQuery);