]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/opac/RecordResultPage.js
moving some of the nav components around to sidebars and stuff
[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 RecordResultPage.prototype.addMenuItems = function(menu, record) {
100
101         var func = buildViewMARCWindow(record);
102         menu.addItem("View MARC", func);
103         if(isXUL())
104                 xulEvtRecordResultDisplayed( menu, record );
105
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                         "location=0,menubar=0,status=0,resizeable,resize," +
124                         "outerHeight=500,outerWidth=400,height=500," +
125                         "width=400,scrollbars=1,screenX=100," +
126                         "screenY=100,top=100,left=100,alwaysraised" )
127                 win.document.write(html);
128                 win.document.close();
129                 win.document.title = "View MARC";
130                 win.focus();
131         }
132         
133         return func;
134 }
135
136
137 RecordResultPage.prototype.mkLink = function(id, type, value) {
138
139         var href;
140
141         var org = globalSelectedLocation;
142         if(org == null)
143                 org = globalLocation;
144
145         switch(type) {
146
147
148                 case "title":
149                         href = createAppElement("a");
150                         add_css_class(href,"record_result_title_link");
151                         href.setAttribute("href",
152                                 "?target=record_detail&record=" + id  +
153                                 "&location=" + org.id() +
154                                 "&depth=" + globalSearchDepth );
155                         href.appendChild(createAppTextNode(value));
156                         href.title = "View title details for " + value;
157                         break;
158
159                 case "author":
160                         href = createAppElement("a");
161                         add_css_class(href,"record_result_author_link");
162                         href.setAttribute("href","?target=mr_result&mr_search_type=author&page=0&mr_search_query=" +
163                                               encodeURIComponent(value));
164                         href.appendChild(createAppTextNode(value));
165                         href.title = "Author search for " + value + "";
166                         break;
167
168         case "img":
169                         href = createAppElement("a");
170                         add_css_class(href,"record_result_image_link");
171                         href.setAttribute("href",
172                                         "?target=record_detail&page=0&mrid=" + id  +
173                                 "&location=" + org.id() +
174                                 "&depth=" + globalSearchDepth );
175                         href.title = "View title details for " + value;
176                         break;
177
178                 default:
179                         throw new EXArg("Unknown link type: " + type );
180         }
181
182         return href;
183
184 }
185
186 RecordResultPage.prototype.toString = function() {
187
188         return "\nRecordResultPage:\n"  +
189                 "page: "                                                + this.page + "\n" +
190                 "searchOffset: "                        + this.searchOffset + "\n" +
191                 "recordIDs: "                           + this.recordIDs + "\n" +
192                 "hitCount: "                            + this.hitCount + "\n" +
193                 "hitsPerPage: "                 + this.hitsPerPage + "\n";
194
195 }
196
197
198 RecordResultPage.prototype.isNewSearch = function() {
199         if(this.page == 0)
200                 return true;
201         return false;
202
203 }
204
205 /* performs a new search */
206 RecordResultPage.prototype.doSearch = function() {
207
208         if(recordResultRedirect) { 
209                 /* if the user is just hitting the 'back' button */
210                 recordResultRedirect = false;
211                 history.back();
212                 return;
213         }
214
215         debug( "Key Value Array \n" + js2JSON( paramObj ) );
216
217         this.page                       = parseInt(paramObj.__page);
218         var hitsper                     = paramObj.__hits_per_page;
219
220         if(hitsper)
221                 this.hitsPerPage = parseInt(hitsper);
222
223         debug("******* Hits per = " + this.hitsPerPage);
224
225         this.hitsPerPageSelector = getById('hits_per_page');
226         for( var i in this.hitsPerPageSelector.options ) {
227                 var hits_obj = this.hitsPerPageSelector.options[i];
228                 if(hits_obj == null) continue;
229                 var hits = hits_obj.value;
230                 debug(hits);
231                 if( this.hitsPerPage == parseInt(hits) ) {
232                         this.hitsPerPageSelector.options[i].selected = true;
233                         debug("Setting selected on selector with hits " + hits);
234                 }
235         }
236
237         if(this.page == null)
238                 this.page = 0;
239
240         this.searchOffset = this.page * this.hitsPerPage;
241
242
243         if(this.isNewSearch()) {
244                 debug("RecordResultPage resetting search..");
245                 this.resetSearch();
246         }
247
248         var offset = parseInt(this.searchOffset);
249         var hitspp      = parseInt(this.hitsPerPage);
250
251         /* is this just a call to next/prev? */
252         if( this.recordIDs && this.recordIDs[offset] != null )  {
253                 debug("We have the first part of the ID's");
254                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
255                                 this.recordIDs[this.hitCount - 1] != null ) {
256                         /* we already have all of the IDS */
257                         debug("We alread have the required mr " + 
258                                         "ids for the search: [" + this.string + "]");
259                         this.collectRecords();
260                         return;
261                 }
262         }
263
264
265         if( paramObj.__mrid != null ) {
266                 this.mrSearch(paramObj.__mrid);
267                 return;
268         }
269
270         if( paramObj.__search == "global" ) {
271
272                 if( paramObj.__tcn != null ) {
273                         this.globalSearch("tcn", paramObj.__tcn);
274                         return;
275                 }
276
277                 if( paramObj.__isbn != null ) {
278                         this.globalSearch("isbn", paramObj.__isbn);
279                         return;
280                 }
281                 if( paramObj.__barcode != null ) {
282                         this.globalSearch("barcode", paramObj.__barcode);
283                         return;
284                 }
285
286         }
287                 
288 }
289
290
291 /* these are the simple global searches */
292 RecordResultPage.prototype.globalSearch = function(type, term) {
293
294         if( !term || term.length < 2 )
295                 throw new EXArg( "globalSearch needs valid term: " + term );
296
297         debug("Performing Global search [" + type + "] for term: " + term );
298
299         var method;
300         switch( type ) {
301                 case "tcn":
302                         method = "open-ils.search.biblio.tcn";
303                         break;
304
305                 case "isbn":
306                         method = "open-ils.search.biblio.isbn";
307                         break;
308
309                 case "barcode":
310                         method = "open-ils.search.biblio.find_by_barcode";
311                         break;
312         }
313
314         var request = new RemoteRequest( "open-ils.search",  method, term );
315
316         var obj = this;
317         request.setCompleteCallback(
318                 function(req) {
319                         try {
320                                 var result = req.getResultObject();
321                                 debug( "Global Search returned: " + js2JSON(result) );
322                                 obj.gatherIDs(result) 
323                                 obj.collectRecords();
324                         } catch(E) { throw ("Search Error " + E ); }
325                 }
326         );
327         request.send();
328 }
329
330
331 RecordResultPage.prototype.mrSearch = function(mrid) {
332
333         var request = new RemoteRequest("open-ils.search",
334                 "open-ils.search.biblio.metarecord_to_records", mrid );
335         debug("Gathering doc ids for metarecord " + mrid );
336
337         var obj = this;
338         request.setCompleteCallback(
339                 function(req) {
340                         try{
341                                 obj.gatherIDs(req.getResultObject());
342
343                                 if(!recordResultRedirect) { /* if the user isn't just hitting the 'back' button */
344                                         if(parseInt(obj.hitCount) == 1) {
345                                                 recordResultRedirect = true;
346                                                 debug("Redirecting to record detail page with record " + obj.recordIDs[0] );
347                                         url_redirect( [
348                                                         "target", "record_detail",
349                                                         "record", obj.recordIDs[0] ] );
350                                                 return;
351                                         }
352                                 } else { 
353                                         recordResultRedirect = false;
354                                         history.back();
355                                 }
356
357                                 obj.collectRecords();
358                         } catch(E) { throw ("Search Error " + E ); }
359                 }
360         );
361         request.send();
362 }
363
364 RecordResultPage.prototype.collectRecords = function() {
365
366         var i = this.searchOffset;
367
368         while( i < (this.searchOffset + this.hitsPerPage) ) {
369                 var id = this.recordIDs[i];
370
371                 if(id==null){ i++;  continue; }
372
373                 var request = new RemoteRequest( "open-ils.search",
374                         "open-ils.search.biblio.record.mods_slim.retrieve", id );
375
376                 request.name = "record_request_" + i;
377                 request.search_id = i;
378                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
379
380                 /* define the callback for when we receive the record */
381                 var obj = this;
382                 request.setCompleteCallback(
383                         function(req) {
384                                 var record = req.getResultObject();
385                                 obj.displayRecord( record, req.search_id, req.page_id );
386                                 obj.doCopyCount( record, req.search_id, req.page_id );
387                         }
388                 );
389
390                 request.send();
391                 i++;
392         }
393 }
394
395 RecordResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
396
397         var copy_box    = getById("record_result_copy_count_box_" + page_id );
398
399         /* kick off the copy count search */
400         var orgunit = globalSelectedLocation;
401         if(!orgunit) orgunit = globalLocation;
402
403         var copy_request = new RemoteRequest( "open-ils.search",
404                 "open-ils.search.biblio.record.copy_count", 
405                 orgunit.id(), record.doc_id() );
406
407
408         copy_request.search_id = search_id;
409         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
410
411         debug("Sending copy request " + search_id + ":" + record.doc_id() );
412
413         var obj = this;
414         copy_request.setCompleteCallback( 
415                 function(req) {
416                         try {   
417                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
418                         } catch(E) { 
419                                 debug("****** Copy Count Retrieval Error:\n" + E ); 
420                         }
421                 }
422         );
423
424         copy_request.send();
425 }
426
427
428
429