2 * Description: display a query result in a datatables-powered <table>
3 * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
12 var QueryTable = Plugin.extend({
14 init: function(options, element) {
15 this._super(options, element);
17 /* Member variables */
18 // in general we expect 2 queries here
19 // query_uuid refers to a single object (typically a slice)
20 // query_all_uuid refers to a list (typically resources or users)
21 // these can return in any order so we keep track of which has been received yet
22 this.received_all_query = false;
23 this.received_query = false;
25 // // We need to remember the active filter for datatables filtering
26 // this.filters = Array();
28 // an internal buffer for records that are 'in' and thus need to be checked
29 this.buffered_records_to_check = [];
32 // this.$element.on('show.Datatables', this.on_show);
33 this.elmt().on('show', this, this.on_show);
34 // Unbind all events using namespacing
36 // $(window).unbind('QueryTable');
38 var query = manifold.query_store.find_analyzed_query(this.options.query_uuid);
39 this.method = query.object;
41 // xxx beware that this.key needs to contain a key that all records will have
42 // in general query_all will return well populated records, but query
43 // returns records with only the fields displayed on startup.
44 this.key = (this.options.id_key);
46 // if not specified by caller, decide from metadata
47 var keys = manifold.metadata.get_key(this.method);
48 this.key = (keys && keys.length == 1) ? keys[0] : null;
50 if (! this.key) messages.warning("querytable.init could not kind valid key");
52 if (debug) messages.debug("querytable: key="+this.key);
54 /* Setup query and record handlers */
55 this.listen_query(options.query_uuid);
56 this.listen_query(options.query_all_uuid, 'all');
58 /* GUI setup and event binding */
59 this.initialize_table();
64 on_show: function(e) {
66 self.table.fnAdjustColumnSizing()
71 /* GUI MANIPULATION */
73 initialize_table: function() {
74 // compute columns based on columns and hidden_columns
75 this.slick_columns = [];
76 var all_columns = this.options.columns; // .concat(this.options.hidden_columns)
77 for (c in all_columns) {
78 var column=all_columns[c];
79 this.slick_columns.push ( {id:column, name:column, field:column });
82 // xxx should be extensible from caller with this.options.slickgrid_options
83 this.slick_options = {
84 enableCellNavigation: false,
85 enableColumnReorder: true,
90 var selector="#grid-"+this.options.domid;
92 messages.debug("slick grid selector is " + selector);
93 for (c in this.slick_columns) {
94 var col=this.slick_columns[c];
96 for (k in col) msg = msg+" col["+k+"]="+col[k];
97 messages.debug("slick_column["+c+"]:"+msg);
100 this.slick_grid = new Slick.Grid(selector, this.slick_data, this.slick_columns, this.slick_options);
102 }, // initialize_table
104 // Determine index of key in the table columns
105 getColIndex: function(key, cols) {
106 var tabIndex = $.map(cols, function(x, i) { if (x.sTitle == key) return i; });
107 return (tabIndex.length > 0) ? tabIndex[0] : -1;
110 checkbox_html : function (key, value) {
111 // if (debug) messages.debug("checkbox_html, value="+value);
113 // Prefix id with plugin_uuid
115 result += " class='querytable-checkbox'";
116 result += " id='" + this.flat_id(this.id('checkbox', value)) + "'";
117 result += " name='" + key + "'";
118 result += " type='checkbox'";
119 result += " autocomplete='off'";
120 if (value === undefined) {
121 messages.warning("querytable.checkbox_html - undefined value");
123 result += " value='" + value + "'";
125 result += "></input>";
129 new_record: function(record) {
130 this.slick_data.push(record);
133 clear_table: function() {
134 console.log("clear_table not implemented");
137 redraw_table: function() {
141 show_column: function(field) {
142 var oSettings = this.table.fnSettings();
143 var cols = oSettings.aoColumns;
144 var index = this.getColIndex(field,cols);
146 this.table.fnSetColumnVis(index, true);
149 hide_column: function(field) {
150 console.log("hide_column not implemented - field="+field);
153 set_checkbox: function(record, checked) {
154 console.log("set_checkbox not implemented");
156 /* Default: checked = true */
157 if (checked === undefined) checked = true;
160 /* The function accepts both records and their key */
161 switch (manifold.get_type(record)) {
166 /* XXX Test the key before ? */
167 id = record[this.key];
170 throw "Not implemented";
175 if (id === undefined) {
176 messages.warning("querytable.set_checkbox record has no id to figure which line to tick");
179 var checkbox_id = this.flat_id(this.id('checkbox', id));
180 // function escape_id(myid) is defined in portal/static/js/common.functions.js
181 checkbox_id = escape_id(checkbox_id);
182 // using dataTables's $ to search also in nodes that are not currently displayed
183 var element = this.table.$(checkbox_id);
185 messages.debug("set_checkbox checked=" + checked
186 + " id=" + checkbox_id + " matches=" + element.length);
187 element.attr('checked', checked);
190 /*************************** QUERY HANDLER ****************************/
192 on_filter_added: function(filter) {
193 this.filters.push(filter);
197 on_filter_removed: function(filter) {
198 // Remove corresponding filters
199 this.filters = $.grep(this.filters, function(x) {
205 on_filter_clear: function() {
209 on_field_added: function(field) {
210 this.show_column(field);
213 on_field_removed: function(field) {
214 this.hide_column(field);
217 on_field_clear: function() {
218 alert('QueryTable::clear_fields() not implemented');
221 /* XXX TODO: make this generic a plugin has to subscribe to a set of Queries to avoid duplicated code ! */
222 /*************************** ALL QUERY HANDLER ****************************/
224 on_all_filter_added: function(filter) {
229 on_all_filter_removed: function(filter) {
234 on_all_filter_clear: function() {
239 on_all_field_added: function(field) {
240 this.show_column(field);
243 on_all_field_removed: function(field) {
244 this.hide_column(field);
247 on_all_field_clear: function() {
248 alert('QueryTable::clear_fields() not implemented');
252 /*************************** RECORD HANDLER ***************************/
254 on_new_record: function(record) {
255 if (this.received_all_query) {
256 // if the 'all' query has been dealt with already we may turn on the checkbox
257 this.set_checkbox(record, true);
259 // otherwise we need to remember that and do it later on
260 if (debug) messages.debug("Remembering record to check " + record[this.key]);
261 this.buffered_records_to_check.push(record);
265 on_clear_records: function() {
268 // Could be the default in parent
269 on_query_in_progress: function() {
273 on_query_done: function() {
274 this.received_query = true;
275 // unspin once we have received both
276 if (this.received_all_query && this.received_query) this.unspin();
279 on_field_state_changed: function(data) {
280 switch(data.request) {
281 case FIELD_REQUEST_ADD:
282 case FIELD_REQUEST_ADD_RESET:
283 this.set_checkbox(data.value, true);
285 case FIELD_REQUEST_REMOVE:
286 case FIELD_REQUEST_REMOVE_RESET:
287 this.set_checkbox(data.value, false);
294 /* XXX TODO: make this generic a plugin has to subscribe to a set of Queries to avoid duplicated code ! */
296 on_all_field_state_changed: function(data) {
297 switch(data.request) {
298 case FIELD_REQUEST_ADD:
299 case FIELD_REQUEST_ADD_RESET:
300 this.set_checkbox(data.value, true);
302 case FIELD_REQUEST_REMOVE:
303 case FIELD_REQUEST_REMOVE_RESET:
304 this.set_checkbox(data.value, false);
311 on_all_new_record: function(record) {
312 this.new_record(record);
315 on_all_clear_records: function() {
320 on_all_query_in_progress: function() {
323 }, // on_all_query_in_progress
325 on_all_query_done: function() {
326 if (debug) messages.debug("1-shot initializing dataTables content with " + this.slick_data.length + " lines");
327 this.slick_grid.setData (this.slick_data, true);
329 for (k in this.slick_data[0]) messages.debug("slick_data[0]["+k+"]="+this.slick_data[0][k]);
330 this.slick_grid.render();
333 // if we've already received the slice query, we have not been able to set
334 // checkboxes on the fly at that time (dom not yet created)
335 $.each(this.buffered_records_to_check, function(i, record) {
336 if (debug) messages.debug ("delayed turning on checkbox " + i + " record= " + record);
337 self.set_checkbox(record, true);
339 this.buffered_records_to_check = [];
341 this.received_all_query = true;
342 // unspin once we have received both
343 if (this.received_all_query && this.received_query) this.unspin();
345 }, // on_all_query_done
347 /************************** PRIVATE METHODS ***************************/
350 * @brief QueryTable filtering function
352 _querytable_filter: function(oSettings, aData, iDataIndex) {
354 $.each (this.filters, function(index, filter) {
355 /* XXX How to manage checkbox ? */
358 var value = filter[2];
360 /* Determine index of key in the table columns */
361 var col = $.map(oSettings.aoColumns, function(x, i) {if (x.sTitle == key) return i;})[0];
363 /* Unknown key: no filtering */
364 if (typeof(col) == 'undefined')
367 col_value=unfold.get_value(aData[col]);
368 /* Test whether current filter is compatible with the column */
369 if (op == '=' || op == '==') {
370 if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
372 }else if (op == '!=') {
373 if ( col_value == value || col_value==null || col_value=="" || col_value=="n/a")
376 if ( parseFloat(col_value) >= value || col_value==null || col_value=="" || col_value=="n/a")
379 if ( parseFloat(col_value) <= value || col_value==null || col_value=="" || col_value=="n/a")
381 } else if(op=='<=' || op=='≤') {
382 if ( parseFloat(col_value) > value || col_value==null || col_value=="" || col_value=="n/a")
384 } else if(op=='>=' || op=='≥') {
385 if ( parseFloat(col_value) < value || col_value==null || col_value=="" || col_value=="n/a")
388 // How to break out of a loop ?
389 alert("filter not supported");
397 _querytable_draw_callback: function() {
399 * Handle clicks on checkboxes: reassociate checkbox click every time
400 * the table is redrawn
402 this.elts('querytable-checkbox').unbind('click').click(this, this._check_click);
407 /* Remove pagination if we show only a few results */
408 var wrapper = this.table; //.parent().parent().parent();
409 var rowsPerPage = this.table.fnSettings()._iDisplayLength;
410 var rowsToShow = this.table.fnSettings().fnRecordsDisplay();
411 var minRowsPerPage = this.table.fnSettings().aLengthMenu[0];
413 if ( rowsToShow <= rowsPerPage || rowsPerPage == -1 ) {
414 $('.querytable_paginate', wrapper).css('visibility', 'hidden');
416 $('.querytable_paginate', wrapper).css('visibility', 'visible');
419 if ( rowsToShow <= minRowsPerPage ) {
420 $('.querytable_length', wrapper).css('visibility', 'hidden');
422 $('.querytable_length', wrapper).css('visibility', 'visible');
426 _check_click: function(e) {
431 // XXX this.value = key of object to be added... what about multiple keys ?
432 if (debug) messages.debug("querytable click handler checked=" + this.checked + " hrn=" + this.value);
433 manifold.raise_event(self.options.query_uuid, this.checked?SET_ADD:SET_REMOVED, this.value);
434 //return false; // prevent checkbox to be checked, waiting response from manifold plugin api
438 _selectAll: function() {
439 // requires jQuery id
440 var uuid=this.id.split("-");
441 var oTable=$("#querytable-"+uuid[1]).dataTable();
442 // Function available in QueryTable 1.9.x
443 // Filter : displayed data only
444 var filterData = oTable._('tr', {"filter":"applied"});
445 /* TODO: WARNING if too many nodes selected, use filters to reduce nuber of nodes */
446 if(filterData.length<=100){
447 $.each(filterData, function(index, obj) {
448 var last=$(obj).last();
449 var key_value=unfold.get_value(last[0]);
450 if(typeof($(last[0]).attr('checked'))=="undefined"){
451 $.publish('selected', 'add/'+key_value);
459 $.plugin('QueryTable', QueryTable);
461 // /* define the 'dom-checkbox' type for sorting in datatables
462 // http://datatables.net/examples/plug-ins/dom_sort.html
463 // using trial and error I found that the actual column number
464 // was in fact given as a third argument, and not second
465 // as the various online resources had it - go figure */
466 // $.fn.dataTableExt.afnSortData['dom-checkbox'] = function ( oSettings, _, iColumn ) {
467 // return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
468 // return result=$('td:eq('+iColumn+') input', tr).prop('checked') ? '1' : '0';