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