]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/opensearchportal.html
rank bugfix
[Evergreen.git] / Open-ILS / src / extras / opensearchportal.html
1 <html>
2         <head>
3                 <title>mikers experimental opensearch portal</title>
4                 <style>
5
6 * {
7         font-family: verdana,arial,helvetica,sans-serif;
8 }
9
10 #result_sources a {
11         font-size: 8px;
12         color: grey;
13         text-decoration: none;
14 }
15
16 .data_table {
17         max-width: 200px;
18 }
19
20 caption {
21         border: solid lightblue 1px;
22 }
23
24 a {
25         color: blue;
26         text-decoration: none;
27 }
28
29 a:hover {
30         color: blue;
31         text-decoration: underline;
32 }
33
34 a:active {
35         color: red;
36         text-decoration: underline;
37 }
38
39 a:visited {
40         color: blue;
41         text-decoration: none;
42 }
43
44 .title_link {
45         font-size: medium;
46         font-weight: bold;
47 }
48
49 .desc_text {
50         font-size: small;
51 }
52
53 .hide {
54         color: lightgray;
55 }
56
57 .col_tab {
58         border-collapse: collapse;
59         border: solid gray 1px;
60 }
61
62 .res_table {
63         border-collapse: collapse;
64 }
65
66 .res_tr {
67         border-bottom: 1px dashed darkgrey;
68 }
69
70                 </style>
71                 <script>
72
73 var isIE = false;
74
75 function create_requestor () {
76         var req;
77         try { 
78                 req = new ActiveXObject("Msxml2.XMLHTTP");
79                 isIE = true;
80         } catch (e) {
81                 try { 
82                         req = new ActiveXObject("Microsoft.XMLHTTP");
83                         isIE = true;
84                 } catch (E) {
85                         req = false;
86                 }
87         }
88
89         if (!req && typeof XMLHttpRequest!='undefined') {
90                 req = new XMLHttpRequest();
91         }
92         
93         if(!req) {
94                 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
95                 return null;
96         }
97
98         return req;
99 }
100
101 var proxy = 'http://gapines.org/opensearch/?fetch=';
102
103 var images = [];
104 var search_templates = [];
105 var search_urls = {};
106 var rel_scales = {};
107
108 var display_mode = 'col';
109 var current_startPage = 1;
110 var current_startIndex = 1;
111 var current_count = 5;
112
113 function opensearch ( term, reset ) {
114
115         if (reset)
116                 current_startPage = 1;
117
118         document.getElementById('next_button').className = 'hide';
119
120         if (current_startPage == 1)
121                 document.getElementById('prev_button').className = 'hide';
122         else 
123                 document.getElementById('prev_button').className = '';
124
125         var tot = document.getElementById('total');
126         while (tot.lastChild)
127                         tot.removeChild(tot.lastChild);
128
129         var src = document.getElementById('result_sources');
130         while (src.lastChild)
131                         src.removeChild(src.lastChild);
132
133         var tab = document.getElementById('results');
134         while (tab.lastChild)
135                         tab.removeChild(tab.lastChild);
136
137         search_count = 0;
138
139         var sources = new Array();
140         var selector = document.getElementById('sources');
141         for (var i = 0; i < selector.options.length; i++) {
142                 if (selector.options[i].selected) {
143                         sources.push(selector.options[i].value);
144                 }
145         }
146
147         search_templates = [];
148         for (var i in sources) {
149                 create_search( sources[i] );
150         }
151
152         current_startIndex = (current_count * (current_startPage - 1)) + 1; 
153
154         search_urls = [];
155         for (var i in search_templates) {
156                 if (!search_templates[i])
157                         continue;
158
159                 if (!rel_scales[i])
160                         rel_scales[i] = 0;
161
162                 var url = search_templates[i].replace(/\{searchTerms\}/,encodeURIComponent(term));
163                 url = url.replace(/\{startPage\}/,current_startPage);
164                 url = url.replace(/\{startIndex\}/,current_startIndex);
165                 url = url.replace(/\{count\}/,current_count);
166                 url = url.replace(/\{relevanceScale}/,rel_scales[i]);
167                 search_urls[i] = proxy + encodeURIComponent(url);
168
169                 src.innerHTML += '<a href="' + url + '">' + url + '</a><br>';
170         }
171
172         for (var i in search_urls) {
173                 if (!search_templates[i])
174                         continue;
175
176                 perform_search(i);
177         }
178
179         document.getElementById('page_label').innerHTML = current_startPage;
180 }
181
182 function perform_search ( source ) {
183         var req = create_requestor();
184
185         req.onreadystatechange = function () {
186                 if (req.readyState != 4)
187                         return;
188
189                 var xml = req.responseXML;
190
191                 var desc  = getElementTextNS('','description',xml,0);
192                 var xml_link  = getElementTextNS('','link',xml,0);
193
194                 var total  = getElementFloatNS('openSearch','totalResults',xml,0);
195                 var scale = getElementFloatNS('openIll','relevanceScale',xml,0);
196
197                 rel_scales[source]  = scale
198                 
199                 var current_tot = getElementFloatNS('','span',document.getElementById('total').parentNode,0);
200                 var tot = document.getElementById('total');
201
202                 if (!current_tot)
203                         current_tot = 0;
204
205                 if (total > (current_startPage * current_count))
206                         document.getElementById('next_button').className = '';
207
208                 current_tot += total
209                 tot.innerHTML = current_tot;
210
211                 var list = xml.getElementsByTagName('item');
212                 for (var i = 0; i < list.length; i++) {
213
214                         if ( typeof list[i] != 'object')
215                                         continue;
216
217                         var tab = document.getElementById('results');
218                         if (display_mode == 'col') {
219                                 var col = document.getElementById(source);
220                                 if (!col) {
221                                         var row = tab.rows[0];
222                                         if (!row)
223                                                 row = tab.insertRow(0);
224
225                                         col = document.createElement('td');
226                                         col.setAttribute('id',source);
227                                         row.appendChild(col);
228
229                                         tab = document.createElement('table');
230                                         tab.setAttribute('valign','top');
231                                         tab.setAttribute('class','col_tab');
232
233                                         var cap = document.createElement('caption');
234                                         tab.appendChild(cap);
235                                         cap.innerHTML = desc + ' -- <a href="' + xml_link + '">XML</a>';
236
237                                         col.appendChild(tab);
238
239                                         var per = parseInt(100 / search_urls.length);
240                                         col.setAttribute('valign','top');
241                                         col.setAttribute('width', + per + '%');
242
243                                 } else {
244                                         tab = col.firstChild;
245                                 }
246                         }
247
248                         if (!tab.rows.length) {
249                                 add_result_row(tab, 0, list[i], source);
250                         } else {
251                                 for (var j = 0; j < tab.rows.length; j++) {
252                                         if ( typeof tab.rows[j] != 'object')
253                                                 continue;
254
255                                         var rank;
256                                         try {
257                                                 rank = getElementFloatNS('openIll','relevance',list[i],0);
258                                         } catch (e) {
259                                                 alert("error getting float relevance: " + e);
260                                                 rank = 0;
261                                         }
262
263                                         if ( rank < parseFloat(tab.rows[j].firstChild.firstChild.textContent) ) {
264                                                 if ( (j + 1) == tab.rows.length) {
265                                                         add_result_row(tab, tab.rows.length, list[i], source);
266                                                         break
267                                                 }
268                                                 continue;
269                                         }
270                                         add_result_row(tab, j, list[i], source);
271                                         break;
272                                 }
273                         }
274                 }
275         };
276
277         req.open('GET', proxy + encodeURIComponent(search_urls[source]), true);
278         req.send(null);
279 }
280
281
282 // retrieve float of an XML document element, including
283 // elements using namespaces
284 function getElementFloatNS(prefix, local, parentElem, index) {
285     var result = "";
286     if (prefix && isIE) {
287         // IE/Windows way of handling namespaces
288         result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
289     } else {
290         // the namespace versions of this method 
291         // (getElementsByTagNameNS()) operate
292         // differently in Safari and Mozilla, but both
293         // return value with just local name, provided 
294         // there aren't conflicts with non-namespace element
295         // names
296         result = parentElem.getElementsByTagName(local)[index];
297     }
298     if (result) {
299         // get text, accounting for possible
300         // whitespace (carriage return) text nodes 
301         if (result.childNodes.length > 1) {
302             return parseFloat(result.childNodes[1].nodeValue);
303         } else {
304             return parseFloat(result.textContent);              
305         }
306     } else {
307         return 0;
308     }
309 }
310
311 // retrieve text of an XML document element, including
312 // elements using namespaces
313 function getElementTextNS(prefix, local, parentElem, index) {
314     var result = "";
315     if (prefix && isIE) {
316         // IE/Windows way of handling namespaces
317         result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
318     } else {
319         // the namespace versions of this method 
320         // (getElementsByTagNameNS()) operate
321         // differently in Safari and Mozilla, but both
322         // return value with just local name, provided 
323         // there aren't conflicts with non-namespace element
324         // names
325         result = parentElem.getElementsByTagName(local)[index];
326     }
327     if (result) {
328         // get text, accounting for possible
329         // whitespace (carriage return) text nodes 
330         if (result.childNodes.length > 1) {
331             return result.childNodes[1].nodeValue;
332         } else {
333             return result.textContent;                  
334         }
335     } else {
336         return '';
337     }
338 }
339
340 function add_result_row (tab, index, xml, source) {
341         var img = images[source];
342         var rank,title,tlink,desc;
343
344         try {
345                 rank = getElementFloatNS('openIll','relevance',xml,0);
346         } catch (e) {
347                 alert("error getting relevance: " + e);
348                 rank = '0';
349         }
350         
351         try {
352                 title = xml.getElementsByTagName('title')[0].textContent;
353         } catch (e) {
354                 title = '';
355         }
356         
357         try {
358                 tlink = xml.getElementsByTagName('link')[0].textContent;
359         } catch (e) {
360                 tlink = '';
361         }
362
363         try {
364                 description = xml.getElementsByTagName('description')[0].textContent;
365         } catch (e) {
366                 description = '';
367         }
368
369         var row = tab.insertRow(index);
370         row.className = 'res_tr';
371         row.innerHTML = '<td style="padding: 4px"><div style="display: none; visibility: hidden;">' + rank +
372                                                  '</div><span class="title_link"><a href="' + tlink + '">' + title +
373                                                  '</a></span><br/><span class="desc_text">' + description + '</span></td>' +
374                                                  '<td><img title="' + parseInt(rank) + '% Relevant" width="32" height="32" src="' + img + '"></td>';
375 }
376
377 function create_search ( s ) {
378         var req = create_requestor();
379
380         req.open('GET',proxy +  encodeURIComponent(s),false);
381         req.send(null);
382
383         try {
384                 var xml = req.responseXML;
385                 search_templates[s] = xml.getElementsByTagName('Url')[0].textContent;
386                 var i =  xml.getElementsByTagName('Image');
387                 if (i.length)
388                         images[s] = i[0].textContent;
389         } catch (e) {
390                 alert('BAD XML!\n\n' + e + '\n\n' + req.responseText);
391                 search_templates[s] = null;
392                 images[s] = null;
393         }
394
395 }
396
397                 </script>
398         </head>
399         <body>
400                 <br/>
401                 <form onsubmit="opensearch(document.getElementById('term').value, true); return false;">
402                 <table>
403                         <tr valign="top">
404                                 <td>Keyword Search: </td>
405                                 <td><input type="text" id="term" value="javascript"/></td>
406                         </tr>
407                         <tr valign="top">
408                                 <td>Sources: </td>
409                                 <td>
410                                         <select id="sources" multiple="multiple" size=5>
411                                                 <option value="http://gapines.org/opensearch.xml" selected>GPLS Pines</option>
412                                                 <option value="http://rsinger.library.gatech.edu/opensearch/osdd-gil.xml" selected>GIL Universal Catalog</option>
413                                                 <option value="http://search.athenscounty.lib.oh.us/cgi-bin/koha/opensearchdescription">NPL/Koha</option>
414                                                 <option value="http://www.webdevref.com/blog/opensearchdescription.xml">WebDefRef</option>
415                                         </select>
416                         </tr>
417                         <tr valign="top">
418                                 <td>Hits per Source: </td>
419                                 <td>
420                                         <select onchange="current_count=this.options[this.selectedIndex].value;">
421                                                 <option value="5" selected>5</option>
422                                                 <option value="10">10</option>
423                                                 <option value="25">25</option>
424                                         </select>
425                         </tr>
426                         <tr valign="top">
427                                 <td>Display mode: </td>
428                                 <td>
429                                         <select onchange="display_mode=this.options[this.selectedIndex].value;">
430                                                 <option value="int">Integrated</option>
431                                                 <option value="col" selected>Columns</option>
432                                         </select> (Use "Columns" when searching unranked sources)
433                         </tr>
434                         <tr valign="top">
435                                 <td colspan="2"><input type="submit" value="Go!"/></td>
436                         </tr>
437                 </table>
438                 </form>
439
440                 <div id="result_sources"></div>
441                 <br/>
442                 <div>Current page: <span id="page_label"></span> -- 
443                         <button class='hide' id='prev_button' onclick="if (this.className != 'hide') {current_startPage -= 1; opensearch(document.getElementById('term').value);}">Previous Page</button>
444                         ...
445                         <button class='hide' id='next_button' onclick="if (this.className != 'hide') {current_startPage += 1; opensearch(document.getElementById('term').value);}">Next Page</button>
446                 </div>
447                 <div>Total results: <span id="total"/></div>
448                 <h1>Search Results</h1>
449                 <hr/>
450                 <br/>
451                 <br/>
452                 <table class="res_table" id="results"></table>
453         </body>
454 </html>