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