web/data/cubicweb.facets.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 05 Mar 2010 12:18:22 +0100
changeset 4812 2eb6a9b33aa5
parent 3129 fab471bee6c1
child 5112 5bf8baecfaf8
permissions -rw-r--r--
backport stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
/*
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
 *  :organization: Logilab
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
 *  :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
 *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
CubicWeb.require('htmlhelpers.js');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
CubicWeb.require('ajax.js');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
//============= filter form functions ========================================//
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
function copyParam(origparams, newparams, param) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
    var index = findValue(origparams[0], param);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
    if (index > -1) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
	newparams[param] = origparams[1][index];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
function facetFormContent(form) {
4
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    19
    var names = [];
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    20
    var values = [];
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    21
    jQuery(form).find('.facet').each(function () {
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    22
        var facetName = jQuery(this).find('.facetTitle').attr('cubicweb:facetName');
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    23
        var facetValues = jQuery(this).find('.facetValueSelected').each(function(x) {
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    24
  	    names.push(facetName);
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    25
  	    values.push(this.getAttribute('cubicweb:value'));
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    26
        });
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    27
    });
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    28
    jQuery(form).find('input').each(function () {
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    29
        names.push(this.name);
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    30
        values.push(this.value);
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    31
    });
1894
c41c539b8f34 [javascript] the @attr (xpath) syntax doesn't work anymore with jquery1.3, forget the @
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1419
diff changeset
    32
    jQuery(form).find('select option[selected]').each(function () {
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
	names.push(this.parentNode.name);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
	values.push(this.value);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    });
4
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    36
    return [names, values];
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
function buildRQL(divid, vid, paginate, vidargs) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    jQuery(CubicWeb).trigger('facets-content-loading', [divid, vid, paginate, vidargs]);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    var form = getNode(divid+'Form');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    var zipped = facetFormContent(form);
4
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    43
    zipped[0].push('facetargs');
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    44
    zipped[1].push(vidargs);
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1076
diff changeset
    45
    var d = asyncRemoteExec('filter_build_rql', zipped[0], zipped[1]);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    d.addCallback(function(result) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
	var rql = result[0];
5
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    48
	var $bkLink = jQuery('#facetBkLink');
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    49
	if ($bkLink.length) {
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    50
	    var bkUrl = $bkLink.attr('cubicweb:target') + '&path=view?rql=' + rql;
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    51
	    if (vid) {
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    52
		bkUrl += '&vid=' + vid;
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    53
	    }
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    54
	    $bkLink.attr('href', bkUrl);
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    55
	}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
	var toupdate = result[1];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
	var extraparams = vidargs;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
	var displayactions = jQuery('#' + divid).attr('cubicweb:displayactions');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
	if (displayactions) { extraparams['displayactions'] = displayactions; }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
	if (paginate) { extraparams['paginate'] = '1'; }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
	// copy some parameters
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
	// XXX cleanup vid/divid mess
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
	// if vid argument is specified , the one specified in form params will
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
	// be overriden by replacePageChunk
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
	copyParam(zipped, extraparams, 'vid');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
	extraparams['divid'] = divid;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
	copyParam(zipped, extraparams, 'divid');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
	copyParam(zipped, extraparams, 'subvid');
3129
fab471bee6c1 [facets] duhh, necessary fromformfilter hidden was missing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3107
diff changeset
    69
	copyParam(zipped, extraparams, 'fromformfilter');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
	// paginate used to know if the filter box is acting, in which case we
3008
094c597c1d01 [facets] reload breadcrumbs on facets search
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3006
diff changeset
    71
	// want to reload action box to match current selection (we don't want
094c597c1d01 [facets] reload breadcrumbs on facets search
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3006
diff changeset
    72
	// this from a table filter)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
	replacePageChunk(divid, rql, vid, extraparams, true, function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
	  jQuery(CubicWeb).trigger('facets-content-loaded', [divid, rql, vid, extraparams]);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
	});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
	if (paginate) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
	    // FIXME the edit box might not be displayed in which case we don't
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
	    // know where to put the potential new one, just skip this case
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
	    // for now
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
	    if (jQuery('#edit_box').length) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
		reloadComponent('edit_box', rql, 'boxes', 'edit_box');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
	    }
3008
094c597c1d01 [facets] reload breadcrumbs on facets search
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3006
diff changeset
    83
	    if (jQuery('#breadcrumbs').length) {
094c597c1d01 [facets] reload breadcrumbs on facets search
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3006
diff changeset
    84
		reloadComponent('breadcrumbs', rql, 'components', 'breadcrumbs');
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3008
diff changeset
    85
	    }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
	}
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1076
diff changeset
    87
	var d = asyncRemoteExec('filter_select_content', toupdate, rql);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
	d.addCallback(function(updateMap) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
	    for (facetId in updateMap) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
		var values = updateMap[facetId];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
		jqNode(facetId).find('.facetCheckBox').each(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
		    var value = this.getAttribute('cubicweb:value');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
		    if (!values.contains(value)) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
			if (!jQuery(this).hasClass('facetValueDisabled')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
			    jQuery(this).addClass('facetValueDisabled');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
		    } else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
			if (jQuery(this).hasClass('facetValueDisabled')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
			    jQuery(this).removeClass('facetValueDisabled');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
		    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
	    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
	});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
var SELECTED_IMG = baseuri()+"data/black-check.png";
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
var UNSELECTED_IMG = baseuri()+"data/no-check-no-border.png";
2012
64c82e15fa98 [facets] add new unchecked's checkbox image
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1894
diff changeset
   111
var UNSELECTED_BORDER_IMG = baseuri()+"data/black-uncheck.png";
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
933
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   113
function initFacetBoxEvents(root) {
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   114
    // facetargs : (divid, vid, paginate, extraargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
    root = root || document;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    jQuery(root).find('form').each(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
	var form = jQuery(this);
933
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   118
	// NOTE: don't evaluate facetargs here but in callbacks since its value
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   119
	//       may changes and we must send its value when the callback is
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   120
	//       called, not when the page is initialized
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   121
	var facetargs = form.attr('cubicweb:facetargs');
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   122
	if (facetargs !== undefined) {
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
	    form.submit(function() {
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   124
	        buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
	        return false;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
	    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
	    form.find('div.facet').each(function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
		var facet = jQuery(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
		facet.find('div.facetCheckBox').each(function (i) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
		    this.setAttribute('cubicweb:idx', i);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
		facet.find('div.facetCheckBox').click(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
		    var $this = jQuery(this);
3107
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   134
		    // NOTE : add test on the facet operator (i.e. OR, AND)
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   135
		    // if ($this.hasClass('facetValueDisabled')){
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   136
		    //  	    return
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   137
		    // }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
		    if ($this.hasClass('facetValueSelected')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
			$this.removeClass('facetValueSelected');
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   140
			$this.find('img').each(function (i){
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   141
			if (this.getAttribute('cubicweb:unselimg')){
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   142
			       this.setAttribute('src', UNSELECTED_BORDER_IMG);
2324
810fcd045f7c # set right alt values, fix disapearing values in facetCheckBox
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2013
diff changeset
   143
			       this.setAttribute('alt', (_('not selected')));
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   144
			    }
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   145
			    else{
2012
64c82e15fa98 [facets] add new unchecked's checkbox image
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1894
diff changeset
   146
			       this.setAttribute('src', UNSELECTED_IMG);
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2324
diff changeset
   147
			       this.setAttribute('alt', (_('not selected')));
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   148
			    }
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   149
			});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
			var index = parseInt($this.attr('cubicweb:idx'));
2342
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   151
			// we dont need to move the element when cubicweb:idx == 0
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   152
			if (index > 0){
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   153
			    var shift = jQuery.grep(facet.find('.facetValueSelected'), function (n) {
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   154
				    var nindex = parseInt(n.getAttribute('cubicweb:idx'));
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   155
				    return nindex > index;
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   156
				}).length;
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   157
			    index += shift;
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   158
			    var parent = this.parentNode;
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   159
			    var $insertAfter = jQuery(parent).find('.facetCheckBox:nth('+index+')');
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   160
			    if ( ! ($insertAfter.length == 1 && shift == 0) ) {
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   161
				// only rearrange element if necessary
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   162
				$insertAfter.after(this);
923111e6ab33 do nothing if the clicked facet has facetValueDisabled class; do not move facet when cubicweb:idx == 0
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2325
diff changeset
   163
			    }
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2324
diff changeset
   164
			}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
		    } else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
			var lastSelected = facet.find('.facetValueSelected:last');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
			if (lastSelected.length) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
			    lastSelected.after(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
			} else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
			    var parent = this.parentNode;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
			    jQuery(parent).prepend(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
			jQuery(this).addClass('facetValueSelected');
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2324
diff changeset
   174
			var $img = jQuery(this).find('img');
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2324
diff changeset
   175
			$img.attr('src', SELECTED_IMG).attr('alt', (_('selected')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
		    }
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   177
		    buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
		    facet.find('.facetBody').animate({scrollTop: 0}, '');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
		facet.find('select.facetOperator').change(function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
		    var nbselected = facet.find('div.facetValueSelected').length;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
		    if (nbselected >= 2) {
933
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   183
			buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
		    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
		facet.find('div.facetTitle').click(function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
		  facet.find('div.facetBody').toggleClass('hidden').toggleClass('opened');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
		  jQuery(this).toggleClass('opened');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
		   });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
	    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
	}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
// trigger this function on document ready event if you provide some kind of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
// persistent search (eg crih)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
function reorderFacetsItems(root){
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
    root = root || document;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    jQuery(root).find('form').each(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
	var form = jQuery(this);
933
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   202
	if (form.attr('cubicweb:facetargs')) {
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
	    form.find('div.facet').each(function() {
4
8a607bdc11dc [javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   204
		var facet = jQuery(this);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
		var lastSelected = null;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
		facet.find('div.facetCheckBox').each(function (i) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
		    var $this = jQuery(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
		    if ($this.hasClass('facetValueSelected')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
			if (lastSelected) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
			    lastSelected.after(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
			} else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
			    var parent = this.parentNode;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
			    jQuery(parent).prepend(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
			lastSelected = $this;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
		    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
	    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
	}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
// we need to differenciate cases where initFacetBoxEvents is called
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
// with one argument or without any argument. If we use `initFacetBoxEvents`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
// as the direct callback on the jQuery.ready event, jQuery will pass some argument
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
// of his, so we use this small anonymous function instead.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
jQuery(document).ready(function() {initFacetBoxEvents();});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
3006
4d85386b06a5 rename cubicweb.formfilter.js into cubicweb.facets.js
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2342
diff changeset
   229
CubicWeb.provide('facets.js');