3bcc76743fb373bd60a41823323918a715614031
[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         goTo(buildOPACLink(arg));
81 }
82
83 function advBuildSearch( contains, nocontains, exact ) {
84         var string = "";
85         if(contains) string = contains;
86
87         if( exact ) {
88                 if(exact.indexOf('"') > -1) string += " " + exact;
89                 else string += " \"" + exact + "\"";
90         }
91
92         if(nocontains) {
93                 var words = nocontains.split(" ");
94                 for( var i in words ) 
95                         string += " -" + words[i];
96         }
97         return string;
98 }
99
100
101 function advRefinedRun() {
102         var title       = $('advanced.refined.title_contains').value;
103         var author      = $('advanced.refined.author_contains').value;
104         var subject = $('advanced.refined.subject_contains').value;
105         var series      = $('advanced.refined.series_contains').value;
106         var form = getSelectorVal($('advanced.refined.form_selector'));
107         var sort                = getSelectorVal($('advanced.refined.sort_by'));
108         var sortdir     = getSelectorVal($('advanced.refined.sort_dir'));
109
110         var blob = {};
111         title = advRefinedTerm('title', title);
112         author = advRefinedTerm('author', author);
113         subject = advRefinedTerm('subject', subject);
114         series = advRefinedTerm('series', series);
115
116         if(title) { blob.title = {}; blob.title.term =  title; }
117         if(author) { blob.author = {}; blob.author.term = author;}
118         if(subject) { blob.subject = {}; blob.subject.term = subject;}
119         if(series) { blob.series = {}; blob.series.term = series; }
120
121         var arg                                 = {};
122         arg.page                                        = MRESULT;
123         arg[PARAM_FORM]         = form;
124         arg[PARAM_STYPE]                = "";
125         arg[PARAM_TERM]         = "";
126         arg[PARAM_ADVTERM]      = js2JSON(blob);
127         arg[PARAM_DEPTH]                = depthSelGetDepth();
128         arg[PARAM_LOCATION]     = depthSelGetNewLoc();
129         arg[PARAM_OFFSET]               = 0;
130         arg[PARAM_ADVTYPE]      = ADVTYPE_MULTI;
131         arg[PARAM_SORT]         = sort;
132         arg[PARAM_SORT_DIR]     = sortdir;
133
134         goTo(buildOPACLink(arg));
135
136 }
137
138 function advISBNRun() {
139         var isbn = $('opac.advanced.quick.isbn').value;
140         if(!isbn) return;
141         var arg                                 = {};
142         arg.page                                        = MRESULT;
143         arg[PARAM_STYPE]                = "";
144         arg[PARAM_TERM]         = "";
145         arg[PARAM_ADVTERM]      = isbn;
146         arg[PARAM_OFFSET]               = 0;
147         arg[PARAM_ADVTYPE]      = ADVTYPE_ISBN;
148         goTo(buildOPACLink(arg));
149 }
150
151 function advISSNRun() {
152         var issn = $('opac.advanced.quick.issn').value;
153         if(!issn) return;
154         var arg                                 = {};
155         arg.page                                        = MRESULT;
156         arg[PARAM_STYPE]                = "";
157         arg[PARAM_TERM]         = "";
158         arg[PARAM_ADVTERM]      = issn;
159         arg[PARAM_OFFSET]               = 0;
160         arg[PARAM_ADVTYPE]      = ADVTYPE_ISSN;
161         goTo(buildOPACLink(arg));
162 }
163
164 function advCNRun() {
165         var cn = $('opac.advanced.quick.cn').value;
166         if(!cn) return;
167         var arg                 = {};
168         arg.page                        = CNBROWSE;
169         arg[PARAM_CN]   = cn;
170         goTo(buildOPACLink(arg));
171 }
172
173
174 function advRefinedTerm( type, term ) {
175         var t = getSelectorVal($('advanced.refined.' + type + '_type'));
176         var string;
177
178         if( t == 'contains' ) string = advBuildSearch( term );
179         else if( t == 'nocontains' ) string = advBuildSearch( null, term );
180         else if( t == 'exact' ) string = advBuildSearch( null, null, term );
181
182         return string;
183 }
184
185
186 function advAddMARC() {
187         $('advanced.marc.tbody').insertBefore(
188                 $('advanced.marc.template').cloneNode(true),
189                 $('advanced.marc.submit.row'));
190 }
191
192 function advMARCRun() {
193
194         var t = $('advanced.marc.tbody');
195         var searches = [];
196         /*
197         for( var c in t.childNodes ) {
198                 var child = t.childNodes[c];
199                 if(child.nodeName == 'tr') {
200                         alert("hi");
201                         var val = advExtractMARC(child);
202                         if(val) searches.push(val);
203                 }
204         }
205         */
206
207         var children = t.getElementsByTagName('tr');
208         for( var c in children ) {
209                 var child = children[c];
210                 if(!(child && child.innerHTML)) continue;
211                 var val = advExtractMARC(child);
212                 if(val) searches.push(val);
213         }
214
215         if(searches.length == 0) return;
216
217         var arg = {};
218         arg.page = MRESULT;
219         arg[PARAM_FORM] = 'all'
220         arg[PARAM_STYPE] = "";
221         arg[PARAM_OFFSET] = 0;
222         arg[PARAM_DEPTH]        = depthSelGetDepth();
223         arg[PARAM_LOCATION]     = depthSelGetNewLoc();
224         arg[PARAM_ADVTERM] = js2JSON(searches);
225         arg[PARAM_ADVTYPE] = ADVTYPE_MARC;
226         arg[PARAM_TERM] = "";
227
228         goTo(buildOPACLink(arg));
229 }
230
231
232 /* EXAMPLE => {"term":"0516011901","restrict":[{"tag":"020","subfield":"a"}]} */
233 function advExtractMARC(row) {
234         if(!row || row.id == 'advanced.marc.submit.row') return null;
235         var term = $n(row, 'advanced.marc.value').value;
236         if(!term) return null;
237
238         var subfield = $n(row, 'advanced.marc.subfield').value;
239         if(!subfield) subfield = "_";
240
241         var tag = $n(row, 'advanced.marc.tag').value;
242         if(!tag) return null;
243
244         return { 'term' : term, 'restrict' :  [ { 'tag' : tag, 'subfield' : subfield } ] };
245 }
246