3 * MySlice QuickFilter plugin
5 * URL: http://www.myslice.info
6 * Description: A simple Plugin that prints some important variables, to be used as a template
8 * Author: The MySlice Team
9 * Copyright: Copyright 2012 UPMC Sorbonne Universités
16 init : function( options ) {
18 return this.each(function(){
20 var $this = jQuery(this),
21 data = $this.data('QuickFilter'), QuickFilter = jQuery('<div />', {text : $this.attr('title')});
23 // If the plugin hasn't been initialized yet
26 /* Plugin initialization */
28 data = jQuery(this).data();
30 /* Subscribe to selection updates published by the resource display plugins*/
31 //jQuery.subscribe('selected', {instance: $this}, resource_selected);
32 jQuery.subscribe('/query/' + options.query_uuid + '/changed', {instance: $this}, query_changed);
35 /* End of plugin initialization */
37 jQuery(this).data('QuickFilter', {
38 plugin_uuid: options.plugin_uuid,
39 query_uuid: options.query_uuid,
41 QuickFilter : QuickFilter
44 jQuery(this).data('current_query', null);
46 initialize_plugin(jQuery(this).data());
48 function update_options(e, rows){
51 jQuery.each (rows, function(index, obj) {
52 jQuery.each(obj,function(key,value){
53 value = get_value(value);
54 if(!availableTags.hasOwnProperty(key)){availableTags[key]=new Array();}
55 //availableTags[key].push(value);
56 var currentArray=availableTags[key];
58 if(jQuery.inArray(value,currentArray)==-1){availableTags[key].push(value);}
62 jQuery.each(availableTags, function(key, value){
64 if(jQuery("#"+options.plugin_uuid+"-select_"+key).length>0){
65 jQuery.each(value, function(k, optValue){
66 jQuery("#"+options.plugin_uuid+"-select_"+key).append('<option>'+optValue+'</option>');
69 if(jQuery("#QuickFilter-string-"+key).length>0){
70 jQuery("#QuickFilter-string-"+key).autocomplete({
72 minLength: 0, // allows to browse items with no value typed in
73 select: function(event, ui) {
74 //var key=getKeySplitId(this.id,"-");
76 var val=ui.item.value;
78 query=d.current_query;
79 query.update_filter(key,op,val);
80 // Publish the query changed, the other plugins with subscribe will get the changes
81 jQuery.publish('/query/' + query.uuid + '/changed', query);
82 //add_ActiveFilter("#QuickFilter-string-"+key,ui.item.value,d);
91 /* Subscribe to results in order to redraw the table when updates arrive */
92 jQuery.subscribe('/results/' + options.query_uuid + '/changed', {instance: $this}, update_options);
97 destroy : function( ) {
99 return this.each(function(){
100 var $this = jQuery(this), data = $this.data('QuickFilter');
101 jQuery(window).unbind('QuickFilter');
102 data.QuickFilter.remove();
103 $this.removeData('QuickFilter');
108 reposition : function( ) { // ... },
109 show : function( ) { // ... },
110 hide : function( ) { // ... },
112 update : function( content ) { }
115 jQuery.fn.QuickFilter = function( method ) {
116 /* Method calling logic */
117 if ( methods[method] ) {
118 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
119 } else if ( typeof method === 'object' || ! method ) {
120 return methods.init.apply( this, arguments );
122 jQuery.error( 'Method ' + method + ' does not exist on jQuery.QuickFilter' );
128 /* Private methods */
130 function query_changed(e, query)
132 //panos: this takes a lot of time!
133 data = e.data.instance.data();
134 var plugin_uuid=data.QuickFilter.plugin_uuid;
136 /* Compare current and advertised query to get added and removed fields */
137 var previous_query=data.current_query;
138 /* Save the query as the current query */
139 data.current_query=query;
141 // XXX can we modify data directly ?
142 //jQuery(data.QuickFilter.target).data('current_query', query);
144 if(previous_query!=null){
145 // If query has changed in another plugin
146 // set the value on each filter that exists in QuickFilter plugin
147 if (typeof(previous_query) != 'undefined') {
148 var tmp=previous_query.diff_filter(query);
149 // Remove first to clean up select boxes
150 var removed_filters = tmp.removed;
151 jQuery.each(removed_filters, function(i,filter){
152 console.log(filter[0]);
153 allowedValues=getMetadata_property('resource', filter[0], 'allowed_values');
154 if (allowedValues!='' && allowedValues!="N/A") {
155 //if(all_headers[filter[0]]['allowed_values']!=''){
156 jQuery('#QuickFilter_select_field').val("#");
157 jQuery('#QuickFilter_select_value').children().remove().end();
158 jQuery('#QuickFilter_select_value_container').hide();
160 if(jQuery('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
161 jQuery('#'+plugin_uuid+'-select_'+filter[0]).val(null);
163 if(jQuery("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
164 jQuery("#QuickFilter-string-"+filter[0]).val(null);
168 var added_filters = tmp.added;
169 jQuery.each(added_filters, function(i,filter){
170 if(jQuery('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
171 jQuery('#'+plugin_uuid+'-select_'+filter[0]).val(filter[2]);
173 if(jQuery("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
174 jQuery("#QuickFilter-string-"+filter[0]).val(filter[2]);
178 jQuery.publish('debug', "Quick Filter received fields: " + query.fields+" - filter = "+query.filter);
182 function initialize_plugin(data) {
184 jQuery('#QuickFilter_select_value_div').hide();
185 jQuery('#QuickFilter_string_value_div').hide();
186 jQuery('#QuickFilter_int_value_div').hide();
188 jQuery('#QuickFilter_only_visible').click( function () {
190 var only_visible = this.checked;
191 // Clear all options in the select box, Then add None option
192 jQuery('#QuickFilter_select_field').children().remove().end().append("<option value='#'>None</option>");
194 // Get the current query (ONLY AFTER THE PLUGIN HAS BEEN INITIALIZED)
195 var query = data.current_query;
196 // iterate to remove each active filter
198 if (typeof(query.fields) != 'undefined') {
199 jQuery.each (query.fields, function(index, value) {
200 jQuery('#QuickFilter_select_field').append("<option>"+value+"</option>");
204 headers=getMetadata_fields('resource');
205 jQuery.each (headers, function (key, value) {
206 jQuery('#QuickFilter_select_field').append("<option>"+value['column']+"</option>");
211 jQuery('#QuickFilter_select_field').change( function () {
212 var field = jQuery(this).val();
214 jQuery('input[id^="QuickFilter-string-"]').hide();
215 jQuery('#QuickFilter_int_value_div').hide();
217 jQuery('#QuickFilter_select_value_container').hide();
219 jQuery('#QuickFilter_select_value_container').show();
220 jQuery.publish('debug','field selected = '+field);
221 valType=getMetadata_property('resource', field, 'value_type');
222 if (valType == 'string' || valType=="N/A") {
223 // If this key has predefined values, build a select with each allowed values as options
224 allowedValues=getMetadata_property('resource', field, 'allowed_values');
225 if (allowedValues!='' && allowedValues!="N/A") {
226 jQuery('#QuickFilter_string_value_div').hide();
227 jQuery('#QuickFilter_int_value_div').hide();
228 jQuery('#QuickFilter_select_value_div').show();
229 jQuery('#QuickFilter_select_value').show();
230 jQuery('#QuickFilter_select_value').children().remove().end().append("<option value=''>all</option>");
231 // @TODO: define seperator as ;
232 allowed_values = allowedValues.split(",");
233 jQuery.each (allowed_values, function (key, value) {
234 jQuery('#QuickFilter_select_value').append("<option>"+value+"</option>");
236 // Else build an autocomplete based on the values of result query
238 jQuery('#QuickFilter_select_value_div').hide();
239 jQuery('#QuickFilter_string_value_div').show();
240 jQuery('.QuickFilter-filter-value').hide();
241 jQuery('#QuickFilter-string-'+field).show();
242 jQuery('#QuickFilter_int_value_div').hide();
245 else if (valType == 'int') {
246 jQuery('#QuickFilter_select_value_div').hide();
247 jQuery('#QuickFilter_string_value_div').hide();
248 jQuery('#QuickFilter_int_value_div').show();
253 jQuery('.QuickFilter-filter-value').change( function () {
254 var query = data.current_query;
256 var filter_value = jQuery(this).val();
257 var filter_field = jQuery('#QuickFilter_select_field').val();
259 query.update_filter(filter_field, '=', filter_value);
260 jQuery.publish('/query/' + query.uuid + '/changed', query);
263 jQuery('.QuickFilter_select').change( function() {
264 console.log(this.id);
265 var query = data.current_query;
266 var f_value = $(this).val();
268 var key = this.id.split("_");
272 if (f_value == "Network")
275 if(typeof(key[1])!="undefined"){
276 console.log(key[1]+'='+f_value);
278 query.remove_filter(key[1],"","");
280 query.update_filter(key[1], '=', f_value);
283 jQuery.publish('/query/' + query.uuid + '/changed', query);