fixes testbed filter when no testbed is selected (closes ticket #50).
[unfold.git] / plugins / testbeds / static / js / testbeds.js
index e424c24..e21446c 100644 (file)
  * License:     GPLv3
  */
 
-(function($){
+// XXX Inherit from an AngularPlugin class ?
+(function (ng, app) {
+
+    // Define our Controller constructor.
+    function Controller($scope) {
+        /* Contructor */
+
+        /* Plugin instance */
+        $scope.instance = null;
+
+        /* Models */
+        $scope.testbeds = Array();
+
+        /* Click event */
+        $scope.select = function(testbed)
+        {
+            var selected, prev_selected, num, num_selected, num_prev_selected, filter;
+
+            prev_selected = $.map($scope.testbeds, function(x, i) {
+                return x.active ? x.network_hrn : null;
+            });
+
+            testbed.active = !testbed.active;
+
+            selected = $.map($scope.testbeds, function(x, i) {
+                return x.active ? x.network_hrn : null;
+            });
+
+            num = $scope.testbeds.length;
+            prev_num_selected = prev_selected.length;
+            num_selected = selected.length;
+
+            
+            if ((prev_num_selected != 0) && (prev_num_selected != num)) {
+                // Remove previous filter
+                filter = ['network_hrn', 'included', prev_selected];
+                manifold.raise_event($scope.instance.options.query_uuid, FILTER_REMOVED, filter);
+            }
 
+            if (num_selected != num) {
+                filter = ['network_hrn', 'included', selected];
+                manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
+            }
+        };
+
+        /* Return object reference */
+        return (this);
+    }
+
+    // Define the Controller as the constructor function.
+    app.controller("TestbedsCtrl", Controller);
+
+})(angular, ManifoldApp);
+
+(function($){
     var TestbedsPlugin = Plugin.extend({
 
         /** XXX to check
          * @return : a jQuery collection of objects on which the plugin is
          *     applied, which allows to maintain chainability of calls
          */
-        init: function(options, element) {
-           // for debugging tools
-           this.classname="testbedsplugin";
+        init: function(options, element) 
+        {
             // Call the parent constructor, see FAQ when forgotten
             this._super(options, element);
 
             /* Member variables */
+            this.filters = Array();
+            this.testbeds = Array();
 
-            /* Plugin events */
-
-            /* Setup query and record handlers */
+            this._get_scope().instance = this;
 
-            // Explain this will allow query events to be handled
-            // What happens when we don't define some events ?
-            // Some can be less efficient
+            /* Handlers */
             this.listen_query(options.query_uuid);
-            this.listen_query(options.query_all_uuid, 'all');
-            this.listen_query(options.query_network_uuid, 'network');
-
-            /* GUI setup and event binding */
-            // call function
-
+            this.listen_query(options.query_networks_uuid, 'networks');
         },
 
-        /* PLUGIN EVENTS */
-        // on_show like in querytable
-
 
-        /* GUI EVENTS */
+        /* HANDLERS */
 
-        // a function to bind events here: click change
-        // how to raise manifold events
-
-
-        /* GUI MANIPULATION */
-
-        // We advise you to write function to change behaviour of the GUI
-        // Will use naming helpers to access content _inside_ the plugin
-        // always refer to these functions in the remaining of the code
-
-        show_hide_button: function() 
+        /* When a filter is added/removed, update the list of filters local to the plugin */
+        on_filter_added: function(filter)
         {
-            // this.id, this.el, this.cl, this.elts
-            // same output as a jquery selector with some guarantees
+            this.filters.push(filter);
+            if(filter[0]=='network_hrn'){
+                if(filter[1]=='included'){
+                    $.each(filter[2], function(value){
+                        $("#testbeds-filter_"+value).addClass("active");
+                    });
+                }else if(filter[1]=='=' || filter[1]=='=='){
+                    $("#testbeds-filter_"+filter[2]).addClass("active");
+                }
+                // XXX NAMING
+                // XXX How to display unsupported filters
+                // XXX Constants for operators
+            }
+        },
+        on_filter_removed: function(filter)
+        {
+            this.filters = $.grep(this.filters, function(x) {
+                return x == filter;
+            });
+            if(filter[0]=='network_hrn'){
+                if(filter[1]=='included'){
+                    $.each(filter[2], function(value){
+                        $("#testbeds-filter_"+value).removeClass("active");
+                    });
+                }else if(filter[1]=='=' || filter[1]=='=='){
+                    $("#testbeds-filter_"+filter[2]).removeClass("active");
+                }
+            }
         },
 
-        /* TEMPLATES */
-
-        // see in the html template
-        // How to load a template, use of mustache
-
-        /* QUERY HANDLERS */
+        // ... be sure to list all events here
 
-        // How to make sure the plugin is not desynchronized
-        // He should manifest its interest in filters, fields or records
-        // functions triggered only if the proper listen is done
+        on_networks_query_done: function()
+        {
+             this.set_networks();
+        },
 
-        // no prefix
+        /* INTERNAL FUNCTIONS */
 
-        on_filter_added: function(filter)
+        set_networks: function()
         {
-
+            var scope = this._get_scope();
+            var query_ext = manifold.query_store.find_analyzed_query_ext(this.options.query_networks_uuid);
+            scope.testbeds = query_ext.records.values();
+            $.each(scope.testbeds, function(i, testbed) { testbed.active = true });
+            scope.$apply();
         },
 
-        // ... be sure to list all events here
-
-        /* RECORD HANDLERS */
-        on_network_new_record: function(record)
+        _get_scope : function()
         {
-            console.log(record);
-            row  = '<a href="#" class="list-group-item sl-platform" data-platform="'+record["network_hrn"]+'">';
-            row += '<span class="list-group-item-heading">'+record["platform"]+'</span>';
-            //row += '<span class="list-group-item-heading">'+record["network_hrn"]+'</span></a>';
-            row += '<p class="list-group-item-text">'+record["network_hrn"]+'</p></a>';
-            $('#testbeds-filter').append(row);
+            return angular.element('[ng-controller=TestbedsCtrl]').scope()
         },
 
-        /* INTERNAL FUNCTIONS */
-        _dummy: function() {
-            // only convention, not strictly enforced at the moment
+/*
+        _addFilter: function(key, op, value)
+        {
+            values = Array();
+            // get the previous list of values for this key, ex: [ple,nitos]
+            // remove the previous filter
+            network_filter = $.grep(this.filters, function(x) {
+                return x[0] == "network_hrn";
+            });
+            if(network_filter.length > 0){
+                $.each(network_filter, function(i,f){
+                    values = f[2];
+                });
+            }
+            // Add the new value to list of values, ex: wilab
+            values.push(value);
+            
+            // Update the filter with the new list of values, ex: [ple,nitos,wilab]
+            manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
         },
 
+        _removeFilter: function(key, op, value)
+        {
+            console.log("remove "+value);
+            var self = this;
+            values = Array();
+            // get the previous list of values for this key, ex: [ple,nitos,wilab]
+            // remove the previous filter
+            network_filter = $.grep(this.filters, function(x) {
+                return x[0] == "network_hrn";
+            });
+            if(network_filter.length > 0){
+                $.each(network_filter, function(i,f){
+                    values = f[2];
+                    manifold.raise_event(self.options.query_uuid, FILTER_REMOVED, [key, op, values]);
+                });
+            }
+
+            // remove the value from the list of values, ex: wilab
+            values = $.grep(values, function(x) {
+                return x != value;
+            });
+            if(values.length>0){
+                // Update the filter with the new list of values, ex: [ple,nitos]
+                manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
+            }
+        }
+*/
     });
 
     /* Plugin registration */