]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/opac/RecordResultPage.js
format handling in the opac, added format to searches
[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         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.back();
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
223         if(hitsper)
224                 this.hitsPerPage = parseInt(hitsper);
225
226         debug("******* Hits per = " + this.hitsPerPage);
227
228         this.hitsPerPageSelector = getById('hits_per_page');
229         for( var i in this.hitsPerPageSelector.options ) {
230                 var hits_obj = this.hitsPerPageSelector.options[i];
231                 if(hits_obj == null) continue;
232                 var hits = hits_obj.value;
233                 debug(hits);
234                 if( this.hitsPerPage == parseInt(hits) ) {
235                         this.hitsPerPageSelector.options[i].selected = true;
236                         debug("Setting selected on selector with hits " + hits);
237                 }
238         }
239
240         if(this.page == null)
241                 this.page = 0;
242
243         this.searchOffset = this.page * this.hitsPerPage;
244
245
246         if(this.isNewSearch()) {
247                 debug("RecordResultPage resetting search..");
248                 this.resetSearch();
249         }
250
251         var offset = parseInt(this.searchOffset);
252         var hitspp      = parseInt(this.hitsPerPage);
253
254         /* is this just a call to next/prev? */
255         if( this.recordIDs && this.recordIDs[offset] != null )  {
256                 debug("We have the first part of the ID's");
257                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
258                                 this.recordIDs[this.hitCount - 1] != null ) {
259                         /* we already have all of the IDS */
260                         debug("We alread have the required mr " + 
261                                         "ids for the search: [" + this.string + "]");
262                         this.collectRecords();
263                         return;
264                 }
265         }
266
267
268         if( paramObj.__mrid != null ) {
269                 this.mrSearch(paramObj.__mrid);
270                 return;
271         }
272
273         if( paramObj.__search == "global" ) {
274
275                 if( paramObj.__tcn != null ) {
276                         this.globalSearch("tcn", paramObj.__tcn);
277                         return;
278                 }
279
280                 if( paramObj.__isbn != null ) {
281                         this.globalSearch("isbn", paramObj.__isbn);
282                         return;
283                 }
284                 if( paramObj.__barcode != null ) {
285                         this.globalSearch("barcode", paramObj.__barcode);
286                         return;
287                 }
288
289         }
290                 
291 }
292
293
294 /* these are the simple global searches */
295 RecordResultPage.prototype.globalSearch = function(type, term) {
296
297         if( !term || term.length < 2 )
298                 throw new EXArg( "globalSearch needs valid term: " + term );
299
300         debug("Performing Global search [" + type + "] for term: " + term );
301
302         var method;
303         switch( type ) {
304                 case "tcn":
305                         method = "open-ils.search.biblio.tcn";
306                         break;
307
308                 case "isbn":
309                         method = "open-ils.search.biblio.isbn";
310                         break;
311
312                 case "barcode":
313                         method = "open-ils.search.biblio.find_by_barcode";
314                         break;
315         }
316
317         var request = new RemoteRequest( "open-ils.search",  method, term );
318
319         var obj = this;
320         request.setCompleteCallback(
321                 function(req) {
322                         try {
323                                 var result = req.getResultObject();
324                                 debug( "Global Search returned: " + js2JSON(result) );
325                                 obj.gatherIDs(result) 
326                                 obj.collectRecords();
327                         } catch(E) { throw ("Search Error " + E ); }
328                 }
329         );
330         request.send();
331 }
332
333
334 RecordResultPage.prototype.mrSearch = function(mrid) {
335
336         var form = this.format;
337         if(form == "all") form = null;
338         var request = new RemoteRequest("open-ils.search",
339                 "open-ils.search.biblio.metarecord_to_records", mrid, form );
340
341         debug("Gathering doc ids for metarecord " + mrid );
342
343         var obj = this;
344         request.setCompleteCallback(
345                 function(req) {
346                         try{
347                                 obj.gatherIDs(req.getResultObject());
348
349                                 if(!recordResultRedirect) { /* if the user isn't just hitting the 'back' button */
350                                         if(parseInt(obj.hitCount) == 1) {
351                                                 recordResultRedirect = true;
352                                                 debug("Redirecting to record detail page with record " + obj.recordIDs[0] );
353                                         url_redirect( [
354                                                         "target", "record_detail",
355                                                         "record", obj.recordIDs[0] ] );
356                                                 return;
357                                         }
358                                 } else { 
359                                         recordResultRedirect = false;
360                                         history.back();
361                                 }
362
363                                 obj.collectRecords();
364                         } catch(E) { throw ("Search Error " + E ); }
365                 }
366         );
367         request.send();
368 }
369
370 RecordResultPage.prototype.collectRecords = function() {
371
372         var i = this.searchOffset;
373
374         while( i < (this.searchOffset + this.hitsPerPage) ) {
375                 var id = this.recordIDs[i];
376
377                 if(id==null){ i++;  continue; }
378
379                 var request = new RemoteRequest( "open-ils.search",
380                         "open-ils.search.biblio.record.mods_slim.retrieve", id );
381
382                 request.name = "record_request_" + i;
383                 request.search_id = i;
384                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
385
386                 /* define the callback for when we receive the record */
387                 var obj = this;
388                 request.setCompleteCallback(
389                         function(req) {
390                                 var record = req.getResultObject();
391                                 obj.displayRecord( record, req.search_id, req.page_id );
392                                 obj.doCopyCount( record, req.search_id, req.page_id );
393                         }
394                 );
395
396                 request.send();
397                 i++;
398         }
399 }
400
401 RecordResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
402
403         var copy_box    = getById("record_result_copy_count_box_" + page_id );
404
405         /* kick off the copy count search */
406         var orgunit = globalSelectedLocation;
407         if(!orgunit) orgunit = globalLocation;
408
409         var copy_request = new RemoteRequest( "open-ils.search",
410                 "open-ils.search.biblio.record.copy_count", 
411                 orgunit.id(), record.doc_id() );
412
413
414         copy_request.search_id = search_id;
415         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
416
417         debug("Sending copy request " + search_id + ":" + record.doc_id() );
418
419         var obj = this;
420         copy_request.setCompleteCallback( 
421                 function(req) {
422                         try {   
423                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
424                         } catch(E) { 
425                                 debug("****** Copy Count Retrieval Error:\n" + E ); 
426                         }
427                 }
428         );
429
430         copy_request.send();
431 }
432
433
434
435