]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/opensearchportal.html
adding column view back in
[working/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         font-size: small;
9 }
10
11 #result_sources a {
12         font-size: 8px;
13         color: grey;
14         text-decoration: none;
15 }
16
17 td {
18         vertical-align: top;
19 }
20
21 caption {
22         border: solid black 1px;
23 }
24
25 .header {
26         border: solid lightblue 1px;
27         background-color: lightblue;
28 }
29
30 a {
31         color: blue;
32         text-decoration: none;
33 }
34
35 a:hover {
36         color: blue;
37         text-decoration: underline;
38 }
39
40 a:active {
41         color: red;
42         text-decoration: underline;
43 }
44
45 a:visited {
46         color: blue;
47         text-decoration: none;
48 }
49
50 .title_link {
51         font-size: medium;
52         font-weight: bold;
53 }
54
55 .desc_text {
56         font-size: small;
57 }
58
59 .hide {
60         color: lightgray;
61 }
62
63 .col_tab {
64         border-collapse: collapse;
65         border: solid gray 1px;
66 }
67
68 .res_table {
69         border-collapse: collapse;
70 }
71
72 #col_res {
73         max-width: 300px !important;
74         min-width: 200px !important;
75 }
76
77 #int_res {
78         max-width: 100% !important;
79 }
80
81 #int_res_hide {
82         max-width: 100% !important;
83         min-width: 400px !important;
84 }
85
86 .res_tr {
87         border-bottom: 1px dashed darkgrey;
88 }
89
90 .noshow {
91         display: none;
92         visibility: hidden;
93 }
94
95                 </style>
96                 <script>
97
98 var isIE = false;
99
100 function create_requestor () {
101         var req;
102         try { 
103                 req = new ActiveXObject("Msxml2.XMLHTTP");
104                 isIE = true;
105         } catch (e) {
106                 try { 
107                         req = new ActiveXObject("Microsoft.XMLHTTP");
108                         isIE = true;
109                 } catch (E) {
110                         req = false;
111                 }
112         }
113
114         if (!req && typeof XMLHttpRequest!='undefined') {
115                 req = new XMLHttpRequest();
116         }
117         
118         if(!req) {
119                 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
120                 return null;
121         }
122
123         return req;
124 }
125
126 var proxy = 'http://gapines.org/opensearch/?fetch=';
127
128 var images = [];
129 var search_templates = [];
130 var search_urls = {};
131 var rel_scales = {};
132
133 var current_startPage = 1;
134 var current_startIndex = 1;
135 var current_count = 5;
136
137 function opensearch ( term, reset ) {
138
139         if (reset) {
140                 current_startPage = 1;
141                 rel_scales = {};
142         }
143
144         document.getElementById('next_button').className = 'hide';
145
146         if (current_startPage == 1)
147                 document.getElementById('prev_button').className = 'hide';
148         else 
149                 document.getElementById('prev_button').className = '';
150
151         var tot = document.getElementById('total');
152         while (tot.lastChild)
153                         tot.removeChild(tot.lastChild);
154
155         var src = document.getElementById('result_sources');
156         while (src.lastChild)
157                         src.removeChild(src.lastChild);
158
159         document.getElementById('int_res_hide').className = 'noshow';
160         document.getElementById('col_res_hide').className = 'noshow';
161
162         var tab = document.getElementById('int_res');
163         while (tab.lastChild)
164                         tab.removeChild(tab.lastChild);
165
166         tab = document.getElementById('col_res');
167         while (tab.lastChild)
168                         tab.removeChild(tab.lastChild);
169
170         search_count = 0;
171
172         var sources = new Array();
173         var selector = document.getElementsByName('source');
174         for (var i = 0; i < selector.length; i++) {
175                 if (selector[i].checked) {
176                         sources.push(selector[i].value);
177                 }
178         }
179
180         search_templates = [];
181         for (var i in sources) {
182                 create_search( sources[i] );
183         }
184
185         current_startIndex = (current_count * (current_startPage - 1)) + 1; 
186
187         search_urls = [];
188         for (var i in search_templates) {
189                 if (!search_templates[i])
190                         continue;
191
192                 if (!rel_scales[i])
193                         rel_scales[i] = 0;
194
195                 var url = search_templates[i].replace(/\{searchTerms\}/,encodeURIComponent(term));
196                 url = url.replace(/\{startPage\}/,current_startPage);
197                 url = url.replace(/\{startIndex\}/,current_startIndex);
198                 url = url.replace(/\{count\}/,current_count);
199                 url = url.replace(/\{relevanceScale}/,rel_scales[i]);
200                 search_urls[i] = proxy + encodeURIComponent(url);
201
202                 src.innerHTML += '<a href="' + url + '">' + url + '</a><br>';
203         }
204
205         for (var i in search_urls) {
206                 if (!search_templates[i])
207                         continue;
208
209                 perform_search(i);
210         }
211
212         document.getElementById('page_label').innerHTML = current_startPage;
213 }
214
215 function perform_search ( source ) {
216         var req = create_requestor();
217
218         req.onreadystatechange = function () {
219                 if (req.readyState != 4)
220                         return;
221
222                 var xml = req.responseXML;
223
224                 var desc  = getElementTextNS('','description',xml,0);
225                 var xml_link  = getElementTextNS('','link',xml,0);
226
227                 var total  = getElementFloatNS('openSearch','totalResults',xml,0);
228                 var integratible = (getElementNS('openIll','relevance',xml,0) != null);
229                 var scale = getElementFloatNS('openIll','relevanceScale',xml,0);
230
231                 rel_scales[source]  = scale;
232                 
233                 var current_tot = getElementFloatNS('','span',document.getElementById('total').parentNode,0);
234                 var tot = document.getElementById('total');
235                 var res_type = document.getElementById('res_type');
236
237                 if (res_type.options[res_type.selectedIndex].value == 'col')
238                         integratible = 0;
239
240                 if (!current_tot)
241                         current_tot = 0;
242
243                 if (total > (current_startPage * current_count))
244                         document.getElementById('next_button').className = '';
245
246                 current_tot += total
247                 tot.innerHTML = current_tot;
248
249                 var list = xml.getElementsByTagName('item');
250                 for (var i = 0; i < list.length; i++) {
251
252                         if ( typeof list[i] != 'object')
253                                         continue;
254
255                         var tab;
256                         if (!integratible) {
257                                 tab = document.getElementById('col_res');
258                                 document.getElementById('col_res_hide').className = '';
259                                 var col = document.getElementById(source);
260                                 if (!col) {
261                                         var row = tab.rows[0];
262                                         if (!row)
263                                                 row = tab.insertRow(0);
264
265                                         col = document.createElement('td');
266                                         col.setAttribute('id',source);
267                                         row.appendChild(col);
268
269                                         tab = document.createElement('table');
270                                         tab.setAttribute('valign','top');
271                                         tab.setAttribute('class','col_tab');
272
273                                         var cap = document.createElement('caption');
274                                         tab.appendChild(cap);
275                                         cap.innerHTML = desc + ' -- <a href="' + xml_link + '">XML</a>';
276
277                                         col.appendChild(tab);
278
279                                         var per = parseInt(100 / (search_urls.length * 2)) - 1;
280                                         col.setAttribute('valign','top');
281                                         col.setAttribute('width', + per + '%');
282
283                                 } else {
284                                         tab = col.firstChild;
285                                 }
286                         } else {
287                                 tab = document.getElementById('int_res');
288                                 document.getElementById('int_res_hide').className = '';
289                         }
290
291                         if (!tab.rows.length) {
292                                 add_result_row(tab, 0, list[i], source);
293                         } else {
294                                 for (var j = 0; j < tab.rows.length; j++) {
295                                         if ( typeof tab.rows[j] != 'object')
296                                                 continue;
297
298                                         var rank;
299                                         try {
300                                                 rank = getElementFloatNS('openIll','relevance',list[i],0);
301                                         } catch (e) {
302                                                 alert("error getting float relevance: " + e);
303                                                 rank = 0;
304                                         }
305
306                                         if ( rank < parseFloat(tab.rows[j].firstChild.firstChild.textContent) ) {
307                                                 if ( (j + 1) == tab.rows.length) {
308                                                         add_result_row(tab, tab.rows.length, list[i], source);
309                                                         break
310                                                 }
311                                                 continue;
312                                         }
313                                         add_result_row(tab, j, list[i], source);
314                                         break;
315                                 }
316                         }
317                 }
318         };
319
320         req.open('GET', proxy + encodeURIComponent(search_urls[source]), true);
321         req.send(null);
322 }
323
324
325 // retrieve float of an XML document element, including
326 // elements using namespaces
327 function getElementFloatNS(prefix, local, parentElem, index) {
328     var result = getElementNS(prefix, local, parentElem, index);
329     if (result) {
330         // get text, accounting for possible
331         // whitespace (carriage return) text nodes 
332         if (result.childNodes.length > 1) {
333             return parseFloat(result.childNodes[1].nodeValue);
334         } else {
335             return parseFloat(result.textContent);              
336         }
337     } else {
338         return 0;
339     }
340 }
341
342 function getElementNS(prefix, local, parentElem, index) {
343     var result = "";
344     if (prefix && isIE) {
345         // IE/Windows way of handling namespaces
346         return parentElem.getElementsByTagName(prefix + ":" + local)[index];
347     } else {
348         // the namespace versions of this method 
349         // (getElementsByTagNameNS()) operate
350         // differently in Safari and Mozilla, but both
351         // return value with just local name, provided 
352         // there aren't conflicts with non-namespace element
353         // names
354         return parentElem.getElementsByTagName(local)[index];
355     }
356 }
357
358 // retrieve text of an XML document element, including
359 // elements using namespaces
360 function getElementTextNS(prefix, local, parentElem, index) {
361     var result = getElementNS(prefix, local, parentElem, index);
362     if (result) {
363         // get text, accounting for possible
364         // whitespace (carriage return) text nodes 
365         if (result.childNodes.length > 1) {
366             return result.childNodes[1].nodeValue;
367         } else {
368             return result.firstChild.nodeValue;                 
369         }
370     } else {
371         return '';
372     }
373 }
374
375 function add_result_row (tab, index, xml, source) {
376         var img = images[source];
377         var rank,title,tlink,desc;
378
379         try {
380                 rank = getElementFloatNS('openIll','relevance',xml,0);
381         } catch (e) {
382                 alert("error getting relevance: " + e);
383                 rank = '0';
384         }
385         
386         try {
387                 title = getElementTextNS('','title',xml,0);
388         } catch (e) {
389                 title = '';
390         }
391         
392         try {
393                 tlink = getElementTextNS('','link',xml,0);
394         } catch (e) {
395                 tlink = '';
396         }
397
398         try {
399                 description = getElementTextNS('','description',xml,0);
400                 if (description.length > 1024)
401                         description = description.substring(1,1024);
402         } catch (e) {
403                 description = '';
404         }
405
406         var row = tab.insertRow(index);
407         row.className = 'res_tr';
408         row.innerHTML = '<td style="padding: 4px"><div style="display: none; visibility: hidden;">' + rank +
409                                                  '</div><span class="title_link"><a href="' + tlink + '">' + title +
410                                                  '</a></span><br/><span class="desc_text">' + description + '</span></td>' +
411                                                  '<td><img title="' + parseInt(rank) + '% Relevant" width="32" height="32" src="' + img + '"></td>';
412 }
413
414 function create_search ( s ) {
415         var req = create_requestor();
416
417         req.open('GET',proxy +  encodeURIComponent(s),false);
418         req.send(null);
419
420         try {
421                 var xml = req.responseXML;
422                 search_templates[s] = xml.getElementsByTagName('Url')[0].textContent;
423                 var i =  xml.getElementsByTagName('Image');
424                 if (i.length)
425                         images[s] = i[0].textContent;
426         } catch (e) {
427                 alert('BAD XML!\n\n' + e + '\n\n' + req.responseText);
428                 search_templates[s] = null;
429                 images[s] = null;
430         }
431
432 }
433
434                 </script>
435         </head>
436         <body>
437                 <br/>
438                 <form>
439                 <table style="border-collapse: collapse; margin: 5px;" width="100%">
440                         <tr style="border-bottom: dotted black 1px;" valign="top">
441                                 <td align="right">Keyword Search: </td>
442                                 <td align="left">
443                                         <input type="text" id="term" value="javascript"/>
444                                         <input type="submit" value="Go!" onclick="opensearch(document.getElementById('term').value, true); return false;"/>
445                                 </td>
446                                 <td align="left">Hits per Source for each page: 
447                                         <select onchange="current_count=this.options[this.selectedIndex].value;">
448                                                 <option value="5" selected>5</option>
449                                                 <option value="10">10</option>
450                                                 <option value="25">25</option>
451                                         </select>
452                                 </td>
453                                 <td align="left">Display style: 
454                                         <select id="res_type">
455                                                 <option value="int" selected>Merged Results</option>
456                                                 <option value="col">Seperate Columns</option>
457                                         </select>
458                                 </td>
459                         </tr>
460                         <tr style="border-bottom: dotted black 1px;" valign="top">
461                                 <td align="right">Sources: </td>
462                                 <td colspan=2>
463                                         <table width="100%" style="border-collapse: collapse;">
464                                                 <tr>
465                                                         <td>
466                                                                 <label class="source_input">
467                                                                         <input
468                                                                                 name="source"
469                                                                                 type="checkbox"
470                                                                                 value="http://gapines.org/opensearch.xml"
471                                                                                 checked>GPLS Pines
472                                                                 </label>
473                                                         </td>
474                                                         <td>
475                                                                 <label class="source_input">
476                                                                         <input
477                                                                                 name="source"
478                                                                                 type="checkbox"
479                                                                                 value="http://rsinger.library.gatech.edu/opensearch/osdd-gil.xml"
480                                                                                 checked>GIL Universal Catalog
481                                                                 </label>
482                                                         </td>
483                                                         <td>
484                                                                 <label class="source_input">
485                                                                         <input
486                                                                                 name="source"
487                                                                                 type="checkbox"
488                                                                                 value="http://search.athenscounty.lib.oh.us/cgi-bin/koha/opensearchdescription">NPL/Koha
489                                                                 </label>
490                                                         </td>
491                                                         <td>
492                                                                 <label class="source_input">
493                                                                         <input
494                                                                                 name="source"
495                                                                                 type="checkbox"
496                                                                                 value="http://www.koders.com/search/KodersSourceCodeSearchDescription.xml">Koders Source Code
497                                                                 </label>
498                                                         </td>
499                                                         <td>
500                                                 </tr>
501                                                 <tr>
502                                                         <td>
503                                                                 <label class="source_input">
504                                                                         <input
505                                                                                 name="source"
506                                                                                 type="checkbox"
507                                                                                 value="http://cnx.rice.edu/content/opensearchdescription">rice.edu Connexions
508                                                                 </label>
509                                                         </td>
510                                                         <td>
511                                                                 <label class="source_input">
512                                                                         <input
513                                                                                 name="source"
514                                                                                 type="checkbox"
515                                                                                 value="http://redlightgreen.com/ucwprod/web/opensearchDescription.xml">RedLightGreen
516                                                                 </label>
517                                                         </td>
518                                                         <td>
519                                                                 <label class="source_input">
520                                                                         <input
521                                                                                 name="source"
522                                                                                 type="checkbox"
523                                                                                 value="http://www.itpapers.com/itpaperssearchdescription.xml">ITPapers
524                                                                 </label>
525                                                         </td>
526                                         <!-- <input name="source" type="checkbox" value="http://www.webdevref.com/blog/opensearchdescription.xml">WebDefRef -->
527                                                 </tr>
528                                         </table>
529                                 </td>
530                         </tr>
531                         <tr>
532                                 <td>Total results: </td>
533                                 <td colspan=2 id="total"></td>
534                         </tr>
535                         <tr>
536                                 <td>Current page: </td>
537                                 <td id="page_label"></td>
538                                 <td>
539                                         <button
540                                                 class='hide'
541                                                 id='prev_button'
542                                                 onclick="
543                                                         if (this.className != 'hide') {
544                                                                 current_startPage -= 1;
545                                                                 opensearch(document.getElementById('term').value);
546                                                         }
547                                                         return false;">Previous Page
548                                         </button> ...
549                                         <button
550                                                 class='hide'
551                                                 id='next_button'
552                                                 onclick="
553                                                         if (this.className != 'hide') {
554                                                                 current_startPage += 1;
555                                                                 opensearch(document.getElementById('term').value);
556                                                         }
557                                                         return false;">Next Page
558                                         </button>
559                                 </td>
560                         </tr>
561                 </table>
562                 </form>
563
564                 <hr/>
565                 <br/>
566                 <table id="results" width="100%">
567                         <tr>
568                                 <td id="int_res_hide" class="noshow" width="100%">
569                                         <table width="100%">
570                                                 <caption class="header">Merged search results</caption>
571                                                 <tr>
572                                                         <td width="100%">
573                                                                 <table id='int_res' class="res_table" width="100%"></table>
574                                                         </td>
575                                                 </tr>
576                                         </table>
577                                 </td>
578                                 <td id='col_res_hide' class="noshow">
579                                         <table>
580                                                 <caption class="header">Unranked search results</caption>
581                                                 <tr>
582                                                         <td>
583                                                                 <table id='col_res' class="res_table"></table>
584                                                         </td>
585                                                 </tr>
586                                         </table>
587                                 </td>
588                         </tr>
589                 </table>
590                 <div id="result_sources"></div>
591                 <br/>
592         </body>
593 </html>