84be66edd8d57a8f807230bc9a38a93cc589b2cf
[myslice.git] / plugins / hazelnut / hazelnut.js
1 /**
2  * Description: display a query result in a datatables-powered <table>
3  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 /*
8  * It's a best practice to pass jQuery to an IIFE (Immediately Invoked Function
9  * Expression) that maps it to the dollar sign so it can't be overwritten by
10  * another library in the scope of its execution.
11  */
12 (function($){
13
14     var debug=false;
15     debug=true
16
17     // routing calls
18     $.fn.Hazelnut = function( method ) {
19         if ( methods[method] ) {
20             return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
21         } else if ( typeof method === 'object' || ! method ) {
22             return methods.init.apply( this, arguments );
23         } else {
24             $.error( 'Method ' +  method + ' does not exist on jQuery.Hazelnut' );
25         }    
26     };
27
28     /***************************************************************************
29      * Public methods
30      ***************************************************************************/
31
32     var methods = {
33
34         /**
35          * @brief Plugin initialization
36          * @param options : an associative array of setting values
37          * @return : a jQuery collection of objects on which the plugin is
38          *     applied, which allows to maintain chainability of calls
39          */
40         init : function ( options ) {
41
42             /* Default settings */
43             var options = $.extend( {
44                 'checkboxes': false
45             }, options);
46
47             return this.each(function() {
48
49                 var $this = $(this);
50
51                 /* An object that will hold private variables and methods */
52                 var hazelnut = new Hazelnut (options);
53                 $this.data('Hazelnut', hazelnut);
54
55                 /* Events */
56                 $this.on('show.Datatables', methods.show);
57
58                 // This is the new plugin API meant to replace the weird publish_subscribe mechanism
59                 $this.set_query_handler(options.query_uuid, hazelnut.query_handler);
60                 $this.set_record_handler(options.query_uuid, hazelnut.record_handler); 
61
62 //                /* Subscriptions */
63 //                var query_channel   = '/query/' + options.query_uuid + '/changed';
64 //                var update_channel  = '/update-set/' + options.query_uuid;
65 //                var results_channel = '/results/' + options.query_uuid + '/changed';
66 //
67 //                // xxx not tested yet
68 //                $.subscribe(query_channel,  function(e, query) { hazelnut.set_query(query); });
69 //                // xxx not tested yet
70 //                $.subscribe(update_channel, function(e, resources, instance) { hazelnut.set_resources(resources, instance); });
71 //                // expected to work
72 //                $.subscribe(results_channel, $this, function(e, rows) { hazelnut.update_plugin(e,rows); });
73 //                if (debug)
74 //                    messages.debug("hazelnut '" + this.id + "' subscribed to e.g." + results_channel);
75
76             }); // this.each
77         }, // init
78
79         /**
80          * @brief Plugin destruction
81          * @return : a jQuery collection of objects on which the plugin is
82          *     applied, which allows to maintain chainability of calls
83          */
84         destroy : function( ) {
85
86             return this.each(function() {
87                 var $this = $(this);
88                 var hazelnut = $this.data('Hazelnut');
89
90                 // Unbind all events using namespacing
91                 $(window).unbind('Hazelnut');
92
93                 // Remove associated data
94                 hazelnut.remove();
95                 $this.removeData('Hazelnut');
96             });
97         }, // destroy
98
99         show : function( ) {
100             var $this=$(this);
101             // xxx wtf. why [1] ? would expect 0...
102             if (debug)
103                 messages.debug("Hitting suspicious line in hazelnut.show");
104             var oTable = $($('.dataTable', $this)[1]).dataTable();
105             oTable.fnAdjustColumnSizing()
106         
107             /* Refresh dataTabeles if click on the menu to display it : fix dataTables 1.9.x Bug */        
108             $(this).each(function(i,elt) {
109                 if (jQuery(elt).hasClass('dataTables')) {
110                     var myDiv=jQuery('#hazelnut-' + this.id).parent();
111                     if(myDiv.height()==0) {
112                         var oTable=$('#hazelnut-' + this.id).dataTable();            
113                         oTable.fnDraw();
114                     }
115                 }
116             });
117         } // show
118
119     }; // var methods;
120
121     /***************************************************************************
122      * Hazelnut object
123      ***************************************************************************/
124
125     function Hazelnut(options) 
126     {
127         /* member variables */
128         this.options = options;
129
130         // xxx thierry : initialize this here - it was not, I expect this relied on set_query somehow..
131         //this.current_query = null;
132         this.current_query=manifold.find_query(this.options.query_uuid);
133         //  if (debug) messages.debug("Hazelnut constructor: have set current_query -> " + this.current_query.__repr());
134         this.query_update = null;
135         this.current_resources = Array();
136
137         var object = this;
138
139         /* Transforms the table into DataTable, and keep a pointer to it */
140         actual_options = {
141             // Customize the position of Datatables elements (length,filter,button,...)
142             // we use a fluid row on top and another on the bottom, making sure we take 12 grid elt's each time
143             sDom: "<'row-fluid'<'span5'l><'span1'r><'span6'f>>t<'row-fluid'<'span5'i><'span7'p>>",
144             sPaginationType: 'bootstrap',
145             // Handle the null values & the error : Datatables warning Requested unknown parameter
146             // http://datatables.net/forums/discussion/5331/datatables-warning-...-requested-unknown-parameter/p2
147             aoColumnDefs: [{sDefaultContent: '',aTargets: [ '_all' ]}],
148             // WARNING: this one causes tables in a 'tabs' that are not exposed at the time this is run to show up empty
149             // sScrollX: '100%',       /* Horizontal scrolling */
150             bProcessing: true,      /* Loading */
151             fnDrawCallback: function() { hazelnut_draw_callback.call(object, options); }
152         };
153         // the intention here is that options.datatables_options as coming from the python object take precedence
154         //  XXX DISABLED by jordan: was causing errors in datatables.js     $.extend(actual_options, options.datatables_options );
155         this.table = $('#hazelnut-' + options.plugin_uuid).dataTable(actual_options);
156
157         /* Setup the SelectAll button in the dataTable header */
158         /* xxx not sure this is still working */
159         var oSelectAll = $('#datatableSelectAll-'+ options.plugin_uuid);
160         oSelectAll.html("<span class='ui-icon ui-icon-check' style='float:right;display:inline-block;'></span>Select All");
161         oSelectAll.button();
162         oSelectAll.css('font-size','11px');
163         oSelectAll.css('float','right');
164         oSelectAll.css('margin-right','15px');
165         oSelectAll.css('margin-bottom','5px');
166         oSelectAll.unbind('click');
167         oSelectAll.click(selectAll);
168
169         /* Add a filtering function to the current table 
170          * Note: we use closure to get access to the 'options'
171          */
172         $.fn.dataTableExt.afnFiltering.push(function( oSettings, aData, iDataIndex ) { 
173             /* No filtering if the table does not match */
174             if (oSettings.nTable.id != "hazelnut-" + options.plugin_uuid)
175                 return true;
176             return hazelnut_filter.call(object, oSettings, aData, iDataIndex);
177         });
178
179         /* methods */
180
181 // DEPRECATED //        this.set_query = function(query) {
182 // DEPRECATED //            messages.info('hazelnut.set_query');
183 // DEPRECATED //            var options = this.options;
184 // DEPRECATED //            /* Compare current and advertised query to get added and removed fields */
185 // DEPRECATED //            previous_query = this.current_query;
186 // DEPRECATED //            /* Save the query as the current query */
187 // DEPRECATED //            this.current_query = query;
188 // DEPRECATED //            if (debug)
189 // DEPRECATED //                messages.debug("hazelnut.set_query, current_query is now -> " + this.current_query);
190 // DEPRECATED //
191 // DEPRECATED //            /* We check all necessary fields : in column editor I presume XXX */
192 // DEPRECATED //            // XXX ID naming has no plugin_uuid
193 // DEPRECATED //            if (typeof(query.fields) != 'undefined') {        
194 // DEPRECATED //                $.each (query.fields, function(index, value) { 
195 // DEPRECATED //                    if (!$('#hazelnut-checkbox-' + options.plugin_uuid + "-" + value).attr('checked'))
196 // DEPRECATED //                        $('#hazelnut-checkbox-' + options.plugin_uuid + "-" + value).attr('checked', true);
197 // DEPRECATED //                });
198 // DEPRECATED //            }
199 // DEPRECATED //
200 // DEPRECATED //            /* Process updates in filters / current_query must be updated before this call for filtering ! */
201 // DEPRECATED //            this.table.fnDraw();
202 // DEPRECATED //
203 // DEPRECATED //            /*
204 // DEPRECATED //             * Process updates in fields
205 // DEPRECATED //             */
206 // DEPRECATED //            if (typeof(query.fields) != 'undefined') {     
207 // DEPRECATED //                /* DataTable Settings */
208 // DEPRECATED //                var oSettings = this.table.dataTable().fnSettings();
209 // DEPRECATED //                var cols = oSettings.aoColumns;
210 // DEPRECATED //                var colnames = cols.map(function(x) {return x.sTitle});
211 // DEPRECATED //                colnames = $.grep(colnames, function(value) {return value != "+/-";});
212 // DEPRECATED //
213 // DEPRECATED //                if (previous_query == null) {
214 // DEPRECATED //                    var added_fields = query.fields;
215 // DEPRECATED //                    var removed_fields = colnames;            
216 // DEPRECATED //                    removed_fields = $.grep(colnames, function(x) { return $.inArray(x, added_fields) == -1});
217 // DEPRECATED //                } else {
218 // DEPRECATED //                    var tmp = previous_query.diff_fields(query);
219 // DEPRECATED //                    var added_fields = tmp.added;
220 // DEPRECATED //                    var removed_fields = tmp.removed;
221 // DEPRECATED //                }
222 // DEPRECATED //
223 // DEPRECATED //                /* Hide/unhide columns to match added/removed fields */
224 // DEPRECATED //                var object = this;
225 // DEPRECATED //                $.each(added_fields, function (index, field) {            
226 // DEPRECATED //                    var index = object.getColIndex(field,cols);
227 // DEPRECATED //                    if(index != -1)
228 // DEPRECATED //                        object.table.fnSetColumnVis(index, true);
229 // DEPRECATED //                });
230 // DEPRECATED //                $.each(removed_fields, function (index, field) {
231 // DEPRECATED //                    var index = object.getColIndex(field,cols);
232 // DEPRECATED //                    if(index != -1)
233 // DEPRECATED //                        object.table.fnSetColumnVis(index, false);
234 // DEPRECATED //                });            
235 // DEPRECATED //            }
236 // DEPRECATED //        }
237
238 // DEPRECATED //        this.set_resources = function(resources, instance) {
239 // DEPRECATED //            if (debug)
240 // DEPRECATED //                messages.debug("entering hazelnut.set_resources");
241 // DEPRECATED //            var options = this.options;
242 // DEPRECATED //            var previous_resources = this.current_resources;
243 // DEPRECATED //            this.current_resources = resources;
244 // DEPRECATED //    
245 // DEPRECATED //            /* We uncheck all checkboxes ... */
246 // DEPRECATED //            $('hazelnut-checkbox-' + options.plugin_uuid).attr('checked', false);
247 // DEPRECATED //            /* ... and check the ones specified in the resource list */
248 // DEPRECATED //            $.each(this.current_resources, function(index, urn) {
249 // DEPRECATED //                $('#hazelnut-checkbox-' + options.plugin_uuid + "-" + urn).attr('checked', true)
250 // DEPRECATED //            });
251 // DEPRECATED //            
252 // DEPRECATED //        }
253
254         /**
255          * @brief Determine index of key in the table columns 
256          * @param key
257          * @param cols
258          */
259         this.getColIndex = function(key, cols) {
260             var tabIndex = $.map(cols, function(x, i) { if (x.sTitle == key) return i; });
261             return (tabIndex.length > 0) ? tabIndex[0] : -1;
262         }
263     
264 // DEPRECATED //        /**
265 // DEPRECATED //         * @brief
266 // DEPRECATED //         * XXX will be removed/replaced
267 // DEPRECATED //         */
268 // DEPRECATED //        this.selected_changed = function(e, change) {
269 // DEPRECATED //        if (debug) messages.debug("entering hazelnut.selected_changed");
270 // DEPRECATED //            var actions = change.split("/");
271 // DEPRECATED //            if (actions.length > 1) {
272 // DEPRECATED //                var oNodes = this.table.fnGetNodes();
273 // DEPRECATED //                var myNode = $.grep(oNodes, function(value) {
274 // DEPRECATED //                    if (value.id == actions[1]) { return value; }
275 // DEPRECATED //                });                
276 // DEPRECATED //                if( myNode.length>0 ) {
277 // DEPRECATED //                    if ((actions[2]=="add" && actions[0]=="cancel") || actions[0]=="del")
278 // DEPRECATED //                        checked='';
279 // DEPRECATED //                    else
280 // DEPRECATED //                        checked="checked='checked' ";
281 // DEPRECATED //                    var newValue = this.checkbox(actions[1], 'node', checked, false);
282 // DEPRECATED //                    var columnPos = this.table.fnSettings().aoColumns.length - 1;
283 // DEPRECATED //                    this.table.fnUpdate(newValue, myNode[0], columnPos);
284 // DEPRECATED //                    this.table.fnDraw();
285 // DEPRECATED //                }
286 // DEPRECATED //            }
287 // DEPRECATED //        }
288     
289         this.update_plugin = function(e, rows) {
290             // e.data is what we passed in second argument to subscribe
291             // so here it is the jquery object attached to the plugin <div>
292             var $plugindiv=e.data;
293             if (debug)
294                 messages.debug("entering hazelnut.update_plugin on id '" + $plugindiv.attr('id') + "'");
295             // clear the spinning wheel: look up an ancestor that has the need-spin class
296             // do this before we might return
297             $plugindiv.closest('.need-spin').spin(false);
298
299             var options = this.options;
300             var hazelnut = this;
301     
302             /* if we get no result, or an error, try to make that clear, and exit */
303             if (rows.length==0) {
304                 if (debug) 
305                     messages.debug("Empty result on hazelnut " + this.options.domid);
306                 var placeholder=$(this.table).find("td.dataTables_empty");
307                 console.log("placeholder "+placeholder);
308                 if (placeholder.length==1) 
309                     placeholder.html(unfold.warning("Empty result"));
310                 else
311                     this.table.html(unfold.warning("Empty result"));
312                     return;
313             } else if (typeof(rows[0].error) != 'undefined') {
314                 // we now should have another means to report errors that this inline/embedded hack
315                 if (debug) 
316                     messages.error ("undefined result on " + this.options.domid + " - should not happen anymore");
317                 this.table.html(unfold.error(rows[0].error));
318                 return;
319             }
320
321             /* 
322              * fill the dataTables object
323              * we cannot set html content directly here, need to use fnAddData
324              */
325             var lines = new Array();
326     
327             this.current_resources = Array();
328     
329             $.each(rows, function(index, row) {
330                 // this models a line in dataTables, each element in the line describes a cell
331                 line = new Array();
332      
333                 // go through table headers to get column names we want
334                 // in order (we have temporarily hack some adjustments in names)
335                 var cols = object.table.fnSettings().aoColumns;
336                 var colnames = cols.map(function(x) {return x.sTitle})
337                 var nb_col = cols.length;
338                 /* if we've requested checkboxes, then forget about the checkbox column for now */
339                 if (options.checkboxes) nb_col -= 1;
340
341                 /* fill in stuff depending on the column name */
342                 for (var j = 0; j < nb_col; j++) {
343                     if (typeof colnames[j] == 'undefined') {
344                         line.push('...');
345                     } else if (colnames[j] == 'hostname') {
346                         if (row['type'] == 'resource,link')
347                             //TODO: we need to add source/destination for links
348                             line.push('');
349                         else
350                             line.push(row['hostname']);
351                     } else {
352                         if (row[colnames[j]])
353                             line.push(row[colnames[j]]);
354                         else
355                             line.push('');
356                     }
357                 }
358     
359                 /* catch up with the last column if checkboxes were requested */
360                 if (options.checkboxes) {
361                     var checked = '';
362                     // xxx problem is, we don't get this 'sliver' thing set apparently
363                     if (typeof(row['sliver']) != 'undefined') { /* It is equal to null when <sliver/> is present */
364                         checked = 'checked ';
365                         hazelnut.current_resources.push(row['urn']);
366                     }
367                     // Use a key instead of hostname (hard coded...)
368                     line.push(hazelnut.checkbox(options.plugin_uuid, row['urn'], row['type'], checked, false));
369                 }
370     
371                 lines.push(line);
372     
373             });
374     
375             this.table.fnClearTable();
376             if (debug)
377                 messages.debug("hazelnut.update_plugin: total of " + lines.length + " rows");
378             this.table.fnAddData(lines);
379         
380         };
381
382         this.checkbox = function (plugin_uuid, header, field, selected_str, disabled_str)
383         {
384             var result="";
385             // Prefix id with plugin_uuid
386             result += "<input";
387             result += " class='hazelnut-checkbox-" + plugin_uuid + "'";
388             result += " id='hazelnut-checkbox-" + plugin_uuid + "-" + unfold.get_value(header) + "'";
389             result += " name='" + unfold.get_value(field) + "'";
390             result += " type='checkbox'";
391             result += selected_str;
392             result += disabled_str;
393             result += " autocomplete='off'";
394             result += " value='" + unfold.get_value(header) + "'";
395             result += "></input>";
396             return result;
397         };
398
399         ////////////////////////////////////////////////////////////////////////
400         // New plugin API (in tests)
401
402         // TODO : signal empty/non empty table
403
404         this.new_record = function(record)
405         {
406             // this models a line in dataTables, each element in the line describes a cell
407             line = new Array();
408      
409             // go through table headers to get column names we want
410             // in order (we have temporarily hack some adjustments in names)
411             var cols = object.table.fnSettings().aoColumns;
412             var colnames = cols.map(function(x) {return x.sTitle})
413             var nb_col = cols.length;
414             /* if we've requested checkboxes, then forget about the checkbox column for now */
415             if (options.checkboxes) nb_col -= 1;
416
417             /* fill in stuff depending on the column name */
418             for (var j = 0; j < nb_col; j++) {
419                 if (typeof colnames[j] == 'undefined') {
420                     line.push('...');
421                 } else if (colnames[j] == 'hostname') {
422                     if (record['type'] == 'resource,link')
423                         //TODO: we need to add source/destination for links
424                         line.push('');
425                     else
426                         line.push(record['hostname']);
427                 } else {
428                     if (record[colnames[j]])
429                         line.push(record[colnames[j]]);
430                     else
431                         line.push('');
432                 }
433             }
434     
435             /* catch up with the last column if checkboxes were requested */
436             if (options.checkboxes) {
437                 var checked = '';
438                 // xxx problem is, we don't get this 'sliver' thing set apparently
439                 if (typeof(record['sliver']) != 'undefined') { /* It is equal to null when <sliver/> is present */
440                     checked = 'checked ';
441                     hazelnut.current_resources.push(record['urn']);
442                 }
443                 // Use a key instead of hostname (hard coded...)
444                 line.push(object.checkbox(options.plugin_uuid, record['urn'], record['type'], checked, false));
445             }
446     
447             // XXX Is adding an array of lines more efficient ?
448             this.table.fnAddData(line);
449
450         };
451
452         this.record_handler = function(e, event_type, record)
453         {
454             switch(event_type) {
455                 case NEW_RECORD:
456                     object.new_record(record);
457                     break;
458                 case CLEAR_RECORDS:
459                     object.table.fnClearTable();
460                     break;
461                 case IN_PROGRESS:
462                     manifold.spin($(this));
463                     break;
464                 case DONE:
465                     manifold.spin($(this), false);
466                     break;
467             }
468         };
469
470         this.query_handler = function(e, event_type, query)
471         {
472             // This replaces the complex set_query function
473             // The plugin does not need to remember the query anymore
474             switch(event_type) {
475                 // Filters
476                 case FILTER_ADDED:
477                 case FILTER_REMOVED:
478                 case CLEAR_FILTERS:
479                     // XXX Here we might need to maintain the list of filters !
480                     /* Process updates in filters / current_query must be updated before this call for filtering ! */
481                     object.table.fnDraw();
482                     break;
483
484                 // Fields
485                 /* Hide/unhide columns to match added/removed fields */
486                 case FIELD_ADDED:
487                     var object = this;
488                     $.each(added_fields, function (index, field) {            
489                         var index = object.getColIndex(field,cols);
490                         if(index != -1)
491                             object.table.fnSetColumnVis(index, true);
492                     });
493                     break;
494                 case FIELD_REMOVED:
495                     var object = this;
496                     $.each(removed_fields, function (index, field) {
497                         var index = object.getColIndex(field,cols);
498                         if(index != -1)
499                             object.table.fnSetColumnVis(index, false);
500                     });            
501                     break;
502                 case CLEAR_FIELDS:
503                     alert('Hazelnut::clear_fields() not implemented');
504                     break;
505             } // switch
506
507
508         }
509     } // constructor
510
511     /***************************************************************************
512      * Private methods
513      * xxx I'm not sure why this should not be methods in the Hazelnut class above
514      ***************************************************************************/
515
516     /** 
517      * @brief Hazelnut filtering function
518      */
519     function hazelnut_filter (oSettings, aData, iDataIndex) {
520         var cur_query = this.current_query;
521         if (!cur_query) return true;
522         var ret = true;
523
524         /* We have an array of filters : a filter is an array (key op val) 
525          * field names (unless shortcut)    : oSettings.aoColumns  = [ sTitle ]
526          *     can we exploit the data property somewhere ?
527          * field values (unless formatting) : aData
528          *     formatting should leave original data available in a hidden field
529          *
530          * The current line should validate all filters
531          */
532         $.each (cur_query.filters, function(index, filter) { 
533             /* XXX How to manage checkbox ? */
534             var key = filter[0]; 
535             var op = filter[1];
536             var value = filter[2];
537
538             /* Determine index of key in the table columns */
539             var col = $.map(oSettings.aoColumns, function(x, i) {if (x.sTitle == key) return i;})[0];
540
541             /* Unknown key: no filtering */
542             if (typeof(col) == 'undefined')
543                 return;
544
545             col_value=unfold.get_value(aData[col]);
546             /* Test whether current filter is compatible with the column */
547             if (op == '=' || op == '==') {
548                 if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
549                     ret = false;
550             }else if (op == '!=') {
551                 if ( col_value == value || col_value==null || col_value=="" || col_value=="n/a")
552                     ret = false;
553             } else if(op=='<') {
554                 if ( parseFloat(col_value) >= value || col_value==null || col_value=="" || col_value=="n/a")
555                     ret = false;
556             } else if(op=='>') {
557                 if ( parseFloat(col_value) <= value || col_value==null || col_value=="" || col_value=="n/a")
558                     ret = false;
559             } else if(op=='<=' || op=='≤') {
560                 if ( parseFloat(col_value) > value || col_value==null || col_value=="" || col_value=="n/a")
561                     ret = false;
562             } else if(op=='>=' || op=='≥') {
563                 if ( parseFloat(col_value) < value || col_value==null || col_value=="" || col_value=="n/a")
564                     ret = false;
565             }else{
566                 // How to break out of a loop ?
567                 alert("filter not supported");
568                 return false;
569             }
570
571         });
572         return ret;
573     }
574
575     function hazelnut_draw_callback() {
576         var options = this.options;
577         /* 
578          * Handle clicks on checkboxes: reassociate checkbox click every time
579          * the table is redrawn 
580          */
581         $('.hazelnut-checkbox-' + options.plugin_uuid).unbind('click');
582         $('.hazelnut-checkbox-' + options.plugin_uuid).click({instance: this}, check_click);
583
584         if (!this.table)
585             return;
586
587         /* Remove pagination if we show only a few results */
588         var wrapper = this.table; //.parent().parent().parent();
589         var rowsPerPage = this.table.fnSettings()._iDisplayLength;
590         var rowsToShow = this.table.fnSettings().fnRecordsDisplay();
591         var minRowsPerPage = this.table.fnSettings().aLengthMenu[0];
592
593         if ( rowsToShow <= rowsPerPage || rowsPerPage == -1 ) {
594             $('.hazelnut_paginate', wrapper).css('visibility', 'hidden');
595         } else {
596             $('.hazelnut_paginate', wrapper).css('visibility', 'visible');
597         }
598
599         if ( rowsToShow <= minRowsPerPage ) {
600             $('.hazelnut_length', wrapper).css('visibility', 'hidden');
601         } else {
602             $('.hazelnut_length', wrapper).css('visibility', 'visible');
603         }
604     }
605
606     function check_click (e) {
607
608         var object = e.data.instance;
609
610         /* The key of the object to be added */
611         // XXX What about multiple keys ?
612         var value = this.value; 
613
614         // NEW PLUGIN API
615         manifold.raise_event(object.options.query_uuid, this.checked?SET_ADD:SET_REMOVED, value);
616         
617         // OLD PLUGIN API BELOW
618
619         if (this.checked) {
620             object.current_resources.push(value);
621         } else {
622             tmp = $.grep(object.current_resources, function(x) { return x != value; });
623             object.current_resources = tmp;
624         }
625
626         /* inform slice that our selected resources have changed */
627         $.publish('/update-set/' + object.options.query_uuid, [object.current_resources, true]);
628
629     }
630
631     function selectAll() {
632         // requires jQuery id
633         var uuid=this.id.split("-");
634         var oTable=$("#hazelnut-"+uuid[1]).dataTable();
635         // Function available in Hazelnut 1.9.x
636         // Filter : displayed data only
637         var filterData = oTable._('tr', {"filter":"applied"});   
638         /* TODO: WARNING if too many nodes selected, use filters to reduce nuber of nodes */        
639         if(filterData.length<=100){
640             $.each(filterData, function(index, obj) {
641                 var last=$(obj).last();
642                 var key_value=unfold.get_value(last[0]);
643                 if(typeof($(last[0]).attr('checked'))=="undefined"){
644                     $.publish('selected', 'add/'+key_value);
645                 }
646             });
647         }
648     }
649     
650 })( jQuery );