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