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 = '/pazpar2/search.pz2';
9 var showResponseType = '';
10 var querys = {'su': '', 'au': '', 'xt': ''};
12 if (document.location.hash == '#useproxy' || document.location.search.match("useproxy=true")) {
14 pazpar2path = '/service-proxy/';
15 showResponseType = 'json';
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 authCb(authData) {
56 if (!authData.loginFailed) {
61 if (authData.loggedIn == true) {
62 showhide("recordview");
66 function logOutClick() {
67 auth.logOut(authCb, authCb);
70 function loggedOut() {
71 var login = document.getElementById("login");
72 login.innerHTML = 'Login';
75 function loggingOutFailed() {
76 alert("Logging out failed");
84 auth.logOut(loggedOut, loggingOutFailed, true);
87 function logInOrOut() {
88 var loginElement = document.getElementById("login");
89 if (loginElement.innerHTML == 'Login')
95 var login = document.getElementById("login");
96 login.innerHTML = 'Logout(' + auth.displayName + ')';
97 document.getElementById("log").innerHTML = login.innerHTML;
100 function auth_check() {
101 auth.check(loggedIn, login, true);
106 // Pz2.js event handlers:
108 function my_oninit() {
113 function my_onshow(data) {
114 totalRec = data.merged;
116 var pager = document.getElementById("pager");
117 pager.innerHTML = "";
118 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
119 + (data.start + 1) + ' to ' + (data.start + data.num) +
120 ' of ' + data.merged + ' (found: '
121 + data.total + ')</div>';
124 var results = document.getElementById("results");
127 for (var i = 0; i < data.hits.length; i++) {
128 var hit = data.hits[i];
129 html.push('<li id="recdiv_'+hit.recid+'" >'
130 /* +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>' */
131 +'<a href="#" id="rec_'+hit.recid
132 +'" onclick="showDetails(this.id);return false;">'
133 + hit["md-title"] +'</a> ');
134 if (hit["md-title-responsibility"] !== undefined) {
135 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
136 if (hit["md-title-remainder"] !== undefined) {
137 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
140 if (hit.recid == curDetRecId) {
141 html.push(renderDetails_iphone(curDetRecData));
145 replaceHtml(results, html.join(''));
148 function my_onstat(data) {
149 var stat = document.getElementById("stat");
153 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
155 + '/' + data.clients + ' -- </span>'
156 + '<span>Retrieved records: ' + data.records
157 + '/' + data.hits + ' :.</span>';
160 function showhide(newtab) {
161 var showtermlist = false;
165 if (tab == "recordview") {
166 document.getElementById("recordview").style.display = '';
169 document.getElementById("recordview").style.display = 'none';
171 if (tab == "xtargets") {
172 document.getElementById("term_xtargets").style.display = '';
176 document.getElementById("term_xtargets").style.display = 'none';
178 if (tab == "subjects") {
179 document.getElementById("term_subjects").style.display = '';
183 document.getElementById("term_subjects").style.display = 'none';
185 if (tab == "authors") {
186 document.getElementById("term_authors").style.display = '';
190 document.getElementById("term_authors").style.display = 'none';
192 if (showtermlist == false)
193 document.getElementById("termlist").style.display = 'none';
195 document.getElementById("termlist").style.display = '';
197 var tabDiv = document.getElementById("loginDiv");
198 if (tab == "login") {
199 tabDiv.style.display = '';
202 tabDiv.style.display = 'none';
206 function my_onterm(data) {
209 termlists.push('<div id="term_xtargets" >');
210 termlists.push('<h4 class="termtitle">Sources</h4>');
211 termlists.push('<ul>');
212 termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
213 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
214 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
215 + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">'
216 + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
218 termlists.push('</ul>');
219 termlists.push('</div>');
221 termlists.push('<div id="term_subjects" >');
222 termlists.push('<h4>Subjects</h4>');
223 termlists.push('<ul>');
224 termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
225 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
226 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">'
227 + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
229 termlists.push('</ul>');
230 termlists.push('</div>');
232 termlists.push('<div id="term_authors" >');
233 termlists.push('<h4 class="termtitle">Authors</h4>');
234 termlists.push('<ul>');
235 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
236 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
237 termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.author[i].name +'\');return false;">'
238 + data.author[i].name
240 + data.author[i].freq
243 termlists.push('</ul>');
244 termlists.push('</div>');
245 var termlist = document.getElementById("termlist");
246 replaceHtml(termlist, termlists.join(''));
251 function my_onterm_iphone(data) {
253 var targets = "reset_xt|All\n";
255 for (var i = 0; i < data.xtargets.length; i++ ) {
257 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
259 termlist["xtargets"] = targets;
260 var subjects = "reset_su|All\n";
261 for (var i = 0; i < data.subject.length; i++ ) {
262 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
264 termlist["subjects"] = subjects;
265 var authors = "reset_au|All\n";
266 for (var i = 0; i < data.author.length; i++ ) {
267 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
269 termlist["authors"] = authors;
270 callback.send("termlist", "refresh");
273 function getTargets() {
274 return termlist['xtargets'];
277 function getSubjects() {
278 return termlist['subjects'];
281 function getAuthors() {
282 return termlist['authors'];
285 function my_onrecord(data) {
286 // FIXME: record is async!!
287 clearTimeout(my_paz.recordTimer);
288 // in case on_show was faster to redraw element
289 var detRecordDiv = document.getElementById('det_'+data.recid);
290 if (detRecordDiv) return;
291 curDetRecData = data;
292 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
293 var html = renderDetails_iphone(curDetRecData);
294 recordDiv.innerHTML += html;
297 function my_onrecord_iphone(data) {
299 callback.send("record", data.recid, data, data.xtargets[i].freq);
303 function my_onbytarget(data) {
304 var targetDiv = document.getElementById("bytarget");
305 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
306 +'<td>Records</td><td>State</td></tr></thead><tbody>';
308 for (var i = 0; i < data.length; i++ ) {
309 table += "<tr><td>" + data[i].id +
310 "</td><td>" + data[i].hits +
311 "</td><td>" + data[i].diagnostic +
312 "</td><td>" + data[i].records +
313 "</td><td>" + data[i].state + "</td></tr>";
316 table += '</tbody></table>';
317 targetDiv.innerHTML = table;
320 ////////////////////////////////////////////////////////////////////////////////
321 ////////////////////////////////////////////////////////////////////////////////
323 // wait until the DOM is ready
326 document.search.onsubmit = onFormSubmitEventHandler;
327 document.search.query.value = '';
328 document.select.sort.onchange = onSelectDdChange;
329 document.select.perpage.onchange = onSelectDdChange;
330 if (document.location.search.match("inApp=true"))
331 applicationMode(true);
333 applicationMode(false);
336 function applicationMode(newmode)
338 var searchdiv = document.getElementById("searchForm");
342 document.getElementById("heading").style.display="none";
343 searchdiv.style.display = 'none';
346 document.getElementById("nav").style.display="";
347 searchdiv.style.display = '';
348 document.search.onsubmit = onFormSubmit;
352 // when search button pressed
353 function onFormSubmitEventHandler()
356 document.getElementById("logo").style.display = 'none';
363 function onSelectDdChange()
365 if (!submitted) return false;
368 my_paz.show(0, recPerPage, curSort);
378 function triggerSearch ()
380 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
383 function loadSelect ()
385 curSort = document.select.sort.value;
386 recPerPage = document.select.perpage.value;
389 // limit the query after clicking the facet
390 function limitQuery(field, value)
392 var newQuery = ' and ' + field + '="' + value + '"';
393 querys[field] += newQuery;
394 document.search.query.value += newQuery;
395 onFormSubmitEventHandler();
396 showhide("recordview");
399 //limit the query after clicking the facet
400 function removeQuery (field, value) {
401 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
402 onFormSubmitEventHandler();
403 showhide("recordview");
406 //limit the query after clicking the facet
407 function limitOrResetQuery (field, value, selected) {
408 if (field == 'reset_su' || field == 'reset_au') {
409 var reset_field = field.substring(6);
410 document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
411 querys[reset_field] = '';
412 onFormSubmitEventHandler();
413 showhide("recordview");
416 limitQuery(field, value);
417 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
420 // limit by target functions
421 function limitTarget (id, name)
423 curFilter = 'pz:id=' + id;
427 showhide("recordview");
431 function delimitTarget ()
440 function limitOrResetTarget(id, name) {
441 if (id == 'reset_xt') {
445 limitTarget(id,name);
449 function drawPager (pagerDiv)
451 //client indexes pages from 1 but pz2 from 0
453 var pages = Math.ceil(totalRec / recPerPage);
455 var firstClkbl = ( curPage - onsides > 0 )
459 var lastClkbl = firstClkbl + 2*onsides < pages
460 ? firstClkbl + 2*onsides
463 var prev = '<span id="prev"><< Prev</span><b> | </b>';
465 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
466 +'<< Prev</a><b> | </b>';
469 for(var i = firstClkbl; i <= lastClkbl; i++) {
472 numLabel = '<b>' + i + '</b>';
474 middle += '<a href="#" onclick="showPage(' + i + ')"> '
475 + numLabel + ' </a>';
478 var next = '<b> | </b><span id="next">Next >></span>';
479 if (pages - curPage > 0)
480 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
481 +'Next >></a>';
488 if (lastClkbl < pages)
491 pagerDiv.innerHTML += '<div style="float: none">'
492 + prev + predots + middle + postdots + next + '</div><hr/>';
495 function showPage (pageNum)
498 my_paz.showPage( curPage - 1 );
501 // simple paging functions
503 function pagerNext() {
504 if ( totalRec - recPerPage*curPage > 0) {
510 function pagerPrev() {
511 if ( my_paz.showPrev() != false )
515 // swithing view between targets and records
517 function switchView(view) {
519 var targets = document.getElementById('targetview');
520 var records = document.getElementById('recordview');
524 targets.style.display = "block";
525 records.style.display = "none";
528 targets.style.display = "none";
529 records.style.display = "block";
532 alert('Unknown view.');
536 // detailed record drawing
537 function showDetails (prefixRecId) {
538 var recId = prefixRecId.replace('rec_', '');
539 var oldRecId = curDetRecId;
542 // remove current detailed view if any
543 var detRecordDiv = document.getElementById('det_'+oldRecId);
544 //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null);
547 detRecordDiv.parentNode.removeChild(detRecordDiv);
549 // if the same clicked, just hide
550 if (recId == oldRecId) {
552 curDetRecData = null;
555 // request the record
556 my_paz.record(recId);
559 function replaceHtml(el, html) {
560 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
561 /*@cc_on // Pure innerHTML is slightly faster in IE
562 oldEl.innerHTML = html;
565 var newEl = oldEl.cloneNode(false);
566 newEl.innerHTML = html;
567 oldEl.parentNode.replaceChild(newEl, oldEl);
568 /* Since we just removed the old element from the DOM, return a reference
569 to the new element, which can be used to restore variable references. */
573 function renderDetails(data, marker)
575 var details = '<div class="details" id="det_'+data.recid+'"><table>';
576 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
577 if (data["md-title"] != undefined) {
578 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
579 if (data["md-title-remainder"] !== undefined) {
580 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
582 if (data["md-title-responsibility"] !== undefined) {
583 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
585 details += '</td></tr>';
587 if (data["md-date"] != undefined)
588 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
589 if (data["md-author"] != undefined)
590 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
591 if (data["md-electronic-url"] != undefined)
592 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>';
593 if (data["location"][0]["md-subject"] != undefined)
594 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
595 if (data["location"][0]["@name"] != undefined)
596 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
597 details += '</table></div>';
601 function renderLine(title, value) {
602 if (value != undefined)
603 return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
607 function renderLineURL(title, URL, display) {
608 if (URL != undefined)
609 return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
613 function renderLineEmail(dtitle, email, display) {
614 if (email != undefined)
615 return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
619 function renderDetails_iphone(data, marker)
621 //return renderDetails(data,marker);
625 var details = '<div class="details" id="det_'+data.recid+'" >'
627 details = '<div id="header" id="det_'+data.recid+'">'
628 + '<h1>Detailed Info</h1>'
629 + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>'
633 details += '<h4>'+ marker + '</h4>';
634 details += '<ul class="field">';
635 if (data["md-title"] != undefined) {
636 details += '<li><h3>Title</h3> <big> ' + data["md-title"];
637 if (data["md-title-remainder"] !== undefined) {
638 details += ' ' + data["md-title-remainder"] + ' ';
640 if (data["md-title-responsibility"] !== undefined) {
641 details += '<i>'+ data["md-title-responsibility"] +'</i>';
647 +=renderLine('Date', data["md-date"])
648 + renderLine('Author', data["md-author"])
649 + renderLineURL('URL', data["md-electronic-url"], data["md-electronic-url"])
650 + renderLine('Subject', data["location"][0]["md-subject"]);
652 if (data["location"][0]["@name"] != undefined)
653 details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
654 details += '</ul></div>';