forget about manage.py collectstatic and come up with our own brew
[myslice.git] / plugins / quickfilter / quickfilter.js
1
2 /**
3  * MySlice QuickFilter plugin
4  * Version: 0.1.0
5  * URL: http://www.myslice.info
6  * Description: A simple Plugin that prints some important variables, to be used as a template
7  * Requires: 
8  * Author: The MySlice Team
9  * Copyright: Copyright 2012 UPMC Sorbonne Universités
10  * License: GPLv3
11  */
12
13 (function( $ ){
14
15   var methods = {
16      init : function( options ) {
17
18        return this.each(function(){
19          
20          var $this = jQuery(this),
21              data = $this.data('QuickFilter'), QuickFilter = jQuery('<div />', {text : $this.attr('title')});
22          
23          // If the plugin hasn't been initialized yet
24          if ( ! data ) {
25          
26             /* Plugin initialization */
27             
28             data = jQuery(this).data();
29
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);
33
34
35             /* End of plugin initialization */
36
37             jQuery(this).data('QuickFilter', {
38                 plugin_uuid: options.plugin_uuid,
39                 query_uuid: options.query_uuid,
40                 target : $this,
41                 QuickFilter : QuickFilter
42             });
43
44             jQuery(this).data('current_query', null);
45
46             initialize_plugin(jQuery(this).data());
47             
48             function update_options(e, rows){
49                 var d = data;
50                 var availableTags={};
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];
57                         if(value!=null){
58                             if(jQuery.inArray(value,currentArray)==-1){availableTags[key].push(value);}
59                         }
60                     });                    
61                 });                
62                 jQuery.each(availableTags, function(key, value){
63                     value.sort();
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>');
67                         });
68                     }                    
69                     if(jQuery("#QuickFilter-string-"+key).length>0){
70                         jQuery("#QuickFilter-string-"+key).autocomplete({
71                                     source: value,
72                                     minLength: 0, // allows to browse items with no value typed in
73                                     select: function(event, ui) {
74                                         //var key=getKeySplitId(this.id,"-");
75                                         var op='=';
76                                         var val=ui.item.value;
77
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);
83                                     }
84                         });
85                     }
86                 });   
87                 
88                 
89             }     
90             
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);
93          }
94
95        });
96      },
97     destroy : function( ) {
98
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');
104         })
105
106     },
107 /*
108     reposition : function( ) { // ... },
109     show : function( ) { // ... },
110     hide : function( ) { // ... },
111 */
112     update : function( content ) { }
113   };
114
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 );
121         } else {
122             jQuery.error( 'Method ' +  method + ' does not exist on jQuery.QuickFilter' );
123         }    
124
125     };
126
127
128     /* Private methods */
129
130     function query_changed(e, query)
131     {
132         //panos: this takes a lot of time!
133         data = e.data.instance.data();
134         var plugin_uuid=data.QuickFilter.plugin_uuid;
135         
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;
140         
141         // XXX can we modify data directly ?
142         //jQuery(data.QuickFilter.target).data('current_query', query);
143
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(MANIFOLD_METADATA[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();
159                     }
160                     if(jQuery('#'+plugin_uuid+'-select_'+filter[0]).length>0 && filter[1]=="="){
161                         jQuery('#'+plugin_uuid+'-select_'+filter[0]).val(null);
162                     }
163                     if(jQuery("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
164                         jQuery("#QuickFilter-string-"+filter[0]).val(null); 
165                     }
166                 });
167                 // Then Add filters
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]);
172                     }
173                     if(jQuery("#QuickFilter-string-"+filter[0]).length>0 && filter[1]=="="){
174                         jQuery("#QuickFilter-string-"+filter[0]).val(filter[2]); 
175                     }
176                 });
177             }
178         jQuery.publish('debug', "Quick Filter received fields: " + query.fields+" - filter = "+query.filter);
179         }
180     }
181     
182     function initialize_plugin(data) {
183
184         jQuery('#QuickFilter_select_value_div').hide();
185         jQuery('#QuickFilter_string_value_div').hide();
186         jQuery('#QuickFilter_int_value_div').hide();
187
188         jQuery('#QuickFilter_only_visible').click( function () {
189
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>");
193             
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
197             if (only_visible) {
198                 if (typeof(query.fields) != 'undefined') {
199                     jQuery.each (query.fields, function(index, value) {
200                         jQuery('#QuickFilter_select_field').append("<option>"+value+"</option>");  
201                     });            
202                 }
203             }else{
204                 headers=getMetadata_fields('resource');
205                 jQuery.each (headers, function (key, value) {
206                         jQuery('#QuickFilter_select_field').append("<option>"+value['column']+"</option>");
207                 });
208             }
209         });
210
211         jQuery('#QuickFilter_select_field').change( function () {
212             var field = jQuery(this).val();
213             console.log(field);
214             jQuery('input[id^="QuickFilter-string-"]').hide();
215             jQuery('#QuickFilter_int_value_div').hide();
216             if(field=="#"){
217                 jQuery('#QuickFilter_select_value_container').hide();
218             }else{
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>");
235                         });
236                     // Else build an autocomplete based on the values of result query
237                     }else{
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();
243                     }
244                 }
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();
249                 }
250             }
251         });
252
253         jQuery('.QuickFilter-filter-value').change( function () {
254             var query = data.current_query;
255
256             var filter_value = jQuery(this).val();
257             var filter_field = jQuery('#QuickFilter_select_field').val();
258
259             query.update_filter(filter_field, '=', filter_value);
260             jQuery.publish('/query/' + query.uuid + '/changed', query);
261         });
262         
263         jQuery('.QuickFilter_select').change( function() {
264             console.log(this.id);
265             var query = data.current_query;
266             var f_value = $(this).val();
267             
268             var key = this.id.split("_");
269
270             // jordan ???
271 /*            
272                 if (f_value == "Network")
273                     f_value = "";
274 */
275             if(typeof(key[1])!="undefined"){
276                 console.log(key[1]+'='+f_value);
277                 if(f_value==""){
278                     query.remove_filter(key[1],"","");
279                 }else{
280                     query.update_filter(key[1], '=', f_value);
281                 }
282             }
283             jQuery.publish('/query/' + query.uuid + '/changed', query);
284         });
285
286     }
287
288 })( jQuery );
289
290
291