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