web/data/cubicweb.formfilter.js
changeset 0 b97547f5f1fa
child 4 8a607bdc11dc
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 /*
       
     2  *  :organization: Logilab
       
     3  *  :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     4  *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     5  */
       
     6 
       
     7 CubicWeb.require('htmlhelpers.js');
       
     8 CubicWeb.require('ajax.js');
       
     9 
       
    10 //============= filter form functions ========================================//
       
    11 
       
    12 function copyParam(origparams, newparams, param) {
       
    13     var index = findValue(origparams[0], param);
       
    14     if (index > -1) {
       
    15 	newparams[param] = origparams[1][index];
       
    16     }
       
    17 }
       
    18 
       
    19 function facetFormContent(form) {
       
    20   var names = [];
       
    21   var values = [];
       
    22   jQuery(form).find('.facet').each(function () {
       
    23       var facetName = jQuery(this).find('.facetTitle').attr('cubicweb:facetName');
       
    24       var facetValues = jQuery(this).find('.facetValueSelected').each(function(x) {
       
    25 	  names.push(facetName);
       
    26 	  values.push(this.getAttribute('cubicweb:value'));
       
    27       });
       
    28   });
       
    29   jQuery(form).find('input').each(function () {
       
    30       names.push(this.name);
       
    31       values.push(this.value);
       
    32   });
       
    33     jQuery(form).find('select option[@selected]').each(function () {
       
    34 	names.push(this.parentNode.name);
       
    35 	values.push(this.value);
       
    36     });
       
    37   return [names, values];
       
    38 }
       
    39 
       
    40 function buildRQL(divid, vid, paginate, vidargs) {
       
    41     jQuery(CubicWeb).trigger('facets-content-loading', [divid, vid, paginate, vidargs]);
       
    42     var form = getNode(divid+'Form');
       
    43     var zipped = facetFormContent(form);
       
    44     zipped[0].push('facetargs')
       
    45     zipped[1].push(vidargs)
       
    46     var d = async_remote_exec('filter_build_rql', zipped[0], zipped[1]);
       
    47     d.addCallback(function(result) {
       
    48 	var rql = result[0];
       
    49 	var toupdate = result[1];
       
    50 	var extraparams = vidargs;
       
    51 	var displayactions = jQuery('#' + divid).attr('cubicweb:displayactions');
       
    52 	if (displayactions) { extraparams['displayactions'] = displayactions; }
       
    53 	if (paginate) { extraparams['paginate'] = '1'; }
       
    54 	// copy some parameters
       
    55 	// XXX cleanup vid/divid mess
       
    56 	// if vid argument is specified , the one specified in form params will
       
    57 	// be overriden by replacePageChunk
       
    58 	copyParam(zipped, extraparams, 'vid');
       
    59 	extraparams['divid'] = divid;
       
    60 	copyParam(zipped, extraparams, 'divid');
       
    61 	copyParam(zipped, extraparams, 'subvid');
       
    62 	// paginate used to know if the filter box is acting, in which case we
       
    63 	// want to reload action box to match current selection
       
    64 	replacePageChunk(divid, rql, vid, extraparams, true, function() {
       
    65 	  jQuery(CubicWeb).trigger('facets-content-loaded', [divid, rql, vid, extraparams]);
       
    66 	});
       
    67 	if (paginate) {
       
    68 	    // FIXME the edit box might not be displayed in which case we don't
       
    69 	    // know where to put the potential new one, just skip this case
       
    70 	    // for now
       
    71 	    if (jQuery('#edit_box').length) {
       
    72 		reloadComponent('edit_box', rql, 'boxes', 'edit_box');
       
    73 	    }
       
    74 	}
       
    75 	var d = async_remote_exec('filter_select_content', toupdate, rql);
       
    76 	d.addCallback(function(updateMap) {
       
    77 	    for (facetId in updateMap) {
       
    78 		var values = updateMap[facetId];
       
    79 		jqNode(facetId).find('.facetCheckBox').each(function () {
       
    80 		    var value = this.getAttribute('cubicweb:value');
       
    81 		    if (!values.contains(value)) {
       
    82 			if (!jQuery(this).hasClass('facetValueDisabled')) {
       
    83 			    jQuery(this).addClass('facetValueDisabled');
       
    84 			}
       
    85 		    } else {
       
    86 			if (jQuery(this).hasClass('facetValueDisabled')) {
       
    87 			    jQuery(this).removeClass('facetValueDisabled');
       
    88 			}
       
    89 		    }
       
    90 		});
       
    91 	    }
       
    92 	});
       
    93     });
       
    94 }
       
    95 
       
    96 
       
    97 var SELECTED_IMG = baseuri()+"data/black-check.png";
       
    98 var UNSELECTED_IMG = baseuri()+"data/no-check-no-border.png";
       
    99 
       
   100 function initFacetBoxEvents(root){
       
   101     root = root || document;
       
   102     jQuery(root).find('div.facetBody').each(function (){
       
   103     	 var height = jQuery(this).height();
       
   104 	 if (height > 160){
       
   105 	     jQuery(this).addClass('owerflowed');
       
   106 	 }
       
   107     });
       
   108     jQuery(root).find('form').each(function () {
       
   109 	var form = jQuery(this);
       
   110 	var facetargs = form.attr('cubicweb:facetargs');
       
   111 	if (facetargs) {
       
   112 	    form.submit(function() {
       
   113 		var facetargs = evalJSON(form.attr('cubicweb:facetargs'));
       
   114 	        buildRQL.apply(null, facetargs); //(divid, vid, paginate, extraargs);
       
   115 	        return false;
       
   116 	    });
       
   117 	    form.find('div.facet').each(function() {
       
   118 		var facet = jQuery(this);
       
   119 		facet.find('div.facetCheckBox').each(function (i) {
       
   120 		    this.setAttribute('cubicweb:idx', i);
       
   121 		});
       
   122 		facet.find('div.facetCheckBox').click(function () {
       
   123 		    var facetargs = evalJSON(form.attr('cubicweb:facetargs'));
       
   124 		    var $this = jQuery(this);
       
   125 		    if ($this.hasClass('facetValueSelected')) {
       
   126 			$this.removeClass('facetValueSelected');
       
   127 			$this.find('img').attr('src', UNSELECTED_IMG);
       
   128 			var index = parseInt($this.attr('cubicweb:idx'));
       
   129 			var shift = jQuery.grep(facet.find('.facetValueSelected'), function (n) {
       
   130 			    var nindex = parseInt(n.getAttribute('cubicweb:idx'));
       
   131 			    return nindex > index;
       
   132 			}).length;
       
   133 			index += shift;
       
   134 			var parent = this.parentNode;
       
   135 			jQuery(parent).find('.facetCheckBox:nth('+index+')').after(this);
       
   136 		    } else {
       
   137 			var lastSelected = facet.find('.facetValueSelected:last');
       
   138 			if (lastSelected.length) {
       
   139 			    lastSelected.after(this);
       
   140 			} else {
       
   141 			    var parent = this.parentNode;
       
   142 			    jQuery(parent).prepend(this);
       
   143 			}
       
   144 			jQuery(this).addClass('facetValueSelected');
       
   145 			jQuery(this).find('img').attr('src', SELECTED_IMG);
       
   146 		    }
       
   147 		    buildRQL.apply(null, facetargs); // (divid, vid, paginate, extraargs);
       
   148 		    facet.find('.facetBody').animate({scrollTop: 0}, '');
       
   149 		});
       
   150 		facet.find('select.facetOperator').change(function() {
       
   151 		    var nbselected = facet.find('div.facetValueSelected').length;
       
   152 		    if (nbselected >= 2) {
       
   153 			buildRQL.apply(null, facetargs); // (divid, vid, paginate, extraargs);
       
   154 		    }
       
   155 		});
       
   156 		facet.find('div.facetTitle').click(function() {
       
   157 		  facet.find('div.facetBody').toggleClass('hidden').toggleClass('opened');
       
   158 		  jQuery(this).toggleClass('opened');
       
   159 		   });
       
   160 
       
   161 	    });
       
   162 	}
       
   163     });
       
   164 }
       
   165 
       
   166 // trigger this function on document ready event if you provide some kind of
       
   167 // persistent search (eg crih)
       
   168 function reorderFacetsItems(root){
       
   169     root = root || document;
       
   170     jQuery(root).find('form').each(function () {
       
   171 	var form = jQuery(this);
       
   172 	var facetargs = form.attr('cubicweb:facetargs');
       
   173 	if (facetargs) {
       
   174 	    form.find('div.facet').each(function() {
       
   175 		var facet = jQuery(this);	
       
   176 		var lastSelected = null;
       
   177 		facet.find('div.facetCheckBox').each(function (i) {
       
   178 		    var $this = jQuery(this);
       
   179 		    if ($this.hasClass('facetValueSelected')) {
       
   180 			if (lastSelected) {
       
   181 			    lastSelected.after(this);
       
   182 			} else {
       
   183 			    var parent = this.parentNode;
       
   184 			    jQuery(parent).prepend(this);
       
   185 			}
       
   186 			lastSelected = $this;
       
   187 		    }
       
   188 		});
       
   189 	    });
       
   190 	}
       
   191     });
       
   192 }
       
   193 
       
   194 // we need to differenciate cases where initFacetBoxEvents is called
       
   195 // with one argument or without any argument. If we use `initFacetBoxEvents`
       
   196 // as the direct callback on the jQuery.ready event, jQuery will pass some argument
       
   197 // of his, so we use this small anonymous function instead.
       
   198 jQuery(document).ready(function() {initFacetBoxEvents();});
       
   199 
       
   200 CubicWeb.provide('formfilter.js');