1 /* A very simple client that shows a basic usage of the pz2.js
4 // create a parameters array and pass it to the pz2's constructor
5 // then register the form submit event with the pz2.search function
6 // autoInit is set to true on default
7 var usesessions = true;
8 var pazpar2path = '/service-proxy/';
9 var showResponseType = '';
10 var querys = {'su': '', 'au': '', 'xt': ''};
11 var showResponseType = 'json';
12 if (document.location.hash == '#pazpar2' || document.location.search.match("useproxy=false")) {
14 pazpar2path = '/pazpar2/search.pz2';
15 showResponseType = 'xml';
19 my_paz = new pz2( { "onshow": my_onshow,
20 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
21 "pazpar2path": pazpar2path,
24 "onterm": my_onterm_iphone,
25 "termlist": "xtargets,subject,author",
26 "onbytarget": my_onbytarget,
27 "usesessions" : usesessions,
28 "showResponseType": showResponseType,
29 "onrecord": my_onrecord } );
35 var curDetRecData = null;
36 var curSort = 'relevance';
37 var curFilter = 'ALL';
38 var submitted = false;
42 var tab = "recordview";
47 function loginFormSubmit() {
48 triedUser = document.loginForm.username.value;
49 triedPass = document.loginForm.password.value;
50 auth.login( {"username": triedUser,
51 "password": triedPass},
55 function handleKeyPress(e, formId, focusId)
59 key = window.event.keyCode;
63 if(key == 13 || key == 10)
65 document.getElementById(formId).submit();
66 focusElement = document.getElementById(focusId);
75 function authCb(authData) {
76 if (!authData.loginFailed) {
81 if (authData.loggedIn == true) {
82 showhide("recordview");
86 function logOutClick() {
87 auth.logOut(authCb, authCb);
90 function loggedOut() {
91 var login = document.getElementById("login");
92 login.innerHTML = 'Login';
95 function loggingOutFailed() {
96 alert("Logging out failed");
104 auth.logOut(loggedOut, loggingOutFailed, true);
107 function logInOrOut() {
108 var loginElement = document.getElementById("login");
109 if (loginElement.innerHTML == 'Login')
114 function loggedIn() {
115 var login = document.getElementById("login");
116 login.innerHTML = 'Logout(' + auth.displayName + ')';
117 document.getElementById("log").innerHTML = login.innerHTML;
120 function auth_check() {
121 auth.check(loggedIn, login);
126 // Pz2.js event handlers:
128 function my_oninit() {
133 function my_onshow(data) {
134 totalRec = data.merged;
136 var pager = document.getElementById("pager");
137 pager.innerHTML = "";
138 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
139 + (data.start + 1) + ' to ' + (data.start + data.num) +
140 ' of ' + data.merged + ' (found: '
141 + data.total + ')</div>';
144 var results = document.getElementById("results");
147 if (data.hits == undefined)
149 for (var i = 0; i < data.hits.length; i++) {
150 var hit = data.hits[i];
151 html.push('<li id="recdiv_'+hit.recid+'" >'
152 /* +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>' */
153 +'<a href="#" id="rec_'+hit.recid
154 +'" onclick="showDetails(this.id);return false;">'
155 + hit["md-title"] +'</a> ');
156 if (hit["md-title-remainder"] !== undefined) {
157 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
158 if (hit["md-author"] != undefined) {
159 html.push('<a href="#">'+hit["md-author"]+'</a> ');
161 else if (hit["md-title-responsibility"] !== undefined) {
162 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
165 if (hit.recid == curDetRecId) {
166 html.push(renderDetails_iphone(curDetRecData));
170 replaceHtml(results, html.join(''));
173 function my_onstat(data) {
174 var stat = document.getElementById("stat");
178 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
180 + '/' + data.clients + ' -- </span>'
181 + '<span>Retrieved records: ' + data.records
182 + '/' + data.hits + ' :.</span>';
185 function showhide(newtab) {
186 var showtermlist = false;
190 if (tab == "recordview") {
191 document.getElementById("recordview").style.display = '';
194 document.getElementById("recordview").style.display = 'none';
196 if (tab == "xtargets") {
197 document.getElementById("term_xtargets").style.display = '';
201 document.getElementById("term_xtargets").style.display = 'none';
203 if (tab == "subjects") {
204 document.getElementById("term_subjects").style.display = '';
208 document.getElementById("term_subjects").style.display = 'none';
210 if (tab == "authors") {
211 document.getElementById("term_authors").style.display = '';
215 document.getElementById("term_authors").style.display = 'none';
217 if (showtermlist == false)
218 document.getElementById("termlist").style.display = 'none';
220 document.getElementById("termlist").style.display = '';
222 var tabDiv = document.getElementById("loginDiv");
223 if (tab == "login") {
224 tabDiv.style.display = '';
227 tabDiv.style.display = 'none';
231 function my_onterm(data) {
234 termlists.push('<div id="term_xtargets" >');
235 termlists.push('<h4 class="termtitle">Sources</h4>');
236 termlists.push('<ul>');
237 termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
238 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
239 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
240 + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">'
241 + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
243 termlists.push('</ul>');
244 termlists.push('</div>');
246 termlists.push('<div id="term_subjects" >');
247 termlists.push('<h4>Subjects</h4>');
248 termlists.push('<ul>');
249 termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
250 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
251 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">'
252 + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
254 termlists.push('</ul>');
255 termlists.push('</div>');
257 termlists.push('<div id="term_authors" >');
258 termlists.push('<h4 class="termtitle">Authors</h4>');
259 termlists.push('<ul>');
260 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
261 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
262 termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.author[i].name +'\');return false;">'
263 + data.author[i].name
265 + data.author[i].freq
268 termlists.push('</ul>');
269 termlists.push('</div>');
270 var termlist = document.getElementById("termlist");
271 replaceHtml(termlist, termlists.join(''));
276 function my_onterm_iphone(data) {
278 var targets = "reset_xt|All\n";
280 for (var i = 0; i < data.xtargets.length; i++ ) {
282 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
284 termlist["xtargets"] = targets;
285 var subjects = "reset_su|All\n";
286 for (var i = 0; i < data.subject.length; i++ ) {
287 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
289 termlist["subjects"] = subjects;
290 var authors = "reset_au|All\n";
291 for (var i = 0; i < data.author.length; i++ ) {
292 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
294 termlist["authors"] = authors;
295 callback.send("termlist", "refresh");
298 function getTargets() {
299 return termlist['xtargets'];
302 function getSubjects() {
303 return termlist['subjects'];
306 function getAuthors() {
307 return termlist['authors'];
310 function my_onrecord(data) {
311 // FIXME: record is async!!
312 clearTimeout(my_paz.recordTimer);
313 // in case on_show was faster to redraw element
314 var detRecordDiv = document.getElementById('det_'+data.recid);
315 if (detRecordDiv) return;
316 curDetRecData = data;
317 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
318 var html = renderDetails_iphone(curDetRecData);
319 recordDiv.innerHTML += html;
322 function my_onrecord_iphone(data) {
324 callback.send("record", data.recid, data, data.xtargets[i].freq);
328 function my_onbytarget(data) {
329 var targetDiv = document.getElementById("bytarget");
330 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
331 +'<td>Records</td><td>State</td></tr></thead><tbody>';
333 for (var i = 0; i < data.length; i++ ) {
334 table += "<tr><td>" + data[i].id +
335 "</td><td>" + data[i].hits +
336 "</td><td>" + data[i].diagnostic +
337 "</td><td>" + data[i].records +
338 "</td><td>" + data[i].state + "</td></tr>";
341 table += '</tbody></table>';
342 targetDiv.innerHTML = table;
345 ////////////////////////////////////////////////////////////////////////////////
346 ////////////////////////////////////////////////////////////////////////////////
348 // wait until the DOM is ready
351 document.search.onsubmit = onFormSubmitEventHandler;
352 document.search.query.value = '';
353 document.select.sort.onchange = onSelectDdChange;
354 document.select.perpage.onchange = onSelectDdChange;
355 if (document.location.search.match("inApp=true"))
356 applicationMode(true);
358 applicationMode(false);
361 function applicationMode(newmode)
363 var searchdiv = document.getElementById("searchForm");
367 document.getElementById("heading").style.display="none";
368 searchdiv.style.display = 'none';
372 document.getElementById("nav").style.display="";
373 document.getElementById("normal").style.display="inline";
374 document.getElementById("normal").style.visibility="";
375 searchdiv.style.display = '';
376 document.search.onsubmit = onFormSubmit;
380 // when search button pressed
381 function onFormSubmitEventHandler()
384 document.getElementById("logo").style.display = 'none';
391 function onSelectDdChange()
393 if (!submitted) return false;
396 my_paz.show(0, recPerPage, curSort);
406 function triggerSearch ()
408 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
412 "limit" : getFacets()
418 function loadSelect ()
420 curSort = document.select.sort.value;
421 recPerPage = document.select.perpage.value;
424 // limit the query after clicking the facet
425 function limitQuery(field, value)
427 var newQuery = ' and ' + field + '="' + value + '"';
428 querys[field] += newQuery;
429 document.search.query.value += newQuery;
430 onFormSubmitEventHandler();
431 showhide("recordview");
434 // limit the query after clicking the facet
435 function removeQuery (field, value) {
436 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
437 onFormSubmitEventHandler();
438 showhide("recordview");
441 // limit the query after clicking the facet
442 function limitOrResetQuery (field, value, selected) {
443 if (field == 'reset_su' || field == 'reset_au') {
444 var reset_field = field.substring(6);
445 document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
446 querys[reset_field] = '';
447 onFormSubmitEventHandler();
448 showhide("recordview");
451 limitQuery(field, value);
452 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
455 // limit by target functions
456 function limitTarget (id, name)
458 curFilter = 'pz:id=' + id;
462 showhide("recordview");
466 function delimitTarget ()
475 function limitOrResetTarget(id, name) {
476 if (id == 'reset_xt') {
480 limitTarget(id,name);
484 function drawPager (pagerDiv)
486 //client indexes pages from 1 but pz2 from 0
488 var pages = Math.ceil(totalRec / recPerPage);
490 var firstClkbl = ( curPage - onsides > 0 )
494 var lastClkbl = firstClkbl + 2*onsides < pages
495 ? firstClkbl + 2*onsides
498 var prev = '<span id="prev"><< Prev</span><b> | </b>';
500 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
501 +'<< Prev</a><b> | </b>';
504 for(var i = firstClkbl; i <= lastClkbl; i++) {
507 numLabel = '<b>' + i + '</b>';
509 middle += '<a href="#" onclick="showPage(' + i + ')"> '
510 + numLabel + ' </a>';
513 var next = '<b> | </b><span id="next">Next >></span>';
514 if (pages - curPage > 0)
515 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
516 +'Next >></a>';
523 if (lastClkbl < pages)
526 pagerDiv.innerHTML += '<div style="float: none">'
527 + prev + predots + middle + postdots + next + '</div><hr/>';
530 function showPage (pageNum)
533 my_paz.showPage( curPage - 1 );
536 // simple paging functions
538 function pagerNext() {
539 if ( totalRec - recPerPage*curPage > 0) {
545 function pagerPrev() {
546 if ( my_paz.showPrev() != false )
550 // swithing view between targets and records
552 function switchView(view) {
554 var targets = document.getElementById('targetview');
555 var records = document.getElementById('recordview');
559 targets.style.display = "block";
560 records.style.display = "none";
563 targets.style.display = "none";
564 records.style.display = "block";
567 alert('Unknown view.');
571 // detailed record drawing
572 function showDetails (prefixRecId) {
573 var recId = prefixRecId.replace('rec_', '');
574 var oldRecId = curDetRecId;
577 // remove current detailed view if any
578 var detRecordDiv = document.getElementById('det_'+oldRecId);
579 //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null);
582 detRecordDiv.parentNode.removeChild(detRecordDiv);
584 // if the same clicked, just hide
585 if (recId == oldRecId) {
587 curDetRecData = null;
590 // request the record
591 my_paz.record(recId);
594 function replaceHtml(el, html) {
595 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
596 /*@cc_on // Pure innerHTML is slightly faster in IE
597 oldEl.innerHTML = html;
600 var newEl = oldEl.cloneNode(false);
601 newEl.innerHTML = html;
602 oldEl.parentNode.replaceChild(newEl, oldEl);
603 /* Since we just removed the old element from the DOM, return a reference
604 to the new element, which can be used to restore variable references. */
608 function renderDetails(data, marker)
610 var details = '<div class="details" id="det_'+data.recid+'"><table>';
611 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
612 if (data["md-title"] != undefined) {
613 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
614 if (data["md-title-remainder"] !== undefined) {
615 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
617 if (data["md-author"] !== undefined) {
618 details += ' <span><i>'+ data["md-auhtor"] +'</i></span>';
620 details += '</td></tr>';
622 if (data["md-date"] != undefined)
623 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
624 if (data["md-author"] != undefined)
625 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
626 if (data["md-electronic-url"] != undefined)
627 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>';
628 if (data["location"][0]["md-subject"] != undefined)
629 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
630 if (data["location"][0]["@name"] != undefined)
631 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
632 details += '</table></div>';
636 function renderLine(title, value) {
637 if (value != undefined)
638 return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
642 function renderLineURL(title, URL, display) {
643 if (URL != undefined)
644 return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
648 function renderLineEmail(dtitle, email, display) {
649 if (email != undefined)
650 return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
654 function renderDetails_iphone(data, marker)
656 //return renderDetails(data,marker);
660 var details = '<div class="details" id="det_'+data.recid+'" >'
662 details = '<div id="header" id="det_'+data.recid+'">'
663 + '<h1>Detailed Info</h1>'
664 + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>'
668 details += '<h4>'+ marker + '</h4>';
669 details += '<ul class="field">';
670 if (data["md-title"] != undefined) {
671 details += '<li><h3>Title</h3> <big> ' + data["md-title"];
672 if (data["md-title-remainder"] !== undefined) {
673 details += ' ' + data["md-title-remainder"] + ' ';
675 if (data["md-author"] !== undefined) {
676 details += '<i>'+ data["md-author"] +'</i>';
677 } else if (data["md-title-responsibility"] !== undefined) {
678 details += '<i>'+ data["md-title-responsibility"] +'</i>';
684 +=renderLine('Date', data["md-date"])
685 + renderLine('Author', data["md-author"])
686 + renderLineURL('URL', data["md-electronic-url"], data["md-electronic-url"])
687 + renderLine('Subject', data["location"][0]["md-subject"]);
689 if (data["location"][0]["@name"] != undefined)
690 details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
691 details += '</ul></div>';