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