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 = false;
8 var pazpar2path = '/service-proxy/';
9 var showResponseType = '';
10 // Facet configuration
11 var querys = {'su': '', 'au': '', 'xt': ''};
12 var query_client_server = {'su': 'subject', 'au': 'author', 'xt': 'xtargets'};
13 var querys_server = {};
15 // Fail to get JSON working stabil.
16 var showResponseType = 'xml';
17 if (document.location.hash == '#pazpar2' || document.location.search.match("useproxy=false")) {
19 pazpar2path = '/pazpar2/search.pz2';
20 showResponseType = 'xml';
24 my_paz = new pz2( { "onshow": my_onshow,
25 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
26 "pazpar2path": pazpar2path,
29 "onterm": my_onterm_iphone,
30 "termlist": "xtargets,subject,author",
31 "onbytarget": my_onbytarget,
32 "usesessions" : usesessions,
33 "showResponseType": showResponseType,
34 "onrecord": my_onrecord } );
40 var curDetRecData = null;
41 var curSort = 'relevance';
42 var curFilter = 'ALL';
43 var submitted = false;
47 var tab = "recordview";
52 function loginFormSubmit() {
53 triedUser = document.loginForm.username.value;
54 triedPass = document.loginForm.password.value;
55 auth.login( {"username": triedUser,
56 "password": triedPass},
60 function handleKeyPress(e)
64 key = window.event.keyCode;
68 if(key == 13 || key == 10)
70 button = document.getElementById('button');
80 function authCb(authData) {
81 if (!authData.loginFailed) {
86 if (authData.loggedIn == true) {
87 showhide("recordview");
91 function logOutClick() {
92 auth.logOut(authCb, authCb);
95 function loggedOut() {
96 var login = document.getElementById("login");
97 login.innerHTML = 'Login';
100 function loggingOutFailed() {
101 alert("Logging out failed");
109 auth.logOut(loggedOut, loggingOutFailed, true);
112 function logInOrOut() {
113 var loginElement = document.getElementById("login");
114 if (loginElement.innerHTML == 'Login')
119 function loggedIn() {
120 var login = document.getElementById("login");
121 login.innerHTML = 'Logout(' + auth.displayName + ')';
122 document.getElementById("log").innerHTML = login.innerHTML;
125 function auth_check() {
126 auth.check(loggedIn, login);
131 // Pz2.js event handlers:
133 function my_oninit() {
138 function my_onshow(data) {
139 totalRec = data.merged;
141 var pager = document.getElementById("pager");
142 pager.innerHTML = "";
143 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
144 + (data.start + 1) + ' to ' + (data.start + data.num) +
145 ' of ' + data.merged + ' (found: '
146 + data.total + ')</div>';
149 var results = document.getElementById("results");
152 if (data.hits == undefined)
154 for (var i = 0; i < data.hits.length; i++) {
155 var hit = data.hits[i];
156 html.push('<li id="recdiv_'+hit.recid+'" >'
157 /* +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>' */
158 +'<a href="#" id="rec_'+hit.recid
159 +'" onclick="showDetails(this.id);return false;">'
160 + hit["md-title"] +'</a> ');
161 if (hit["md-title-responsibility"] !== undefined) {
162 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
163 if (hit["md-title-remainder"] !== undefined) {
164 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
167 if (hit.recid == curDetRecId) {
168 html.push(renderDetails_iphone(curDetRecData));
172 replaceHtml(results, html.join(''));
175 function my_onstat(data) {
176 var stat = document.getElementById("stat");
180 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
182 + '/' + data.clients + ' -- </span>'
183 + '<span>Retrieved records: ' + data.records
184 + '/' + data.hits + ' :.</span>';
187 function showhide(newtab) {
188 var showtermlist = false;
192 if (tab == "recordview") {
193 document.getElementById("recordview").style.display = '';
196 document.getElementById("recordview").style.display = 'none';
198 if (tab == "xtargets") {
199 document.getElementById("term_xtargets").style.display = '';
203 document.getElementById("term_xtargets").style.display = 'none';
205 if (tab == "subjects") {
206 document.getElementById("term_subjects").style.display = '';
210 document.getElementById("term_subjects").style.display = 'none';
212 if (tab == "authors") {
213 document.getElementById("term_authors").style.display = '';
217 document.getElementById("term_authors").style.display = 'none';
219 if (showtermlist == false)
220 document.getElementById("termlist").style.display = 'none';
222 document.getElementById("termlist").style.display = '';
224 var tabDiv = document.getElementById("loginDiv");
225 if (tab == "login") {
226 tabDiv.style.display = '';
229 tabDiv.style.display = 'none';
233 function my_onterm(data) {
236 termlists.push('<div id="term_xtargets" >');
237 termlists.push('<h4 class="termtitle">Sources</h4>');
238 termlists.push('<ul>');
239 termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
240 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
241 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
242 + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">'
243 + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
245 termlists.push('</ul>');
246 termlists.push('</div>');
248 termlists.push('<div id="term_subjects" >');
249 termlists.push('<h4>Subjects</h4>');
250 termlists.push('<ul>');
251 termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
252 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
253 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">'
254 + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
256 termlists.push('</ul>');
257 termlists.push('</div>');
259 termlists.push('<div id="term_authors" >');
260 termlists.push('<h4 class="termtitle">Authors</h4>');
261 termlists.push('<ul>');
262 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
263 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
264 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'au\', \'' + data.author[i].name +'\');return false;">'
265 + data.author[i].name
267 + data.author[i].freq
270 termlists.push('</ul>');
271 termlists.push('</div>');
272 var termlist = document.getElementById("termlist");
273 replaceHtml(termlist, termlists.join(''));
278 function my_onterm_iphone(data) {
280 var targets = "reset_xt|All\n";
282 for (var i = 0; i < data.xtargets.length; i++ ) {
284 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
286 termlist["xtargets"] = targets;
287 var subjects = "reset_su|All\n";
288 for (var i = 0; i < data.subject.length; i++ ) {
289 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
291 termlist["subjects"] = subjects;
292 var authors = "reset_au|All\n";
293 for (var i = 0; i < data.author.length; i++ ) {
294 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
296 termlist["authors"] = authors;
297 callback.send("termlist", "refresh");
300 function getTargets() {
301 return termlist['xtargets'];
304 function getSubjects() {
305 return termlist['subjects'];
308 function getAuthors() {
309 return termlist['authors'];
312 function my_onrecord(data) {
313 // FIXME: record is async!!
314 clearTimeout(my_paz.recordTimer);
315 // in case on_show was faster to redraw element
316 var detRecordDiv = document.getElementById('det_'+data.recid);
317 if (detRecordDiv) return;
318 curDetRecData = data;
319 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
320 var html = renderDetails_iphone(curDetRecData);
321 recordDiv.innerHTML += html;
324 function my_onrecord_iphone(data) {
326 callback.send("record", data.recid, data, data.xtargets[i].freq);
330 function my_onbytarget(data) {
331 var targetDiv = document.getElementById("bytarget");
332 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
333 +'<td>Records</td><td>State</td></tr></thead><tbody>';
335 for (var i = 0; i < data.length; i++ ) {
336 table += "<tr><td>" + data[i].id +
337 "</td><td>" + data[i].hits +
338 "</td><td>" + data[i].diagnostic +
339 "</td><td>" + data[i].records +
340 "</td><td>" + data[i].state + "</td></tr>";
343 table += '</tbody></table>';
344 targetDiv.innerHTML = table;
347 ////////////////////////////////////////////////////////////////////////////////
348 ////////////////////////////////////////////////////////////////////////////////
350 // wait until the DOM is ready
353 document.search.onsubmit = onFormSubmitEventHandler;
354 document.search.query.value = '';
355 document.select.sort.onchange = onSelectDdChange;
356 document.select.perpage.onchange = onSelectDdChange;
357 if (document.location.search.match("inApp=true"))
358 applicationMode(true);
360 applicationMode(false);
363 function applicationMode(newmode)
365 var searchdiv = document.getElementById("searchForm");
369 document.getElementById("heading").style.display="none";
370 searchdiv.style.display = 'none';
374 document.getElementById("nav").style.display="";
375 document.getElementById("normal").style.display="inline";
376 document.getElementById("normal").style.visibility="";
377 searchdiv.style.display = '';
378 document.search.onsubmit = onFormSubmit;
382 // when search button pressed
383 function onFormSubmitEventHandler()
386 document.getElementById("logo").style.display = 'none';
393 function onSelectDdChange()
395 if (!submitted) return false;
398 my_paz.show(0, recPerPage, curSort);
408 function getFacets() {
410 for (var key in querys_server) {
411 if (result.length > 0)
413 result += querys_server[key];
418 function triggerSearch ()
420 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter, undefined,
422 "limit" : getFacets()
427 function loadSelect ()
429 curSort = document.select.sort.value;
430 recPerPage = document.select.perpage.value;
433 // limit the query after clicking the facet
434 function limitQuery(field, value)
436 var newQuery = ' and ' + field + '="' + value + '"';
437 querys[field] += newQuery;
438 document.search.query.value += newQuery;
439 onFormSubmitEventHandler();
440 showhide("recordview");
443 // limit the query after clicking the facet
444 function limitQueryServer(field, value)
446 // Check for client field usage
447 var fieldname = query_client_server[field];
451 var newQuery = fieldname + '=' + value.replace(",", "\\,").replace("|", "\\,");
452 // Does it already exists?
453 if (querys_server[fieldname])
454 querys_server[fieldname] += "," + newQuery;
456 querys_server[fieldname] = newQuery;
457 // document.search.query.value += newQuery;
458 onFormSubmitEventHandler();
459 showhide("recordview");
464 // limit the query after clicking the facet
465 function removeQuery (field, value) {
466 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
467 onFormSubmitEventHandler();
468 showhide("recordview");
471 // limit the query after clicking the facet
472 function limitOrResetQuery (field, value, selected) {
474 limitOrResetQueryServer(field,value, selected);
477 if (field == 'reset_su' || field == 'reset_au') {
478 var reset_field = field.substring(6);
479 document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
480 querys[reset_field] = '';
481 onFormSubmitEventHandler();
482 showhide("recordview");
485 limitQuery(field, value);
486 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
489 // limit the query after clicking the facet
490 function limitOrResetQueryServer (field, value, selected) {
491 if (field.substring(0,6) == 'reset_') {
492 var clientname = field.substring(6);
493 var fieldname = query_client_server[clientname];
495 fieldname = clientname;
496 delete querys_server[fieldname];
497 onFormSubmitEventHandler();
498 showhide("recordview");
501 limitQueryServer(field, value);
502 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
508 // limit by target functions
509 function limitTarget (id, name)
511 curFilter = 'pz:id=' + id;
515 showhide("recordview");
519 function delimitTarget ()
528 function limitOrResetTarget(id, name) {
529 if (id == 'reset_xt') {
533 limitTarget(id,name);
537 function drawPager (pagerDiv)
539 //client indexes pages from 1 but pz2 from 0
541 var pages = Math.ceil(totalRec / recPerPage);
543 var firstClkbl = ( curPage - onsides > 0 )
547 var lastClkbl = firstClkbl + 2*onsides < pages
548 ? firstClkbl + 2*onsides
551 var prev = '<span id="prev"><< Prev</span><b> | </b>';
553 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
554 +'<< Prev</a><b> | </b>';
557 for(var i = firstClkbl; i <= lastClkbl; i++) {
560 numLabel = '<b>' + i + '</b>';
562 middle += '<a href="#" onclick="showPage(' + i + ')"> '
563 + numLabel + ' </a>';
566 var next = '<b> | </b><span id="next">Next >></span>';
567 if (pages - curPage > 0)
568 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
569 +'Next >></a>';
576 if (lastClkbl < pages)
579 pagerDiv.innerHTML += '<div style="float: none">'
580 + prev + predots + middle + postdots + next + '</div><hr/>';
583 function showPage (pageNum)
586 my_paz.showPage( curPage - 1 );
589 // simple paging functions
591 function pagerNext() {
592 if ( totalRec - recPerPage*curPage > 0) {
598 function pagerPrev() {
599 if ( my_paz.showPrev() != false )
603 // swithing view between targets and records
605 function switchView(view) {
607 var targets = document.getElementById('targetview');
608 var records = document.getElementById('recordview');
612 targets.style.display = "block";
613 records.style.display = "none";
616 targets.style.display = "none";
617 records.style.display = "block";
620 alert('Unknown view.');
624 // detailed record drawing
625 function showDetails (prefixRecId) {
626 var recId = prefixRecId.replace('rec_', '');
627 var oldRecId = curDetRecId;
630 // remove current detailed view if any
631 var detRecordDiv = document.getElementById('det_'+oldRecId);
632 //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null);
635 detRecordDiv.parentNode.removeChild(detRecordDiv);
637 // if the same clicked, just hide
638 if (recId == oldRecId) {
640 curDetRecData = null;
643 // request the record
644 my_paz.record(recId);
647 function replaceHtml(el, html) {
648 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
649 /*@cc_on // Pure innerHTML is slightly faster in IE
650 oldEl.innerHTML = html;
653 var newEl = oldEl.cloneNode(false);
654 newEl.innerHTML = html;
655 oldEl.parentNode.replaceChild(newEl, oldEl);
656 /* Since we just removed the old element from the DOM, return a reference
657 to the new element, which can be used to restore variable references. */
661 function renderDetails(data, marker)
663 var details = '<div class="details" id="det_'+data.recid+'"><table>';
664 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
665 if (data["md-title"] != undefined) {
666 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
667 if (data["md-title-remainder"] !== undefined) {
668 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
670 if (data["md-title-responsibility"] !== undefined) {
671 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
673 details += '</td></tr>';
675 if (data["md-date"] != undefined)
676 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
677 if (data["md-author"] != undefined)
678 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
679 if (data["md-electronic-url"] != undefined)
680 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>';
681 if (data["location"][0]["md-subject"] != undefined)
682 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
683 if (data["location"][0]["@name"] != undefined)
684 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
685 details += '</table></div>';
689 function renderLine(title, value) {
690 if (value != undefined)
691 return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
695 function renderLineURL(title, URL, display) {
696 if (URL != undefined)
697 return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
701 function renderLineEmail(dtitle, email, display) {
702 if (email != undefined)
703 return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
707 function renderDetails_iphone(data, marker)
709 //return renderDetails(data,marker);
713 var details = '<div class="details" id="det_'+data.recid+'" >'
715 details = '<div id="header" id="det_'+data.recid+'">'
716 + '<h1>Detailed Info</h1>'
717 + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>'
721 details += '<h4>'+ marker + '</h4>';
722 details += '<ul class="field">';
723 if (data["md-title"] != undefined) {
724 details += '<li><h3>Title</h3> <big> ' + data["md-title"];
725 if (data["md-title-remainder"] !== undefined) {
726 details += ' ' + data["md-title-remainder"] + ' ';
728 if (data["md-title-responsibility"] !== undefined) {
729 details += '<i>'+ data["md-title-responsibility"] +'</i>';
735 +=renderLine('Date', data["md-date"])
736 + renderLine('Author', data["md-author"])
737 + renderLineURL('URL', data["md-electronic-url"], data["md-electronic-url"])
738 + renderLine('Subject', data["location"][0]["md-subject"]);
740 if (data["location"][0]["@name"] != undefined)
741 details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
742 details += '</ul></div>';