a pass on quickfilter, at least it displays something related to the input 'criterias'
[unfold.git] / plugins / quickfilter / quickfilter.js
index 7402b7b..5be5426 100644 (file)
@@ -1,73 +1,82 @@
-
 /**
  * MySlice QuickFilter plugin
- * Version: 0.1.0
- * URL: http://www.myslice.info
- * Description: A simple Plugin that prints some important variables, to be used as a template
- * Requires: 
+ * URL: http://trac.myslice.info
+ * Description: editing search filters
  * Author: The MySlice Team
- * Copyright: Copyright 2012 UPMC Sorbonne Universités
+ * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
  * License: GPLv3
  */
 
-(function( $ ){
-
-  var methods = {
-     init : function( options ) {
-
-       return this.each(function(){
-         
-         var $this = jQuery(this),
-             data = $this.data('QuickFilter'), QuickFilter = jQuery('<div />', {text : $this.attr('title')});
-         
-         // If the plugin hasn't been initialized yet
-         if ( ! data ) {
-         
-            /* Plugin initialization */
-            
-            data = jQuery(this).data();
-
-            /* Subscribe to selection updates published by the resource display plugins*/
-            //jQuery.subscribe('selected', {instance: $this}, resource_selected);
-            jQuery.subscribe('/query/' + options.query_uuid + '/changed', {instance: $this}, query_changed);
+( function($){
 
+    var debug=false;
+    //debug=true;
 
-            /* End of plugin initialization */
-
-            jQuery(this).data('QuickFilter', {
-                plugin_uuid: options.plugin_uuid,
-                query_uuid: options.query_uuid,
-                target : $this,
-                QuickFilter : QuickFilter
-            });
+    $.fn.QuickFilter = function( method ) {
+        /* Method calling logic */
+        if ( methods[method] ) {
+            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
+        } else if ( typeof method === 'object' || ! method ) {
+            return methods.init.apply( this, arguments );
+        } else {
+            $.error( 'Method ' +  method + ' does not exist on $.QuickFilter' );
+        }    
+    };
 
-            jQuery(this).data('current_query', null);
 
-            initialize_plugin(jQuery(this).data());
-            
-            function update_options(e, rows){
-                var d = data;
-                var availableTags={};
-                jQuery.each (rows, function(index, obj) {
-                    jQuery.each(obj,function(key,value){                       
-                        value = 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);}
-                        }
-                    });                    
-                });                
-                jQuery.each(availableTags, function(key, value){
-                    value.sort();
-                    if(jQuery("#"+options.plugin_uuid+"-select_"+key).length>0){
-                        jQuery.each(value, function(k, optValue){
-                            jQuery("#"+options.plugin_uuid+"-select_"+key).append('<option>'+optValue+'</option>');
-                        });
-                    }                    
-                    if(jQuery("#QuickFilter-string-"+key).length>0){
-                        jQuery("#QuickFilter-string-"+key).autocomplete({
+    var methods = {
+
+       init : function( options ) {
+           return this.each(function(){
+               
+               var $this = $(this),
+               data = $this.data('QuickFilter'), QuickFilter = $('<div />', {text : $this.attr('title')});
+               
+               if ( ! data ) {
+                   
+                   data = $(this).data();
+
+                   /* Subscribe to selection updates published by the resource display plugins */
+                   //$.subscribe('selected', {instance: $this}, resource_selected);
+                   $.subscribe('/query/' + options.query_uuid + '/changed', {instance: $this}, query_changed);
+
+
+                   /* End of plugin initialization */
+
+                   $(this).data('QuickFilter', {
+                       plugin_uuid: options.plugin_uuid,
+                       query_uuid: options.query_uuid,
+                       target : $this,
+                       QuickFilter : QuickFilter
+                   });
+
+                   $(this).data('current_query', null);
+
+                   initialize_plugin($(this).data());
+                   
+                   function update_options(e, rows){
+                       var d = data;
+                       var availableTags={};
+                       $.each (rows, function(index, obj) {
+                           $.each(obj,function(key,value){                       
+                               value = get_value(value);
+                               if(!availableTags.hasOwnProperty(key)){availableTags[key]=new Array();}
+                               //availableTags[key].push(value);
+                               var currentArray=availableTags[key];
+                               if(value!=null){
+                                   if($.inArray(value,currentArray)==-1){availableTags[key].push(value);}
+                               }
+                           });                    
+                       });                
+                       $.each(availableTags, function(key, value){
+                           value.sort();
+                           if($("#"+options.plugin_uuid+"-select_"+key).length>0){
+                               $.each(value, function(k, optValue){
+                                   $("#"+options.plugin_uuid+"-select_"+key).append('<option>'+optValue+'</option>');
+                               });
+                           }                    
+                           if($("#QuickFilter-string-"+key).length>0){
+                               $("#QuickFilter-string-"+key).autocomplete({
                                     source: value,
                                     minLength: 0, // allows to browse items with no value typed in
                                     select: function(event, ui) {
                                         query=d.current_query;
                                         query.update_filter(key,op,val);
                                         // Publish the query changed, the other plugins with subscribe will get the changes
-                                        jQuery.publish('/query/' + query.uuid + '/changed', query);
+                                        $.publish('/query/' + query.uuid + '/changed', query);
                                         //add_ActiveFilter("#QuickFilter-string-"+key,ui.item.value,d);
                                     }
-                        });
-                    }
-                });   
-                
-                
-            }     
-            
-            /* Subscribe to results in order to redraw the table when updates arrive */
-            jQuery.subscribe('/results/' + options.query_uuid + '/changed', {instance: $this}, update_options);
-         }
-
-       });
-     },
-    destroy : function( ) {
-
-        return this.each(function(){
-            var $this = jQuery(this), data = $this.data('QuickFilter');
-            jQuery(window).unbind('QuickFilter');
-            data.QuickFilter.remove();
-            $this.removeData('QuickFilter');
-        })
-
-    },
-/*
-    reposition : function( ) { // ... },
-    show : function( ) { // ... },
-    hide : function( ) { // ... },
-*/
-    update : function( content ) { }
-  };
-
-    jQuery.fn.QuickFilter = function( method ) {
-        /* Method calling logic */
-        if ( methods[method] ) {
-            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
-        } else if ( typeof method === 'object' || ! method ) {
-            return methods.init.apply( this, arguments );
-        } else {
-            jQuery.error( 'Method ' +  method + ' does not exist on jQuery.QuickFilter' );
-        }    
-
+                               });
+                           }
+                       });   
+                       
+                       
+                   }     
+                   
+                   /* Subscribe to results in order to redraw the table when updates arrive */
+                   $.subscribe('/results/' + options.query_uuid + '/changed', {instance: $this}, update_options);
+               }
+
+           });
+       },
+       destroy : function( ) {
+
+            return this.each(function(){
+               var $this = $(this), data = $this.data('QuickFilter');
+               $(window).unbind('QuickFilter');
+               data.QuickFilter.remove();
+               $this.removeData('QuickFilter');
+            })
+
+               },
+       update : function( content ) { 
+       },
     };
 
-
     /* Private methods */
 
-    function query_changed(e, query)
-    {
+    function query_changed(e, query) {
         //panos: this takes a lot of time!
         data = e.data.instance.data();
         var plugin_uuid=data.QuickFilter.plugin_uuid;
         data.current_query=query;
         
         // XXX can we modify data directly ?
-        //jQuery(data.QuickFilter.target).data('current_query', query);
+        //$(data.QuickFilter.target).data('current_query', query);
 
         if(previous_query!=null){
             // If query has changed in another plugin
                 var tmp=previous_query.diff_filter(query);
                 // Remove first to clean up select boxes
                 var removed_filters = tmp.removed;
-                jQuery.each(removed_filters, function(i,filter){
+                $.each(removed_filters, function(i,filter){
                     console.log(filter[0]);
                     allowedValues=getMetadata_property('resource', filter[0], 'allowed_values');
                     if (allowedValues!='' && allowedValues!="N/A") {
-                    //if(MANIFOLD_METADATA[filter[0]]['allowed_values']!=''){
-                        jQuery('#QuickFilter_select_field').val("#");
-                        jQuery('#QuickFilter_select_value').children().remove().end();
-                        jQuery('#QuickFilter_select_value_container').hide();
+                       //if(MANIFOLD_METADATA[filter[0]]['allowed_values']!=''){
+                        $('#QuickFilter_select_field').val("#");
+                        $('#QuickFilter_select_value').children().remove().end();
+                        $('#QuickFilter_select_value_container').hide();
                     }
-                    if(jQuery('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
-                        jQuery('#'+plugin_uuid+'-select_'+filter[0]).val(null);
+                    if($('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
+                        $('#'+plugin_uuid+'-select_'+filter[0]).val(null);
                     }
-                    if(jQuery("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
-                        jQuery("#QuickFilter-string-"+filter[0]).val(null); 
+                    if($("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
+                        $("#QuickFilter-string-"+filter[0]).val(null); 
                     }
                 });
                 // Then Add filters
                 var added_filters = tmp.added;
-                jQuery.each(added_filters, function(i,filter){
-                    if(jQuery('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
-                        jQuery('#'+plugin_uuid+'-select_'+filter[0]).val(filter[2]);
+                $.each(added_filters, function(i,filter){
+                    if($('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
+                        $('#'+plugin_uuid+'-select_'+filter[0]).val(filter[2]);
                     }
-                    if(jQuery("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
-                        jQuery("#QuickFilter-string-"+filter[0]).val(filter[2]); 
+                    if($("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
+                        $("#QuickFilter-string-"+filter[0]).val(filter[2]); 
                     }
                 });
             }
-        jQuery.publish('debug', "Quick Filter received fields: " + query.fields+" - filter = "+query.filter);
+            $.publish('debug', "Quick Filter received fields: " + query.fields+" - filter = "+query.filter);
         }
     }
     
     function initialize_plugin(data) {
 
-        jQuery('#QuickFilter_select_value_div').hide();
-        jQuery('#QuickFilter_string_value_div').hide();
-        jQuery('#QuickFilter_int_value_div').hide();
+        $('#QuickFilter_select_value_div').hide();
+        $('#QuickFilter_string_value_div').hide();
+        $('#QuickFilter_int_value_div').hide();
 
-        jQuery('#QuickFilter_only_visible').click( function () {
+        $('#QuickFilter_only_visible').click( function () {
 
             var only_visible = this.checked;
             // Clear all options in the select box, Then add None option
-            jQuery('#QuickFilter_select_field').children().remove().end().append("<option value='#'>None</option>");
+            $('#QuickFilter_select_field').children().remove().end().append("<option value='#'>None</option>");
             
             // Get the current query (ONLY AFTER THE PLUGIN HAS BEEN INITIALIZED)
             var query = data.current_query;
             // iterate to remove each active filter
             if (only_visible) {
                 if (typeof(query.fields) != 'undefined') {
-                    jQuery.each (query.fields, function(index, value) {
-                        jQuery('#QuickFilter_select_field').append("<option>"+value+"</option>");  
+                    $.each (query.fields, function(index, value) {
+                        $('#QuickFilter_select_field').append("<option>"+value+"</option>");  
                     });            
                 }
             }else{
                 headers=getMetadata_fields('resource');
-                jQuery.each (headers, function (key, value) {
-                        jQuery('#QuickFilter_select_field').append("<option>"+value['column']+"</option>");
+                $.each (headers, function (key, value) {
+                    $('#QuickFilter_select_field').append("<option>"+value['column']+"</option>");
                 });
             }
         });
 
-        jQuery('#QuickFilter_select_field').change( function () {
-            var field = jQuery(this).val();
+        $('#QuickFilter_select_field').change( function () {
+            var field = $(this).val();
             console.log(field);
-            jQuery('input[id^="QuickFilter-string-"]').hide();
-            jQuery('#QuickFilter_int_value_div').hide();
+            $('input[id^="QuickFilter-string-"]').hide();
+            $('#QuickFilter_int_value_div').hide();
             if(field=="#"){
-                jQuery('#QuickFilter_select_value_container').hide();
+                $('#QuickFilter_select_value_container').hide();
             }else{
-                jQuery('#QuickFilter_select_value_container').show();
-                jQuery.publish('debug','field selected = '+field);
+                $('#QuickFilter_select_value_container').show();
+                $.publish('debug','field selected = '+field);
                 valType=getMetadata_property('resource', field, 'value_type');
                 if (valType == 'string' || valType=="N/A") {
                     // If this key has predefined values, build a select with each allowed values as options
                     allowedValues=getMetadata_property('resource', field, 'allowed_values');
                     if (allowedValues!='' && allowedValues!="N/A") {
-                        jQuery('#QuickFilter_string_value_div').hide();
-                        jQuery('#QuickFilter_int_value_div').hide();
-                        jQuery('#QuickFilter_select_value_div').show();
-                        jQuery('#QuickFilter_select_value').show();
-                        jQuery('#QuickFilter_select_value').children().remove().end().append("<option value=''>all</option>");
-// @TODO: define seperator as ;
+                        $('#QuickFilter_string_value_div').hide();
+                        $('#QuickFilter_int_value_div').hide();
+                        $('#QuickFilter_select_value_div').show();
+                        $('#QuickFilter_select_value').show();
+                        $('#QuickFilter_select_value').children().remove().end().append("<option value=''>all</option>");
+                       // @TODO: define seperator as ;
                         allowed_values = allowedValues.split(",");
-                        jQuery.each (allowed_values, function (key, value) {
-                            jQuery('#QuickFilter_select_value').append("<option>"+value+"</option>");
+                        $.each (allowed_values, function (key, value) {
+                            $('#QuickFilter_select_value').append("<option>"+value+"</option>");
                         });
-                    // Else build an autocomplete based on the values of result query
+                       // Else build an autocomplete based on the values of result query
                     }else{
-                        jQuery('#QuickFilter_select_value_div').hide();
-                        jQuery('#QuickFilter_string_value_div').show();
-                        jQuery('.QuickFilter-filter-value').hide();
-                        jQuery('#QuickFilter-string-'+field).show();
-                        jQuery('#QuickFilter_int_value_div').hide();
+                        $('#QuickFilter_select_value_div').hide();
+                        $('#QuickFilter_string_value_div').show();
+                        $('.QuickFilter-filter-value').hide();
+                        $('#QuickFilter-string-'+field).show();
+                        $('#QuickFilter_int_value_div').hide();
                     }
                 }
                 else if (valType == 'int') {
-                    jQuery('#QuickFilter_select_value_div').hide();
-                    jQuery('#QuickFilter_string_value_div').hide();
-                    jQuery('#QuickFilter_int_value_div').show();
+                    $('#QuickFilter_select_value_div').hide();
+                    $('#QuickFilter_string_value_div').hide();
+                    $('#QuickFilter_int_value_div').show();
                 }
             }
         });
 
-        jQuery('.QuickFilter-filter-value').change( function () {
+        $('.QuickFilter-filter-value').change( function () {
             var query = data.current_query;
 
-            var filter_value = jQuery(this).val();
-            var filter_field = jQuery('#QuickFilter_select_field').val();
+            var filter_value = $(this).val();
+            var filter_field = $('#QuickFilter_select_field').val();
 
             query.update_filter(filter_field, '=', filter_value);
-            jQuery.publish('/query/' + query.uuid + '/changed', query);
+            $.publish('/query/' + query.uuid + '/changed', query);
         });
         
-        jQuery('.QuickFilter_select').change( function() {
+        $('.QuickFilter_select').change( function() {
             console.log(this.id);
             var query = data.current_query;
            var f_value = $(this).val();
             var key = this.id.split("_");
 
             // jordan ???
-/*            
-               if (f_value == "Network")
-                   f_value = "";
-*/
+           /*            
+                         if (f_value == "Network")
+                         f_value = "";
+           */
             if(typeof(key[1])!="undefined"){
                 console.log(key[1]+'='+f_value);
                 if(f_value==""){
                     query.update_filter(key[1], '=', f_value);
                 }
             }
-            jQuery.publish('/query/' + query.uuid + '/changed', query);
+            $.publish('/query/' + query.uuid + '/changed', query);
         });
 
     }
 
 })( jQuery );
-
-
-