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