added params for filtering pubdate before/after/between/during. added functional...
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / adv_global.js
1
2 attachEvt("common", "run", advgInit);
3 attachEvt("common", "locationChanged", advSyncCopyLocLink );
4
5 var COOKIE_NOGROUP_RECORDS = 'grpt';
6 var advSelectedOrg = null;
7
8 function advgInit() {
9
10         /* XXX */
11         if( cookieManager.read(COOKIE_NOGROUP_RECORDS) || SHOW_MR_DEFAULT )
12                 $('adv_group_titles').checked = true;
13
14         $n($('adv_global_tbody'), 'term').focus();
15
16         var extras = [ 
17                 FETCH_LIT_FORMS, 
18                 FETCH_ITEM_FORMS, 
19                 FETCH_ITEM_TYPES, 
20                 FETCH_AUDIENCES,
21                 FETCH_BIB_LEVELS 
22     ];
23
24         for( var x in extras ) {
25
26                 var req = new Request(extras[x]);
27
28                 if(x == 0) req.request.sel = $('adv_global_lit_form');
29                 if(x == 1) req.request.sel = $('adv_global_item_form');
30                 if(x == 2) req.request.sel = $('adv_global_item_type');
31                 if(x == 3) req.request.sel = $('adv_global_audience');
32                 if(x == 4) req.request.sel = $('adv_global_bib_level');
33
34                 req.callback(advDrawBibExtras);
35                 req.send();
36         }
37
38         var input = $n($('adv_global_trow'), 'term');
39         input.focus();
40         setEnterFunc(input, advSubmitGlobal);
41
42     if(getSort() && getSortDir()) {
43             setSelector($('adv_global_sort_by'), getSort());
44             setSelector($('adv_global_sort_dir'), getSortDir());
45         if(getSort() != 'rel')
46             $('adv_global_sort_dir').disabled = false;
47     }
48
49     if(getAvail())
50         $('opac.result.limit2avail').checked = true;
51
52     // not sure we want to propogate the pubdate filter, 
53     // since other filters are not propogated
54     //advInitPubFilter();
55
56     initSearchBoxes();
57     advSyncCopyLocLink(getLocation());
58 }
59
60 function advInitPubFilter() {
61     var i1 = $('adv_global_pub_date_1');
62     var i2 = $('adv_global_pub_date_2');
63     var sel = $('adv_global_pub_date_type');
64     if(getPubdBefore()) {
65         i1.value = getPubdBefore();
66         setSelector(sel, 'before');
67     } else if(getPubdAfter()) {
68         i1.value = getPubdAfter();
69         setSelector(sel, 'after');
70     } else if(getPubdBetween()) {
71         var values = getPubdBetween().split(','); 
72         i1.value = values[0]
73         if(values[0] == values[1]) {
74             setSelector(sel, 'equals');
75         } else {
76             setSelector(sel, 'between');
77             i2.value = values[1];
78         }
79     }
80 }
81
82 function advSyncCopyLocLink(org) {
83     // display the option to filter by copy location
84     advLocationsLoaded = false;
85     advSelectedOrg = org;
86     removeChildren($('adv_copy_location_filter_select'));
87
88     if(isTrue(findOrgType(findOrgUnit(org).ou_type()).can_have_vols())) {
89         unHideMe($('adv_copy_location_filter_row'));
90         advLoadCopyLocations(org); 
91     } else {
92         hideMe($('adv_copy_location_filter_row'));
93     }
94
95 }
96
97 function clearSearchBoxes() {
98     var rows = $('adv_global_tbody').getElementsByTagName('tr');
99     for(var t = 0; t < rows.length; t++) {
100         if($n(rows[t], 'term')) {
101             $n(rows[t], 'term').value = '';
102             setSelector($n(rows[t], 'contains'), 'contains');
103             setSelector($n(rows[t], 'type'), 'keyword');
104         }
105     }
106     $n(rows[0], 'term').focus();
107 }
108
109
110 function initSearchBoxes() {
111     /* loads the compiled search from the search cookie 
112         and sets the widgets accordingly */
113
114     search = cookieManager.read(COOKIE_SEARCH);
115     if(!search) return;
116     _debug("loaded compiled search cookie: " + search);
117
118     search = JSON2js(search);
119     if(!search) return;
120
121     var types = getObjectKeys(search.searches);
122
123     /* pre-add the needed rows */
124     while($('adv_global_tbody').getElementsByTagName('tr').length - 1 < types.length)
125         advAddGblRow();
126
127     var rows = $('adv_global_tbody').getElementsByTagName('tr');
128
129     for(var t = 0; t < types.length; t++) {
130         var row = rows[t];
131         setSelector($n(row, 'type'), types[t]);
132         var term = search.searches[types[t]].term;
133
134         /* if this is a single -<term> search, set the selector to nocontains */
135         if(match = term.match(/^-(\w+)$/)) {
136             term = match[1];
137             setSelector($n(row, 'contains'), 'nocontains');
138         }
139         $n(row, 'term').value = term;
140     }
141 }
142
143 function advAddGblRow() {
144         var tbody = $("adv_global_tbody");
145         var newrow = $("adv_global_trow").cloneNode(true);
146         tbody.insertBefore(newrow, $("adv_global_addrow"));
147         var input = $n(newrow, "term");
148         input.value = "";
149         setEnterFunc(input, advSubmitGlobal);
150         $n(newrow, 'type').focus();
151 }
152
153 function advDrawBibExtras(r) {
154         var data = r.getResultObject();
155         var sel = r.sel;
156
157         data = data.sort( /* sort alphabetically */
158                 function(a,b) { 
159                         if( a.value() < b.value() ) return -1;
160                         if( a.value() > b.value() ) return 1;
161                         return 0;
162                 }
163         );
164
165         for( var d = 0; d < data.length; d++ ) {
166                 var thing = data[d];
167                 var opt = insertSelectorVal( sel, -1, thing.value(), thing.code() );
168                 opt.setAttribute('title', thing.value());
169         }
170 }
171
172 function advSelToStringList(sel) {
173         var list = getSelectedList(sel);
174         var vals = [];
175         for( var i = 0; i < list.length; i++ ) {
176                 var str = list[i];
177                 for( var j = 0; j < str.length; j++ ) {
178                         //if(str.charAt(j) == ' ') continue;
179                         vals.push(str.charAt(j));
180                 }
181         }
182         return vals.toString();
183 }
184
185 function advGetVisSelectorVals(id) {
186         var basic = id + '_basic';
187         if(! $(id).className.match(/hide_me/)) 
188                 return advSelToStringList($(id));
189         return advSelToStringList($(basic));
190 }
191
192 function advSubmitGlobal() {
193         
194         var sortdir = getSelectorVal($('adv_global_sort_dir'));
195         var sortby  = getSelectorVal($('adv_global_sort_by'));
196
197         var litforms  = advGetVisSelectorVals('adv_global_lit_form');
198         var itemforms = advGetVisSelectorVals('adv_global_item_form');
199         var itemtypes = advGetVisSelectorVals('adv_global_item_type');
200         var audiences = advGetVisSelectorVals('adv_global_audience');
201         var biblevels = advGetVisSelectorVals('adv_global_bib_level');
202     var locations = getSelectedList($('adv_copy_location_filter_select')) + '';
203         var languages = getSelectedList($('adv_global_lang')) + '';     
204     var limit2avail = $('opac.result.limit2avail').checked ? 1 : ''
205
206         var searches = advBuildSearchBlob();
207         if(!searches) return;
208
209         clearSearchParams();
210
211         var args = {};
212         args.page = MRESULT;
213         args[PARAM_ITEMFORM] = itemforms;
214         args[PARAM_ITEMTYPE] = itemtypes;
215         args[PARAM_BIBLEVEL] = biblevels;
216         args[PARAM_LITFORM]     = litforms;
217         args[PARAM_AUDIENCE]    = audiences;
218         args[PARAM_LANGUAGE] = languages;
219         args[PARAM_COPYLOCS] = locations;
220         //args[PARAM_SEARCHES]  = js2JSON(searches); /* break these out */
221         args[PARAM_DEPTH]               = depthSelGetDepth();
222         args[PARAM_LOCATION]    = depthSelGetNewLoc();
223         args[PARAM_SORT]                = sortby;
224         args[PARAM_SORT_DIR]    = sortdir;
225         args[PARAM_ADVTYPE]     = ADVTYPE_MULTI;
226         args[PARAM_STYPE]               = "";
227         args[PARAM_TERM]                = searches;
228         args[PARAM_AVAIL]               = limit2avail;
229
230     // publicate year filtering
231     var pub1;
232     if( (pub1 = $('adv_global_pub_date_1').value) && (''+pub1).match(/\d{4}/)) {
233         switch(getSelectorVal($('adv_global_pub_date_type'))) {
234             case 'equals':
235                     args[PARAM_PUBD_BETWEEN] = pub1+','+pub1;
236                 break;
237             case 'before':
238                     args[PARAM_PUBD_BEFORE] = pub1;
239                 break;
240             case 'after':
241                     args[PARAM_PUBD_AFTER] = pub1;
242                 break;
243             case 'between':
244                 var pub2 = $('adv_global_pub_date_2').value;
245                 if((''+pub2).match(/\d{4}/))
246                         args[PARAM_PUBD_BETWEEN] = pub1+','+pub2;
247                 break;
248         }
249     }
250
251         /* pubdate sorting causes a record (not metarecord) search */
252         if( sortby == SORT_TYPE_PUBDATE || !$('adv_group_titles').checked ) {
253                 args.page = RRESULT;
254                 args[PARAM_RTYPE] = RTYPE_MULTI;
255         }
256
257         if($('adv_group_titles').checked ) 
258                 cookieManager.write(COOKIE_NOGROUP_RECORDS,'1',-1);
259         else
260                 cookieManager.write(COOKIE_NOGROUP_RECORDS,'');
261
262
263         goTo(buildOPACLink(args));
264 }
265
266
267 function advBuildSearchBlob() {
268
269         var searches = '';
270         var tbody    = $('adv_global_tbody');
271         var rows     = tbody.getElementsByTagName('tr');
272
273         for( var i = 0; i < rows.length; i++ ) {
274
275                 var row = rows[i];
276                 if(!(row && typeof row == 'object')) continue;
277                 if(!row.getAttribute('type')) continue;
278                 
279                 var stype        = getSelectorVal($n(row, 'type'));
280                 var contains = getSelectorVal($n(row, 'contains'));
281                 var term                 = $n(row, 'term').value;
282                 if(!term) continue;
283
284                 var string = "";
285                 switch(contains) {
286                         case 'contains' : 
287                                 string += " " + term; 
288                                 break;
289
290                         case 'nocontains' : {
291                                 var words = term.split(" ");
292                                         for( var j in words ) 
293                                                 string += " -" + words[j];
294                                 }
295                                 break;
296
297                         case 'exact' : 
298                                 if(term.indexOf('"') > -1) string += " " + term;
299                                 else string += " \"" + term + "\"";
300                                 break;
301                 }
302                 if(string) {
303                         string = string.replace(/'/g,' ');
304                         string = string.replace(/\\/g,' ');
305             string = string.replace(/^\s*/,'');
306             string = string.replace(/\s*$/,'');
307                         //searches[stype].term = string;
308             if(searches) searches += ' ';
309             searches += stype + ':'+ string;
310                 }
311         }
312
313     _debug("created search query " + searches);
314         return searches;
315 }
316
317
318 // retrieves the shelving locations
319 var advLocationsLoaded = false;
320 function advLoadCopyLocations(org) {
321     if(org == null) 
322         org = advSelectedOrg;
323     var req = new Request(FETCH_COPY_LOCATIONS, org);
324     req.callback(advShowCopyLocations);
325     req.send();
326     advLocationsLoaded = true;
327 }
328
329 // inserts the shelving locations into the multi-select
330 function advShowCopyLocations(r) {
331     var locations = r.getResultObject();
332     var sel = $('adv_copy_location_filter_select');
333     for(var i = 0; i < locations.length; i++) 
334         insertSelectorVal(sel, -1, locations[i].name(), locations[i].id());
335 }
336
337