]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/RecordResultPage.js
more web work
[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         var row = getById("hourglass_row");
13         if(row)
14                 row.parentNode.removeChild(row);
15
16         if( globalRecordResultPage != null ) {
17                 debug("globalRecordResultPage already exists: " + 
18                                 globalRecordResultPage.toString() );
19                 return globalRecordResultPage;
20         }
21         globalRecordResultPage = this;
22         this.resetSearch();
23         debug("Built a new RecordResultPage()");
24
25         var row = getById("hourglass_row");
26         if(row)
27                 row.parentNode.removeChild(row);
28 }
29
30
31 RecordResultPage.buildExtendedLinks = function(record, page_id) {
32         return null;
33 }
34
35 RecordResultPage.prototype.setPageTrail = function() {
36         var box = getById("page_trail");
37         if(!box) return;
38
39         var d = this.buildTrailLink("start", true);
40         if(d) {
41                 box.appendChild(d);
42         } else {
43                 d = this.buildTrailLink("advanced_search", true);
44                 if(d)
45                         box.appendChild(d);
46         }
47
48         var b = this.buildTrailLink("mr_result", true);
49
50         if(b) {
51                 box.appendChild(this.buildDivider());
52                 box.appendChild(b);
53         }
54
55         box.appendChild(this.buildDivider());
56         box.appendChild(
57                 this.buildTrailLink("record_result",false));
58 }
59
60
61
62
63
64 /* returns the global instance. builds the instance if necessary.  All client
65  * code should use this method */
66 RecordResultPage.instance = function() {
67
68         var row = getById("hourglass_row");
69         if(row)
70                 row.parentNode.removeChild(row);
71
72         if( globalRecordResultPage != null ) {
73                 return globalRecordResultPage;
74         } 
75         return new RecordResultPage();
76 }
77
78 RecordResultPage.prototype.next = function() {
79         paramObj.__page = parseInt(paramObj.__page) + 1;
80         var paramArray = build_param_array();
81         url_redirect( paramArray ) 
82 }
83
84
85
86 RecordResultPage.prototype.URLRefresh = function() {
87         paramObj.__page = 0;
88         return build_param_array();
89 }
90
91
92
93 RecordResultPage.prototype.prev = function() {
94         paramObj.__page = parseInt(paramObj.__page) - 1;
95         var paramArray = build_param_array();
96         url_redirect( paramArray ) 
97 }
98
99
100 RecordResultPage.prototype.addMenuItems = function(menu, record) {
101
102         var func = buildViewMARCWindow(record);
103         menu.addItem("View MARC", func);
104         if(isXUL())
105                 xulEvtRecordResultDisplayed( menu, record );
106 }
107
108
109 function buildViewMARCWindow(record) {
110
111         debug("Setting up view marc with record " + record.doc_id());
112         var func = function() {
113                 var req = new RemoteRequest(
114                                 "open-ils.search", 
115                                 "open-ils.search.biblio.record.html",
116                                 record.doc_id());
117                 req.send(true);
118
119                 var html = req.getResultObject();
120                 var id = record.doc_id();
121
122                 //var win = window.open(null,"MARC_" + id,
123                 var win = window.open("about:blank","MARC_" + id,
124                         "location=0,menubar=0,status=0,resizeable,resize," +
125                         "outerHeight=500,outerWidth=400,height=500," +
126                         "width=400,scrollbars=1,screenX=100," +
127                         "screenY=100,top=100,left=100,alwaysraised,chrome" )
128
129                 win.document.write(html);
130                 win.document.close();
131                 win.document.title = "View MARC";
132                 win.focus();
133         }
134         
135         return func;
136 }
137
138
139 RecordResultPage.prototype.mkLink = function(id, type, value) {
140
141         var href;
142
143         var org = globalSelectedLocation;
144         if(org == null)
145                 org = globalLocation;
146
147         switch(type) {
148
149
150                 case "title":
151                         href = createAppElement("a");
152                         add_css_class(href,"record_result_title_link");
153                         href.setAttribute("href",
154                                 "?target=record_detail&record=" + id  +
155                                 "&location=" + org.id() +
156                                 "&depth=" + globalSearchDepth );
157                         href.appendChild(createAppTextNode(value));
158                         href.title = "View title details for " + value;
159                         break;
160
161                 case "author":
162                         href = createAppElement("a");
163                         add_css_class(href,"record_result_author_link");
164                         href.setAttribute("href","?target=mr_result&mr_search_type=author&page=0&mr_search_query=" +
165                                               encodeURIComponent(value));
166                         href.appendChild(createAppTextNode(value));
167                         href.title = "Author search for " + value + "";
168                         break;
169
170         case "img":
171                         href = createAppElement("a");
172                         add_css_class(href,"record_result_image_link");
173                         href.setAttribute("href",
174                                         "?target=record_detail&page=0&mrid=" + id  +
175                                 "&location=" + org.id() +
176                                 "&depth=" + globalSearchDepth );
177                         href.title = "View title details for " + value;
178                         break;
179
180                 default:
181                         throw new EXArg("Unknown link type: " + type );
182         }
183
184         return href;
185
186 }
187
188 RecordResultPage.prototype.toString = function() {
189
190         return "\nRecordResultPage:\n"  +
191                 "page: "                                                + this.page + "\n" +
192                 "searchOffset: "                        + this.searchOffset + "\n" +
193                 "recordIDs: "                           + this.recordIDs + "\n" +
194                 "hitCount: "                            + this.hitCount + "\n" +
195                 "hitsPerPage: "                 + this.hitsPerPage + "\n";
196
197 }
198
199
200 RecordResultPage.prototype.isNewSearch = function() {
201         if(this.page == 0)
202                 return true;
203         return false;
204
205 }
206
207 /* performs a new search */
208 RecordResultPage.prototype.doSearch = function() {
209
210         if(recordResultRedirect) { 
211                 /* if the user is just hitting the 'back' button */
212                 recordResultRedirect = false;
213                 history.go(-1);
214                 return;
215         }
216
217         debug( "Key Value Array \n" + js2JSON( paramObj ) );
218
219         this.page                       = parseInt(paramObj.__page);
220         var hitsper                     = paramObj.__hits_per_page;
221         this.format                     = paramObj.__format;
222         if(!this.format) this.format = "all";
223
224         if(hitsper)
225                 this.hitsPerPage = parseInt(hitsper);
226
227         debug("******* Hits per = " + this.hitsPerPage);
228
229         this.hitsPerPageSelector = getById('hits_per_page');
230         for( var i in this.hitsPerPageSelector.options ) {
231                 var hits_obj = this.hitsPerPageSelector.options[i];
232                 if(hits_obj == null) continue;
233                 var hits = hits_obj.value;
234                 debug(hits);
235                 if( this.hitsPerPage == parseInt(hits) ) {
236                         this.hitsPerPageSelector.options[i].selected = true;
237                         debug("Setting selected on selector with hits " + hits);
238                 }
239         }
240
241         if(this.page == null)
242                 this.page = 0;
243
244         this.searchOffset = this.page * this.hitsPerPage;
245
246
247         if(this.isNewSearch()) {
248                 debug("RecordResultPage resetting search..");
249                 this.resetSearch();
250         }
251
252         var offset = parseInt(this.searchOffset);
253         var hitspp      = parseInt(this.hitsPerPage);
254
255         /* is this just a call to next/prev? */
256         if( this.recordIDs && this.recordIDs[offset] != null )  {
257                 debug("We have the first part of the ID's");
258                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
259                                 this.recordIDs[this.hitCount - 1] != null ) {
260                         /* we already have all of the IDS */
261                         debug("We alread have the required mr " + 
262                                         "ids for the search: [" + this.string + "]");
263                         this.collectRecords();
264                         return;
265                 }
266         }
267
268
269         if( paramObj.__mrid != null ) {
270                 this.mrSearch(paramObj.__mrid);
271                 return;
272         }
273
274         if( paramObj.__search == "global" ) {
275
276                 if( paramObj.__tcn != null ) {
277                         this.globalSearch("tcn", paramObj.__tcn);
278                         return;
279                 }
280
281                 if( paramObj.__isbn != null ) {
282                         this.globalSearch("isbn", paramObj.__isbn);
283                         return;
284                 }
285                 if( paramObj.__barcode != null ) {
286                         this.globalSearch("barcode", paramObj.__barcode);
287                         return;
288                 }
289
290         }
291                 
292 }
293
294
295 /* these are the simple global searches */
296 RecordResultPage.prototype.globalSearch = function(type, term) {
297
298         if( !term || term.length < 2 )
299                 throw new EXArg( "globalSearch needs valid term: " + term );
300
301         debug("Performing Global search [" + type + "] for term: " + term );
302
303         var method;
304         switch( type ) {
305                 case "tcn":
306                         method = "open-ils.search.biblio.tcn";
307                         break;
308
309                 case "isbn":
310                         method = "open-ils.search.biblio.isbn";
311                         break;
312
313                 case "barcode":
314                         method = "open-ils.search.biblio.find_by_barcode";
315                         break;
316         }
317
318         var request = new RemoteRequest( "open-ils.search",  method, term );
319
320         var obj = this;
321         request.setCompleteCallback(
322                 function(req) {
323                         try {
324                                 var result = req.getResultObject();
325                                 debug( "Global Search returned: " + js2JSON(result) );
326                                 obj.gatherIDs(result) 
327                                 obj.collectRecords();
328                         } catch(E) { throw ("Search Error " + E ); }
329                 }
330         );
331         request.send();
332 }
333
334
335 RecordResultPage.prototype.mrSearch = function(mrid) {
336
337         var request;
338         var method = "open-ils.search.biblio.metarecord_to_records";
339         if(this.format == "all")
340                 request = new RemoteRequest("open-ils.search", method, mrid );
341         else
342                 request = new RemoteRequest("open-ils.search", method, mrid, this.format );
343
344         debug("Gathering doc ids for metarecord " + mrid );
345
346         var obj = this;
347         request.setCompleteCallback(
348                 function(req) {
349                         try{
350                                 var ids = req.getResultObject();
351                                 obj.gatherIDs(ids);
352
353                                 if(!recordResultRedirect) { /* if the user isn't just hitting the 'back' button */
354                                         if(parseInt(obj.hitCount) == 1) {
355                                                 recordResultRedirect = true;
356                                                 debug("Redirecting to record detail page with record " + obj.recordIDs[0] );
357                                         url_redirect( [
358                                                         "goto",         "-2",
359                                                         "target", "record_detail",
360                                                         "record", obj.recordIDs[0] ] );
361                                                 return;
362                                         }
363                                 } else { 
364                                         recordResultRedirect = false;
365                                         history.go(-1);
366                                 }
367
368                                 obj.collectRecords();
369                         } catch(E) { throw ("Search Error " + E ); }
370                 }
371         );
372         request.send();
373 }
374
375 RecordResultPage.prototype.collectRecords = function() {
376
377         var i = this.searchOffset;
378
379         while( i < (this.searchOffset + this.hitsPerPage) ) {
380                 var id = this.recordIDs[i];
381
382                 if(id==null){ i++;  continue; }
383
384                 var request = new RemoteRequest( "open-ils.search",
385                         "open-ils.search.biblio.record.mods_slim.retrieve", id );
386
387                 request.name = "record_request_" + i;
388                 request.search_id = i;
389                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
390
391                 /* define the callback for when we receive the record */
392                 var obj = this;
393                 request.setCompleteCallback(
394                         function(req) {
395                                 var record = req.getResultObject();
396                                 obj.displayRecord( record, req.search_id, req.page_id );
397                                 obj.doCopyCount( record, req.search_id, req.page_id );
398                         }
399                 );
400
401                 request.send();
402                 i++;
403         }
404 }
405
406 RecordResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
407
408         var copy_box    = getById("record_result_copy_count_box_" + page_id );
409
410         /* kick off the copy count search */
411         var orgunit = globalSelectedLocation;
412         if(!orgunit) orgunit = globalLocation;
413
414         var copy_request = new RemoteRequest( "open-ils.search",
415                 "open-ils.search.biblio.record.copy_count", 
416                 orgunit.id(), record.doc_id() );
417
418
419         copy_request.search_id = search_id;
420         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
421
422         debug("Sending copy request " + search_id + ":" + record.doc_id() );
423
424         var obj = this;
425         copy_request.setCompleteCallback( 
426                 function(req) {
427                         try {   
428                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
429                         } catch(E) { 
430                                 debug("****** Copy Count Retrieval Error:\n" + E ); 
431                         }
432                 }
433         );
434
435         copy_request.send();
436 }
437
438
439
440