web/data/cubicweb.facets.js
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Wed, 07 Apr 2010 11:56:30 +0200
branchstable
changeset 5171 1c2567af37dd
parent 5112 5bf8baecfaf8
child 5658 7b9553a9db65
child 5668 7bd6a8a126b8
permissions -rw-r--r--
merge
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) {
5112
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3129
diff changeset
    50
	    var bkPath = 'view?rql=' + escape(rql);
5
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) {
5112
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3129
diff changeset
    52
		bkPath += '&vid=' + escape(vid);
5
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    53
	    }
5112
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3129
diff changeset
    54
	    var bkUrl = $bkLink.attr('cubicweb:target') + '&path=' + escape(bkPath);
5
64072193bd48 simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4
diff changeset
    55
	    $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
    56
	}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
	var toupdate = result[1];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
	var extraparams = vidargs;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
	var displayactions = jQuery('#' + divid).attr('cubicweb:displayactions');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
	if (displayactions) { extraparams['displayactions'] = displayactions; }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
	if (paginate) { extraparams['paginate'] = '1'; }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
	// copy some parameters
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
	// XXX cleanup vid/divid mess
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
	// if vid argument is specified , the one specified in form params will
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
	// be overriden by replacePageChunk
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
	copyParam(zipped, extraparams, 'vid');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
	extraparams['divid'] = divid;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
	copyParam(zipped, extraparams, 'divid');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
	copyParam(zipped, extraparams, 'subvid');
3129
fab471bee6c1 [facets] duhh, necessary fromformfilter hidden was missing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3107
diff changeset
    70
	copyParam(zipped, extraparams, 'fromformfilter');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
	// 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
    72
	// 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
    73
	// this from a table filter)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
	replacePageChunk(divid, rql, vid, extraparams, true, function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
	  jQuery(CubicWeb).trigger('facets-content-loaded', [divid, rql, vid, extraparams]);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
	});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
	if (paginate) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
	    // 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
    79
	    // know where to put the potential new one, just skip this case
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
	    // for now
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
	    if (jQuery('#edit_box').length) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
		reloadComponent('edit_box', rql, 'boxes', 'edit_box');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
	    }
3008
094c597c1d01 [facets] reload breadcrumbs on facets search
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3006
diff changeset
    84
	    if (jQuery('#breadcrumbs').length) {
094c597c1d01 [facets] reload breadcrumbs on facets search
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3006
diff changeset
    85
		reloadComponent('breadcrumbs', rql, 'components', 'breadcrumbs');
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3008
diff changeset
    86
	    }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
	}
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1076
diff changeset
    88
	var d = asyncRemoteExec('filter_select_content', toupdate, rql);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
	d.addCallback(function(updateMap) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
	    for (facetId in updateMap) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
		var values = updateMap[facetId];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
		jqNode(facetId).find('.facetCheckBox').each(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
		    var value = this.getAttribute('cubicweb:value');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
		    if (!values.contains(value)) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
			if (!jQuery(this).hasClass('facetValueDisabled')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
			    jQuery(this).addClass('facetValueDisabled');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
		    } else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
			if (jQuery(this).hasClass('facetValueDisabled')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
			    jQuery(this).removeClass('facetValueDisabled');
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
var SELECTED_IMG = baseuri()+"data/black-check.png";
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
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
   112
var UNSELECTED_BORDER_IMG = baseuri()+"data/black-uncheck.png";
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
933
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   114
function initFacetBoxEvents(root) {
ad99da207edd late evaluation of cubicweb:facetargs is necessary since its value may change
sylvain.thenault@logilab.fr
parents: 434
diff changeset
   115
    // facetargs : (divid, vid, paginate, extraargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    root = root || document;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    jQuery(root).find('form').each(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
	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
   119
	// 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
   120
	//       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
   121
	//       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
   122
	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
   123
	if (facetargs !== undefined) {
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
	    form.submit(function() {
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   125
	        buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
	        return false;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
	    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
	    form.find('div.facet').each(function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
		var facet = jQuery(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
		facet.find('div.facetCheckBox').each(function (i) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
		    this.setAttribute('cubicweb:idx', i);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
		facet.find('div.facetCheckBox').click(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
		    var $this = jQuery(this);
3107
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   135
		    // 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
   136
		    // if ($this.hasClass('facetValueDisabled')){
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   137
		    //  	    return
0829808e93c1 fix OR in facets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3011
diff changeset
   138
		    // }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
		    if ($this.hasClass('facetValueSelected')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
			$this.removeClass('facetValueSelected');
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   141
			$this.find('img').each(function (i){
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   142
			if (this.getAttribute('cubicweb:unselimg')){
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   143
			       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
   144
			       this.setAttribute('alt', (_('not selected')));
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   145
			    }
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   146
			    else{
2012
64c82e15fa98 [facets] add new unchecked's checkbox image
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1894
diff changeset
   147
			       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
   148
			       this.setAttribute('alt', (_('not selected')));
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   149
			    }
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   150
			});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
			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
   152
			// 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
   153
			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
   154
			    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
   155
				    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
   156
				    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
   157
				}).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
   158
			    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
   159
			    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
   160
			    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
   161
			    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
   162
				// 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
   163
				$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
   164
			    }
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2324
diff changeset
   165
			}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
		    } else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
			var lastSelected = facet.find('.facetValueSelected:last');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
			if (lastSelected.length) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
			    lastSelected.after(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
			} else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
			    var parent = this.parentNode;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
			    jQuery(parent).prepend(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
			jQuery(this).addClass('facetValueSelected');
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2324
diff changeset
   175
			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
   176
			$img.attr('src', SELECTED_IMG).attr('alt', (_('selected')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
		    }
1076
c510e1935ccc new image for unchecked state
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 933
diff changeset
   178
		    buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs')));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
		    facet.find('.facetBody').animate({scrollTop: 0}, '');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
		facet.find('select.facetOperator').change(function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
		    var nbselected = facet.find('div.facetValueSelected').length;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
		    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
   184
			buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs')));
0
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
		});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
		facet.find('div.facetTitle').click(function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
		  facet.find('div.facetBody').toggleClass('hidden').toggleClass('opened');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
		  jQuery(this).toggleClass('opened');
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
// 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
   198
// persistent search (eg crih)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
function reorderFacetsItems(root){
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    root = root || document;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    jQuery(root).find('form').each(function () {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
	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
   203
	if (form.attr('cubicweb:facetargs')) {
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
	    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
   205
		var facet = jQuery(this);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
		var lastSelected = null;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
		facet.find('div.facetCheckBox').each(function (i) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
		    var $this = jQuery(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
		    if ($this.hasClass('facetValueSelected')) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
			if (lastSelected) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
			    lastSelected.after(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
			} else {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
			    var parent = this.parentNode;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
			    jQuery(parent).prepend(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
			}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
			lastSelected = $this;
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
// we need to differenciate cases where initFacetBoxEvents is called
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
// with one argument or without any argument. If we use `initFacetBoxEvents`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
// 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
   227
// of his, so we use this small anonymous function instead.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
jQuery(document).ready(function() {initFacetBoxEvents();});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
3006
4d85386b06a5 rename cubicweb.formfilter.js into cubicweb.facets.js
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2342
diff changeset
   230
CubicWeb.provide('facets.js');