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