author | sylvain.thenault@logilab.fr |
Fri, 30 Jan 2009 15:26:25 +0100 | |
changeset 528 | 60bd171ecd04 |
parent 434 | 915b4c6ffe5e |
child 933 | ad99da207edd |
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); |
0 | 46 |
var d = async_remote_exec('filter_build_rql', zipped[0], zipped[1]); |
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 |
} |
|
83 |
var d = async_remote_exec('filter_select_content', toupdate, rql); |
|
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"; |
|
107 |
||
108 |
function initFacetBoxEvents(root){ |
|
109 |
root = root || document; |
|
110 |
jQuery(root).find('form').each(function () { |
|
111 |
var form = jQuery(this); |
|
434
915b4c6ffe5e
[javascript][facets] facetargs wasn't evaluated in all cases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
368
diff
changeset
|
112 |
var facetargs = evalJSON(form.attr('cubicweb:facetargs')); |
915b4c6ffe5e
[javascript][facets] facetargs wasn't evaluated in all cases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
368
diff
changeset
|
113 |
if (facetargs !== undefined && facetargs.length) { |
0 | 114 |
form.submit(function() { |
115 |
buildRQL.apply(null, facetargs); //(divid, vid, paginate, extraargs); |
|
116 |
return false; |
|
117 |
}); |
|
118 |
form.find('div.facet').each(function() { |
|
119 |
var facet = jQuery(this); |
|
120 |
facet.find('div.facetCheckBox').each(function (i) { |
|
121 |
this.setAttribute('cubicweb:idx', i); |
|
122 |
}); |
|
123 |
facet.find('div.facetCheckBox').click(function () { |
|
124 |
var $this = jQuery(this); |
|
125 |
if ($this.hasClass('facetValueSelected')) { |
|
126 |
$this.removeClass('facetValueSelected'); |
|
127 |
$this.find('img').attr('src', UNSELECTED_IMG); |
|
128 |
var index = parseInt($this.attr('cubicweb:idx')); |
|
129 |
var shift = jQuery.grep(facet.find('.facetValueSelected'), function (n) { |
|
130 |
var nindex = parseInt(n.getAttribute('cubicweb:idx')); |
|
131 |
return nindex > index; |
|
132 |
}).length; |
|
133 |
index += shift; |
|
134 |
var parent = this.parentNode; |
|
135 |
jQuery(parent).find('.facetCheckBox:nth('+index+')').after(this); |
|
136 |
} else { |
|
137 |
var lastSelected = facet.find('.facetValueSelected:last'); |
|
138 |
if (lastSelected.length) { |
|
139 |
lastSelected.after(this); |
|
140 |
} else { |
|
141 |
var parent = this.parentNode; |
|
142 |
jQuery(parent).prepend(this); |
|
143 |
} |
|
144 |
jQuery(this).addClass('facetValueSelected'); |
|
145 |
jQuery(this).find('img').attr('src', SELECTED_IMG); |
|
146 |
} |
|
147 |
buildRQL.apply(null, facetargs); // (divid, vid, paginate, extraargs); |
|
148 |
facet.find('.facetBody').animate({scrollTop: 0}, ''); |
|
149 |
}); |
|
150 |
facet.find('select.facetOperator').change(function() { |
|
151 |
var nbselected = facet.find('div.facetValueSelected').length; |
|
152 |
if (nbselected >= 2) { |
|
153 |
buildRQL.apply(null, facetargs); // (divid, vid, paginate, extraargs); |
|
154 |
} |
|
155 |
}); |
|
156 |
facet.find('div.facetTitle').click(function() { |
|
157 |
facet.find('div.facetBody').toggleClass('hidden').toggleClass('opened'); |
|
158 |
jQuery(this).toggleClass('opened'); |
|
159 |
}); |
|
160 |
||
161 |
}); |
|
162 |
} |
|
163 |
}); |
|
164 |
} |
|
165 |
||
166 |
// trigger this function on document ready event if you provide some kind of |
|
167 |
// persistent search (eg crih) |
|
168 |
function reorderFacetsItems(root){ |
|
169 |
root = root || document; |
|
170 |
jQuery(root).find('form').each(function () { |
|
171 |
var form = jQuery(this); |
|
172 |
var facetargs = form.attr('cubicweb:facetargs'); |
|
173 |
if (facetargs) { |
|
174 |
form.find('div.facet').each(function() { |
|
4
8a607bdc11dc
[javascript] indentation fixes + missing semi colons
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
175 |
var facet = jQuery(this); |
0 | 176 |
var lastSelected = null; |
177 |
facet.find('div.facetCheckBox').each(function (i) { |
|
178 |
var $this = jQuery(this); |
|
179 |
if ($this.hasClass('facetValueSelected')) { |
|
180 |
if (lastSelected) { |
|
181 |
lastSelected.after(this); |
|
182 |
} else { |
|
183 |
var parent = this.parentNode; |
|
184 |
jQuery(parent).prepend(this); |
|
185 |
} |
|
186 |
lastSelected = $this; |
|
187 |
} |
|
188 |
}); |
|
189 |
}); |
|
190 |
} |
|
191 |
}); |
|
192 |
} |
|
193 |
||
194 |
// we need to differenciate cases where initFacetBoxEvents is called |
|
195 |
// with one argument or without any argument. If we use `initFacetBoxEvents` |
|
196 |
// as the direct callback on the jQuery.ready event, jQuery will pass some argument |
|
197 |
// of his, so we use this small anonymous function instead. |
|
198 |
jQuery(document).ready(function() {initFacetBoxEvents();}); |
|
199 |
||
200 |
CubicWeb.provide('formfilter.js'); |