1 /* A very simple client that shows a basic usage of the pz2.js
6 var pazpar2_url = mkws_config.pazpar2_url ? mkws_config.pazpar2_url : "/pazpar2/search.pz2";
7 var service_proxy_url = mkws_config.service_proxy_url ? mkws_config.service_proxy_url : "/service-proxy/";
9 var pazpar2path = mkws_config.use_service_proxy ? service_proxy_url : pazpar2_url;
10 var usesessions = mkws_config.use_service_proxy ? false : true;
12 // create a parameters array and pass it to the pz2's constructor
13 // then register the form submit event with the pz2.search function
14 // autoInit is set to true on default
15 var my_paz = new pz2( { "onshow": my_onshow,
16 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
17 "pazpar2path": pazpar2path,
21 "termlist": "xtargets,subject,author",
22 "onbytarget": my_onbytarget,
23 "usesessions" : usesessions,
24 "showResponseType": '', // or "json" (for debugging?)
25 "onrecord": my_onrecord } );
31 var curDetRecData = null;
32 var curSort = 'relevance';
34 var submitted = false;
40 // pz2.js event handlers:
42 function my_oninit() {
47 function my_onshow(data) {
48 totalRec = data.merged;
50 var pager = document.getElementById("pager");
52 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
53 + (data.start + 1) + ' to ' + (data.start + data.num) +
54 ' of ' + data.merged + ' (found: '
55 + data.total + ')</div>';
58 var results = document.getElementById("results");
61 for (var i = 0; i < data.hits.length; i++) {
62 var hit = data.hits[i];
63 html.push('<div class="record" id="recdiv_'+hit.recid+'" >'
64 +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>'
65 +'<a href="#" id="rec_'+hit.recid
66 +'" onclick="showDetails(this.id);return false;"><b>'
67 + hit["md-title"] +' </b></a>');
68 if (hit["md-title-remainder"] !== undefined) {
69 html.push('<span>' + hit["md-title-remainder"] + ' </span>');
71 if (hit["md-title-responsibility"] !== undefined) {
72 html.push('<span><i>'+hit["md-title-responsibility"]+'</i></span>');
74 if (hit.recid == curDetRecId) {
75 html.push(renderDetails(curDetRecData));
79 replaceHtml(results, html.join(''));
82 function my_onstat(data) {
83 var stat = document.getElementById("mkwsStat");
87 stat.innerHTML = '<b>STATUS INFO</b> -- Active clients: '
89 + '/' + data.clients + ' -- </span>'
90 + '<span>Retrieved records: ' + data.records
91 + '/' + data.hits + ' :.</span>';
94 function my_onterm(data) {
96 termlists.push('<hr/><b>TERMLISTS:</b><hr/><div class="termtitle">Sources</div>');
97 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
98 termlists.push('<a href="#" target_id='+data.xtargets[i].id
99 + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">' + data.xtargets[i].name
100 + ' </a><span> (' + data.xtargets[i].freq + ')</span><br/>');
103 termlists.push('<hr/><div class="termtitle">Subjects</div>');
104 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
105 termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">' + data.subject[i].name + '</a><span> ('
106 + data.subject[i].freq + ')</span><br/>');
109 termlists.push('<hr/><div class="termtitle">Authors</div>');
110 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
111 termlists.push('<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
112 + data.author[i].name
114 + data.author[i].freq
117 var termlist = document.getElementById("termlist");
118 replaceHtml(termlist, termlists.join(''));
121 function my_onrecord(data) {
122 // FIXME: record is async!!
123 clearTimeout(my_paz.recordTimer);
124 // in case on_show was faster to redraw element
125 var detRecordDiv = document.getElementById('det_'+data.recid);
126 if (detRecordDiv) return;
127 curDetRecData = data;
128 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
129 var html = renderDetails(curDetRecData);
130 recordDiv.innerHTML += html;
133 function my_onbytarget(data) {
134 var targetDiv = document.getElementById("bytarget");
135 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
136 +'<td>Records</td><td>State</td></tr></thead><tbody>';
138 for (var i = 0; i < data.length; i++ ) {
139 table += "<tr><td>" + data[i].id +
140 "</td><td>" + data[i].hits +
141 "</td><td>" + data[i].diagnostic +
142 "</td><td>" + data[i].records +
143 "</td><td>" + data[i].state + "</td></tr>";
146 table += '</tbody></table>';
147 targetDiv.innerHTML = table;
150 ////////////////////////////////////////////////////////////////////////////////
151 ////////////////////////////////////////////////////////////////////////////////
153 // wait until the DOM is ready
156 document.search.onsubmit = onFormSubmitEventHandler;
157 document.search.query.value = '';
158 document.select.sort.onchange = onSelectDdChange;
159 document.select.perpage.onchange = onSelectDdChange;
162 // when search button pressed
163 function onFormSubmitEventHandler()
172 function onSelectDdChange()
174 if (!submitted) return false;
177 my_paz.show(0, recPerPage, curSort);
187 function triggerSearch ()
189 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
192 function loadSelect ()
194 curSort = document.select.sort.value;
195 recPerPage = document.select.perpage.value;
198 // limit the query after clicking the facet
199 function limitQuery (field, value)
201 document.search.query.value += ' and ' + field + '="' + value + '"';
202 onFormSubmitEventHandler();
205 // limit by target functions
206 function limitTarget (id, name)
208 var navi = document.getElementById('navi');
210 'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
212 navi.innerHTML += '<hr/>';
213 curFilter = 'pz:id=' + id;
220 function delimitTarget ()
222 var navi = document.getElementById('navi');
231 function drawPager (pagerDiv)
233 //client indexes pages from 1 but pz2 from 0
235 var pages = Math.ceil(totalRec / recPerPage);
237 var firstClkbl = ( curPage - onsides > 0 )
241 var lastClkbl = firstClkbl + 2*onsides < pages
242 ? firstClkbl + 2*onsides
245 var prev = '<span id="prev"><< Prev</span><b> | </b>';
247 prev = '<a href="#" id="prev" onclick="pagerPrev();">'
248 +'<< Prev</a><b> | </b>';
251 for(var i = firstClkbl; i <= lastClkbl; i++) {
254 numLabel = '<b>' + i + '</b>';
256 middle += '<a href="#" onclick="showPage(' + i + ')"> '
257 + numLabel + ' </a>';
260 var next = '<b> | </b><span id="next">Next >></span>';
261 if (pages - curPage > 0)
262 next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
263 +'Next >></a>';
270 if (lastClkbl < pages)
273 pagerDiv.innerHTML += '<div style="float: clear">'
274 + prev + predots + middle + postdots + next + '</div><hr/>';
277 function showPage (pageNum)
280 my_paz.showPage( curPage - 1 );
283 // simple paging functions
285 function pagerNext() {
286 if ( totalRec - recPerPage*curPage > 0) {
292 function pagerPrev() {
293 if ( my_paz.showPrev() != false )
297 // swithing view between targets and records
299 function switchView(view) {
301 var targets = document.getElementById('mkwsTargets');
302 var records = document.getElementById('mkwsRecords');
306 targets.style.display = "block";
307 records.style.display = "none";
310 targets.style.display = "none";
311 records.style.display = "block";
314 alert('Unknown view.');
318 // detailed record drawing
319 function showDetails (prefixRecId) {
320 var recId = prefixRecId.replace('rec_', '');
321 var oldRecId = curDetRecId;
324 // remove current detailed view if any
325 var detRecordDiv = document.getElementById('det_'+oldRecId);
328 detRecordDiv.parentNode.removeChild(detRecordDiv);
330 // if the same clicked, just hide
331 if (recId == oldRecId) {
333 curDetRecData = null;
336 // request the record
337 my_paz.record(recId);
340 function replaceHtml(el, html) {
341 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
342 /*@cc_on // Pure innerHTML is slightly faster in IE
343 oldEl.innerHTML = html;
346 var newEl = oldEl.cloneNode(false);
347 newEl.innerHTML = html;
348 oldEl.parentNode.replaceChild(newEl, oldEl);
349 /* Since we just removed the old element from the DOM, return a reference
350 to the new element, which can be used to restore variable references. */
354 function renderDetails(data, marker)
356 var details = '<div class="details" id="det_'+data.recid+'"><table>';
357 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
358 if (data["md-title"] != undefined) {
359 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
360 if (data["md-title-remainder"] !== undefined) {
361 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
363 if (data["md-title-responsibility"] !== undefined) {
364 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
366 details += '</td></tr>';
368 if (data["md-date"] != undefined)
369 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
370 if (data["md-author"] != undefined)
371 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
372 if (data["md-electronic-url"] != undefined)
373 details += '<tr><td><b>URL</b></td><td><b>:</b> <a href="' + data["md-electronic-url"] + '" target="_blank">' + data["md-electronic-url"] + '</a>' + '</td></tr>';
374 if (data["location"][0]["md-subject"] != undefined)
375 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
376 if (data["location"][0]["@name"] != undefined)
377 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
378 details += '</table></div>';
383 * All the HTML stuff to render the search forms and
386 function mkws_html_all(data) {
390 sort: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
391 perpage: [10, 20, 30, 50],
392 sort_default: "relevance",
395 switch: true, /* show/hide Records|Targets menu */
400 /* override standard config values by function parameters */
401 for (var k in data) {
405 // For some reason, doing this programmatically results in
406 // document.search.query being undefined, hence the raw HTML.
407 $("#mkwsSearch").html('\
408 <form id="searchForm" name="search">\
409 <input id="query" type="text" size="50" />\
410 <input id="button" type="submit" value="Search" />\
413 $("#mkwsRecords").html('\
414 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
416 <td width="250" valign="top">\
417 <div id="termlist"></div>\
421 <form name="select" id="select">\
422 Sort by' + mkws_html_sort(config) + '\
423 and show ' + mkws_html_perpage(config) + '\
427 <div id="pager"></div>\
428 <div id="navi"></div>\
429 <div id="results"></div>\
435 mkws_html_switch(config);
436 if (mkws_config.use_service_proxy)
437 mkws_service_proxy_auth(config.service_proxy_auth);
442 function mkws_html_switch(config) {
443 $("#mkwsSwitch").html($("<a/>", {
445 onclick: "switchView(\'records\')",
448 $("#mkwsSwitch").append($("<span/>", { text: " | " }));
449 $("#mkwsSwitch").append($("<a/>", {
451 onclick: "switchView(\'targets\')",
455 $("#mkwsTargets").html('\
457 No information available yet.\
459 $("#mkwsTargets").css("display", "none");
461 if (!config.switch) {
462 $("#mkwsSwitch").css("display", "none");
466 function mkws_html_sort(config) {
467 var sort_html = '<select name="sort" id="sort">';
469 for(var i = 0; i < config.sort.length; i++) {
470 var key = config.sort[i][0];
471 var val = config.sort[i].length == 1 ? config.sort[i][0] : config.sort[i][1];
473 sort_html += '<option value="' + key + '"';
474 if (key == config.sort_default) {
475 sort_html += ' selected="selected"';
477 sort_html += '>' + val + '</option>';
479 sort_html += '</select>';
484 function mkws_html_perpage(config) {
485 var perpage_html = '<select name="perpage" id="perpage">';
487 for(var i = 0; i < config.perpage.length; i++) {
488 var key = config.perpage[i];
490 perpage_html += '<option value="' + key + '"';
491 if (key == config.perpage_default) {
492 perpage_html += ' selected="selected"';
494 perpage_html += '>' + key + '</option>';
496 perpage_html += '</select>';
502 * Run service-proxy authentication in background (after page load).
503 * The username/password is configured in the apache config file
506 function mkws_service_proxy_auth(auth_url) {
508 auth_url = "/service-proxy-auth";
510 var jqxhr = jQuery.get(auth_url)
512 alert("service proxy authentication failed, give up!");
514 .success(function(data) {
515 if (!jQuery.isXMLDoc(data)) {
516 alert("service proxy auth response document is not valid XML document, give up!");
519 var status = $(data).find("status");
520 if (status.text() != "OK") {
521 alert("service proxy auth repsonse status: " + status.text() + ", give up!");
528 $(document).ready(function() { mkws_html_all(mkws_config) });