]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/MRResultPage.js
Let the onslaught continue...
[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
34         var location = globalSelectedLocation;
35         if(location == null) 
36                 location = globalLocation.id();
37         else
38                 location = location.id();
39
40         /* if the user has changed the 'location' of the search, it will be
41                 reflected when the user hits the next button.  the search depth
42                 will not change, however, because that is a different search */
43         url_redirect( [ 
44                         "target",                                       "mr_result",
45                         "mr_search_type",                       this.stype,
46                         "mr_search_query",              this.string,
47                         "mr_search_location",   location,
48                         "mr_search_depth",              this.searchDepth,
49                         "page",                                         this.page + 1   
50                         ] );
51 }
52
53
54 MRResultPage.prototype.prev = function() {
55         if(this.page == 0 ) return;
56
57
58         var depth = globalSearchDepth;
59         var location = globalSelectedLocation;
60         if(location == null) 
61                 location = globalLocation.id();
62         else
63                 location = location.id();
64
65         /* if the user has changed the 'location' of the search, it will be
66                 reflected when the user hits this  button.  the search depth
67                 will not change, however, because that is a different search */
68         url_redirect( [ 
69                         "target",                                       "mr_result",
70                         "mr_search_type",                       this.stype,
71                         "mr_search_query",              this.string,
72                         "mr_search_location",   location,
73                         "mr_search_depth",              this.searchDepth,
74                         "page",                                         this.page - 1   
75                         ] );
76 }
77
78
79 MRResultPage.prototype.addMenuItems = function(menu, record) {
80                 menu.addItem("View Metarecord Details", 
81                                                         function() { alert(record.doc_id()); });
82                                 xulEvtMRResultDisplayed( menu, record );
83 }
84
85
86 MRResultPage.prototype.mkLink = function(id, type, value) {
87
88         var href;
89
90         switch(type) {
91
92                 case "title":
93                         href = createAppElement("a");
94                         add_css_class(href,"record_result_title_link");
95                         href.setAttribute("href","?target=record_result&page=0&mrid=" + id );
96                         href.appendChild(createAppTextNode(value));
97                         break;
98
99                 case "author":
100                         href = createAppElement("a");
101                         add_css_class(href,"record_result_author_link");
102                         href.setAttribute("href","?target=mr_result&mr_search_type=author&page=0&mr_search_query=" +
103                                               encodeURIComponent(value));
104                         href.appendChild(createAppTextNode(value));
105                         break;
106
107                 default:
108                         throw new EXArg("Unknown link type: " + type );
109         }
110
111         return href;
112 }
113
114
115
116 /* performs a new search */
117 MRResultPage.prototype.doSearch = function() {
118
119
120         var string                      = paramObj.__mr_search_query;
121         var stype                       = paramObj.__mr_search_type;
122         var location            = paramObj.__mr_search_location;
123         var depth                       = paramObj.__mr_search_depth;
124
125         debug("mr search params string " + string + " stype " + stype +
126                         " location " + location + " depth " + depth );
127
128         if(depth == null)
129                 depth = globalSearchDepth;
130         if(location == null)
131                 location = globalLocation.id();
132
133         if(!stype || !string) return;
134
135         if(this.searchDepth == null)
136                 this.searchDepth = globalSearchDepth;
137
138         /* see if this is a new search */
139         if(     string != this.string                           || 
140                         stype != this.stype                                     ||
141                         this.searchLocation != location ||
142                         this.searchDepth != depth ) {
143                 this.resetSearch();
144         }
145
146         this.searchDepth                = depth;
147         this.searchLocation     = location;
148         this.stype                              = stype;
149         this.string                             = string;
150         this.page                               = parseInt(paramObj.__page);
151         this.searchOffset               = this.page * this.hitsPerPage;
152
153         this.resetPage();
154
155         var offset = parseInt(this.searchOffset);
156         var hitspp      = parseInt(this.hitsPerPage);
157
158         /* is this just a call to next/prev? */
159         if( this.recordIDs && this.recordIDs[offset] != null )  {
160                 debug("We have the first part of the ID's");
161                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
162                                 this.recordIDs[this.hitCount - 1] != null ) {
163                         /* we already have all of the IDS */
164                         debug("We alread have the required mr " + 
165                                         "ids for the search: [" + this.string + "]");
166                         this.collectRecords();
167                         return;
168                 }
169         }
170
171
172         debug("MRResultPage doSearch() with type: " 
173                         + this.stype + " and search [" + this.string + "]"
174                         + " and offset " + this.searchOffset  +
175                         " depth: " + depth + " location: " + location);
176
177
178
179         var request = new RemoteRequest( 
180                         "open-ils.search", "open-ils.search.biblio.class", 
181                         this.stype, this.string, 
182                         this.searchLocation, 
183                         this.searchDepth, "50", this.searchOffset );
184
185         var obj = this;
186         request.setCompleteCallback(
187                 function(req) {
188                         var result = req.getResultObject();
189                         obj.gatherIDs(result) 
190                         obj.collectRecords();
191                         obj.requestBatch.remove(req);
192                 }
193         );
194
195         this.requestBatch.add(request);
196         request.send();
197 }
198
199
200 MRResultPage.prototype.collectRecords = function() {
201
202         
203         var i = this.searchOffset;
204
205         while( i < (this.searchOffset + this.hitsPerPage) ) {
206                 var id = this.recordIDs[i];
207
208                 if(id==null){ i++;  continue; }
209
210                 var request = new RemoteRequest( "open-ils.search",
211                         "open-ils.search.biblio.metarecord.mods_slim.retrieve", id );
212                 this.requestBatch.add(request);
213                 debug( "Sending mods retrieval for metarecord " + id );
214
215                 request.name = "record_request_" + i;
216                 request.search_id = i;
217                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
218
219                 /* define the callback for when we receive the record */
220                 var obj = this;
221                 request.setCompleteCallback(
222                         function(req) {
223                                 var record = req.getResultObject();
224                                 obj.displayRecord( record, req.search_id, req.page_id );
225                                 obj.doCopyCount( record, req.search_id, req.page_id );
226                                 obj.requestBatch.remove(req);
227                         }
228                 );
229
230                 request.send();
231                 i++;
232         }
233 }
234
235
236
237 MRResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
238
239         var copy_box    = getById("record_result_copy_count_box_" + page_id );
240
241         var orgunit = globalSelectedLocation;
242         if(!orgunit) orgunit = globalLocation;
243
244         var copy_request = new RemoteRequest( 
245                 "open-ils.search",
246                 "open-ils.search.biblio.metarecord.copy_count",
247                 this.searchLocation, record.doc_id() );
248
249         copy_request.search_id = search_id;
250         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
251
252         var obj = this;
253         copy_request.setCompleteCallback( 
254                 function(req) {
255                         try {   
256                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
257                         } catch(E) { 
258                                 debug("****** Copy Count Retrieval Error:\n" + E ); 
259                         }
260                 }
261         );
262
263         copy_request.send();
264 }
265
266