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