3faa54734db2fd6068a777600ff5077a15030ec6
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / adv_global.js
1
2 attachEvt("common", "run", advgInit);
3
4 var COOKIE_NOGROUP_RECORDS = 'grpt';
5
6 function advgInit() {
7
8         /* XXX */
9         if( cookieManager.read(COOKIE_NOGROUP_RECORDS) || SHOW_MR_DEFAULT )
10                 $('adv_group_titles').checked = true;
11
12         $n($('adv_global_tbody'), 'term').focus();
13
14         var extras = [ 
15                 FETCH_LIT_FORMS, 
16                 FETCH_ITEM_FORMS, 
17                 FETCH_ITEM_TYPES, 
18                 FETCH_AUDIENCES ];
19
20         for( var x in extras ) {
21
22                 var req = new Request(extras[x]);
23
24                 if(x == 0) req.request.sel = $('adv_global_lit_form');
25                 if(x == 1) req.request.sel = $('adv_global_item_form');
26                 if(x == 2) req.request.sel = $('adv_global_item_type');
27                 if(x == 3) req.request.sel = $('adv_global_audience');
28
29                 req.callback(advDrawBibExtras);
30                 req.send();
31         }
32
33         var input = $n($('adv_global_trow'), 'term');
34         input.focus();
35         setEnterFunc(input, advSubmitGlobal);
36 }
37
38 function advAddGblRow() {
39         var tbody = $("adv_global_tbody");
40         var newrow = $("adv_global_trow").cloneNode(true);
41         tbody.insertBefore(newrow, $("adv_global_addrow"));
42         var input = $n(newrow, "term");
43         input.value = "";
44         setEnterFunc(input, advSubmitGlobal);
45         $n(newrow, 'type').focus();
46 }
47
48 function advDrawBibExtras(r) {
49         var data = r.getResultObject();
50         var sel = r.sel;
51
52         data = data.sort( /* sort alphabetically */
53                 function(a,b) { 
54                         if( a.value() < b.value() ) return -1;
55                         if( a.value() > b.value() ) return 1;
56                         return 0;
57                 }
58         );
59
60         for( var d = 0; d < data.length; d++ ) {
61                 var thing = data[d];
62                 var opt = insertSelectorVal( sel, -1, thing.value(), thing.code() );
63                 opt.setAttribute('title', thing.value());
64         }
65 }
66
67 function advSelToStringList(sel) {
68         var list = getSelectedList(sel);
69         var vals = [];
70         for( var i = 0; i < list.length; i++ ) {
71                 var str = list[i];
72                 for( var j = 0; j < str.length; j++ ) {
73                         if(str.charAt(j) == ' ') continue;
74                         vals.push(str.charAt(j));
75                 }
76         }
77         return vals.toString();
78 }
79
80 function advGetVisSelectorVals(id) {
81         var basic = id + '_basic';
82         if(! $(id).className.match(/hide_me/)) 
83                 return advSelToStringList($(id));
84         return advSelToStringList($(basic));
85 }
86
87 function advSubmitGlobal() {
88         
89         var sortdir = getSelectorVal($('adv_global_sort_dir'));
90         var sortby  = getSelectorVal($('adv_global_sort_by'));
91
92         var litforms  = advGetVisSelectorVals('adv_global_lit_form');
93         var itemforms = advGetVisSelectorVals('adv_global_item_form');
94         var itemtypes = advGetVisSelectorVals('adv_global_item_type');
95         var audiences = advGetVisSelectorVals('adv_global_audience');
96         var languages = getSelectedList($('adv_global_lang')) + '';     
97
98         var searches = advBuildSearchBlob();
99         if(!searches) return;
100
101         clearSearchParams();
102
103         var args = {};
104         args.page = MRESULT;
105         args[PARAM_ITEMFORM] = itemforms;
106         args[PARAM_ITEMTYPE] = itemtypes;
107         args[PARAM_LITFORM]     = litforms;
108         args[PARAM_AUDIENCE]    = audiences;
109         args[PARAM_LANGUAGE] = languages;
110         args[PARAM_SEARCHES]    = js2JSON(searches); /* break these out */
111         args[PARAM_DEPTH]               = depthSelGetDepth();
112         args[PARAM_LOCATION]    = depthSelGetNewLoc();
113         args[PARAM_SORT]                = sortby;
114         args[PARAM_SORT_DIR]    = sortdir;
115         args[PARAM_ADVTYPE]     = ADVTYPE_MULTI;
116         args[PARAM_STYPE]               = "";
117         args[PARAM_TERM]                = "";
118
119         /* pubdate sorting causes a record (not metarecord) search */
120         if( sortby == SORT_TYPE_PUBDATE || !$('adv_group_titles').checked ) {
121                 args.page = RRESULT;
122                 args[PARAM_RTYPE] = RTYPE_MULTI;
123         }
124
125         if($('adv_group_titles').checked ) 
126                 cookieManager.write(COOKIE_NOGROUP_RECORDS,'1',-1);
127         else
128                 cookieManager.write(COOKIE_NOGROUP_RECORDS,'');
129
130
131         goTo(buildOPACLink(args));
132 }
133
134
135 function advBuildSearchBlob() {
136
137         var searches;
138         var tbody    = $('adv_global_tbody');
139         var rows     = tbody.getElementsByTagName('tr');
140
141         for( var i = 0; i < rows.length; i++ ) {
142
143                 var row = rows[i];
144                 if(!(row && typeof row == 'object')) continue;
145                 if(!row.getAttribute('type')) continue;
146                 
147                 var stype        = getSelectorVal($n(row, 'type'));
148                 var contains = getSelectorVal($n(row, 'contains'));
149                 var term                 = $n(row, 'term').value;
150                 if(!term) continue;
151
152                 var string = "";
153
154                 if(!searches) searches = {};
155
156                 if(searches[stype]) 
157                         string = searches[stype].term;
158                 else 
159                         searches[stype] = { term : "" };
160
161                 switch(contains) {
162                         case 'contains' : 
163                                 string += " " + term; 
164                                 break;
165
166                         case 'nocontains' : {
167                                 var words = term.split(" ");
168                                         for( var j in words ) 
169                                                 string += " -" + words[j];
170                                 }
171                                 break;
172
173                         case 'exact' : 
174                                 if(term.indexOf('"') > -1) string += " " + term;
175                                 else string += " \"" + term + "\"";
176                                 break;
177                 }
178                 if(string) {
179                         string = string.replace(/'/,'');
180                         searches[stype].term = string;
181                 }
182         }
183
184         return searches;
185 }
186
187
188
189