plugins: supporting columns adding/removing + passed query instead of uuid as parameters
[myslice.git] / plugins / hazelnut / hazelnut.js
index 84be66e..8cbc537 100644 (file)
@@ -9,6 +9,10 @@
  * Expression) that maps it to the dollar sign so it can't be overwritten by
  * another library in the scope of its execution.
  */
+
+// TEMP
+var ELEMENT_KEY = 'resource_hrn';
+
 (function($){
 
     var debug=false;
                 var $this = $(this);
 
                 /* An object that will hold private variables and methods */
-                var hazelnut = new Hazelnut (options);
-                $this.data('Hazelnut', hazelnut);
+                var plugin = new Hazelnut (options);
+                $this.data('Hazelnut', plugin);
 
                 /* Events */
                 $this.on('show.Datatables', methods.show);
 
                 // This is the new plugin API meant to replace the weird publish_subscribe mechanism
-                $this.set_query_handler(options.query_uuid, hazelnut.query_handler);
-                $this.set_record_handler(options.query_uuid, hazelnut.record_handler); 
+                $this.set_query_handler(options.query_uuid, plugin.query_handler);
+                $this.set_record_handler(options.query_uuid, plugin.record_handler); 
+                $this.set_record_handler(options.query_all_uuid, plugin.record_handler_all); 
 
 //                /* Subscriptions */
 //                var query_channel   = '/query/' + options.query_uuid + '/changed';
     }; // var methods;
 
     /***************************************************************************
-     * Hazelnut object
+     * Plugin object
      ***************************************************************************/
 
     function Hazelnut(options) 
         this.query_update = null;
         this.current_resources = Array();
 
+        // query status
+        this.received_all = false;
+        this.received_set = false;
+        this.in_set_buffer = Array();
+
         var object = this;
 
-        /* Transforms the table into DataTable, and keep a pointer to it */
-        actual_options = {
-            // Customize the position of Datatables elements (length,filter,button,...)
-            // we use a fluid row on top and another on the bottom, making sure we take 12 grid elt's each time
-            sDom: "<'row-fluid'<'span5'l><'span1'r><'span6'f>>t<'row-fluid'<'span5'i><'span7'p>>",
-            sPaginationType: 'bootstrap',
-            // Handle the null values & the error : Datatables warning Requested unknown parameter
-            // http://datatables.net/forums/discussion/5331/datatables-warning-...-requested-unknown-parameter/p2
-            aoColumnDefs: [{sDefaultContent: '',aTargets: [ '_all' ]}],
-            // WARNING: this one causes tables in a 'tabs' that are not exposed at the time this is run to show up empty
-            // sScrollX: '100%',       /* Horizontal scrolling */
-            bProcessing: true,      /* Loading */
-            fnDrawCallback: function() { hazelnut_draw_callback.call(object, options); }
-        };
-        // the intention here is that options.datatables_options as coming from the python object take precedence
-        //  XXX DISABLED by jordan: was causing errors in datatables.js     $.extend(actual_options, options.datatables_options );
-        this.table = $('#hazelnut-' + options.plugin_uuid).dataTable(actual_options);
-
-        /* Setup the SelectAll button in the dataTable header */
-        /* xxx not sure this is still working */
-        var oSelectAll = $('#datatableSelectAll-'+ options.plugin_uuid);
-        oSelectAll.html("<span class='ui-icon ui-icon-check' style='float:right;display:inline-block;'></span>Select All");
-        oSelectAll.button();
-        oSelectAll.css('font-size','11px');
-        oSelectAll.css('float','right');
-        oSelectAll.css('margin-right','15px');
-        oSelectAll.css('margin-bottom','5px');
-        oSelectAll.unbind('click');
-        oSelectAll.click(selectAll);
-
-        /* Add a filtering function to the current table 
-         * Note: we use closure to get access to the 'options'
-         */
-        $.fn.dataTableExt.afnFiltering.push(function( oSettings, aData, iDataIndex ) { 
-            /* No filtering if the table does not match */
-            if (oSettings.nTable.id != "hazelnut-" + options.plugin_uuid)
-                return true;
-            return hazelnut_filter.call(object, oSettings, aData, iDataIndex);
-        });
+        this.initialize = function() {
+            /* Transforms the table into DataTable, and keep a pointer to it */
+            actual_options = {
+                // Customize the position of Datatables elements (length,filter,button,...)
+                // we use a fluid row on top and another on the bottom, making sure we take 12 grid elt's each time
+                sDom: "<'row-fluid'<'span5'l><'span1'r><'span6'f>>t<'row-fluid'<'span5'i><'span7'p>>",
+                sPaginationType: 'bootstrap',
+                // Handle the null values & the error : Datatables warning Requested unknown parameter
+                // http://datatables.net/forums/discussion/5331/datatables-warning-...-requested-unknown-parameter/p2
+                aoColumnDefs: [{sDefaultContent: '',aTargets: [ '_all' ]}],
+                // WARNING: this one causes tables in a 'tabs' that are not exposed at the time this is run to show up empty
+                // sScrollX: '100%',       /* Horizontal scrolling */
+                bProcessing: true,      /* Loading */
+                fnDrawCallback: function() { hazelnut_draw_callback.call(object, options); }
+            };
+            // the intention here is that options.datatables_options as coming from the python object take precedence
+            //  XXX DISABLED by jordan: was causing errors in datatables.js     $.extend(actual_options, options.datatables_options );
+            this.table = $('#hazelnut-' + options.plugin_uuid).dataTable(actual_options);
+
+            /* Setup the SelectAll button in the dataTable header */
+            /* xxx not sure this is still working */
+            var oSelectAll = $('#datatableSelectAll-'+ options.plugin_uuid);
+            oSelectAll.html("<span class='ui-icon ui-icon-check' style='float:right;display:inline-block;'></span>Select All");
+            oSelectAll.button();
+            oSelectAll.css('font-size','11px');
+            oSelectAll.css('float','right');
+            oSelectAll.css('margin-right','15px');
+            oSelectAll.css('margin-bottom','5px');
+            oSelectAll.unbind('click');
+            oSelectAll.click(selectAll);
+
+            /* Add a filtering function to the current table 
+             * Note: we use closure to get access to the 'options'
+             */
+            $.fn.dataTableExt.afnFiltering.push(function( oSettings, aData, iDataIndex ) { 
+                /* No filtering if the table does not match */
+                if (oSettings.nTable.id != "hazelnut-" + options.plugin_uuid)
+                    return true;
+                return hazelnut_filter.call(object, oSettings, aData, iDataIndex);
+            });
+
+            /* Processing hidden_columns */
+            $.each(options.hidden_columns, function(i, field) {
+                object.hide_column(field);
+            });
+        }
 
         /* methods */
 
         this.getColIndex = function(key, cols) {
             var tabIndex = $.map(cols, function(x, i) { if (x.sTitle == key) return i; });
             return (tabIndex.length > 0) ? tabIndex[0] : -1;
-        }
+        };
     
 // DEPRECATED //        /**
 // DEPRECATED //         * @brief
                     // xxx problem is, we don't get this 'sliver' thing set apparently
                     if (typeof(row['sliver']) != 'undefined') { /* It is equal to null when <sliver/> is present */
                         checked = 'checked ';
-                        hazelnut.current_resources.push(row['urn']);
+                        hazelnut.current_resources.push(row[ELEMENT_KEY]);
                     }
                     // Use a key instead of hostname (hard coded...)
