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-responsibility"] !== undefined) {
157 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
158 if (hit["md-title-remainder"] !== undefined) {
159 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
162 if (hit.recid == curDetRecId) {
163 html.push(renderDetails_iphone(curDetRecData));
167 replaceHtml(results, html.join(''));
170 function my_onstat(data) {
171 var stat = document.getElementById("stat");
175 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
177 + '/' + data.clients + ' -- </span>'
178 + '<span>Retrieved records: ' + data.records
179 + '/' + data.hits + ' :.</span>';
182 function showhide(newtab) {
183 var showtermlist = false;
187 if (tab == "recordview") {
188 document.getElementById("recordview").style.display = '';
191 document.getElementById("recordview").style.display = 'none';
193 if (tab == "xtargets") {
194 document.getElementById("term_xtargets").style.display = '';
198 document.getElementById("term_xtargets").style.display = 'none';
200 if (tab == "subjects") {
201 document.getElementById("term_subjects").style.display = '';
205 document.getElementById("term_subjects").style.display = 'none';
207 if (tab == "authors") {
208 document.getElementById("term_authors").style.display = '';
212 document.getElementById("term_authors").style.display = 'none';
214 if (showtermlist == false)
215 document.getElementById("termlist").style.display = 'none';
217 document.getElementById("termlist").style.display = '';
219 var tabDiv = document.getElementById("loginDiv");
220 if (tab == "login") {
221 tabDiv.style.display = '';
224 tabDiv.style.display = 'none';
228 function my_onterm(data) {
231 termlists.push('<div id="term_xtargets" >');
232 termlists.push('<h4 class="termtitle">Sources</h4>');
233 termlists.push('<ul>');
234 termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
235 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
236 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
237 + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">'
238 + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
240 termlists.push('</ul>');
241 termlists.push('</div>');
243 termlists.push('<div id="term_subjects" >');
244 termlists.push('<h4>Subjects</h4>');
245 termlists.push('<ul>');
246 termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
247 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
248 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">'
249 + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
251 termlists.push('</ul>');
252 termlists.push('</div>');
254 termlists.push('<div id="term_authors" >');
255 termlists.push('<h4 class="termtitle">Authors</h4>');
256 termlists.push('<ul>');
257 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
258 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
259 termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.author[i].name +'\');return false;">'
260 + data.author[i].name
262 + data.author[i].freq
265 termlists.push('</ul>');
266 termlists.push('</div>');
267 var termlist = document.getElementById("termlist");
268 replaceHtml(termlist, termlists.join(''));
273 function my_onterm_iphone(data) {
275 var targets = "reset_xt|All\n";
277 for (var i = 0; i < data.xtargets.length; i++ ) {
279 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
281 termlist["xtargets"] = targets;
282 var subjects = "reset_su|All\n";
283 for (var i = 0; i < data.subject.length; i++ ) {
284 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
286 termlist["subjects"] = subjects;
287 var authors = "reset_au|All\n";
288 for (var i = 0; i < data.author.length; i++ ) {
289 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
291 termlist["authors"] = authors;
292 callback.send("termlist", "refresh");
295 function getTargets() {
296 return termlist['xtargets'];
299 function getSubjects() {
300 return termlist['subjects'];
303 function getAuthors() {
304 return termlist['authors'];
307 function my_onrecord(data) {
308 // FIXME: record is async!!
309 clearTimeout(my_paz.recordTimer);
310 // in case on_show was faster to redraw element
311 var detRecordDiv = document.getElementById('det_'+data.recid);
312 if (detRecordDiv) return;
313 curDetRecData = data;
314 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
315 var html = renderDetails_iphone(curDetRecData);
316 recordDiv.innerHTML += html;
319 function my_onrecord_iphone(data) {
321 callback.send("record", data.recid, data, data.xtargets[i].freq);
325 function my_onbytarget(data) {
326 var targetDiv = document.getElementById("bytarget");
327 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
328 +'<td>Records</td><td>State</td></tr></thead><tbody>';
330 for (var i = 0; i < data.length; i++ ) {
331 table += "<tr><td>" + data[i].id +
332 "</td><td>" + data[i].hits +
333 "</td><td>" + data[i].diagnostic +
334 "</td><td>" + data[i].records +
335 "</td><td>" + data[i].state + "</td></tr>";
338 table += '</tbody></table>';
339 targetDiv.innerHTML = table;
342 ////////////////////////////////////////////////////////////////////////////////
343 ////////////////////////////////////////////////////////////////////////////////
345 // wait until the DOM is ready
348 document.search.onsubmit = onFormSubmitEventHandler;
349 document.search.query.value = '';
350 document.select.sort.onchange = onSelectDdChange;
351 document.select.perpage.onchange = onSelectDdChange;
352 if (document.location.search.match("inApp=true"))
353 applicationMode(true);
355 applicationMode(false);
358 function applicationMode(newmode)
360 var searchdiv = document.getElementById("searchForm");
364 document.getElementById("heading").style.display="none";
365 searchdiv.style.display = 'none';
369 document.getElementById("nav").style.display="";
370 document.getElementById("normal").style.display="inline";
371 document.getElementById("normal").style.visibility="";
372 searchdiv.style.display = '';
373 document.search.onsubmit = onFormSubmit;
377 // when search button pressed
378 function onFormSubmitEventHandler()
381 document.getElementById("logo").style.display = 'none';
388 function onSelectDdChange()
390 if (!submitted) return false;
393 my_paz.show(0, recPerPage, curSort);
403 function triggerSearch ()
405 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
409 "limit" : getFacets()
415 function loadSelect ()
417 curSort = document.select.sort.value;
418 recPerPage = document.select.perpage.value;
421 // limit the query after clicking the facet
422 function limitQuery(field, value)
424 var newQuery = ' and ' + field + '="' + value + '"';
425 querys[field] += newQuery;
426 document.search.query.value += newQuery;
427 onFormSubmitEventHandler();
428 showhide("recordview");
431 // limit the query after clicking the facet
432 function removeQuery (field, value) {
433 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
434 onFormSubmitEventHandler();
435 showhide("recordview");
438 // limit the query after clicking the facet
439 function limitOrResetQuery (field, value, selected) {
440 if (field == 'reset_su' || field == 'reset_au') {
441 var reset_field = field.substring(6);
442 document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
443 querys[reset_field] = '';
444 onFormSubmitEventHandler();
445 showhide("recordview");
448 limitQuery(field, value);
449 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
452 // limit by target functions
453 function limitTarget (id, name)
455 curFilter = 'pz:id=' + id;
459 showhide("recordview");
463 function delimitTarget ()
472 function limitOrResetTarget(id, name) {
473 if (id == 'reset_xt') {
477 limitTarget(id,name);
481 function drawPager (pagerDiv)
483 //client indexes pages from 1 but pz2 from 0
485 var pages = Math.ceil(totalRec / recPerPage);
487 var firstClkbl = ( curPage - onsides > 0 )
491 var lastClkbl = firstClkbl + 2*onsides < pages
492 ? firstClkbl + 2*onsides
495 var prev = '<span id="prev"><< Prev</span><b> | </b>';
497 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
498 +'<< Prev</a><b> | </b>';
501 for(var i = firstClkbl; i <= lastClkbl; i++) {
504 numLabel = '<b>' + i + '</b>';
506 middle += '<a href="#" onclick="showPage(' + i + ')"> '
507 + numLabel + ' </a>';
510 var next = '<b> | </b><span id="next">Next >></span>';
511 if (pages - curPage > 0)
512 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
513 +'Next >></a>';
520 if (lastClkbl < pages)
523 pagerDiv.innerHTML += '<div style="float: none">'
524 + prev + predots + middle + postdots + next + '</div><hr/>';
527 function showPage (pageNum)
530 my_paz.showPage( curPage - 1 );
533 // simple paging functions
535 function pagerNext() {
536 if ( totalRec - recPerPage*curPage > 0) {
542 function pagerPrev() {
543 if ( my_paz.showPrev() != false )
547 // swithing view between targets and records
549 function switchView(view) {
551 var targets = document.getElementById('targetview');
552 var records = document.getElementById('recordview');
556 targets.style.display = "block";
557 records.style.display = "none";
560 targets.style.display = "none";
561 records.style.display = "block";
564 alert('Unknown view.');
568 // detailed record drawing
569 function showDetails (prefixRecId) {
570 var recId = prefixRecId.replace('rec_', '');
571 var oldRecId = curDetRecId;
574 // remove current detailed view if any
575 var detRecordDiv = document.getElementById('det_'+oldRecId);
576 //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null);
579 detRecordDiv.parentNode.removeChild(detRecordDiv);
581 // if the same clicked, just hide
582 if (recId == oldRecId) {
584 curDetRecData = null;
587 // request the record
588 my_paz.record(recId);
591 function replaceHtml(el, html) {
592 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
593 /*@cc_on // Pure innerHTML is slightly faster in IE
594 oldEl.innerHTML = html;
597 var newEl = oldEl.cloneNode(false);
598 newEl.innerHTML = html;
599 oldEl.parentNode.replaceChild(newEl, oldEl);
600 /* Since we just removed the old element from the DOM, return a reference
601 to the new element, which can be used to restore variable references. */
605 function renderDetails(data, marker)
607 var details = '<div class="details" id="det_'+data.recid+'"><table>';
608 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
609 if (data["md-title"] != undefined) {
610 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
611 if (data["md-title-remainder"] !== undefined) {
612 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
614 if (data["md-title-responsibility"] !== undefined) {
615 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
617 details += '</td></tr>';
619 if (data["md-date"] != undefined)
620 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
621 if (data["md-author"] != undefined)
622 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
623 if (data["md-electronic-url"] != undefined)
624 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>';
625 if (data["location"][0]["md-subject"] != undefined)
626 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
627 if (data["location"][0]["@name"] != undefined)
628 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
629 details += '</table></div>';
633 function renderLine(title, value) {
634 if (value != undefined)
635 return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
639 function renderLineURL(title, URL, display) {
640 if (URL != undefined)
641 return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
645 function renderLineEmail(dtitle, email, display) {
646 if (email != undefined)
647 return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
651 function renderDetails_iphone(data, marker)
653 //return renderDetails(data,marker);
657 var details = '<div class="details" id="det_'+data.recid+'" >'
659 details = '<div id="header" id="det_'+data.recid+'">'
660 + '<h1>Detailed Info</h1>'
661 + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>'
665 details += '<h4>'+ marker + '</h4>';
666 details += '<ul class="field">';
667 if (data["md-title"] != undefined) {
668 details += '<li><h3>Title</h3> <big> ' + data["md-title"];
669 if (data["md-title-remainder"] !== undefined) {
670 details += ' ' + data["md-title-remainder"] + ' ';
672 if (data["md-title-responsibility"] !== undefined) {
673 details += '<i>'+ data["md-title-responsibility"] +'</i>';
679 +=renderLine('Date', data["md-date"])
680 + renderLine('Author', data["md-author"])
681 + renderLineURL('URL', data["md-electronic-url"], data["md-electronic-url"])
682 + renderLine('Subject', data["location"][0]["md-subject"]);
684 if (data["location"][0]["@name"] != undefined)
685 details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
686 details += '</ul></div>';