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();
32 this.testbeds = 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');
43 /* GUI setup and event binding */
49 // on_show like in querytable
54 // a function to bind events here: click change
55 // how to raise manifold events
58 /* GUI MANIPULATION */
60 // We advise you to write function to change behaviour of the GUI
61 // Will use naming helpers to access content _inside_ the plugin
62 // always refer to these functions in the remaining of the code
64 show_hide_button: function()
66 // this.id, this.el, this.cl, this.elts
67 // same output as a jquery selector with some guarantees
72 // see in the html template
73 // How to load a template, use of mustache
77 // How to make sure the plugin is not desynchronized
78 // He should manifest its interest in filters, fields or records
79 // functions triggered only if the proper listen is done
83 /* When a filter is added/removed, update the list of filters local to the plugin */
84 on_filter_added: function(filter)
86 this.filters.push(filter);
87 if(filter[0]=='network_hrn'){
88 if(filter[1]=='included'){
89 $.each(filter[2], function(value){
90 $("#testbeds-filter_"+value).addClass("active");
92 }else if(filter[1]=='=' || filter[1]=='=='){
93 $("#testbeds-filter_"+filter[2]).addClass("active");
97 on_filter_removed: function(filter)
99 this.filters = $.grep(this.filters, function(x) {
102 if(filter[0]=='network_hrn'){
103 if(filter[1]=='included'){
104 $.each(filter[2], function(value){
105 $("#testbeds-filter_"+value).removeClass("active");
107 }else if(filter[1]=='=' || filter[1]=='=='){
108 $("#testbeds-filter_"+filter[2]).removeClass("active");
113 // ... be sure to list all events here
115 /* RECORD HANDLERS */
116 on_all_new_record: function(record)
119 // If the resource has a network_hrn
120 if(record["network_hrn"]!="None" && record["network_hrn"]!="" && record["network_hrn"]!=null){
121 // If this network_hrn is not listed yet
122 if(jQuery.inArray(record["network_hrn"],self.testbeds)==-1){
123 row = '<a href="#" class="list-group-item sl-platform" id="testbeds-filter_'+record["network_hrn"]+'" data-platform="'+record["network_hrn"]+'">';
124 //row += '<span class="list-group-item-heading">'+record["platform"]+'</span>';
125 //row += '<span class="list-group-item-heading">'+record["network_hrn"]+'</span></a>';
126 row += '<p class="list-group-item-heading">'+record["network_hrn"]+'</p></a>';
127 $('#testbeds-filter').append(row);
128 self.testbeds.push(record["network_hrn"]);
133 /* When the query is done, add the click event to the elements */
134 on_all_query_done: function() {
136 console.log('query network DONE');
137 $("[id^='testbeds-filter_']").on('click',function(e) {
138 $(this).toggleClass("active");
140 // avoid multiple calls when an event is raised to manifold.js
143 value = this.dataset['platform'];
144 // handle the hrn that include . in their name (has to be in sync with the data from SFA)
145 //value = value.replace(/\./g,"\\.");
148 return $(this).hasClass('active') ? self._addFilter(key, op, value) : self._removeFilter(key, op, value);
153 /* INTERNAL FUNCTIONS */
155 // only convention, not strictly enforced at the moment
157 _addFilter: function(key, op, value)
159 console.log("add "+value);
162 // get the previous list of values for this key, ex: [ple,nitos]
163 // remove the previous filter
164 network_filter = $.grep(this.filters, function(x) {
165 return x[0] == "network_hrn";
167 if(network_filter.length > 0){
168 $.each(network_filter, function(i,f){
170 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
173 // Add the new value to list of values, ex: wilab
176 // Update the filter with the new list of values, ex: [ple,nitos,wilab]
177 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
179 _removeFilter: function(key, op, value)
181 console.log("remove "+value);
184 // get the previous list of values for this key, ex: [ple,nitos,wilab]
185 // remove the previous filter
186 network_filter = $.grep(this.filters, function(x) {
187 return x[0] == "network_hrn";
189 if(network_filter.length > 0){
190 $.each(network_filter, function(i,f){
192 manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
196 // remove the value from the list of values, ex: wilab
197 values = $.grep(values, function(x) {
201 // Update the filter with the new list of values, ex: [ple,nitos]
202 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
208 /* Plugin registration */
209 $.plugin('TestbedsPlugin', TestbedsPlugin);
211 // TODO Here use cases for instanciating plugins in different ways like in the pastie.