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