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