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