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