- var $plugindiv=e.data;
- console.log("incoming plugindiv= " + $plugindiv);
- // clear the spinning wheel: look up an ancestor that has the need-spin class
- // do this before we might return
- $plugindiv.closest('.need-spin').spin(false);
+ // e.data is what we passed in second argument to subscribe
+ // so here it is the jquery object attached to the plugin
+ var $plugindiv=e.data;
+ if (debug)
+ messages.debug("entering hazelnut.update_plugin on id '" + $plugindiv.attr('id') + "'");
+ // clear the spinning wheel: look up an ancestor that has the need-spin class
+ // do this before we might return
+ $plugindiv.closest('.need-spin').spin(false);
var options = this.options;
var hazelnut = this;
+ /* if we get no result, or an error, try to make that clear, and exit */
if (rows.length==0) {
- this.table.html(errorDisplay("No Result"));
- return;
- } else {
- if (typeof(rows[0].error) != 'undefined') {
- this.table.html(errorDisplay(rows[0].error));
+ if (debug)
+ messages.debug("Empty result on hazelnut " + this.options.domid);
+ var placeholder=$(this.table).find("td.dataTables_empty");
+ console.log("placeholder "+placeholder);
+ if (placeholder.length==1)
+ placeholder.html(unfold.warning("Empty result"));
+ else
+ this.table.html(unfold.warning("Empty result"));
return;
- }
+ } else if (typeof(rows[0].error) != 'undefined') {
+ // we now should have another means to report errors that this inline/embedded hack
+ if (debug)
+ messages.error ("undefined result on " + this.options.domid + " - should not happen anymore");
+ this.table.html(unfold.error(rows[0].error));
+ return;
}
- newlines = new Array();
+
+ /*
+ * fill the dataTables object
+ * we cannot set html content directly here, need to use fnAddData
+ */
+ var lines = new Array();
this.current_resources = Array();
- $.each(rows, function(index, obj) {
- newline = new Array();
-
+ $.each(rows, function(index, row) {
+ // this models a line in dataTables, each element in the line describes a cell
+ line = new Array();
+
// go through table headers to get column names we want
// in order (we have temporarily hack some adjustments in names)
- var cols = hazelnut.table.fnSettings().aoColumns;
+ var cols = object.table.fnSettings().aoColumns;
var colnames = cols.map(function(x) {return x.sTitle})
- var nb_col = colnames.length;
- if (options.checkboxes)
- nb_col -= 1;
+ var nb_col = cols.length;
+ /* if we've requested checkboxes, then forget about the checkbox column for now */
+ if (options.checkboxes) nb_col -= 1;
+
+ /* fill in stuff depending on the column name */
for (var j = 0; j < nb_col; j++) {
if (typeof colnames[j] == 'undefined') {
- newline.push('...');
+ line.push('...');
} else if (colnames[j] == 'hostname') {
- if (obj['type'] == 'resource,link')
+ if (row['type'] == 'resource,link')
//TODO: we need to add source/destination for links
- newline.push('');
+ line.push('');
else
- newline.push(obj['hostname']);
+ line.push(row['hostname']);
} else {
- if (obj[colnames[j]])
- newline.push(obj[colnames[j]]);
+ if (row[colnames[j]])
+ line.push(row[colnames[j]]);
else
- newline.push('');
+ line.push('');
}
}
+ /* catch up with the last column if checkboxes were requested */
if (options.checkboxes) {
var checked = '';
- if (typeof(obj['sliver']) != 'undefined') { /* It is equal to null when is present */
+ // xxx problem is, we don't get this 'sliver' thing set apparently
+ if (typeof(row['sliver']) != 'undefined') { /* It is equal to null when is present */
checked = 'checked ';
- hazelnut.current_resources.push(obj['urn']);
+ hazelnut.current_resources.push(row[ELEMENT_KEY]);
}
// Use a key instead of hostname (hard coded...)
- newline.push(hazelnut.checkbox(options.plugin_uuid, obj['urn'], obj['type'], checked, false));
+ line.push(hazelnut.checkbox(options.plugin_uuid, row[ELEMENT_KEY], row['type'], checked, false));
}
- newlines.push(newline);
-
+ lines.push(line);
});
-// console.log ("end of each, newlines=" + newlines.length);
- this.table.fnAddData(newlines);
+ this.table.fnClearTable();
+ if (debug)
+ messages.debug("hazelnut.update_plugin: total of " + lines.length + " rows");
+ this.table.fnAddData(lines);
+
+ };
+
+ this.checkbox = function (plugin_uuid, header, field, selected_str, disabled_str)
+ {
+ var result="";
+ if (header === null)
+ header = '';
+ // Prefix id with plugin_uuid
+ result += "";
+ return result;
+ };
+
+ ////////////////////////////////////////////////////////////////////////
+ // New plugin API (in tests)
+
+ // TODO : signal empty/non empty table
+
+ this.new_record = function(record)
+ {
+ // this models a line in dataTables, each element in the line describes a cell
+ line = new Array();
+
+ // go through table headers to get column names we want
+ // in order (we have temporarily hack some adjustments in names)
+ var cols = object.table.fnSettings().aoColumns;
+ var colnames = cols.map(function(x) {return x.sTitle})
+ var nb_col = cols.length;
+ /* if we've requested checkboxes, then forget about the checkbox column for now */
+ if (options.checkboxes) nb_col -= 1;
+
+ /* fill in stuff depending on the column name */
+ for (var j = 0; j < nb_col; j++) {
+ if (typeof colnames[j] == 'undefined') {
+ line.push('...');
+ } else if (colnames[j] == 'hostname') {
+ if (record['type'] == 'resource,link')
+ //TODO: we need to add source/destination for links
+ line.push('');
+ else
+ line.push(record['hostname']);
+ } else {
+ if (record[colnames[j]])
+ line.push(record[colnames[j]]);
+ else
+ line.push('');
+ }
+ }
-// console.log (" exiting update_plugin = ");
+ /* catch up with the last column if checkboxes were requested */
+ if (options.checkboxes) {
+ var checked = '';
+ // xxx problem is, we don't get this 'sliver' thing set apparently
+ if (typeof(record['sliver']) != 'undefined') { /* It is equal to null when is present */
+ checked = 'checked ';
+ hazelnut.current_resources.push(record[ELEMENT_KEY]);
+ }
+ // Use a key instead of hostname (hard coded...)
+ line.push(object.checkbox(options.plugin_uuid, record[ELEMENT_KEY], record['type'], checked, false));
+ }
+
+ // XXX Is adding an array of lines more efficient ?
+ this.table.fnAddData(line);
+
};
- this.checkbox = function (plugin_uuid, header, field, selected_str, disabled_str) {
- var result="";
- /* Prefix id with plugin_uuid */
- result += "";
- return result;
+ 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:
+ object.table.fnClearTable();
+ break;
+ case IN_PROGRESS:
+ manifold.spin($(this));
+ break;
+ case DONE:
+ 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 replaces the complex set_query function
+ // The plugin does not need to remember the query anymore
+ switch(event_type) {
+ // Filters
+ case FILTER_ADDED:
+ case FILTER_REMOVED:
+ case CLEAR_FILTERS:
+ // XXX Here we might need to maintain the list of filters !
+ /* Process updates in filters / current_query must be updated before this call for filtering ! */
+ object.table.fnDraw();
+ break;
+
+ // 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);
+ });
+ 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);
+ });
+ break;
+ case CLEAR_FIELDS:
+ alert('Hazelnut::clear_fields() not implemented');
+ break;
+ } // switch
+
+
+ }
} // constructor
/***************************************************************************
* Private methods
+ * xxx I'm not sure why this should not be methods in the Hazelnut class above
***************************************************************************/
/**
@@ -357,6 +578,7 @@
*/
function hazelnut_filter (oSettings, aData, iDataIndex) {
var cur_query = this.current_query;
+ if (!cur_query) return true;
var ret = true;
/* We have an array of filters : a filter is an array (key op val)
@@ -367,7 +589,7 @@
*
* The current line should validate all filters
*/
- $.each (cur_query.filter, function(index, filter) {
+ $.each (cur_query.filters, function(index, filter) {
/* XXX How to manage checkbox ? */
var key = filter[0];
var op = filter[1];
@@ -380,7 +602,7 @@
if (typeof(col) == 'undefined')
return;
- col_value=get_value(aData[col]);
+ col_value=unfold.get_value(aData[col]);
/* Test whether current filter is compatible with the column */
if (op == '=' || op == '==') {
if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
@@ -442,8 +664,17 @@
}
function check_click (e) {
+
var object = e.data.instance;
- var value = this.value;
+
+ /* The key of the object to be added */
+ // XXX What about multiple keys ?
+ var value = this.value;
+
+ // NEW PLUGIN API
+ manifold.raise_event(object.options.query_uuid, this.checked?SET_ADD:SET_REMOVED, value);
+
+ // OLD PLUGIN API BELOW
if (this.checked) {
object.current_resources.push(value);
@@ -468,7 +699,7 @@
if(filterData.length<=100){
$.each(filterData, function(index, obj) {
var last=$(obj).last();
- var key_value=get_value(last[0]);
+ var key_value=unfold.get_value(last[0]);
if(typeof($(last[0]).attr('checked'))=="undefined"){
$.publish('selected', 'add/'+key_value);
}