Fixed bug: select columns all checked in filters, autocomplete based on query_all...
[myslice.git] / plugins / query_editor / static / js / query_editor.js
index e065a65..7338b69 100644 (file)
         event_filter_added: function(op, suffix) {
             suffix = (typeof suffix === 'undefined') ? '' : manifold.separator + suffix;
             var self = this;
-            return function(e) {
+            return function(e, ui) {
                 var array = self.array_from_id(e.target.id);
                 var key   = self.field_from_id(array); // No need to remove suffix...
-                var value = e.target.value;
+
+                // using autocomplete ui
+                if(typeof(ui) != "undefined"){
+                    var value = ui.item.value;
+                }else{
+                    var value = e.target.value;
+                }
 
                 if (value) {
                     // XXX This should be handled by manifold
 
         init: function(options, element) {
             this._super(options, element);
-
             this.listen_query(options.query_uuid);
+            // this one is the complete list of resources
+            // and will be bound to callbacks like on_all_new_record
+            this.listen_query(options.query_all_uuid, 'all');
 
-            this.els('queryeditor-auto-filter').change(this.event_filter_added('='));
-            this.els('queryeditor-filter').change(this.event_filter_added('='));
-            this.els('queryeditor-filter-min').change(this.event_filter_added('>'));
-            this.els('queryeditor-filter-max').change(this.event_filter_added('<'));
+
+            this.elts('queryeditor-auto-filter').change(this.event_filter_added('='));
+            this.elts('queryeditor-filter').change(this.event_filter_added('='));
+            this.elts('queryeditor-filter-min').change(this.event_filter_added('>'));
+            this.elts('queryeditor-filter-max').change(this.event_filter_added('<'));
 
             var self = this;
-            this.els('queryeditor-check').click(function() { 
+            this.elts('queryeditor-check').click(function() { 
                 manifold.raise_event(self.options.query_uuid, this.checked?FIELD_ADDED:FIELD_REMOVED, this.value);
             });
 
             // XXX Why isn't it done statically ?
             var nCloneTh = document.createElement( 'th' );
             var nCloneTd = document.createElement( 'td' );
-            nCloneTd.innerHTML = "<span class='ui-icon ui-icon-triangle-1-e' style='cursor:pointer'></span>";
+            nCloneTd.innerHTML = "<span class='glyphicon glyphicon-chevron-right' style='cursor:pointer'></span>";
             //nCloneTd.innerHTML = '<img src="/components/com_tophat/images/details_open.png">';
             nCloneTh.innerHTML = '<b>Info</b>';
             nCloneTd.className = "center";
             nCloneTh.className = "center";
-            this.el('table thead tr').each(function() {
+            this.elmt('table thead tr').each(function() {
                 this.insertBefore(nCloneTh, this.childNodes[0]);
             });
-            this.el('table tbody tr').each(function() {
+            this.elmt('table tbody tr').each(function() {
                 this.insertBefore(nCloneTd.cloneNode( true ), this.childNodes[0]);
             });
             */
          
             // We are currently using a DataTable display, but another browsing component could be better
             //jQuery('#'+this.options.plugin_uuid+'-table').dataTable...
-            var  metaTable = this.el('table').dataTable({
-                bFilter     : false,
-                bPaginate   : false,
-                bInfo       : false,
-                sScrollX    : '100%',         // Horizontal scrolling
-                sScrollY    : '200px',
-                //bJQueryUI   : true,           // Use jQuery UI
-                bProcessing : true,           // Loading
-                aaSorting   : [[ 1, "asc" ]], // sort by column fields on load
-                aoColumnDefs: [
-                    { 'bSortable': false, 'aTargets': [ 0 ]},
-                    { 'sWidth': '8px', 'aTargets': [ 0 ] },
-                    { 'sWidth': '8px', 'aTargets': [ 4 ] } // XXX NB OF COLS
-                ]
+            var  metaTable = this.elmt('table').dataTable({
+// Thierry : I'm turning off all the dataTables options for now, so that
+// the table displays more properly again, might need more tuning though
+//                bFilter     : false,
+//                bPaginate   : false,
+//                bInfo       : false,
+//                sScrollX    : '100%',         // Horizontal scrolling
+//                sScrollY    : '200px',
+//                //bJQueryUI   : true,           // Use jQuery UI
+//                bProcessing : true,           // Loading
+//                aaSorting   : [[ 1, "asc" ]], // sort by column fields on load
+//                aoColumnDefs: [
+//                    { 'bSortable': false, 'aTargets': [ 0 ]},
+//                    { 'sWidth': '8px', 'aTargets': [ 0 ] },
+//                    { 'sWidth': '8px', 'aTargets': [ 4 ] } // XXX NB OF COLS
+//                ]
             });
 
-            var self = this;
             // Actions on the newly added fields
-            this.el('table tbody td span').on('click', function() {
+            this.elmt('table tbody td span').on('click', function() {
                 var nTr = this.parentNode.parentNode;
                 // use jQuery UI instead of images to keep a common UI
-                // class="ui-icon treeclick ui-icon-triangle-1-s tree-minus"
-                // East oriented Triangle class="ui-icon-triangle-1-e"
-                // South oriented Triangle class="ui-icon-triangle-1-s"
+                // class="glyphicon glyphicon-chevron-down treeclick tree-minus"
+                // East oriented Triangle class="glyphicon-chevron-right"
+                // South oriented Triangle class="glyphicon-chevron-down"
                 
-                if (this.className=="ui-icon ui-icon-triangle-1-e") {
-                    this.removeClass("ui-icon-triangle-1-e").addClass("ui-icon-triangle-1-s");
+                if (this.hasClass("glyphicon-chevron-right")) {
+                    this.removeClass("glyphicon-chevron-right").addClass("glyphicon-chevron-down");
                     // XXX ??????
                     metaTable.fnOpen(nTr, this.fnFormatDetails(metaTable, nTr, self.options.plugin_uuid+'_div'), 'details' );
                 } else {
-                    this.removeClass("ui-icon-triangle-1-s").addClass("ui-icon-triangle-1-e");
+                    this.removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-right");
                     metaTable.fnClose(nTr);
                 }
             });
 
-            this.el('table_wrapper').css({
+            this.elmt('table_wrapper').css({
                 'padding-top'   : '0em',
                 'padding-bottom': '0em'
             });
+            
+            // autocomplete list of tags
+            this.availableTags = {};
 
         }, // init
 
 
         check_field: function(field)
         {
-            this.el('check', field).attr('checked', true);
+            this.elmt('check', field).attr('checked', true);
         },
 
         uncheck_field: function(field)
         {
-            this.el('check', field).attr('checked', false);
+            this.elmt('check', field).attr('checked', false);
         },
 
         update_filter_value: function(filter, removed)
             var id = this.id_from_field(key);
 
             if (op == '=') {
-                var element = this.el(id);
+                var element = this.elmt(id);
             } else {
                 var suffix;
                 if (op == '<') {
-                    this.el(id, 'max').val(value);
+                    this.elmt(id, 'max').val(value);
                 } else if (op == '>') {
-                    this.el(id, 'min').val(value);
+                    this.elmt(id, 'min').val(value);
                 } else {
                     return;
                 }
-                var element = this.el(id, suffix);
+                var element = this.elmt(id, suffix);
             }
 
             element.val(removed?null:value);
             this.uncheck_field(field);
         },
 
+        /* RECORD HANDLERS */
+        on_query_done: function()
+        {
+            //console.log("Query_Editor: query_done!");
+            //console.log(this.availableTags);
+        },
+        /* Autocomplete based on query_all to get all the fields, where query get only the fields selected  */
+        on_all_new_record: function(record)
+        {
+            availableTags = this.availableTags;           
+            jQuery.each(record,function(key,value){
+                value = unfold.get_value(value);
+                if(!availableTags.hasOwnProperty(key)){availableTags[key]=new Array();}
+                //availableTags[key].push(value);
+                var currentArray = availableTags[key];
+                if(value!=null){
+                    if(jQuery.inArray(value,currentArray)==-1){availableTags[key].push(value);}
+                }
+           });
+           this.availableTags = availableTags;
+           this.update_autocomplete(availableTags);
+        },
+
         /* Former code not used at the moment */
 
         print_field_description: function(field_header, div_id) 
             return output;
         },
 
+        update_autocomplete: function(availableTags)
+        {
+            var self = this;
+            var domid = this.options.plugin_uuid;
+            
+            jQuery.each(availableTags, function(key, value){
+                value.sort();
+                jQuery("#"+domid+"__field__"+key).autocomplete({
+                            source: value,
+                            selectFirst: true,
+                            minLength: 0, // allows to browse items with no value typed in
+                            select: self.event_filter_added('=')
+                });
+            });                
+        }, // update_autocomplete     
+
+/*
         update_autocomplete: function(e, rows, current_query)
         {
             var d = data;
             var availableTags={};
             jQuery.each (rows, function(index, obj) {                    
                 jQuery.each(obj,function(key,value){                       
-                    value = get_value(value); 
+                    value = unfold.get_value(value); 
                     if(!availableTags.hasOwnProperty(key)){availableTags[key]=new Array();}
                     //availableTags[key].push(value);
                     var currentArray=availableTags[key];
                 });
             });                
         }, // update_autocomplete     
-
+*/
         fnFormatDetails: function( metaTable, nTr, div_id ) 
         {
             var aData = metaTable.fnGetData( nTr );