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