2 * Description: display a query result in a slickgrid-powered <table>
3 * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
10 * This is very rough for now and not deemed working
12 * Also it still requires adaptation for the init_key / init_id / canonical_key / id business
13 * if the basic logic was to become usable
18 /* ongoing adaptation to slickgrid
20 . checkboxes really running properly
21 . ability to sort on columns (should be straightforward
22 IIRC this got broken when moving to dataview, see dataview doc
23 . ability to sort on the checkboxes column
24 (e.g. have resources 'in' the slice show up first)
25 not quite clear how to do this
29 . rendering in the sliceview - does not use up all space,
30 this is different from the behaviour with simpleview
40 var QueryGrid = Plugin.extend({
42 init: function(options, element) {
43 this._super(options, element);
45 /* Member variables */
46 // in general we expect 2 queries here
47 // query_uuid refers to a single object (typically a slice)
48 // query_all_uuid refers to a list (typically resources or users)
49 // these can return in any order so we keep track of which has been received yet
50 this.received_all_query = false;
51 this.received_query = false;
53 // // We need to remember the active filter for filtering
54 // this.filters = Array();
56 // an internal buffer for records that are 'in' and thus need to be checked
57 this.buffered_records_to_check = [];
60 this.elmt().on('show', this, this.on_show);
62 var query = manifold.query_store.find_analyzed_query(this.options.query_uuid);
63 this.method = query.object;
65 // xxx beware that this.key needs to contain a key that all records will have
66 // in general query_all will return well populated records, but query
67 // returns records with only the fields displayed on startup.
68 this.key = (this.options.id_key);
70 // if not specified by caller, decide from metadata
71 var keys = manifold.metadata.get_key(this.method);
72 this.key = (keys && keys.length == 1) ? keys[0] : null;
74 if (! this.key) messages.warning("querygrid.init could not kind valid key");
76 if (debug) messages.debug("querygrid: key="+this.key);
78 /* Setup query and record handlers */
79 this.listen_query(options.query_uuid);
80 this.listen_query(options.query_all_uuid, 'all');
82 /* GUI setup and event binding */
83 this.initialize_table();
88 on_show: function(e) {
95 /* GUI MANIPULATION */
97 initialize_table: function() {
98 // compute columns based on columns and hidden_columns
99 this.slick_columns = [];
100 var all_columns = this.options.columns; // .concat(this.options.hidden_columns)
101 // xxx would be helpful to support a column_renamings options arg
102 // for redefining some labels like 'network_hrn' that really are not meaningful
103 for (c in all_columns) {
104 var column=all_columns[c];
105 this.slick_columns.push ( {id:column, name:column, field:column,
106 cssClass: "querygrid-column-"+column,
107 width:100, minWidth:40, });
109 var checkbox_selector = new Slick.CheckboxSelectColumn({
110 cssClass: "slick-checkbox"
112 this.slick_columns.push(checkbox_selector.getColumnDefinition());
114 // xxx should be extensible from caller with this.options.slickgrid_options
115 this.slick_options = {
116 enableCellNavigation: false,
117 enableColumnReorder: true,
119 syncColumnCellResize: true,
122 this.slick_data = [];
123 this.slick_dataview = new Slick.Data.DataView();
125 this.slick_dataview.onRowCountChanged.subscribe ( function (e,args) {
126 self.slick_grid.updateRowCount();
127 self.slick_grid.autosizeColumns();
128 self.slick_grid.render();
131 var selector="#grid-"+this.options.domid;
133 messages.debug("slick grid selector is " + selector);
134 for (c in this.slick_columns) {
135 var col=this.slick_columns[c];
137 for (k in col) msg = msg+" col["+k+"]="+col[k];
138 messages.debug("slick_column["+c+"]:"+msg);
142 this.slick_grid = new Slick.Grid(selector, this.slick_dataview, this.slick_columns, this.slick_options);
143 this.slick_grid.setSelectionModel (new Slick.RowSelectionModel ({selectActiveRow: false}));
144 this.slick_grid.registerPlugin (checkbox_selector);
145 // autotooltips: for showing the full column name when ellipsed
146 var auto_tooltips = new Slick.AutoTooltips ({ enableForHeaderCells: true });
147 this.slick_grid.registerPlugin (auto_tooltips);
149 this.columnpicker = new Slick.Controls.ColumnPicker (this.slick_columns, this.slick_grid, this.slick_options)
153 }, // initialize_table
155 new_record: function(record) {
156 this.slick_data.push(record);
159 clear_table: function() {
161 this.slick_dataview.setItems(this.slick_data,this.key);
164 redraw_table: function() {
165 this.slick_grid.autosizeColumns();
166 this.slick_grid.render();
169 show_column: function(field) {
170 console.log ("querygrid.show_column not yet implemented with slickgrid - field="+field);
173 hide_column: function(field) {
174 console.log("querygrid.hide_column not implemented with slickgrid - field="+field);
177 /*************************** QUERY HANDLER ****************************/
179 on_filter_added: function(filter) {
180 this.filters.push(filter);
184 on_filter_removed: function(filter) {
185 // Remove corresponding filters
186 this.filters = $.grep(this.filters, function(x) {
192 on_filter_clear: function() {
196 on_field_added: function(field) {
197 this.show_column(field);
200 on_field_removed: function(field) {
201 this.hide_column(field);
204 on_field_clear: function() {
205 alert('QueryGrid::clear_fields() not implemented');
208 /* XXX TODO: make this generic a plugin has to subscribe to a set of Queries to avoid duplicated code ! */
209 /*************************** ALL QUERY HANDLER ****************************/
211 on_all_filter_added: function(filter) {
216 on_all_filter_removed: function(filter) {
221 on_all_filter_clear: function() {
226 on_all_field_added: function(field) {
227 this.show_column(field);
230 on_all_field_removed: function(field) {
231 this.hide_column(field);
234 on_all_field_clear: function() {
235 alert('QueryGrid::clear_fields() not implemented');
239 /*************************** RECORD HANDLER ***************************/
241 on_new_record: function(record) {
242 if (this.received_all_query) {
243 // if the 'all' query has been dealt with already we may turn on the checkbox
244 this._set_checkbox(record, true);
246 // otherwise we need to remember that and do it later on
247 if (debug) messages.debug("Remembering record to check " + record[this.key]);
248 this.buffered_records_to_check.push(record);
252 on_clear_records: function() {
255 // Could be the default in parent
256 on_query_in_progress: function() {
260 on_query_done: function() {
261 this.received_query = true;
262 // unspin once we have received both
263 if (this.received_all_query && this.received_query) {
264 this._init_checkboxes();
269 on_field_state_changed: function(data) {
270 switch(data.request) {
271 case FIELD_REQUEST_ADD:
272 case FIELD_REQUEST_ADD_RESET:
273 this._set_checkbox(data.value, true);
275 case FIELD_REQUEST_REMOVE:
276 case FIELD_REQUEST_REMOVE_RESET:
277 this._set_checkbox(data.value, false);
284 /* XXX TODO: make this generic a plugin has to subscribe to a set of Queries to avoid duplicated code ! */
286 on_all_field_state_changed: function(data) {
287 switch(data.request) {
288 case FIELD_REQUEST_ADD:
289 case FIELD_REQUEST_ADD_RESET:
290 this._set_checkbox(data.value, true);
292 case FIELD_REQUEST_REMOVE:
293 case FIELD_REQUEST_REMOVE_RESET:
294 this._set_checkbox(data.value, false);
301 on_all_new_record: function(record) {
302 this.new_record(record);
305 on_all_clear_records: function() {
310 on_all_query_in_progress: function() {
313 }, // on_all_query_in_progress
315 on_all_query_done: function() {
316 var start=new Date();
317 if (debug) messages.debug("1-shot initializing slickgrid content with " + this.slick_data.length + " lines");
318 // use this.key as the key for identifying rows
319 this.slick_dataview.setItems (this.slick_data, this.key);
320 var duration=new Date()-start;
321 if (debug) messages.debug("setItems " + duration + " ms");
323 // show full contents of first row app
324 for (k in this.slick_data[0]) messages.debug("slick_data[0]["+k+"]="+this.slick_data[0][k]);
328 // if we've already received the slice query, we have not been able to set
329 // checkboxes on the fly at that time (dom not yet created)
330 $.each(this.buffered_records_to_check, function(i, record) {
331 if (debug) messages.debug ("delayed turning on checkbox " + i + " record= " + record);
332 self._set_checkbox(record, true);
334 this.buffered_records_to_check = [];
336 this.received_all_query = true;
337 // unspin once we have received both
338 if (this.received_all_query && this.received_query) {
339 this._init_checkboxes();
343 }, // on_all_query_done
345 /************************** PRIVATE METHODS ***************************/
347 _set_checkbox: function(record, checked) {
348 /* Default: checked = true */
349 if (checked === undefined) checked = true;
352 /* The function accepts both records and their key */
353 switch (manifold.get_type(record)) {
358 /* XXX Test the key before ? */
359 id = record[this.key];
362 throw "Not implemented";
367 if (id === undefined) {
368 messages.warning("querygrid._set_checkbox record has no id to figure which line to tick");
371 var index = this.slick_dataview.getIdxById(id);
372 var selectedRows=this.slick_grid.getSelectedRows();
373 if (checked) // add index in current list
374 selectedRows=selectedRows.concat(index);
375 else // remove index from current list
376 selectedRows=selectedRows.filter(function(idx) {return idx!=index;});
377 this.slick_grid.setSelectedRows(selectedRows);
380 // initializing checkboxes
381 // have tried 2 approaches, but none seems to work as we need it
382 // issue summarized in here
383 // http://stackoverflow.com/questions/20425193/slickgrid-selection-changed-callback-how-to-tell-between-manual-and-programmat
384 // arm the click callback on checkboxes
385 _init_checkboxes_manual : function () {
386 // xxx looks like checkboxes can only be the last column??
387 var checkbox_col = this.slick_grid.getColumns().length-1; // -1 +1 =0
388 console.log ("checkbox_col="+checkbox_col);
390 console.log ("HERE 1 with "+this.slick_dataview.getLength()+" sons");
391 for (var index=0; index < this.slick_dataview.getLength(); index++) {
392 // retrieve key (i.e. hrn) for this line
393 var key=this.slick_dataview.getItem(index)[this.key];
394 // locate cell <div> for the checkbox
395 var div=this.slick_grid.getCellNode(index,checkbox_col);
396 if (index <=30) console.log("HERE2 div",div," index="+index+" col="+checkbox_col);
397 // arm callback on single son of <div> that is the <input>
398 $(div).children("input").each(function () {
399 if (index<=30) console.log("HERE 3, index="+index+" key="+key);
400 $(this).click(function() {self._checkbox_clicked(self,this,key);});
405 // onSelectedRowsChanged will fire even when
406 _init_checkboxes : function () {
407 console.log("_init_checkboxes");
408 var grid=this.slick_grid;
409 this.slick_grid.onSelectedRowsChanged.subscribe(function(){
410 row_ids = grid.getSelectedRows();
411 console.log(row_ids);
415 // the callback for when user clicks
416 _checkbox_clicked: function(querygrid,input,key) {
417 // XXX this.value = key of object to be added... what about multiple keys ?
418 if (debug) messages.debug("querygrid click handler checked=" + input.checked + " key=" + key);
419 manifold.raise_event(querygrid.options.query_uuid, input.checked?SET_ADD:SET_REMOVED, key);
420 //return false; // prevent checkbox to be checked, waiting response from manifold plugin api
424 // xxx from this and down, probably needs further tweaks for slickgrid
426 _querygrid_filter: function(oSettings, aData, iDataIndex) {
428 $.each (this.filters, function(index, filter) {
429 /* XXX How to manage checkbox ? */
432 var value = filter[2];
434 /* Determine index of key in the table columns */
435 var col = $.map(oSettings.aoColumns, function(x, i) {if (x.sTitle == key) return i;})[0];
437 /* Unknown key: no filtering */
438 if (typeof(col) == 'undefined')
441 col_value=unfold.get_value(aData[col]);
442 /* Test whether current filter is compatible with the column */
443 if (op == '=' || op == '==') {
444 if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
446 }else if (op == '!=') {
447 if ( col_value == value || col_value==null || col_value=="" || col_value=="n/a")
450 if ( parseFloat(col_value) >= value || col_value==null || col_value=="" || col_value=="n/a")
453 if ( parseFloat(col_value) <= value || col_value==null || col_value=="" || col_value=="n/a")
455 } else if(op=='<=' || op=='≤') {
456 if ( parseFloat(col_value) > value || col_value==null || col_value=="" || col_value=="n/a")
458 } else if(op=='>=' || op=='≥') {
459 if ( parseFloat(col_value) < value || col_value==null || col_value=="" || col_value=="n/a")
462 // How to break out of a loop ?
463 alert("filter not supported");
471 _selectAll: function() {
472 // requires jQuery id
473 var uuid=this.id.split("-");
474 var oTable=$("#querygrid-"+uuid[1]).dataTable();
475 // Function available in QueryGrid 1.9.x
476 // Filter : displayed data only
477 var filterData = oTable._('tr', {"filter":"applied"});
478 /* TODO: WARNING if too many nodes selected, use filters to reduce nuber of nodes */
479 if(filterData.length<=100){
480 $.each(filterData, function(index, obj) {
481 var last=$(obj).last();
482 var key_value=unfold.get_value(last[0]);
483 if(typeof($(last[0]).attr('checked'))=="undefined"){
484 $.publish('selected', 'add/'+key_value);
492 $.plugin('QueryGrid', QueryGrid);
494 // /* define the 'dom-checkbox' type for sorting in datatables
495 // http://datatables.net/examples/plug-ins/dom_sort.html
496 // using trial and error I found that the actual column number
497 // was in fact given as a third argument, and not second
498 // as the various online resources had it - go figure */
499 // $.fn.dataTableExt.afnSortData['dom-checkbox'] = function ( oSettings, _, iColumn ) {
500 // return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
501 // return result=$('td:eq('+iColumn+') input', tr).prop('checked') ? '1' : '0';