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 = 'xml';
18 my_paz = new pz2( { "onshow": my_onshow,
19 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
20 "pazpar2path": pazpar2path,
23 "onterm": my_onterm_iphone,
24 "termlist": "xtargets,subject,author",
25 "onbytarget": my_onbytarget,
26 "usesessions" : usesessions,
27 "showResponseType": showResponseType,
28 "onrecord": my_onrecord } );
34 var curDetRecData = null;
35 var curSort = 'relevance';
37 var submitted = false;
41 var tab = "recordview";
43 function loggedOut() {
44 var login = document.getElementById("login");
45 login.innerHTML = 'Login';
48 function loggingOutFailed() {
49 alert("Logging out failed");
53 window.location = "login.html";
57 auth.logOut(loggedOut, loggingOutFailed, false);
60 function logInOrOut() {
61 var loginElement = document.getElementById("login");
62 if (loginElement.innerHTML == 'Login')
68 var login = document.getElementById("login");
69 login.innerHTML = 'Logout';
70 // login.onclick = 'logout();';
74 function auth_check() {
75 auth.check(loggedIn, login);
79 // Pz2.js event handlers:
81 function my_oninit() {
86 function my_onshow(data) {
87 totalRec = data.merged;
89 var pager = document.getElementById("pager");
91 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
92 + (data.start + 1) + ' to ' + (data.start + data.num) +
93 ' of ' + data.merged + ' (found: '
94 + data.total + ')</div>';
97 var results = document.getElementById("results");
100 for (var i = 0; i < data.hits.length; i++) {
101 var hit = data.hits[i];
102 html.push('<li id="recdiv_'+hit.recid+'" >'
103 /* +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>' */
104 +'<a href="#" id="rec_'+hit.recid
105 +'" onclick="showDetails(this.id);return false;">'
106 + hit["md-title"] +'</a> ');
107 if (hit["md-title-responsibility"] !== undefined) {
108 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
109 if (hit["md-title-remainder"] !== undefined) {
110 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
113 if (hit.recid == curDetRecId) {
114 html.push(renderDetails_iphone(curDetRecData));
118 replaceHtml(results, html.join(''));
121 function my_onstat(data) {
122 var stat = document.getElementById("stat");
126 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
128 + '/' + data.clients + ' -- </span>'
129 + '<span>Retrieved records: ' + data.records
130 + '/' + data.hits + ' :.</span>';
133 function showhide(newtab) {
134 var showtermlist = false;
138 if (tab == "recordview") {
139 document.getElementById("recordview").style.display = '';
142 document.getElementById("recordview").style.display = 'none';
144 if (tab == "xtargets") {
145 document.getElementById("term_xtargets").style.display = '';
149 document.getElementById("term_xtargets").style.display = 'none';
151 if (tab == "subjects") {
152 document.getElementById("term_subjects").style.display = '';
156 document.getElementById("term_subjects").style.display = 'none';
158 if (tab == "authors") {
159 document.getElementById("term_authors").style.display = '';
163 document.getElementById("term_authors").style.display = 'none';
165 if (showtermlist == false)
166 document.getElementById("termlist").style.display = 'none';
168 document.getElementById("termlist").style.display = '';
171 function my_onterm(data) {
174 termlists.push('<div id="term_xtargets" >');
175 termlists.push('<h4 class="termtitle">Sources</h4>');
176 termlists.push('<ul>');
177 termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
178 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
179 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
180 + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">'
181 + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
183 termlists.push('</ul>');
184 termlists.push('</div>');
186 termlists.push('<div id="term_subjects" >');
187 termlists.push('<h4>Subjects</h4>');
188 termlists.push('<ul>');
189 termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
190 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
191 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">'
192 + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
194 termlists.push('</ul>');
195 termlists.push('</div>');
197 termlists.push('<div id="term_authors" >');
198 termlists.push('<h4 class="termtitle">Authors</h4>');
199 termlists.push('<ul>');
200 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
201 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
202 termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.author[i].name +'\');return false;">'
203 + data.author[i].name
205 + data.author[i].freq
208 termlists.push('</ul>');
209 termlists.push('</div>');
210 var termlist = document.getElementById("termlist");
211 replaceHtml(termlist, termlists.join(''));
215 function serialize(array) {
216 var t = typeof (obj);
217 if (t != "object" || obj === null) {
221 // recurse array or object
222 var n, v, json = [], arr = (obj && obj.constructor == Array);
228 else if (t == "object" && v !== null)
229 v = JSON.stringify(v);
230 json.push((arr ? "" : '"' + n + '":') + String(v));
232 return (arr ? "" : "") + String(json) + (arr ? "]" : "}");
237 function my_onterm_iphone(data) {
239 var targets = "reset_xt|All\n";
241 for (var i = 0; i < data.xtargets.length; i++ ) {
243 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
245 termlist["xtargets"] = targets;
246 var subjects = "reset_su|All\n";
247 for (var i = 0; i < data.subject.length; i++ ) {
248 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
250 termlist["subjects"] = subjects;
251 var authors = "reset_au|All\n";
252 for (var i = 0; i < data.author.length; i++ ) {
253 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
255 termlist["authors"] = authors;
256 //document.getElementById("log").innerHTML = targets + "\n" + subjects + "\n" + authors;
257 callback.send("termlist", "refresh");
260 function getTargets() {
261 return termlist['xtargets'];
264 function getSubjects() {
265 return termlist['subjects'];
268 function getAuthors() {
269 return termlist['authors'];
272 function my_onrecord(data) {
273 // FIXME: record is async!!
274 clearTimeout(my_paz.recordTimer);
275 // in case on_show was faster to redraw element
276 var detRecordDiv = document.getElementById('det_'+data.recid);
277 if (detRecordDiv) return;
278 curDetRecData = data;
279 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
280 var html = renderDetails_iphone(curDetRecData);
281 recordDiv.innerHTML += html;
284 function my_onrecord_iphone(data) {
286 callback.send("record", data.recid, data, data.xtargets[i].freq);
290 function my_onbytarget(data) {
291 var targetDiv = document.getElementById("bytarget");
292 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
293 +'<td>Records</td><td>State</td></tr></thead><tbody>';
295 for (var i = 0; i < data.length; i++ ) {
296 table += "<tr><td>" + data[i].id +
297 "</td><td>" + data[i].hits +
298 "</td><td>" + data[i].diagnostic +
299 "</td><td>" + data[i].records +
300 "</td><td>" + data[i].state + "</td></tr>";
303 table += '</tbody></table>';
304 targetDiv.innerHTML = table;
307 ////////////////////////////////////////////////////////////////////////////////
308 ////////////////////////////////////////////////////////////////////////////////
310 // wait until the DOM is ready
313 document.search.onsubmit = onFormSubmitEventHandler;
314 document.search.query.value = '';
315 document.select.sort.onchange = onSelectDdChange;
316 document.select.perpage.onchange = onSelectDdChange;
317 if (document.location.search.match("inApp=true"))
318 applicationMode(true);
320 applicationMode(false);
323 function applicationMode(newmode)
325 var searchdiv = document.getElementById("searchForm");
329 document.getElementById("heading").style.display="none";
330 searchdiv.style.display = 'none';
333 searchdiv.style.display = '';
334 document.search.onsubmit = onFormSubmit;
338 // when search button pressed
339 function onFormSubmitEventHandler()
342 document.getElementById("logo").style.display = 'none';
349 function onSelectDdChange()
351 if (!submitted) return false;
354 my_paz.show(0, recPerPage, curSort);
364 function triggerSearch ()
366 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
369 function loadSelect ()
371 curSort = document.select.sort.value;
372 recPerPage = document.select.perpage.value;
375 // limit the query after clicking the facet
376 function limitQuery(field, value)
378 var newQuery = ' and ' + field + '="' + value + '"';
379 querys[field] += newQuery;
380 document.search.query.value += newQuery;
381 onFormSubmitEventHandler();
382 showhide("recordview");
385 //limit the query after clicking the facet
386 function removeQuery (field, value) {
387 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
388 onFormSubmitEventHandler();
389 showhide("recordview");
392 //limit the query after clicking the facet
393 function limitOrResetQuery (field, value, selected) {
394 if (field == 'reset_su' || field == 'reset_au') {
395 var reset_field = field.substring(6);
396 document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
397 querys[reset_field] = '';
398 onFormSubmitEventHandler();
399 showhide("recordview");
402 limitQuery(field, value);
403 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
406 // limit by target functions
407 function limitTarget (id, name)
409 curFilter = 'pz:id=' + id;
413 showhide("recordview");
417 function delimitTarget ()
426 function limitOrResetTarget(id, name) {
427 if (id == 'reset_xt') {
431 limitTarget(id,name);
435 function drawPager (pagerDiv)
437 //client indexes pages from 1 but pz2 from 0
439 var pages = Math.ceil(totalRec / recPerPage);
441 var firstClkbl = ( curPage - onsides > 0 )
445 var lastClkbl = firstClkbl + 2*onsides < pages
446 ? firstClkbl + 2*onsides
449 var prev = '<span id="prev"><< Prev</span><b> | </b>';
451 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
452 +'<< Prev</a><b> | </b>';
455 for(var i = firstClkbl; i <= lastClkbl; i++) {
458 numLabel = '<b>' + i + '</b>';
460 middle += '<a href="#" onclick="showPage(' + i + ')"> '
461 + numLabel + ' </a>';
464 var next = '<b> | </b><span id="next">Next >></span>';
465 if (pages - curPage > 0)
466 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
467 +'Next >></a>';
474 if (lastClkbl < pages)
477 pagerDiv.innerHTML += '<div style="float: none">'
478 + prev + predots + middle + postdots + next + '</div><hr/>';
481 function showPage (pageNum)
484 my_paz.showPage( curPage - 1 );
487 // simple paging functions
489 function pagerNext() {
490 if ( totalRec - recPerPage*curPage > 0) {
496 function pagerPrev() {
497 if ( my_paz.showPrev() != false )
501 // swithing view between targets and records
503 function switchView(view) {
505 var targets = document.getElementById('targetview');
506 var records = document.getElementById('recordview');
510 targets.style.display = "block";
511 records.style.display = "none";
514 targets.style.display = "none";
515 records.style.display = "block";
518 alert('Unknown view.');
522 // detailed record drawing
523 function showDetails (prefixRecId) {
524 var recId = prefixRecId.replace('rec_', '');
525 var oldRecId = curDetRecId;
528 // remove current detailed view if any
529 var detRecordDiv = document.getElementById('det_'+oldRecId);
530 //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null);
533 detRecordDiv.parentNode.removeChild(detRecordDiv);
535 // if the same clicked, just hide
536 if (recId == oldRecId) {
538 curDetRecData = null;
541 // request the record
542 my_paz.record(recId);
545 function replaceHtml(el, html) {
546 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
547 /*@cc_on // Pure innerHTML is slightly faster in IE
548 oldEl.innerHTML = html;
551 var newEl = oldEl.cloneNode(false);
552 newEl.innerHTML = html;
553 oldEl.parentNode.replaceChild(newEl, oldEl);
554 /* Since we just removed the old element from the DOM, return a reference
555 to the new element, which can be used to restore variable references. */
559 function renderDetails(data, marker)
561 var details = '<div class="details" id="det_'+data.recid+'"><table>';
562 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
563 if (data["md-title"] != undefined) {
564 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
565 if (data["md-title-remainder"] !== undefined) {
566 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
568 if (data["md-title-responsibility"] !== undefined) {
569 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
571 details += '</td></tr>';
573 if (data["md-date"] != undefined)
574 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
575 if (data["md-author"] != undefined)
576 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
577 if (data["md-electronic-url"] != undefined)
578 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>';
579 if (data["location"][0]["md-subject"] != undefined)
580 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
581 if (data["location"][0]["@name"] != undefined)
582 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
583 details += '</table></div>';
587 function renderLine(title, value) {
588 if (value != undefined)
589 return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
593 function renderLineURL(title, URL, display) {
594 if (URL != undefined)
595 return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
599 function renderLineEmail(dtitle, email, display) {
600 if (email != undefined)
601 return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
605 function renderDetails_iphone(data, marker)
607 //return renderDetails(data,marker);
610 var details = '<div class="details" id="det_'+data.recid+'" >'
612 details = '<div id="header" id="det_'+data.recid+'">'
613 + '<h1>Detailed Info</h1>'
614 + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>'
618 details += '<h4>'+ marker + '</h4>';
619 details += '<ul class="field">';
620 if (data["md-title"] != undefined) {
621 details += '<li><h3>Title</h3> <big> ' + data["md-title"];
622 if (data["md-title-remainder"] !== undefined) {
623 details += ' ' + data["md-title-remainder"] + ' ';
625 if (data["md-title-responsibility"] !== undefined) {
626 details += '<i>'+ data["md-title-responsibility"] +'</i>';
632 +=renderLine('Date', data["md-date"])
633 + renderLine('Author', data["md-author"])
634 + renderLineURL('URL', data["md-electronic-url"], data["md-electronic-url"])
635 + renderLine('Subject', data["location"][0]["md-subject"]);
637 if (data["location"][0]["@name"] != undefined)
638 details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
639 details += '</ul></div>';