X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plugins%2Fquickfilter%2Fquickfilter.js;h=5be5426f52dc5cc96b9f778467166394ed24d679;hb=433a5d301336bc9d402af49b2aceb257f57514bb;hp=7402b7b744a3de6d05de68ed3361fb47878c5b83;hpb=a2ae7986853adb271361957a08effd0c9537e7a3;p=unfold.git diff --git a/plugins/quickfilter/quickfilter.js b/plugins/quickfilter/quickfilter.js index 7402b7b7..5be5426f 100644 --- a/plugins/quickfilter/quickfilter.js +++ b/plugins/quickfilter/quickfilter.js @@ -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('
', {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(''); - }); - } - 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 = $('
', {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(''); + }); + } + 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) { @@ -78,57 +87,39 @@ 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; @@ -139,7 +130,7 @@ 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 @@ -148,119 +139,119 @@ 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(""); + $('#QuickFilter_select_field').children().remove().end().append(""); // 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(""); + $.each (query.fields, function(index, value) { + $('#QuickFilter_select_field').append(""); }); } }else{ headers=getMetadata_fields('resource'); - jQuery.each (headers, function (key, value) { - jQuery('#QuickFilter_select_field').append(""); + $.each (headers, function (key, value) { + $('#QuickFilter_select_field').append(""); }); } }); - 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(""); -// @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(""); + // @TODO: define seperator as ; allowed_values = allowedValues.split(","); - jQuery.each (allowed_values, function (key, value) { - jQuery('#QuickFilter_select_value').append(""); + $.each (allowed_values, function (key, value) { + $('#QuickFilter_select_value').append(""); }); - // 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(); @@ -268,10 +259,10 @@ 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==""){ @@ -280,12 +271,9 @@ query.update_filter(key[1], '=', f_value); } } - jQuery.publish('/query/' + query.uuid + '/changed', query); + $.publish('/query/' + query.uuid + '/changed', query); }); } })( jQuery ); - - -