fixes testbed filter when no testbed is selected (closes ticket #50).
[unfold.git] / plugins / testbeds / static / js / testbeds.js
index b977127..e21446c 100644 (file)
  * License:     GPLv3
  */
 
-(function($){
+// XXX Inherit from an AngularPlugin class ?
+(function (ng, app) {
 
-    var TestbedsPlugin = Plugin.extend({
+    // Define our Controller constructor.
+    function Controller($scope) {
+        /* Contructor */
 
-        /** XXX to check
-         * @brief Plugin constructor
-         * @param options : an associative array of setting values
-         * @param element : 
-         * @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";
-            // Call the parent constructor, see FAQ when forgotten
-            this._super(options, element);
+        /* Plugin instance */
+        $scope.instance = null;
 
-            /* Member variables */
-            this.filters = Array();
+        /* Models */
+        $scope.testbeds = Array();
 
-            /* Plugin events */
-
-            /* Setup query and record handlers */
+        /* Click event */
+        $scope.select = function(testbed)
+        {
+            var selected, prev_selected, num, num_selected, num_prev_selected, filter;
 
-            // Explain this will allow query events to be handled
-            // What happens when we don't define some events ?
-            // Some can be less efficient
-            this.listen_query(options.query_uuid);
-            this.listen_query(options.query_all_uuid, 'all');
-            this.listen_query(options.query_network_uuid, 'network');
+            prev_selected = $.map($scope.testbeds, function(x, i) {
+                return x.active ? x.network_hrn : null;
+            });
 
-            /* GUI setup and event binding */
-            // call function
+            testbed.active = !testbed.active;
 
-        },
+            selected = $.map($scope.testbeds, function(x, i) {
+                return x.active ? x.network_hrn : null;
+            });
 
-        /* PLUGIN EVENTS */
-        // on_show like in querytable
+            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);
+            }
 
-        /* GUI EVENTS */
+            if (num_selected != num) {
+                filter = ['network_hrn', 'included', selected];
+                manifold.raise_event($scope.instance.options.query_uuid, FILTER_ADDED, filter);
+            }
+        };
 
-        // a function to bind events here: click change
-        // how to raise manifold events
+        /* Return object reference */
+        return (this);
+    }
 
+    // Define the Controller as the constructor function.
+    app.controller("TestbedsCtrl", Controller);
 
-        /* GUI MANIPULATION */
+})(angular, ManifoldApp);
 
-        // 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
+(function($){
+    var TestbedsPlugin = Plugin.extend({
 
-        show_hide_button: function() 
+        /** XXX to check
+         * @brief Plugin constructor
+         * @param options : an associative array of setting values
+         * @param element : 
+         * @return : a jQuery collection of objects on which the plugin is
+         *     applied, which allows to maintain chainability of calls
+         */
+        init: function(options, element) 
         {
-            // this.id, this.el, this.cl, this.elts
-            // same output as a jquery selector with some guarantees
-        },
+            // Call the parent constructor, see FAQ when forgotten
+            this._super(options, element);
 
-        /* TEMPLATES */
+            /* Member variables */
+            this.filters = Array();
+            this.testbeds = Array();
 
-        // see in the html template
-        // How to load a template, use of mustache
+            this._get_scope().instance = this;
 
-        /* QUERY HANDLERS */
+            /* Handlers */
+            this.listen_query(options.query_uuid);
+            this.listen_query(options.query_networks_uuid, 'networks');
+        },
 
-        // 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
 
-        // no prefix
+        /* HANDLERS */
 
         /* When a filter is added/removed, update the list of filters local to the plugin */
         on_filter_added: function(filter)
                 }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)
 
         // ... be sure to list all events here
 
-        /* RECORD HANDLERS */
-        on_network_new_record: function(record)
+        on_networks_query_done: function()
         {
-            row  = '<a href="#" class="list-group-item sl-platform" id="testbeds-filter_'+record["network_hrn"]+'" 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);
+             this.set_networks();
         },
 
-        /* When the network query is done, add the click event to the elements  */
-        on_network_query_done: function() {
-            var self = this;
-            console.log('query network DONE');
-            $("[id^='testbeds-filter_']").on('click',function(e) {
-                $(this).toggleClass("active");
-
-                // avoid multiple calls when an event is raised to manifold.js
-                e.stopPropagation();
-
-                value = this.dataset['platform'];
-                // handle the hrn that include . in their name (has to be in sync with the data from SFA)
-                //value = value.replace(/\./g,"\\.");
-                key = "network_hrn";
-                op = "included";
-                return $(this).hasClass('active') ? self._addFilter(key, op, value) : self._removeFilter(key, op, value);
-            });
-           
+        /* INTERNAL FUNCTIONS */
+
+        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();
         },
 
-        /* INTERNAL FUNCTIONS */
-        _dummy: function() {
-            // only convention, not strictly enforced at the moment
+        _get_scope : function()
+        {
+            return angular.element('[ng-controller=TestbedsCtrl]').scope()
         },
+
+/*
         _addFilter: function(key, op, value)
         {
-            console.log("add "+value);
-            var self = this;
             values = Array();
             // get the previous list of values for this key, ex: [ple,nitos]
             // remove the previous filter
             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]);
                 });
             }
             // Add the new value to list of values, ex: wilab
             // 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);
                 manifold.raise_event(this.options.query_uuid, FILTER_ADDED, [key, op, values]);
             }
         }
-
+*/
     });
 
     /* Plugin registration */