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 if (document.location.hash == '#useproxy') {
12 pazpar2path = '/service-proxy/';
13 showResponseType = 'json';
16 my_paz = new pz2( { "onshow": my_onshow,
17 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
18 "pazpar2path": pazpar2path,
21 "onterm": my_onterm_iphone,
22 "termlist": "xtargets,subject,author",
23 "onbytarget": my_onbytarget,
24 "usesessions" : usesessions,
25 "showResponseType": showResponseType,
26 "onrecord": my_onrecord } );
32 var curDetRecData = null;
33 var curSort = 'relevance';
35 var submitted = false;
39 var tab = "recordview";
43 // pz2.js event handlers:
45 function my_oninit() {
50 function my_onshow(data) {
51 totalRec = data.merged;
53 var pager = document.getElementById("pager");
55 pager.innerHTML +='<hr/><div style="float: right">Displaying: '
56 + (data.start + 1) + ' to ' + (data.start + data.num) +
57 ' of ' + data.merged + ' (found: '
58 + data.total + ')</div>';
61 var results = document.getElementById("results");
64 for (var i = 0; i < data.hits.length; i++) {
65 var hit = data.hits[i];
66 html.push('<li id="recdiv_'+hit.recid+'" >'
67 /* +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>' */
68 +'<a href="#" id="rec_'+hit.recid
69 +'" onclick="showDetails(this.id);return false;">'
70 + hit["md-title"] +'</a> ');
71 if (hit["md-title-responsibility"] !== undefined) {
72 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
73 if (hit["md-title-remainder"] !== undefined) {
74 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
77 if (hit.recid == curDetRecId) {
78 html.push(renderDetails(curDetRecData));
82 replaceHtml(results, html.join(''));
85 function my_onstat(data) {
86 var stat = document.getElementById("stat");
90 stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
92 + '/' + data.clients + ' -- </span>'
93 + '<span>Retrieved records: ' + data.records
94 + '/' + data.hits + ' :.</span>';
97 function showhide(newtab) {
98 var showtermlist = false;
102 if (tab == "recordview") {
103 document.getElementById("recordview").style.display = '';
106 document.getElementById("recordview").style.display = 'none';
108 if (tab == "xtargets") {
109 document.getElementById("term_xtargets").style.display = '';
113 document.getElementById("term_xtargets").style.display = 'none';
114 if (tab == "subjects") {
115 document.getElementById("term_subjects").style.display = '';
119 document.getElementById("term_subjects").style.display = 'none';
120 if (tab == "authors") {
121 document.getElementById("term_authors").style.display = '';
125 document.getElementById("term_authors").style.display = 'none';
127 if (showtermlist == false)
128 document.getElementById("termlist").style.display = 'none';
130 document.getElementById("termlist").style.display = '';
133 function my_onterm(data) {
136 termlists.push('<div id="term_xtargets" >');
137 termlists.push('<div class="termtitle">.::Sources</div>');
138 for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
139 termlists.push('<a href="#" target_id='+data.xtargets[i].id
140 + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">' + data.xtargets[i].name
141 + ' </a><span> (' + data.xtargets[i].freq + ')</span><br/>');
143 termlists.push('</div>');
145 termlists.push('<div id="term_subjects" >');
146 termlists.push('<div id="subjects" class="termtitle">.::Subjects</div>');
147 for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
148 termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">' + data.subject[i].name + '</a><span> ('
149 + data.subject[i].freq + ')</span><br/>');
151 termlists.push('</div>');
153 termlists.push('<div id="term_authors" >');
154 termlists.push('<div class="termtitle">.::Authors</div>');
155 for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
156 termlists.push('<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
157 + data.author[i].name
159 + data.author[i].freq
162 termlists.push('</div>');
163 var termlist = document.getElementById("termlist");
164 replaceHtml(termlist, termlists.join(''));
167 for (d in ("xtargets", "subjects", "authors")) {
170 document.getElementById("term_" + d).style.display = '';
172 document.getElementById("term_" +d ).style.display = 'none';
178 function serialize(array) {
179 var t = typeof (obj);
180 if (t != "object" || obj === null) {
184 // recurse array or object
185 var n, v, json = [], arr = (obj && obj.constructor == Array);
191 else if (t == "object" && v !== null)
192 v = JSON.stringify(v);
193 json.push((arr ? "" : '"' + n + '":') + String(v));
195 return (arr ? "" : "") + String(json) + (arr ? "]" : "}");
200 function my_onterm_iphone(data) {
202 var targets = "-|All|\n";
204 for (var i = 0; i < data.xtargets.length; i++ ) {
206 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
208 termlist["xtargets"] = targets;
209 var subjects = "-|All|\n";
210 for (var i = 0; i < data.subject.length; i++ ) {
211 subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
213 termlist["subjects"] = subjects;
214 var authors = "-|All|\n";
215 for (var i = 0; i < data.author.length; i++ ) {
216 authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
218 termlist["authors"] = authors;
219 //document.getElementById("log").innerHTML = targets + "\n" + subjects + "\n" + authors;
220 callback.send("termlist", "refresh");
223 function getTargets() {
224 return termlist['xtargets'];
227 function getSubjects() {
228 return termlist['subjects'];
231 function getAuthors() {
232 return termlist['authors'];
235 function my_onrecord(data) {
236 // FIXME: record is async!!
237 clearTimeout(my_paz.recordTimer);
238 // in case on_show was faster to redraw element
239 var detRecordDiv = document.getElementById('det_'+data.recid);
240 if (detRecordDiv) return;
241 curDetRecData = data;
242 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
243 var html = renderDetails(curDetRecData);
244 recordDiv.innerHTML += html;
247 function my_onrecord_iphone(data) {
249 callback.send("record", data.recid, data, data.xtargets[i].freq);
253 function my_onbytarget(data) {
254 var targetDiv = document.getElementById("bytarget");
255 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
256 +'<td>Records</td><td>State</td></tr></thead><tbody>';
258 for (var i = 0; i < data.length; i++ ) {
259 table += "<tr><td>" + data[i].id +
260 "</td><td>" + data[i].hits +
261 "</td><td>" + data[i].diagnostic +
262 "</td><td>" + data[i].records +
263 "</td><td>" + data[i].state + "</td></tr>";
266 table += '</tbody></table>';
267 targetDiv.innerHTML = table;
270 ////////////////////////////////////////////////////////////////////////////////
271 ////////////////////////////////////////////////////////////////////////////////
273 // wait until the DOM is ready
276 document.search.onsubmit = onFormSubmitEventHandler;
277 document.search.query.value = '';
278 document.select.sort.onchange = onSelectDdChange;
279 document.select.perpage.onchange = onSelectDdChange;
280 if (document.location.search.match("inApp=true"))
281 applicationMode(true);
284 function applicationMode(newmode)
286 var searchdiv = document.getElementById("searchForm");
290 document.getElementById("heading").style.display="none";
291 searchdiv.style.display = 'none';
294 searchdiv.style.display = '';
295 document.search.onsubmit = onFormSubmit;
299 // when search button pressed
300 function onFormSubmitEventHandler()
309 function onSelectDdChange()
311 if (!submitted) return false;
314 my_paz.show(0, recPerPage, curSort);
324 function triggerSearch ()
326 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
329 function loadSelect ()
331 curSort = document.select.sort.value;
332 recPerPage = document.select.perpage.value;
335 // limit the query after clicking the facet
336 function limitQuery (field, value)
338 document.search.query.value += ' and ' + field + '="' + value + '"';
339 onFormSubmitEventHandler();
340 showhide("recordview");
343 // limit by target functions
344 function limitTarget (id, name)
346 var navi = document.getElementById('navi');
348 'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
350 navi.innerHTML += '<hr/>';
351 curFilter = 'pz:id=' + id;
355 showhide("recordview");
359 function delimitTarget ()
361 var navi = document.getElementById('navi');
370 function drawPager (pagerDiv)
372 //client indexes pages from 1 but pz2 from 0
374 var pages = Math.ceil(totalRec / recPerPage);
376 var firstClkbl = ( curPage - onsides > 0 )
380 var lastClkbl = firstClkbl + 2*onsides < pages
381 ? firstClkbl + 2*onsides
384 var prev = '<span id="prev"><< Prev</span><b> | </b>';
386 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
387 +'<< Prev</a><b> | </b>';
390 for(var i = firstClkbl; i <= lastClkbl; i++) {
393 numLabel = '<b>' + i + '</b>';
395 middle += '<a href="#" onclick="showPage(' + i + ')"> '
396 + numLabel + ' </a>';
399 var next = '<b> | </b><span id="next">Next >></span>';
400 if (pages - curPage > 0)
401 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
402 +'Next >></a>';
409 if (lastClkbl < pages)
412 pagerDiv.innerHTML += '<div style="float: clear">'
413 + prev + predots + middle + postdots + next + '</div><hr/>';
416 function showPage (pageNum)
419 my_paz.showPage( curPage - 1 );
422 // simple paging functions
424 function pagerNext() {
425 if ( totalRec - recPerPage*curPage > 0) {
431 function pagerPrev() {
432 if ( my_paz.showPrev() != false )
436 // swithing view between targets and records
438 function switchView(view) {
440 var targets = document.getElementById('targetview');
441 var records = document.getElementById('recordview');
445 targets.style.display = "block";
446 records.style.display = "none";
449 targets.style.display = "none";
450 records.style.display = "block";
453 alert('Unknown view.');
457 // detailed record drawing
458 function showDetails (prefixRecId) {
459 var recId = prefixRecId.replace('rec_', '');
460 var oldRecId = curDetRecId;
463 // remove current detailed view if any
464 var detRecordDiv = document.getElementById('det_'+oldRecId);
467 detRecordDiv.parentNode.removeChild(detRecordDiv);
469 // if the same clicked, just hide
470 if (recId == oldRecId) {
472 curDetRecData = null;
475 // request the record
476 my_paz.record(recId);
479 function replaceHtml(el, html) {
480 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
481 /*@cc_on // Pure innerHTML is slightly faster in IE
482 oldEl.innerHTML = html;
485 var newEl = oldEl.cloneNode(false);
486 newEl.innerHTML = html;
487 oldEl.parentNode.replaceChild(newEl, oldEl);
488 /* Since we just removed the old element from the DOM, return a reference
489 to the new element, which can be used to restore variable references. */
493 function renderDetails(data, marker)
495 var details = '<div class="details" id="det_'+data.recid+'"><table>';
496 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
497 if (data["md-title"] != undefined) {
498 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
499 if (data["md-title-remainder"] !== undefined) {
500 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
502 if (data["md-title-responsibility"] !== undefined) {
503 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
505 details += '</td></tr>';
507 if (data["md-date"] != undefined)
508 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
509 if (data["md-author"] != undefined)
510 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
511 if (data["md-electronic-url"] != undefined)
512 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>';
513 if (data["location"][0]["md-subject"] != undefined)
514 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
515 if (data["location"][0]["@name"] != undefined)
516 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
517 details += '</table></div>';