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