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