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
15 var TestbedsPlugin = Plugin.extend({
18 * @brief Plugin constructor
19 * @param options : an associative array of setting values
21 * @return : a jQuery collection of objects on which the plugin is
22 * applied, which allows to maintain chainability of calls
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);
30 /* Member variables */
31 this.filters = Array();
35 /* Setup query and record handlers */
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');
44 /* GUI setup and event binding */
50 // on_show like in querytable
55 // a function to bind events here: click change
56 // how to raise manifold events
59 /* GUI MANIPULATION */
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
65 show_hide_button: function()
67 // this.id, this.el, this.cl, this.elts
68 // same output as a jquery selector with some guarantees
73 // see in the html template
74 // How to load a template, use of mustache
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
84 /* When a filter is added/removed, update the list of filters local to the plugin */
85 on_filter_added: function(filter)
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");
93 }else if(filter[1]=='=' || filter[1]=='=='){
94 $("#testbeds-filter_"+filter[2]).addClass("active");
98 on_filter_removed: function(filter)
100 this.filters = $.grep(this.filters, function(x) {
103 if(filter[0]=='network_hrn'){
104 if(filter[1]=='included'){
105 $.each(filter[2], function(value){
106 $("#testbeds-filter_"+value).removeClass("active");
108 }else if(filter[1]=='=' || filter[1]=='=='){
109 $("#testbeds-filter_"+filter[2]).removeClass("active");
114 // ... be sure to list all events here
116 /* RECORD HANDLERS */
117 on_network_new_record: function(record)
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);
126 /* When the network query is done, add the click event to the elements */
127 on_network_query_done: function() {
129 console.log('query network DONE');
130 $("[id^='testbeds-filter_']").on('click',function(e) {
131 $(this).toggleClass("active");
133 // avoid multiple calls when an event is raised to manifold.js
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,"\\.");
141 return $(this).hasClass('active') ? self._addFilter(key, op, value) : self._removeFilter(key, op, value);
146 /* INTERNAL FUNCTIONS */
148 // only convention, not strictly enforced at the moment
150 _addFilter: function(key, op, value)
152 console.log("add "+value);
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";
160 if(network_filter.length > 0){
161 $.each(network_filter, function(i,f){
163 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
166 // Add the new value to list of values, ex: wilab
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]);
172 _removeFilter: function(key, op, value)
174 console.log("remove "+value);
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";
182 if(network_filter.length > 0){
183 $.each(network_filter, function(i,f){
185 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
189 // remove the value from the list of values, ex: wilab
190 values = $.grep(values, function(x) {
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]);
201 /* Plugin registration */
202 $.plugin('TestbedsPlugin', TestbedsPlugin);
204 // TODO Here use cases for instanciating plugins in different ways like in the pastie.