]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/opac/RecordResultPage.js
opac pile of js updates, slowing adding tweeks
[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.prototype.setPageTrail = function() {
32         var box = getById("page_trail");
33         if(!box) return;
34
35         var d = this.buildTrailLink("start", true);
36         if(d) {
37                 box.appendChild(d);
38         } else {
39                 d = this.buildTrailLink("advanced_search", true);
40                 if(d)
41                         box.appendChild(d);
42         }
43
44         var b = this.buildTrailLink("mr_result", true);
45
46         if(b) {
47                 box.appendChild(this.buildDivider());
48                 box.appendChild(b);
49         }
50
51         box.appendChild(this.buildDivider());
52         box.appendChild(
53                 this.buildTrailLink("record_result",false));
54 }
55
56
57
58
59
60 /* returns the global instance. builds the instance if necessary.  All client
61  * code should use this method */
62 RecordResultPage.instance = function() {
63
64         var row = getById("hourglass_row");
65         if(row)
66                 row.parentNode.removeChild(row);
67
68         if( globalRecordResultPage != null ) {
69                 return globalRecordResultPage;
70         } 
71         return new RecordResultPage();
72 }
73
74 RecordResultPage.prototype.next = function() {
75         paramObj.__page = parseInt(paramObj.__page) + 1;
76         var paramArray = build_param_array();
77         url_redirect( paramArray ) 
78 }
79
80
81
82 RecordResultPage.prototype.URLRefresh = function() {
83         paramObj.__page = 0;
84         return build_param_array();
85 }
86
87
88
89 RecordResultPage.prototype.prev = function() {
90         paramObj.__page = parseInt(paramObj.__page) - 1;
91         var paramArray = build_param_array();
92         url_redirect( paramArray ) 
93 }
94
95 RecordResultPage.prototype.addMenuItems = function(menu, record) {
96
97         var func = function() {
98                 var req = new RemoteRequest(
99                                 "open-ils.search", 
100                                 "open-ils.search.biblio.record.html",
101                                 record.doc_id());
102                 req.send(true);
103                 buildViewMARCWindow(req.getResultObject(), record.doc_id() );
104         }
105
106         menu.addItem("View MARC", func);
107         if(isXUL())
108                 xulEvtRecordResultDisplayed( menu, record );
109
110 }
111
112
113 function buildViewMARCWindow(html, id) {
114         var win = window.open(null,"MARC_" + id,
115                 "location=0,menubar=0,status=0,resizeable,resize," +
116                 "outerHeight=500,outerWidth=400,height=500," +
117                 "width=400,scrollbars=1,screenX=100," +
118                 "screenY=100,top=100,left=100,alwaysraised" )
119         win.document.write(html);
120         win.document.close();
121         win.document.title = "View MARC";
122         win.focus();
123
124 }
125
126
127 RecordResultPage.prototype.mkLink = function(id, type, value) {
128
129         var href;
130
131         switch(type) {
132
133                 case "title":
134                         href = createAppElement("a");
135                         add_css_class(href,"record_result_title_link");
136                         href.setAttribute("href","?target=record_detail&record=" + id );
137                         href.appendChild(createAppTextNode(value));
138                         href.title = "View title details for " + value;
139                         break;
140
141                 case "author":
142                         href = createAppElement("a");
143                         add_css_class(href,"record_result_author_link");
144                         href.setAttribute("href","?target=mr_result&mr_search_type=author&mr_search_query=" +
145                                               encodeURIComponent(value));
146                         href.appendChild(createAppTextNode(value));
147                         href.title = "Author search for " + value + "";
148                         break;
149
150         case "img":
151                         href = createAppElement("a");
152                         add_css_class(href,"record_result_image_link");
153                         href.setAttribute("href","?target=record_detail&page=0&mrid=" + id );
154                         href.title = "View title details for " + value;
155                         break;
156
157                 default:
158                         throw new EXArg("Unknown link type: " + type );
159         }
160
161         return href;
162
163 }
164
165 RecordResultPage.prototype.toString = function() {
166
167         return "\nRecordResultPage:\n"  +
168                 "page: "                                                + this.page + "\n" +
169                 "searchOffset: "                        + this.searchOffset + "\n" +
170                 "recordIDs: "                           + this.recordIDs + "\n" +
171                 "hitCount: "                            + this.hitCount + "\n" +
172                 "hitsPerPage: "                 + this.hitsPerPage + "\n";
173
174 }
175
176
177 RecordResultPage.prototype.isNewSearch = function() {
178         if(this.page == 0)
179                 return true;
180         return false;
181
182 }
183
184 /* performs a new search */
185 RecordResultPage.prototype.doSearch = function() {
186
187         debug( "Key Value Array \n" + js2JSON( paramObj ) );
188
189         this.page                       = parseInt(paramObj.__page);
190         var hitsper                     = paramObj.__hits_per_page;
191
192         /*
193         if(hitsper)
194                 this.hitsPerPage = parseInt(hitsper);
195
196         debug("******* Hits per = " + this.hitsPerPage);
197
198         this.hitsPerPageSelector = getById('hits_per_page');
199         for( var i in this.hitsPerPageSelector.options ) {
200                 var hits_obj = this.hitsPerPageSelector.options[i];
201                 if(hits_obj == null) continue;
202                 var hits = hits_obj.value;
203                 debug(hits);
204                 if( this.hitsPerPage == parseInt(hits) ) {
205                         this.hitsPerPageSelector.options[i].selected = true;
206                         debug("Setting selected on selector with hits " + hits);
207                 }
208         }
209         */
210
211         if(this.page == null)
212                 this.page = 0;
213
214         this.searchOffset = this.page * this.hitsPerPage;
215
216
217         if(this.isNewSearch()) {
218                 debug("RecordResultPage resetting search..");
219                 this.resetSearch();
220         }
221
222         var offset = parseInt(this.searchOffset);
223         var hitspp      = parseInt(this.hitsPerPage);
224
225         /* is this just a call to next/prev? */
226         if( this.recordIDs && this.recordIDs[offset] != null )  {
227                 debug("We have the first part of the ID's");
228                 if( this.recordIDs[offset + (hitspp -1 )] != null  ||
229                                 this.recordIDs[this.hitCount - 1] != null ) {
230                         /* we already have all of the IDS */
231                         debug("We alread have the required mr " + 
232                                         "ids for the search: [" + this.string + "]");
233                         this.collectRecords();
234                         return;
235                 }
236         }
237
238
239         if( paramObj.__mrid != null ) {
240                 this.mrSearch(paramObj.__mrid);
241                 return;
242         }
243
244         if( paramObj.__search == "global" ) {
245
246                 if( paramObj.__tcn != null ) {
247                         this.globalSearch("tcn", paramObj.__tcn);
248                         return;
249                 }
250
251                 if( paramObj.__isbn != null ) {
252                         this.globalSearch("isbn", paramObj.__isbn);
253                         return;
254                 }
255                 if( paramObj.__barcode != null ) {
256                         this.globalSearch("barcode", paramObj.__barcode);
257                         return;
258                 }
259
260         }
261                 
262 }
263
264
265 /* these are the simple global searches */
266 RecordResultPage.prototype.globalSearch = function(type, term) {
267
268         if( !term || term.length < 2 )
269                 throw new EXArg( "globalSearch needs valid term: " + term );
270
271         debug("Performing Global search [" + type + "] for term: " + term );
272
273         var method;
274         switch( type ) {
275                 case "tcn":
276                         method = "open-ils.search.biblio.tcn";
277                         break;
278
279                 case "isbn":
280                         method = "open-ils.search.biblio.isbn";
281                         break;
282
283                 case "barcode":
284                         method = "open-ils.search.biblio.find_by_barcode";
285                         break;
286         }
287
288         var request = new RemoteRequest( "open-ils.search",  method, term );
289
290         var obj = this;
291         request.setCompleteCallback(
292                 function(req) {
293                         try {
294                                 var result = req.getResultObject();
295                                 debug( "Global Search returned: " + js2JSON(result) );
296                                 obj.gatherIDs(result) 
297                                 obj.collectRecords();
298                         } catch(E) { throw ("Search Error " + E ); }
299                 }
300         );
301         request.send();
302 }
303
304
305 RecordResultPage.prototype.mrSearch = function(mrid) {
306
307         var request = new RemoteRequest("open-ils.search",
308                 "open-ils.search.biblio.metarecord_to_records", mrid );
309         debug("Gathering doc ids for metarecord " + mrid );
310
311         var obj = this;
312         request.setCompleteCallback(
313                 function(req) {
314                         try{
315                                 obj.gatherIDs(req.getResultObject());
316                                 obj.collectRecords();
317                         } catch(E) { throw ("Search Error " + E ); }
318                 }
319         );
320         request.send();
321 }
322
323 RecordResultPage.prototype.collectRecords = function() {
324
325         var i = this.searchOffset;
326
327         while( i < (this.searchOffset + this.hitsPerPage) ) {
328                 var id = this.recordIDs[i];
329
330                 if(id==null){ i++;  continue; }
331
332                 var request = new RemoteRequest( "open-ils.search",
333                         "open-ils.search.biblio.record.mods_slim.retrieve", id );
334
335                 request.name = "record_request_" + i;
336                 request.search_id = i;
337                 request.page_id = parseInt(i) - parseInt(this.searchOffset);
338
339                 /* define the callback for when we receive the record */
340                 var obj = this;
341                 request.setCompleteCallback(
342                         function(req) {
343                                 var record = req.getResultObject();
344                                 obj.displayRecord( record, req.search_id, req.page_id );
345                                 obj.doCopyCount( record, req.search_id, req.page_id );
346                         }
347                 );
348
349                 request.send();
350                 i++;
351         }
352 }
353
354 RecordResultPage.prototype.doCopyCount = function( record, search_id, page_id ) {
355
356         var copy_box    = getById("record_result_copy_count_box_" + page_id );
357
358         /* kick off the copy count search */
359         var orgunit = globalSelectedLocation;
360         if(!orgunit) orgunit = globalLocation;
361
362         var copy_request = new RemoteRequest( "open-ils.search",
363                 "open-ils.search.biblio.record.copy_count", 
364                 orgunit.id(), record.doc_id() );
365
366
367         copy_request.search_id = search_id;
368         copy_request.name = "copy_request_" + (search_id+this.searchOffset);
369
370         debug("Sending copy request " + search_id + ":" + record.doc_id() );
371
372         var obj = this;
373         copy_request.setCompleteCallback( 
374                 function(req) {
375                         try {   
376                                 obj.displayCopyCounts(req.getResultObject(), search_id, page_id );
377                         } catch(E) { 
378                                 debug("****** Copy Count Retrieval Error:\n" + E ); 
379                         }
380                 }
381         );
382
383         copy_request.send();
384 }
385
386
387
388