]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/AbstractRecordResultPage.js
Let the onslaught continue...
[Evergreen.git] / Open-ILS / src / javascript / opac / AbstractRecordResultPage.js
1
2 AbstractRecordResultPage.prototype                                      = new Page();
3 AbstractRecordResultPage.prototype.constructor  = AbstractRecordResultPage;
4 AbstractRecordResultPage.baseClass                                      = Page.constructor;
5
6
7 /* constructor for our singleton object */
8 function AbstractRecordResultPage() {}
9
10
11 /* initialize all of the UI components and set up data structures */
12 AbstractRecordResultPage.prototype.init = function() {
13
14         debug( "Initing an AbstractRecordResultPage" );
15
16         /* included page chunks */
17         this.searchBar                  = new SearchBarChunk();
18
19         /* UI objects */
20         this.recordBox                  = getById("record_result_box");
21
22         this.authorBox = new Box();
23         this.authorBox.init("Relevant Authors", true, true, 15);
24         this.authorBox.sortByCount();
25
26         this.subjectBox = new Box();
27         this.subjectBox.init("Relevant Subjects", true, true, 15);
28         this.subjectBox.sortByCount();
29
30         this.sidebarBox         = getById("record_sidebar_box");
31
32
33         this.hitsPerPage                = 8;     /* how many hits are displayed per page */
34         this.resetPage();
35
36         this.statusBar                  = getById("top_status_bar_table");
37         this.theadDrawn         = false;
38         this.bigOlBox                   = getById("big_ol_box");
39
40 }
41
42
43
44 /** Resets data structures for a new search */
45 AbstractRecordResultPage.prototype.resetPage = function() {
46         this.searchBar.reset();
47         var spot = getById("progress_bar_location");
48         var spot2 = getById("progress_bar_percent_location");
49         if(spot) {
50                 while(spot.lastChild) 
51                         spot.removeChild(spot.lastChild);
52
53                 /* progress items for each record and it's hit count listing */
54                 this.progressBar = new ProgressBar(parseInt(this.hitsPerPage) * 2);
55                 spot.appendChild(this.progressBar.getNode());
56         }
57         if(spot2 && this.progressBar)
58                 spot2.appendChild(this.progressBar.percentDiv);
59         this.received = 0;
60
61         this.requestBatch = new RequestBatch();
62         this.finalized = false;
63 }
64
65 AbstractRecordResultPage.prototype.resetSearch = function() {
66         this.recordIDs                          = new Array();
67         this.ranks                                      = new Array();
68         this.hitCount                           = 0;                                    /* hits for the current search */
69         this.searchOffset                       = 0;                                    /* the offset for the search display */
70
71 }
72
73 AbstractRecordResultPage.prototype.gatherIDs = function(result) {
74
75         this.hitCount = parseInt(result.count);
76         if(this.hitCount < 1 ) {
77                 this.finalizePage();
78                 return false;
79         }
80         
81
82         /* the 'ids' field consist of [record, rank] */
83         /* gather all of the ID's */
84         if( result.ids  && typeof result.ids == 'object' 
85                         && result.ids[0] != null
86                         && result.ids[0].constructor == Array ) {
87
88                 for( var i in result.ids ) {
89                         if(result.ids[i]==null || result.ids[i][0] == null) break;
90                         var offset = parseInt(i) + parseInt(this.searchOffset);
91                         this.recordIDs[offset] = result.ids[i][0];
92                         var rank = parseFloat(result.ids[i][1]);
93                         if(rank == 0)
94                                 rank = 0.00000001; /* protect divide by 0 */
95                         this.ranks[offset] =  rank;
96                         /*
97                         debug("adding ranks[" + offset + "] = " + result.ids[i][1] + 
98                                         "  \nrecordIDs["+offset+"], result.ids["+i+"][0]");
99                                         */
100                 }
101
102         } else {
103
104                 for( var i in result.ids ) {
105                         if(result.ids[i]==null) break;
106                         var offset = parseInt(i) + parseInt(this.searchOffset);
107                         this.recordIDs[offset] = result.ids[i];
108                         debug("adding recordIDs["+offset+"], result.ids["+i+"]");
109                 }
110         }
111
112         return true;
113 }
114
115
116
117 AbstractRecordResultPage.prototype.complete = function() {
118
119 }
120
121
122 AbstractRecordResultPage.prototype.displayRecord = 
123         function( record, search_id, page_id ) {
124
125         if(page_id == 0)
126                 this.buildNextLinks();
127
128         this.received += 1;
129
130         this.progressBar.manualNext();
131
132         var id = parseInt(page_id);
133         var title_row = table_row_find_or_create(this.recordBox, id * 2 + 1 );
134         var author_row = table_row_find_or_create(this.recordBox, id * 2 + 2 );
135
136         author_row.id = "record_result_author_row_" + id;
137         title_row.id = "record_result_title_row_" + id;
138
139         /* build the appropriate context node for this result */
140         var menu = globalMenuManager.buildMenu(
141                 "record_result_row_" + page_id );
142
143         this.addMenuItems( menu, record );
144         globalMenuManager.setContext(title_row, menu);
145         globalMenuManager.setContext(author_row, menu);
146         getDocument().body.appendChild(menu.getNode());
147         /* ------------------------------------ */
148
149
150         var isbn = record.isbn();
151         if(isbn) isbn = isbn.replace(/\s+/,"");
152         else isbn = "";
153
154         var pic_cell = title_row.insertCell(0);
155         pic_cell.setAttribute("rowspan","2");
156         pic_cell.rowSpan = 2;
157
158
159         var rankBox = "";
160         if( this.ranks.length > 0 ) {
161                 var x = (parseInt(this.page) * parseInt(this.hitsPerPage)) + parseInt(page_id);
162                 var per = parseInt(this.ranks[x] / this.ranks[0] * 100.0);
163                 rankBox = "<div class='relevance_box'><div style='width:" + 
164                         per + "%' class='relevance'>&nbsp;</div></div>";
165         }
166                         
167
168         /* pull from amazon for now... */
169         pic_cell.innerHTML = rankBox + 
170                 "<img height='50' width='45' src='http://images.amazon.com/images/P/" 
171                 + isbn + ".01.MZZZZZZZ.jpg'>";
172
173
174         var title_cell = title_row.insertCell(title_row.cells.length);
175         title_cell.id = "record_result_title_box_" + id;
176         add_css_class( title_cell, "record_result_title_box");
177
178         var author_cell = author_row.insertCell(author_row.cells.length);
179         author_cell.id = "record_result_author_box_" + id;
180         add_css_class(author_cell, "record_result_author_box");
181
182
183         /* limit the length of the title and author lines */
184         var tlength = 100;
185
186         if( record.title() ) {
187                 if(record.title().length > tlength) {
188                         record.title(record.title().substr(0,tlength));
189                         record.title(record.title() + "...");
190                 }
191                 record.title(normalize(record.title()));
192         }
193
194         if( record.author() ) {
195                 if(record.author().length > tlength) {
196                         record.author( record.author().substr(0,tlength));
197                         record.author(record.author() + "...");
198                 }
199                 record.author(normalize(record.author()));
200         }       
201
202         title_cell.appendChild(this.mkLink(record.doc_id(), "title", record.title()));
203         author_cell.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
204         author_cell.appendChild(this.mkLink(record.doc_id(), "author", record.author()));
205
206         var classname = "result_even";
207         if((page_id%2) != 0) 
208                 classname = "result_odd";
209
210         add_css_class(title_row, classname);
211         add_css_class(author_row, classname);
212
213         /* now grab the record authors and subjects */
214         if( record.author() ) {
215                 this.authorBox.addItem( this.mkAuthorLink(record.author()), record.author() );
216         }
217
218         /* gather the subjects.  subjects are either a string or an array of
219                 [subject, broader topic].  currently, they're all just treated like
220                 subjects */
221         var arr = record.subject();
222         for( var sub in arr ) {
223
224                 var ss = arr[sub];
225                 if( ss.constructor != Array )
226                         ss = [ss];
227
228                 for( var i in ss ) {
229                         var s = normalize(ss[i]);
230                         this.subjectBox.addItem( this.mkSubjectLink(s), s );
231                 }
232         }
233
234         /* requestBatch will only have one request in it when the current
235                 record is the last record requested */
236         if( this.requestBatch.pending() < 2  )
237                 this.finalizePage();
238 }
239
240 AbstractRecordResultPage.prototype.mkAuthorLink = function(auth) {
241         var href = createAppElement("a");
242         add_css_class(href,"record_result_sidebar_link");
243         href.setAttribute("href",
244                 "?target=mr_result&mr_search_type=author&page=0&mr_search_query=" +
245                 encodeURIComponent(auth));
246         href.appendChild(createAppTextNode(auth));
247         return href;
248 }
249
250 AbstractRecordResultPage.prototype.mkSubjectLink = function(sub) {
251         var href = createAppElement("a");
252         add_css_class(href,"record_result_sidebar_link");
253         href.setAttribute("href",
254                 "?target=mr_result&mr_search_type=subject&page=0&mr_search_query=" +
255                 encodeURIComponent(sub));
256         href.appendChild(createAppTextNode(sub));
257         return href;
258 }
259
260 AbstractRecordResultPage.prototype.finalizePage = function() {
261
262         if( this.finalized )
263                 return;
264         this.finalized = true;
265
266         this.subjectBox.finalize();
267         this.authorBox.finalize();
268
269         this.sidebarBox.appendChild(this.subjectBox.getNode());
270         this.sidebarBox.appendChild(createAppElement("br"));
271         this.sidebarBox.appendChild(this.authorBox.getNode());
272         this.sidebarBox.appendChild(createAppElement("br"));
273
274 //      showMe(this.buttonsBox);
275
276         var ses = UserSession.instance().getSessionId();
277         var box = this.sidebarBox;
278
279         if(ses) {
280                 Survey.retrieveOpacRandom(ses, 
281                         function(sur) { 
282                                 sur.setSubmitCallback(
283                                         function() { alert("Thanks!"); return true; });
284                                 box.appendChild( sur.getNode() ); 
285                                 sur.setHidden(false);
286                         }
287                 );
288         } else {
289                 Survey.retrieveOpacRandomGlobal( 
290                         function(sur) { 
291                                 sur.setSubmitCallback(
292                                         function() { alert("Thanks!"); return true; });
293                                 box.appendChild( sur.getNode() ); 
294                                 sur.setHidden(false);
295                         }
296                 );
297         }
298
299
300         if(this.hitCount < 1)
301                 if(this.progressBar) this.progressBar.stop();
302
303         /* in case we're hidden */
304         showMe(this.bigOlBox);
305
306 }
307
308
309 AbstractRecordResultPage.prototype.displayCopyCounts = 
310         function(copy_counts, search_id, page_id) {
311                 
312         this.progressBar.manualNext();
313         var titlerow  = getById("record_result_title_row_" + page_id );
314         var authorrow  = getById("record_result_author_row_" + page_id );
315
316         var tcell = getById("record_result_title_box_" + page_id );
317
318         if(!this.theadDrawn) {
319                 var trow = getById("record_result_thead_row");
320                 for( var i in copy_counts) {
321                         var cell = trow.insertCell(trow.cells.length);
322                         add_css_class(cell,"record_result_thead_header");
323                         cell.innerHTML = 
324                                 findOrgType(findOrgUnit(copy_counts[i].org_unit).ou_type()).name();
325                 }
326                 this.theadDrawn = true;
327         }
328
329         for( var i in copy_counts) {
330                 var cell = createAppElement("td");
331                 add_css_class(cell, "copy_count_cell");
332                 cell.innerHTML = copy_counts[i].available + " / " + copy_counts[i].count;
333                 cell.setAttribute("rowspan","2");
334                 cell.rowSpan = 2;
335                 titlerow.appendChild(cell);
336         }
337
338         if(page_id  == (parseInt(this.hitsPerPage) - 1) ) {
339                 if(this.progressBar) this.progressBar.stop();
340         }
341
342         if( (page_id  == ((parseInt(this.hitCount) - 1 ) - parseInt(this.searchOffset))) ||
343                         (page_id == (parseInt(this.hitsPerPage) - 1) )) 
344                 if(this.progressBar) this.progressBar.stop();
345 }
346
347
348
349 AbstractRecordResultPage.prototype.buildNextLinks = function() {
350
351
352         var obj = this;
353         var next;
354         var prev;
355
356         debug("Building links");
357         if( this.searchOffset < (parseInt(this.hitCount) - this.hitsPerPage)) {
358                 next = createAppElement("a");
359                 add_css_class(next,"record_next_button");
360                 add_css_class(next,"record_next_button_active");
361                 next.href = "javascript:globalPage.next();";
362         } else {
363                 next = createAppElement("span");
364                 add_css_class(next,"record_next_button_inactive");
365         }
366
367         if(this.searchOffset > 0) {
368                 prev = createAppElement("a");
369                 add_css_class(prev,"record_next_button");
370                 add_css_class(prev,"record_next_button_active");
371                 prev.href = "javascript:globalPage.prev();";
372         } else {
373                 prev = createAppElement("span");
374                 add_css_class(prev,"record_next_button_inactive");
375         }
376
377         next.appendChild(createAppTextNode("Next"));
378         prev.appendChild(createAppTextNode("Previous"));
379
380
381         var i = this.searchOffset;
382         var max = parseInt(i) + this.hitsPerPage;
383         if( max > this.hitCount )
384                 max = this.hitCount;
385
386         var hcell = getById("hit_count_cell");
387
388         hcell.appendChild(
389                 createAppTextNode( "Displaying " + 
390                 ( parseInt(i) + 1 ) + " to " + max + " of " + this.hitCount));
391
392         hcell.appendChild(createAppTextNode(" "));
393         hcell.appendChild(createAppTextNode(" "));
394         hcell.appendChild(createAppTextNode(" "));
395
396         /*
397         var div = createAppElement("div");
398         div.appendChild(createAppTextNode("."));
399         div.setAttribute("style", "color:#FFF;float:left;width:10px;border:1px solid black;position:relative");
400         hcell.appendChild(div);
401         */
402
403         hcell.appendChild(prev);
404         var span = createAppElement("span");
405         span.appendChild(createAppTextNode(" ... "));
406         hcell.appendChild(span);
407         hcell.appendChild(next);
408
409
410         
411 }
412
413
414