-                    line.push(hazelnut.checkbox(options.plugin_uuid, row['urn'], row['type'], checked, false));
+                    line.push(hazelnut.checkbox(options.plugin_uuid, row[ELEMENT_KEY], row['type'], checked, false));
                 }
     
                 lines.push(line);
         this.checkbox = function (plugin_uuid, header, field, selected_str, disabled_str)
         {
             var result="";
+            if (header === null)
+                header = '';
             // Prefix id with plugin_uuid
             result += "<input";
             result += " class='hazelnut-checkbox-" + plugin_uuid + "'";
-            result += " id='hazelnut-checkbox-" + plugin_uuid + "-" + unfold.get_value(header) + "'";
+            result += " id='hazelnut-checkbox-" + plugin_uuid + "-" + unfold.get_value(header).replace(/\\/g, '')  + "'";
             result += " name='" + unfold.get_value(field) + "'";
             result += " type='checkbox'";
             result += selected_str;
                 // xxx problem is, we don't get this 'sliver' thing set apparently
                 if (typeof(record['sliver']) != 'undefined') { /* It is equal to null when <sliver/> is present */
                     checked = 'checked ';
-                    hazelnut.current_resources.push(record['urn']);
+                    hazelnut.current_resources.push(record[ELEMENT_KEY]);
                 }
                 // Use a key instead of hostname (hard coded...)
-                line.push(object.checkbox(options.plugin_uuid, record['urn'], record['type'], checked, false));
+                line.push(object.checkbox(options.plugin_uuid, record[ELEMENT_KEY], record['type'], checked, false));
             }
     
             // XXX Is adding an array of lines more efficient ?
 
         };
 
