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