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 queryBeforeLimit = null;
12 if (document.location.hash == '#useproxy') {
14 pazpar2path = '/service-proxy/';
15 showResponseType = 'json';
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(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';
116 if (tab == "subjects") {
117 document.getElementById("term_subjects").style.display = '';
121 document.getElementById("term_subjects").style.display = 'none';
122 if (tab == "authors") {
123 document.getElementById("term_authors").style.display = '';
127 document.getElementById("term_authors").style.display = 'none';
129 if (showtermlist == false)
130 document.getElementById("termlist").style.display = 'none';
132 document.getElementById("termlist").style.display = '';
135 function my_onterm(data) {
138 termlists.push('<div id="term_xtargets" >');
139 termlists.push('<h4 class="termtitle">Sources</h4>');
140 termlists.push('<ul>');
141 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
142 termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
143 + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">' + data.xtargets[i].name
144 + ' (' + data.xtargets[i].freq + ')</a></li>');
146 termlists.push('</ul>');
147 termlists.push('</div>');
149 termlists.push('<div id="term_subjects" >');
150 termlists.push('<h4 id="subjects" class="termtitle">Subjects</h4>');
151 termlists.push('<ul>');
152 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
153 termlists.push('<li><a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">' + data.subject[i].name + '('
154 + data.subject[i].freq + ')</a></li>');
156 termlists.push('</ul>');
157 termlists.push('</div>');
159 termlists.push('<div id="term_authors" >');
160 termlists.push('<h4 class="termtitle">Authors</h4>');
161 termlists.push('<ul>');
162 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
163 termlists.push('<li><a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
164 + data.author[i].name
166 + data.author[i].freq
169 termlists.push('</ul>');
170 termlists.push('</div>');
171 var termlist = document.getElementById("termlist");
172 replaceHtml(termlist, termlists.join(''));
175 for (d in ("xtargets", "subjects", "authors")) {
178 document.getElementById("term_" + d).style.display = '';
180 document.getElementById("term_" +d ).style.display = 'none';
186 function serialize(array) {
187 var t = typeof (obj);
188 if (t != "object" || obj === null) {
192 // recurse array or object
193 var n, v, json = [], arr = (obj && obj.constructor == Array);
199 else if (t == "object" && v !== null)
200 v = JSON.stringify(v);
201 json.push((arr ? "" : '"' + n + '":') + String(v));
203 return (arr ? "" : "") + String(json) + (arr ? "]" : "}");
208 function my_onterm_iphone(data) {
210 var targets = "reset_to_all|All\n";
212 for (var i = 0; i < data.xtargets.length; i++ ) {
214 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
216 termlist["xtargets"] = targets;
217 var subjects = "reset_to_all|All\n";
218 for (var i = 0; i < data.subject.length; i++ ) {
219 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
221 termlist["subjects"] = subjects;
222 var authors = "reset_to_all|All\n";
223 for (var i = 0; i < data.author.length; i++ ) {
224 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
226 termlist["authors"] = authors;
227 //document.getElementById("log").innerHTML = targets + "\n" + subjects + "\n" + authors;
228 callback.send("termlist", "refresh");
231 function getTargets() {
232 return termlist['xtargets'];
235 function getSubjects() {
236 return termlist['subjects'];
239 function getAuthors() {
240 return termlist['authors'];
243 function my_onrecord(data) {
244 // FIXME: record is async!!
245 clearTimeout(my_paz.recordTimer);
246 // in case on_show was faster to redraw element
247 var detRecordDiv = document.getElementById('det_'+data.recid);
248 if (detRecordDiv) return;
249 curDetRecData = data;
250 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
251 var html = renderDetails(curDetRecData);
252 recordDiv.innerHTML += html;
255 function my_onrecord_iphone(data) {
257 callback.send("record", data.recid, data, data.xtargets[i].freq);
261 function my_onbytarget(data) {
262 var targetDiv = document.getElementById("bytarget");
263 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
264 +'<td>Records</td><td>State</td></tr></thead><tbody>';
266 for (var i = 0; i < data.length; i++ ) {
267 table += "<tr><td>" + data[i].id +
268 "</td><td>" + data[i].hits +
269 "</td><td>" + data[i].diagnostic +
270 "</td><td>" + data[i].records +
271 "</td><td>" + data[i].state + "</td></tr>";
274 table += '</tbody></table>';
275 targetDiv.innerHTML = table;
278 ////////////////////////////////////////////////////////////////////////////////
279 ////////////////////////////////////////////////////////////////////////////////
281 // wait until the DOM is ready
284 document.search.onsubmit = onFormSubmitEventHandler;
285 document.search.query.value = '';
286 document.select.sort.onchange = onSelectDdChange;
287 document.select.perpage.onchange = onSelectDdChange;
288 if (document.location.search.match("inApp=true"))
289 applicationMode(true);
292 function applicationMode(newmode)
294 var searchdiv = document.getElementById("searchForm");
298 document.getElementById("heading").style.display="none";
299 searchdiv.style.display = 'none';
302 searchdiv.style.display = '';
303 document.search.onsubmit = onFormSubmit;
307 // when search button pressed
308 function onFormSubmitEventHandler()
317 function onSelectDdChange()
319 if (!submitted) return false;
322 my_paz.show(0, recPerPage, curSort);
332 function triggerSearch ()
334 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
337 function loadSelect ()
339 curSort = document.select.sort.value;
340 recPerPage = document.select.perpage.value;
343 // limit the query after clicking the facet
344 function limitQuery (field, value)
346 if (!queryBeforeLimit)
347 queryBeforeLimit = document.search.query.value;
348 document.search.query.value += ' and ' + field + '="' + value + '"';
349 onFormSubmitEventHandler();
350 showhide("recordview");
353 //limit the query after clicking the facet
354 function removeQuery (field, value) {
355 document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
356 onFormSubmitEventHandler();
357 showhide("recordview");
360 //limit the query after clicking the facet
361 function limitOrResetQuery (field, value, selected) {
362 if (field == 'reset_to_all') {
363 document.search.query.value = queryBeforeLimit;
364 queryBeforeLimit = null;
367 limitQuery(field, value);
368 alert("query: " + document.search.query.value);
371 // limit by target functions
372 function limitTarget (id, name)
374 var navi = document.getElementById('navi');
376 'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
378 navi.innerHTML += '<hr/>';
379 curFilter = 'pz:id=' + id;
383 showhide("recordview");
387 function delimitTarget ()
389 var navi = document.getElementById('navi');
398 function drawPager (pagerDiv)
400 //client indexes pages from 1 but pz2 from 0
402 var pages = Math.ceil(totalRec / recPerPage);
404 var firstClkbl = ( curPage - onsides > 0 )
408 var lastClkbl = firstClkbl + 2*onsides < pages
409 ? firstClkbl + 2*onsides
412 var prev = '<span id="prev"><< Prev</span><b> | </b>';
414 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
415 +'<< Prev</a><b> | </b>';
418 for(var i = firstClkbl; i <= lastClkbl; i++) {
421 numLabel = '<b>' + i + '</b>';
423 middle += '<a href="#" onclick="showPage(' + i + ')"> '
424 + numLabel + ' </a>';
427 var next = '<b> | </b><span id="next">Next >></span>';
428 if (pages - curPage > 0)
429 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
430 +'Next >></a>';
437 if (lastClkbl < pages)
440 pagerDiv.innerHTML += '<div style="float: clear">'
441 + prev + predots + middle + postdots + next + '</div><hr/>';
444 function showPage (pageNum)
447 my_paz.showPage( curPage - 1 );
450 // simple paging functions
452 function pagerNext() {
453 if ( totalRec - recPerPage*curPage > 0) {
459 function pagerPrev() {
460 if ( my_paz.showPrev() != false )
464 // swithing view between targets and records
466 function switchView(view) {
468 var targets = document.getElementById('targetview');
469 var records = document.getElementById('recordview');
473 targets.style.display = "block";
474 records.style.display = "none";
477 targets.style.display = "none";
478 records.style.display = "block";
481 alert('Unknown view.');
485 // detailed record drawing
486 function showDetails (prefixRecId) {
487 var recId = prefixRecId.replace('rec_', '');
488 var oldRecId = curDetRecId;
491 // remove current detailed view if any
492 var detRecordDiv = document.getElementById('det_'+oldRecId);
495 detRecordDiv.parentNode.removeChild(detRecordDiv);
497 // if the same clicked, just hide
498 if (recId == oldRecId) {
500 curDetRecData = null;
503 // request the record
504 my_paz.record(recId);
507 function replaceHtml(el, html) {
508 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
509 /*@cc_on // Pure innerHTML is slightly faster in IE
510 oldEl.innerHTML = html;
513 var newEl = oldEl.cloneNode(false);
514 newEl.innerHTML = html;
515 oldEl.parentNode.replaceChild(newEl, oldEl);
516 /* Since we just removed the old element from the DOM, return a reference
517 to the new element, which can be used to restore variable references. */
521 function renderDetails(data, marker)
523 var details = '<div class="details" id="det_'+data.recid+'"><table>';
524 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
525 if (data["md-title"] != undefined) {
526 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
527 if (data["md-title-remainder"] !== undefined) {
528 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
530 if (data["md-title-responsibility"] !== undefined) {
531 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
533 details += '</td></tr>';
535 if (data["md-date"] != undefined)
536 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
537 if (data["md-author"] != undefined)
538 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
539 if (data["md-electronic-url"] != undefined)
540 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>';
541 if (data["location"][0]["md-subject"] != undefined)
542 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
543 if (data["location"][0]["@name"] != undefined)
544 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
545 details += '</table></div>';