]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/MRResultPage.js
web batch update
[Evergreen.git] / Open-ILS / src / javascript / opac / MRResultPage.js
1 var globalMRResultPage = null;                                  /* our global object */
2
3 MRResultPage.prototype                                  = new AbstractRecordResultPage();
4 MRResultPage.prototype.constructor      = MRResultPage;
5 MRResultPage.baseClass                                  = AbstractRecordResultPage.constructor;
6
7 /* constructor for our singleton object */
8 function MRResultPage() {
9         debug("MRResultPage()");
10         //this.searchBar = new SearchBarChunk();
11
12         if( globalMRResultPage != null ) {
13                 debug("MRResultPage() exists, returning");
14                 return globalMRResultPage;
15         }
16
17         //this.progressBar = new ProgressBar(getById('progress_bar'));
18         this.resetSearch();
19         globalMRResultPage = this;
20 }
21
22
23 /* returns the global instance. builds the instance if necessary.  All client
24  * code should use this method */
25 MRResultPage.instance = function() {
26         if( globalMRResultPage != null ) {
27                 return globalMRResultPage;
28         } 
29         return new MRResultPage();
30 }
31
32 MRResultPage.prototype.next = function() {
33         url_redirect( [ 
34                         "target",                               "mr_result",
35                         "mr_search_type",               this.stype,
36                         "mr_search_query",      this.string,
37                         "page",                                 this.page + 1   
38                         ] );
39 }
40
41
42 MRResultPage.prototype.prev = function() {
43         if(this.page == 0 ) return;
44         url_redirect( [ 
45                         "target",                               "mr_result",
46                         "mr_search_type",               this.stype,
47                         "mr_search_query",      this.string,
48                         "page",                                 this.page - 1   
49                         ] );
50 }
51
52
53 MRResultPage.prototype.addMenuItems = function(menu, record) {
54                 menu.addItem("View Metarecord Details", 
55                                                         function() { alert(record.doc_id()); });
56                                 xulEvtMRResultDisplayed( menu, record );
57 }
58
59
60 MRResultPage.prototype.mkLink = function(id, type, value) {
61
62         var href;
63
64         switch(type) {
65
66                 case "title":
67                         href = createAppElement("a");
68                         add_css_class(href,"record_result_title_link");
69                         href.setAttribute("href","?target=record_result&page=0&mrid=" + id );
70                         href.appendChild(createAppTextNode(value));
71                         break;
72
73                 case "author":
74                         href = createAppElement("a");
75                         add_css_class(href,"record_result_author_link");
76                         href.setAttribute("href","?target=mr_result&mr_search_type=author&page=0&mr_search_query=" +
77                                               encodeURIComponent(value));
78                         href.appendChild(createAppTextNode(value));
79                         break;
80
81                 default:
82                         throw new EXArg("Unknown link type: " + type );
83         }
84
85         return href;
86 }
87
88
89
90 /* performs a new search */
91 MRResultPage.prototype.doSearch = function() {
92
93
94         var string                      = paramObj.__mr_search_query;
95         var stype                       = paramObj.__mr_search_type;
96         if(!stype || !string) return;
97
98         var orgunit;
99         if(globalSelectedLocation) 
100                 orgunit = globalSelectedLocation;
101         else orgunit = globalLocation;
102
103         if(this.searchDepth == null)
104                 this.searchDepth = globalSearchDepth;
105
106         debug("Current search depth: " + globalSearchDepth);
107         debug("My search depth: " + this.searchDepth);
108
109         /* see if this is a new search */
110         if(     string != this.string                           || 
111                         stype != this.stype                                     ||
112                         this.searchLocation != orgunit  ||
113                         this.searchDepth != globalSearchDepth ) {
114
115                 this.resetSearch();
116                 this.searchDepth = globalSearchDepth;
117         }
118
119         this.searchLocation     = orgunit;
120         this.stype                              = stype;
121         this.string                             = string;
122         this.page                               = parseInt(paramObj.__page);
123         this.searchOffset               = this.page * this.hitsPerPage;
124
125
126         //this.progressBar.progressStart();
127
128         this.resetPage();
129
130         var offset = parseInt(this.searchOffset);
131         var hitspp      = parseInt(this.hitsPerPage);
132
133         /* is this just a call to next/prev? */
134         if( this.recordIDs && this.recordIDs[offset] != null )  {
135                 debug("We have the first part of the ID's");
136                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
137                                 this.recordIDs[this.hitCount - 1] != null ) {
138                         /* we already have all of the IDS */
139                         debug("We alread have the required mr " + 
140                                         "ids for the search: [" + this.string + "]");
141                         this.collectRecords();
142                         return;
143                 }
144         }
145
146
147         debug("MRResultPage doSearch() with type: " 
148                         + this.stype + " and search [" + this.string + "]"
149                         + " and offset " + this.searchOffset );
150
151
152
153         var request = new RemoteRequest( 
154                         "open-ils.search", "open-ils.search.biblio.class", 
155                         this.stype, this.string, 
156                         this.searchLocation.id(), 
157                         this.searchDepth, "50", this.searchOffset );
158
159         var obj = this;
160         request.setCompleteCallback(
161                 function(req) {
162                         try {
163                                 var result = req.getResultObject();
164                                 debug( "MRSearch returned: " + js2JSON(result) );
165                                 obj.gatherIDs(result) 
166                                 obj.collectRecords();
167                         } catch(E) { throw ("Search Error " + E ); }
168                 }
169         );
170
171         request.send();
172 }
173
174
175 MRResultPage.prototype.collectRecords = function() {
176
177         var i = this.searchOffset;
178
179         var hcell = getById("hit_count_cell");
180         hcell.innerHTML = "Hits";
181         hcell.innerHTML += "  ";
182         hcell.innerHTML += this.hitCount;
183
184         while( i < (this.searchOffset + this.hitsPerPage) ) {
185                 var id = this.recordIDs[i];
186
187                 if(id==null){ i++;  continue; }
188
189                 var request = new RemoteRequest( "open-ils.search",
190                         "open-ils.search.biblio.metarecord.mods_slim.retrieve", id );
191                 this.requestBatch.push(request);
192
193                 request.name = "record_request_" + i;
194                 request.search_id = i;
195                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
196
197                 debug("Collecting metarecord for id " + id + " and search_id " + i);
198
199
200                 /* define the callback for when we receive the record */
201                 var obj = this;
202                 request.setCompleteCallback(
203                         function(req) {
204                                 //try {
205                                         var record = req.getResultObject();
206                                         obj.displayRecord( record, req.search_id, req.page_id );
207                                         obj.doCopyCount( record, req.search_id, req.page_id );
208                                 //} catch(E) { 
209                                 //      alert("Doc Retrieval Error:\n" + E); 
210                                 //}
211                         }
212                 );
213
214                 request.send();
215                 i++;
216         }
217 }
218
219 MRResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
220
221         var copy_box    = getById("record_result_copy_count_box_" + page_id );
222
223         /* kick off the copy count search */
224         debug("Grabbing copy count for record " + record.doc_id() );
225         var copy_request = new RemoteRequest( "open-ils.search",
226                 "open-ils.search.biblio.metarecord.copy_count", 1, record.doc_id() );
227         this.requestBatch.push(copy_request);
228
229         copy_request.search_id = search_id;
230         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
231
232         debug("Sending copy request " + search_id + ":" + record.doc_id() );
233
234         var obj = this;
235         copy_request.setCompleteCallback( 
236                 function(req) {
237                         try {   
238                                 copy_box.innerHTML = req.getResultObject();     
239                         } catch(E) { 
240                                 //alert("Copy Count Retrieval Error:\n" + E ); 
241                         }
242                 }
243         );
244
245         copy_request.send();
246 }
247
248
249
250 MRResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
251
252         var copy_box    = getById("record_result_copy_count_box_" + page_id );
253
254         /* kick off the copy count search */
255         var orgunit = globalSelectedLocation;
256         if(!orgunit) orgunit = globalLocation;
257
258         var copy_request = new RemoteRequest( "open-ils.search",
259                 "open-ils.search.biblio.metarecord.copy_count",
260                 orgunit.id(), record.doc_id() );
261
262         this.requestBatch.push(copy_request);
263
264         copy_request.search_id = search_id;
265         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
266
267         debug("Sending copy request " + search_id + ":" + record.doc_id() );
268
269         var obj = this;
270         copy_request.setCompleteCallback( 
271                 function(req) {
272                         try {   
273                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
274                         } catch(E) { 
275                                 //alert("Copy Count Retrieval Error:\n" + E ); 
276                         }
277                 }
278         );
279
280         copy_request.send();
281 }
282
283
284 /*
285 MRResultPage.prototype.gatherIDs = function(result) {
286
287         this.hitCount = parseInt(result.count);
288         debug("here");
289
290         for( var i in result.ids ) {
291                 if(result.ids[i]==null || result.ids[i][0] == null) break;
292                 var offset = parseInt(i) + parseInt(this.searchOffset);
293                 this.recordIDs[offset] = result.ids[i][0];
294                 this.ranks[offset] = parseFloat(result.ids[i][1]);
295                 debug("adding ranks[" + offset + "] = " + result.ids[i][1] + 
296                                 "  \nrecordIDs["+offset+"], result.ids["+i+"][0]");
297         }
298
299 }
300 */
301
302