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') {
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";
45 // pz2.js event handlers:
47 function my_oninit() {
52 function my_onshow(data) {
53 totalRec = data.merged;
55 var pager = document.getElementById("pager");
57 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
58 + (data.start + 1) + ' to ' + (data.start + data.num) +
59 ' of ' + data.merged + ' (found: '
60 + data.total + ')</div>';
63 var results = document.getElementById("results");
66 for (var i = 0; i < data.hits.length; i++) {
67 var hit = data.hits[i];
68 html.push('<li id="recdiv_'+hit.recid+'" >'
69 /* +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>' */
70 +'<a href="#" id="rec_'+hit.recid
71 +'" onclick="showDetails(this.id);return false;">'
72 + hit["md-title"] +'</a> ');
73 if (hit["md-title-responsibility"] !== undefined) {
74 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
75 if (hit["md-title-remainder"] !== undefined) {
76 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
79 if (hit.recid == curDetRecId) {
80 html.push(renderDetails_iphone(curDetRecData));
84 replaceHtml(results, html.join(''));
87 function my_onstat(data) {
88 var stat = document.getElementById("stat");
92 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
94 + '/' + data.clients + ' -- </span>'
95 + '<span>Retrieved records: ' + data.records
96 + '/' + data.hits + ' :.</span>';
99 function showhide(newtab) {
100 var showtermlist = false;
104 if (tab == "recordview") {
105 document.getElementById("recordview").style.display = '';
108 document.getElementById("recordview").style.display = 'none';
110 if (tab == "xtargets") {
111 document.getElementById("term_xtargets").style.display = '';
115 document.getElementById("term_xtargets").style.display = 'none';
117 if (tab == "subjects") {
118 document.getElementById("term_subjects").style.display = '';
122 document.getElementById("term_subjects").style.display = 'none';
124 if (tab == "authors") {
125 document.getElementById("term_authors").style.display = '';
129 document.getElementById("term_authors").style.display = 'none';
131 if (showtermlist == false)
132 document.getElementById("termlist").style.display = 'none';
134 document.getElementById("termlist").style.display = '';
137 function my_onterm(data) {
140 termlists.push('<div id="term_xtargets" >');
141 termlists.push('<h4 class="termtitle">Sources</h4>');
142 termlists.push('<ul>');
143 termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
144 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
145 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
146 + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">'
147 + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
149 termlists.push('</ul>');
150 termlists.push('</div>');
152 termlists.push('<div id="term_subjects" >');
153 termlists.push('<h4>Subjects</h4>');
154 termlists.push('<ul>');
155 termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
156 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
157 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">'
158 + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
160 termlists.push('</ul>');
161 termlists.push('</div>');
163 termlists.push('<div id="term_authors" >');
164 termlists.push('<h4 class="termtitle">Authors</h4>');
165 termlists.push('<ul>');
166 termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
167 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
168 termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.author[i].name +'\');return false;">'
169 + data.author[i].name
171 + data.author[i].freq
174 termlists.push('</ul>');
175 termlists.push('</div>');
176 var termlist = document.getElementById("termlist");
177 replaceHtml(termlist, termlists.join(''));
181 function serialize(array) {
182 var t = typeof (obj);
183 if (t != "object" || obj === null) {
187 // recurse array or object
188 var n, v, json = [], arr = (obj && obj.constructor == Array);
194 else if (t == "object" && v !== null)
195 v = JSON.stringify(v);
196 json.push((arr ? "" : '"' + n + '":') + String(v));
198 return (arr ? "" : "") + String(json) + (arr ? "]" : "}");
203 function my_onterm_iphone(data) {
205 var targets = "reset_xt|All\n";
207 for (var i = 0; i < data.xtargets.length; i++ ) {
209 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
211 termlist["xtargets"] = targets;
212 var subjects = "reset_su|All\n";
213 for (var i = 0; i < data.subject.length; i++ ) {
214 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
216 termlist["subjects"] = subjects;
217 var authors = "reset_au|All\n";
218 for (var i = 0; i < data.author.length; i++ ) {
219 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
221 termlist["authors"] = authors;
222 //document.getElementById("log").innerHTML = targets + "\n" + subjects + "\n" + authors;
223 callback.send("termlist", "refresh");
226 function getTargets() {
227 return termlist['xtargets'];
230 function getSubjects() {
231 return termlist['subjects'];
234 function getAuthors() {
235 return termlist['authors'];
238 function my_onrecord(data) {
239 // FIXME: record is async!!
240 clearTimeout(my_paz.recordTimer);
241 // in case on_show was faster to redraw element
242 var detRecordDiv = document.getElementById('det_'+data.recid);
243 if (detRecordDiv) return;
244 curDetRecData = data;
245 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
246 var html = renderDetails_iphone(curDetRecData);
247 recordDiv.innerHTML += html;
250 function my_onrecord_iphone(data) {
252 callback.send("record", data.recid, data, data.xtargets[i].freq);
256 function my_onbytarget(data) {
257 var targetDiv = document.getElementById("bytarget");
258 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
259 +'<td>Records</td><td>State</td></tr></thead><tbody>';
261 for (var i = 0; i < data.length; i++ ) {
262 table += "<tr><td>" + data[i].id +
263 "</td><td>" + data[i].hits +
264 "</td><td>" + data[i].diagnostic +
265 "</td><td>" + data[i].records +
266 "</td><td>" + data[i].state + "</td></tr>";
269 table += '</tbody></table>';
270 targetDiv.innerHTML = table;
273 ////////////////////////////////////////////////////////////////////////////////
274 ////////////////////////////////////////////////////////////////////////////////
276 // wait until the DOM is ready
279 document.search.onsubmit = onFormSubmitEventHandler;
280 document.search.query.value = '';
281 document.select.sort.onchange = onSelectDdChange;
282 document.select.perpage.onchange = onSelectDdChange;
283 if (document.location.search.match("inApp=true"))
284 applicationMode(true);
287 function applicationMode(newmode)
289 var searchdiv = document.getElementById("searchForm");
293 document.getElementById("heading").style.display="none";
294 searchdiv.style.display = 'none';
297 searchdiv.style.display = '';
298 document.search.onsubmit = onFormSubmit;
302 // when search button pressed
303 function onFormSubmitEventHandler()
306 document.getElementById("logo").style.display = 'none';
313 function onSelectDdChange()
315 if (!submitted) return false;
318 my_paz.show(0, recPerPage, curSort);
328 function triggerSearch ()
330 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
333 function loadSelect ()
335 curSort = document.select.sort.value;
336 recPerPage = document.select.perpage.value;
339 // limit the query after clicking the facet
340 function limitQuery(field, value)
342 var newQuery = ' and ' + field + '="' + value + '"';
343 querys[field] += newQuery;
344 document.search.query.value += newQuery;
345 onFormSubmitEventHandler();
346 showhide("recordview");
349 //limit the query after clicking the facet
350 function removeQuery (field, value) {
351 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
352 onFormSubmitEventHandler();
353 showhide("recordview");
356 //limit the query after clicking the facet
357 function limitOrResetQuery (field, value, selected) {
358 if (field == 'reset_su' || field == 'reset_au') {
359 var reset_field = field.substring(6);
360 document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
361 querys[reset_field] = '';
362 onFormSubmitEventHandler();
363 showhide("recordview");
366 limitQuery(field, value);
367 //alert("limitOrResetQuerry: query after: " + document.search.query.value);
370 // limit by target functions
371 function limitTarget (id, name)
373 curFilter = 'pz:id=' + id;
377 showhide("recordview");
381 function delimitTarget ()
390 function limitOrResetTarget(id, name) {
391 if (id == 'reset_xt') {
395 limitTarget(id,name);
399 function drawPager (pagerDiv)
401 //client indexes pages from 1 but pz2 from 0
403 var pages = Math.ceil(totalRec / recPerPage);
405 var firstClkbl = ( curPage - onsides > 0 )
409 var lastClkbl = firstClkbl + 2*onsides < pages
410 ? firstClkbl + 2*onsides
413 var prev = '<span id="prev"><< Prev</span><b> | </b>';
415 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
416 +'<< Prev</a><b> | </b>';
419 for(var i = firstClkbl; i <= lastClkbl; i++) {
422 numLabel = '<b>' + i + '</b>';
424 middle += '<a href="#" onclick="showPage(' + i + ')"> '
425 + numLabel + ' </a>';
428 var next = '<b> | </b><span id="next">Next >></span>';
429 if (pages - curPage > 0)
430 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
431 +'Next >></a>';
438 if (lastClkbl < pages)
441 pagerDiv.innerHTML += '<div style="float: none">'
442 + prev + predots + middle + postdots + next + '</div><hr/>';
445 function showPage (pageNum)
448 my_paz.showPage( curPage - 1 );
451 // simple paging functions
453 function pagerNext() {
454 if ( totalRec - recPerPage*curPage > 0) {
460 function pagerPrev() {
461 if ( my_paz.showPrev() != false )
465 // swithing view between targets and records
467 function switchView(view) {
469 var targets = document.getElementById('targetview');
470 var records = document.getElementById('recordview');
474 targets.style.display = "block";
475 records.style.display = "none";
478 targets.style.display = "none";
479 records.style.display = "block";
482 alert('Unknown view.');
486 // detailed record drawing
487 function showDetails (prefixRecId) {
488 var recId = prefixRecId.replace('rec_', '');
489 var oldRecId = curDetRecId;
492 // remove current detailed view if any
493 var detRecordDiv = document.getElementById('det_'+oldRecId);
494 //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null);
497 detRecordDiv.parentNode.removeChild(detRecordDiv);
499 // if the same clicked, just hide
500 if (recId == oldRecId) {
502 curDetRecData = null;
505 // request the record
506 my_paz.record(recId);
509 function replaceHtml(el, html) {
510 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
511 /*@cc_on // Pure innerHTML is slightly faster in IE
512 oldEl.innerHTML = html;
515 var newEl = oldEl.cloneNode(false);
516 newEl.innerHTML = html;
517 oldEl.parentNode.replaceChild(newEl, oldEl);
518 /* Since we just removed the old element from the DOM, return a reference
519 to the new element, which can be used to restore variable references. */
523 function renderDetails(data, marker)
525 var details = '<div class="details" id="det_'+data.recid+'"><table>';
526 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
527 if (data["md-title"] != undefined) {
528 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
529 if (data["md-title-remainder"] !== undefined) {
530 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
532 if (data["md-title-responsibility"] !== undefined) {
533 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
535 details += '</td></tr>';
537 if (data["md-date"] != undefined)
538 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
539 if (data["md-author"] != undefined)
540 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
541 if (data["md-electronic-url"] != undefined)
542 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>';
543 if (data["location"][0]["md-subject"] != undefined)
544 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
545 if (data["location"][0]["@name"] != undefined)
546 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
547 details += '</table></div>';
551 function renderLine(title, value) {
552 if (value != undefined)
553 return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
557 function renderLineURL(title, URL, display) {
558 if (URL != undefined)
559 return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
563 function renderLineEmail(dtitle, email, display) {
564 if (email != undefined)
565 return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
569 function renderDetails_iphone(data, marker)
571 //return renderDetails(data,marker);
574 var details = '<div class="details" id="det_'+data.recid+'" >'
576 details = '<div id="header" id="det_'+data.recid+'">'
577 + '<h1>Detailed Info</h1>'
578 + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>'
582 details += '<h4>'+ marker + '</h4>';
583 details += '<ul class="field">';
584 if (data["md-title"] != undefined) {
585 details += '<li><h3>Title</h3> <big> ' + data["md-title"];
586 if (data["md-title-remainder"] !== undefined) {
587 details += ' ' + data["md-title-remainder"] + ' ';
589 if (data["md-title-responsibility"] !== undefined) {
590 details += '<i>'+ data["md-title-responsibility"] +'</i>';
596 +=renderLine('Date', data["md-date"])
597 + renderLine('Author', data["md-author"])
598 + renderLineURL('URL', data["md-electronic-url"], data["md-electronic-url"])
599 + renderLine('Subject', data["location"][0]["md-subject"]);
601 if (data["location"][0]["@name"] != undefined)
602 details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
603 details += '</ul></div>';