]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/RecordResultPage.js
Let the onslaught continue...
[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 RecordResultPage.prototype.addMenuItems = function(menu, record) {
46
47         var func = function() {
48                 var req = new RemoteRequest(
49                                 "open-ils.search", 
50                                 "open-ils.search.biblio.record.html",
51                                 record.doc_id());
52                 req.send(true);
53                 buildViewMARCWindow(req.getResultObject(), record.doc_id() );
54         }
55
56         menu.addItem("View MARC", func);
57         xulEvtRecordResultDisplayed( menu, record );
58
59 }
60
61
62 function buildViewMARCWindow(html, id) {
63         var win = window.open(null,"MARC_" + id,
64                 "location=0,menubar=0,status=0,resizeable,resize," +
65                 "outerHeight=500,outerWidth=400,height=500," +
66                 "width=400,scrollbars=1,screenX=100," +
67                 "screenY=100,top=100,left=100,alwaysraised" )
68         win.document.write(html);
69         win.document.close();
70         win.document.title = "View MARC";
71         win.focus();
72
73 }
74
75
76 RecordResultPage.prototype.mkLink = function(id, type, value) {
77
78         var href;
79
80         switch(type) {
81
82                 case "title":
83                         href = createAppElement("a");
84                         add_css_class(href,"record_result_title_link");
85                         href.setAttribute("href","?target=record_detail&record=" + id );
86                         href.appendChild(createAppTextNode(value));
87                         break;
88
89                 case "author":
90                         href = createAppElement("a");
91                         add_css_class(href,"record_result_author_link");
92                         href.setAttribute("href","?target=mr_result&mr_search_type=author&mr_search_query=" +
93                                               encodeURIComponent(value));
94                         href.appendChild(createAppTextNode(value));
95                         break;
96
97                 default:
98                         throw new EXArg("Unknown link type: " + type );
99         }
100
101         return href;
102
103 }
104
105 RecordResultPage.prototype.toString = function() {
106
107         return "\nRecordResultPage:\n"  +
108                 "page: "                                                + this.page + "\n" +
109                 "searchOffset: "                        + this.searchOffset + "\n" +
110                 "recordIDs: "                           + this.recordIDs + "\n" +
111                 "hitCount: "                            + this.hitCount + "\n" +
112                 "hitsPerPage: "                 + this.hitsPerPage + "\n";
113
114 }
115
116
117 RecordResultPage.prototype.isNewSearch = function() {
118         if(this.page == 0)
119                 return true;
120         return false;
121
122 }
123
124 /* performs a new search */
125 RecordResultPage.prototype.doSearch = function() {
126
127         debug( "Key Value Array \n" + js2JSON( paramObj ) );
128
129         this.page                       = parseInt(paramObj.__page);
130         this.searchOffset = this.page * this.hitsPerPage;
131
132
133         if(this.isNewSearch()) {
134                 debug("RecordResultPage resetting search..");
135                 this.resetSearch();
136         }
137
138         var offset = parseInt(this.searchOffset);
139         var hitspp      = parseInt(this.hitsPerPage);
140
141         /* is this just a call to next/prev? */
142         if( this.recordIDs && this.recordIDs[offset] != null )  {
143                 debug("We have the first part of the ID's");
144                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
145                                 this.recordIDs[this.hitCount - 1] != null ) {
146                         /* we already have all of the IDS */
147                         debug("We alread have the required mr " + 
148                                         "ids for the search: [" + this.string + "]");
149                         this.collectRecords();
150                         return;
151                 }
152         }
153
154
155         if( paramObj.__mrid != null ) {
156                 this.mrSearch(paramObj.__mrid);
157                 return;
158         }
159
160         if( paramObj.__search == "global" ) {
161
162                 if( paramObj.__tcn != null ) {
163                         this.globalSearch("tcn", paramObj.__tcn);
164                         return;
165                 }
166
167                 if( paramObj.__isbn != null ) {
168                         this.globalSearch("isbn", paramObj.__isbn);
169                         return;
170                 }
171                 if( paramObj.__barcode != null ) {
172                         this.globalSearch("barcode", paramObj.__barcode);
173                         return;
174                 }
175
176         }
177                 
178 }
179
180
181 /* these are the simple global searches */
182 RecordResultPage.prototype.globalSearch = function(type, term) {
183
184         if( !term || term.length < 2 )
185                 throw new EXArg( "globalSearch needs valid term: " + term );
186
187         debug("Performing Global search [" + type + "] for term: " + term );
188
189         var method;
190         switch( type ) {
191                 case "tcn":
192                         method = "open-ils.search.biblio.tcn";
193                         break;
194
195                 case "isbn":
196                         method = "open-ils.search.biblio.isbn";
197                         break;
198
199                 case "barcode":
200                         method = "open-ils.search.biblio.find_by_barcode";
201                         break;
202         }
203
204         var request = new RemoteRequest( "open-ils.search",  method, term );
205
206         var obj = this;
207         request.setCompleteCallback(
208                 function(req) {
209                         try {
210                                 var result = req.getResultObject();
211                                 debug( "Global Search returned: " + js2JSON(result) );
212                                 obj.gatherIDs(result) 
213                                 obj.collectRecords();
214                         } catch(E) { throw ("Search Error " + E ); }
215                 }
216         );
217         request.send();
218 }
219
220
221 RecordResultPage.prototype.mrSearch = function(mrid) {
222
223         var request = new RemoteRequest("open-ils.search",
224                 "open-ils.search.biblio.metarecord_to_records", mrid );
225         debug("Gathering doc ids for metarecord " + mrid );
226
227         var obj = this;
228         request.setCompleteCallback(
229                 function(req) {
230                         try{
231                                 obj.gatherIDs(req.getResultObject());
232                                 obj.collectRecords();
233                         } catch(E) { throw ("Search Error " + E ); }
234                 }
235         );
236         request.send();
237 }
238
239 RecordResultPage.prototype.collectRecords = function() {
240
241         var i = this.searchOffset;
242
243         while( i < (this.searchOffset + this.hitsPerPage) ) {
244                 var id = this.recordIDs[i];
245
246                 if(id==null){ i++;  continue; }
247
248                 var request = new RemoteRequest( "open-ils.search",
249                         "open-ils.search.biblio.record.mods_slim.retrieve", id );
250
251                 request.name = "record_request_" + i;
252                 request.search_id = i;
253                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
254
255                 /* define the callback for when we receive the record */
256                 var obj = this;
257                 request.setCompleteCallback(
258                         function(req) {
259                                 var record = req.getResultObject();
260                                 obj.displayRecord( record, req.search_id, req.page_id );
261                                 obj.doCopyCount( record, req.search_id, req.page_id );
262                         }
263                 );
264
265                 request.send();
266                 i++;
267         }
268 }
269
270 RecordResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
271
272         var copy_box    = getById("record_result_copy_count_box_" + page_id );
273
274         /* kick off the copy count search */
275         var orgunit = globalSelectedLocation;
276         if(!orgunit) orgunit = globalLocation;
277
278         var copy_request = new RemoteRequest( "open-ils.search",
279                 "open-ils.search.biblio.record.copy_count", 
280                 orgunit.id(), record.doc_id() );
281
282
283         copy_request.search_id = search_id;
284         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
285
286         debug("Sending copy request " + search_id + ":" + record.doc_id() );
287
288         var obj = this;
289         copy_request.setCompleteCallback( 
290                 function(req) {
291                         try {   
292                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
293                         } catch(E) { 
294                                 debug("****** Copy Count Retrieval Error:\n" + E ); 
295                         }
296                 }
297         );
298
299         copy_request.send();
300 }
301
302
303
304