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