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) {
203 for (var i = 0; i < data.xtargets.length; i++ ) {
205 targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
207 termlist["xtargets"] = targets;
209 for (var i = 0; i < data.subject.length; i++ ) {
210 subjects = subjects + "-" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
212 termlist["subjects"] = subjects;
214 for (var i = 0; i < data.author.length; i++ ) {
215 authors = authors + "-" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
217 termlist["authors"] = authors;
218 //document.getElementById("log").innerHTML = targets + "\n" + subjects + "\n" + authors;
219 callback.send("termlist", "refresh");
222 function getTargets() {
223 return termlist['xtargets'];
226 function getSubjects() {
227 return termlist['subjects'];
230 function getAuthors() {
231 return termlist['authors'];
234 function my_onrecord(data) {
235 // FIXME: record is async!!
236 clearTimeout(my_paz.recordTimer);
237 // in case on_show was faster to redraw element
238 var detRecordDiv = document.getElementById('det_'+data.recid);
239 if (detRecordDiv) return;
240 curDetRecData = data;
241 var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
242 var html = renderDetails(curDetRecData);
243 recordDiv.innerHTML += html;
246 function my_onrecord_iphone(data) {
248 callback.send("record", data.recid, data, data.xtargets[i].freq);
252 function my_onbytarget(data) {
253 var targetDiv = document.getElementById("bytarget");
254 var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
255 +'<td>Records</td><td>State</td></tr></thead><tbody>';
257 for (var i = 0; i < data.length; i++ ) {
258 table += "<tr><td>" + data[i].id +
259 "</td><td>" + data[i].hits +
260 "</td><td>" + data[i].diagnostic +
261 "</td><td>" + data[i].records +
262 "</td><td>" + data[i].state + "</td></tr>";
265 table += '</tbody></table>';
266 targetDiv.innerHTML = table;
269 ////////////////////////////////////////////////////////////////////////////////
270 ////////////////////////////////////////////////////////////////////////////////
272 // wait until the DOM is ready
275 document.search.onsubmit = onFormSubmitEventHandler;
276 document.search.query.value = '';
277 document.select.sort.onchange = onSelectDdChange;
278 document.select.perpage.onchange = onSelectDdChange;
279 applicationMode(true);
282 function applicationMode(newmode)
284 var searchdiv = document.getElementById("searchForm");
288 document.getElementById("heading").style.display="none";
289 searchdiv.style.display = 'none';
292 searchdiv.style.display = '';
293 document.search.onsubmit = onFormSubmit;
297 // when search button pressed
298 function onFormSubmitEventHandler()
307 function onSelectDdChange()
309 if (!submitted) return false;
312 my_paz.show(0, recPerPage, curSort);
322 function triggerSearch ()
324 my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
327 function loadSelect ()
329 curSort = document.select.sort.value;
330 recPerPage = document.select.perpage.value;
333 // limit the query after clicking the facet
334 function limitQuery (field, value)
336 document.search.query.value += ' and ' + field + '="' + value + '"';
337 onFormSubmitEventHandler();
338 showhide("recordview");
341 // limit by target functions
342 function limitTarget (id, name)
344 var navi = document.getElementById('navi');
346 'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
348 navi.innerHTML += '<hr/>';
349 curFilter = 'pz:id=' + id;
353 showhide("recordview");
357 function delimitTarget ()
359 var navi = document.getElementById('navi');
368 function drawPager (pagerDiv)
370 //client indexes pages from 1 but pz2 from 0
372 var pages = Math.ceil(totalRec / recPerPage);
374 var firstClkbl = ( curPage - onsides > 0 )
378 var lastClkbl = firstClkbl + 2*onsides < pages
379 ? firstClkbl + 2*onsides
382 var prev = '<span id="prev"><< Prev</span><b> | </b>';
384 var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
385 +'<< Prev</a><b> | </b>';
388 for(var i = firstClkbl; i <= lastClkbl; i++) {
391 numLabel = '<b>' + i + '</b>';
393 middle += '<a href="#" onclick="showPage(' + i + ')"> '
394 + numLabel + ' </a>';
397 var next = '<b> | </b><span id="next">Next >></span>';
398 if (pages - curPage > 0)
399 var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
400 +'Next >></a>';
407 if (lastClkbl < pages)
410 pagerDiv.innerHTML += '<div style="float: clear">'
411 + prev + predots + middle + postdots + next + '</div><hr/>';
414 function showPage (pageNum)
417 my_paz.showPage( curPage - 1 );
420 // simple paging functions
422 function pagerNext() {
423 if ( totalRec - recPerPage*curPage > 0) {
429 function pagerPrev() {
430 if ( my_paz.showPrev() != false )
434 // swithing view between targets and records
436 function switchView(view) {
438 var targets = document.getElementById('targetview');
439 var records = document.getElementById('recordview');
443 targets.style.display = "block";
444 records.style.display = "none";
447 targets.style.display = "none";
448 records.style.display = "block";
451 alert('Unknown view.');
455 // detailed record drawing
456 function showDetails (prefixRecId) {
457 var recId = prefixRecId.replace('rec_', '');
458 var oldRecId = curDetRecId;
461 // remove current detailed view if any
462 var detRecordDiv = document.getElementById('det_'+oldRecId);
465 detRecordDiv.parentNode.removeChild(detRecordDiv);
467 // if the same clicked, just hide
468 if (recId == oldRecId) {
470 curDetRecData = null;
473 // request the record
474 my_paz.record(recId);
477 function replaceHtml(el, html) {
478 var oldEl = typeof el === "string" ? document.getElementById(el) : el;
479 /*@cc_on // Pure innerHTML is slightly faster in IE
480 oldEl.innerHTML = html;
483 var newEl = oldEl.cloneNode(false);
484 newEl.innerHTML = html;
485 oldEl.parentNode.replaceChild(newEl, oldEl);
486 /* Since we just removed the old element from the DOM, return a reference
487 to the new element, which can be used to restore variable references. */
491 function renderDetails(data, marker)
493 var details = '<div class="details" id="det_'+data.recid+'"><table>';
494 if (marker) details += '<tr><td>'+ marker + '</td></tr>';
495 if (data["md-title"] != undefined) {
496 details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
497 if (data["md-title-remainder"] !== undefined) {
498 details += ' : <span>' + data["md-title-remainder"] + ' </span>';
500 if (data["md-title-responsibility"] !== undefined) {
501 details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
503 details += '</td></tr>';
505 if (data["md-date"] != undefined)
506 details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
507 if (data["md-author"] != undefined)
508 details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
509 if (data["md-electronic-url"] != undefined)
510 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>';
511 if (data["location"][0]["md-subject"] != undefined)
512 details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
513 if (data["location"][0]["@name"] != undefined)
514 details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
515 details += '</table></div>';