+        this.set_checkbox = function(record)
+        {
+            // XXX urn should be replaced by the key
+            // XXX we should enforce that both queries have the same key !!
+            checkbox_id = "#hazelnut-checkbox-" + object.options.plugin_uuid + "-" + unfold.escape_id(record[ELEMENT_KEY].replace(/\\/g, ''))
+            $(checkbox_id, object.table.fnGetNodes()).attr('checked', true);
+        }
+
         this.record_handler = function(e, event_type, record)
         {
+            // elements in set
+            switch(event_type) {
+                case NEW_RECORD:
+                    /* NOTE in fact we are doing a join here */
+                    if (object.received_all)
+                        // update checkbox for record
+                        object.set_checkbox(record);
+                    else
+                        // store for later update of checkboxes
+                        object.in_set_buffer.push(record);
+                    break;
+                case CLEAR_RECORDS:
+                    // nothing to do here
+                    break;
+                case IN_PROGRESS:
+                    manifold.spin($(this));
+                    break;
+                case DONE:
+                    if (object.received_all)
+                        manifold.spin($(this), false);
+                    object.received_set = true;
+                    break;
+            }
+        };
+
+        this.record_handler_all = function(e, event_type, record)
+        {
+            // all elements
             switch(event_type) {
                 case NEW_RECORD:
+                    // Add the record to the table
                     object.new_record(record);
                     break;
                 case CLEAR_RECORDS:
                     manifold.spin($(this));
                     break;
                 case DONE:
-                    manifold.spin($(this), false);
+                    if (object.received_set) {
+                        /* XXX needed ? XXX We uncheck all checkboxes ... */
+                        $("[id^='datatables-checkbox-" + object.options.plugin_uuid +"']").attr('checked', false);
+
+                        /* ... and check the ones specified in the resource list */
+                        $.each(object.in_set_buffer, function(i, record) {
+                            object.set_checkbox(record);
+                        });
+
+                        manifold.spin($(this), false);
+                    }
+                    object.received_all = true;
                     break;
             }
         };
 
-        this.query_handler = function(e, event_type, query)
+        this.show_column = function(field)
+        {
+            var oSettings = object.table.fnSettings();
+            var cols = oSettings.aoColumns;
+            var index = object.getColIndex(field,cols);
+            if (index != -1)
+                object.table.fnSetColumnVis(index, true);
+        }
+
+        this.hide_column = function(field)
+        {
+            var oSettings = object.table.fnSettings();
+            var cols = oSettings.aoColumns;
+            var index = object.getColIndex(field,cols);
+            if (index != -1)
+                object.table.fnSetColumnVis(index, false);
+        }
+
+        this.query_handler = function(e, event_type, data)
         {
             // This replaces the complex set_query function
             // The plugin does not need to remember the query anymore
                 // Fields
                 /* Hide/unhide columns to match added/removed fields */
                 case FIELD_ADDED:
-                    var object = this;
-                    $.each(added_fields, function (index, field) {            
-                        var index = object.getColIndex(field,cols);
-                        if(index != -1)
-                            object.table.fnSetColumnVis(index, true);
-                    });
+                    object.show_column(data);
                     break;
                 case FIELD_REMOVED:
-                    var object = this;
-                    $.each(removed_fields, function (index, field) {
-                        var index = object.getColIndex(field,cols);
-                        if(index != -1)
-                            object.table.fnSetColumnVis(index, false);
-                    });            
+                    object.hide_column(data);
                     break;
                 case CLEAR_FIELDS:
                     alert('Hazelnut::clear_fields() not implemented');
 
 
         }
+
+        // Constructor
+        object.initialize();
+
     } // constructor
 
     /***************************************************************************