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