not submitting when no search is done
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / advanced.js
1 /* advanced search interface */
2
3 attachEvt("common", "run", advInit);
4
5 /* tests the cookie rresult stuff 
6
7 function advTestCookie() {
8         var ids = [ 
9                 40791, 40792, 40793, 40794, 40795, 40797, 40798, 40799, 362178, 362179, 
10                 362180, 362170, 338215, 333398, 329010, 17, 362182, 362183, 362184, 362185 ];
11
12         cookieManager.write(COOKIE_RIDS, js2JSON(ids), '+1h');
13         var a = {};
14         a.page = RRESULT;
15         a[PARAM_RTYPE] = RTYPE_COOKIE;
16         goTo(buildOPACLink(a));
17 }
18 */
19
20
21
22 function advInit() { 
23         $('advanced.wizard.contains').focus();
24         depthSelInit(); 
25
26         var wiz = [
27                 'advanced.wizard.contains',
28                 'advanced.wizard.nocontains',
29                 'advanced.wizard.exact',
30                 'search_type_selector',
31                 'advanced.wizard.form_selector' ];
32         for( var node in wiz ) 
33                 setEnterFunc( $(wiz[node]), advWizardRun );
34
35         var ref = [
36                 'advanced.refined.title_contains',
37                 'advanced.refined.author_contains',
38                 'advanced.refined.subject_contains',
39                 'advanced.refined.series_contains',
40                 'advanced.refined.form_selector',
41                 'advanced.refined.series_type',
42                 'advanced.refined.subject_type',
43                 'advanced.refined.author_type',
44                 'advanced.refined.title_type' ];
45         for( var i in ref ) 
46                 setEnterFunc( $(ref[i]), advRefinedRun );
47
48         setEnterFunc($('opac.advanced.quick.isbn'), advISBNRun );
49         setEnterFunc($('opac.advanced.quick.issn'), advISSNRun );
50         setEnterFunc($('opac.advanced.quick.cn'), advCNRun );
51         setEnterFunc( $n( $('advanced.marc.tbody'), 'advanced.marc.value'), advMARCRun );
52 }
53
54
55 function advWizardRun() {
56         var contains = $('advanced.wizard.contains').value;
57         var nocontains = $('advanced.wizard.nocontains').value; 
58         var exact       = $('advanced.wizard.exact').value; 
59         var form                = getSelectorVal($('advanced.wizard.form_selector'));
60         var type                = getSelectorVal($('search_type_selector'));
61         var sort                = getSelectorVal($('advanced.wizard.sort_by'));
62         var sortdir     = getSelectorVal($('advanced.wizard.sort_dir'));
63
64         var arg = {};
65         arg.page = MRESULT;
66         arg[PARAM_FORM]         = form;
67         arg[PARAM_STYPE]                = type;
68         arg[PARAM_OFFSET]               = 0;
69         arg[PARAM_DEPTH]                = depthSelGetDepth();
70         arg[PARAM_LOCATION]     = depthSelGetNewLoc();
71         arg[PARAM_TERM]         = advBuildSearch( contains, nocontains, exact );
72         arg[PARAM_SORT]         = sort;
73         arg[PARAM_SORT_DIR]     = sortdir;
74
75         if( sort == SORT_TYPE_PUBDATE ) {
76                 arg.page = RRESULT;
77                 arg[PARAM_RTYPE]        = type;
78         }
79
80         if(!arg[PARAM_TERM]) return;
81
82         goTo(buildOPACLink(arg));
83 }
84
85 function advBuildSearch( contains, nocontains, exact ) {
86         var string = "";
87         if(contains) string = contains;
88
89         if( exact ) {
90                 if(exact.indexOf('"') > -1) string += " " + exact;
91                 else string += " \"" + exact + "\"";
92         }
93
94         if(nocontains) {
95                 var words = nocontains.split(" ");
96                 for( var i in words ) 
97                         string += " -" + words[i];
98         }
99         return string;
100 }
101
102
103 function advRefinedRun() {
104         var title       = $('advanced.refined.title_contains').value;
105         var author      = $('advanced.refined.author_contains').value;
106         var subject = $('advanced.refined.subject_contains').value;
107         var series      = $('advanced.refined.series_contains').value;
108         var form = getSelectorVal($('advanced.refined.form_selector'));
109         var sort                = getSelectorVal($('advanced.refined.sort_by'));
110         var sortdir     = getSelectorVal($('advanced.refined.sort_dir'));
111
112         var blob = {};
113         title = advRefinedTerm('title', title);
114         author = advRefinedTerm('author', author);
115         subject = advRefinedTerm('subject', subject);
116         series = advRefinedTerm('series', series);
117
118         if(title) { blob.title = {}; blob.title.term =  title; }
119         if(author) { blob.author = {}; blob.author.term = author;}
120         if(subject) { blob.subject = {}; blob.subject.term = subject;}
121         if(series) { blob.series = {}; blob.series.term = series; }
122
123         if( !(title || author|| subject|| series) ) return;
124
125         var arg                                 = {};
126         arg.page                                        = MRESULT;
127         arg[PARAM_FORM]         = form;
128         arg[PARAM_STYPE]                = "";
129         arg[PARAM_TERM]         = "";
130         arg[PARAM_ADVTERM]      = js2JSON(blob);
131         arg[PARAM_DEPTH]                = depthSelGetDepth();
132         arg[PARAM_LOCATION]     = depthSelGetNewLoc();
133         arg[PARAM_OFFSET]               = 0;
134         arg[PARAM_ADVTYPE]      = ADVTYPE_MULTI;
135         arg[PARAM_SORT]         = sort;
136         arg[PARAM_SORT_DIR]     = sortdir;
137
138         goTo(buildOPACLink(arg));
139
140 }
141
142 function advISBNRun() {
143         var isbn = $('opac.advanced.quick.isbn').value;
144         if(!isbn) return;
145         var arg                                 = {};
146         arg.page                                        = MRESULT;
147         arg[PARAM_STYPE]                = "";
148         arg[PARAM_TERM]         = "";
149         arg[PARAM_ADVTERM]      = isbn;
150         arg[PARAM_OFFSET]               = 0;
151         arg[PARAM_ADVTYPE]      = ADVTYPE_ISBN;
152         goTo(buildOPACLink(arg));
153 }
154
155 function advISSNRun() {
156         var issn = $('opac.advanced.quick.issn').value;
157         if(!issn) return;
158         var arg                                 = {};
159         arg.page                                        = MRESULT;
160         arg[PARAM_STYPE]                = "";
161         arg[PARAM_TERM]         = "";
162         arg[PARAM_ADVTERM]      = issn;
163         arg[PARAM_OFFSET]               = 0;
164         arg[PARAM_ADVTYPE]      = ADVTYPE_ISSN;
165         goTo(buildOPACLink(arg));
166 }
167
168 function advCNRun() {
169         var cn = $('opac.advanced.quick.cn').value;
170         if(!cn) return;
171         var arg                 = {};
172         arg.page                        = CNBROWSE;
173         arg[PARAM_CN]   = cn;
174         goTo(buildOPACLink(arg));
175 }
176
177
178 function advRefinedTerm( type, term ) {
179         var t = getSelectorVal($('advanced.refined.' + type + '_type'));
180         var string;
181
182         if( t == 'contains' ) string = advBuildSearch( term );
183         else if( t == 'nocontains' ) string = advBuildSearch( null, term );
184         else if( t == 'exact' ) string = advBuildSearch( null, null, term );
185
186         return string;
187 }
188
189
190 function advAddMARC() {
191         $('advanced.marc.tbody').insertBefore(
192                 $('advanced.marc.template').cloneNode(true),
193                 $('advanced.marc.submit.row'));
194 }
195
196 function advMARCRun() {
197
198         var t = $('advanced.marc.tbody');
199         var searches = [];
200         /*
201         for( var c in t.childNodes ) {
202                 var child = t.childNodes[c];
203                 if(child.nodeName == 'tr') {
204                         alert("hi");
205                         var val = advExtractMARC(child);
206                         if(val) searches.push(val);
207                 }
208         }
209         */
210
211         var children = t.getElementsByTagName('tr');
212         for( var c in children ) {
213                 var child = children[c];
214                 if(!(child && child.innerHTML)) continue;
215                 var val = advExtractMARC(child);
216                 if(val) searches.push(val);
217         }
218
219         if(searches.length == 0) return;
220
221         var arg = {};
222         arg.page = MRESULT;
223         arg[PARAM_FORM] = 'all'
224         arg[PARAM_STYPE] = "";
225         arg[PARAM_OFFSET] = 0;
226         arg[PARAM_DEPTH]        = depthSelGetDepth();
227         arg[PARAM_LOCATION]     = depthSelGetNewLoc();
228         arg[PARAM_ADVTERM] = js2JSON(searches);
229         arg[PARAM_ADVTYPE] = ADVTYPE_MARC;
230         arg[PARAM_TERM] = "";
231
232         goTo(buildOPACLink(arg));
233 }
234
235
236 /* EXAMPLE => {"term":"0516011901","restrict":[{"tag":"020","subfield":"a"}]} */
237 function advExtractMARC(row) {
238         if(!row || row.id == 'advanced.marc.submit.row') return null;
239         var term = $n(row, 'advanced.marc.value').value;
240         if(!term) return null;
241
242         var subfield = $n(row, 'advanced.marc.subfield').value;
243         if(!subfield) subfield = "_";
244
245         var tag = $n(row, 'advanced.marc.tag').value;
246         if(!tag) return null;
247
248         return { 'term' : term, 'restrict' :  [ { 'tag' : tag, 'subfield' : subfield } ] };
249 }
250