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